Example code cleanup

This commit is contained in:
2021-11-04 11:31:57 -07:00
parent e7c9848f63
commit 21e19ba995
5 changed files with 72 additions and 91 deletions

View File

@ -16,6 +16,11 @@ add_executable(${TOOL_NAME})
target_sources(${TOOL_NAME}
PRIVATE
texture_generation.c
../utils/file.c
)
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/../
)
target_link_libraries(${TOOL_NAME}
PUBLIC

View File

@ -6,53 +6,13 @@
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#if defined(_MSC_VER)
#include <direct.h>
#define getcwd _getcwd
#define FILE_PATH_SEP '\\'
#elif defined(__GNUC__)
#include <unistd.h>
#define FILE_PATH_SEP '/'
#endif
#include "../utils/file.h"
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#endif
// void mkdirp(const char *dir) {
// char tmp[FILENAME_MAX + 1];
// char *p = NULL;
// size_t len;
// // Copy the input to the output
// snprintf(tmp, sizeof(tmp), "%s", dir);
// len = strlen(tmp);// Determine string length
// // Does the string end with a directory terminator?
// if(tmp[len - 1] == '/' || tmp[len - 1] == '\\') {
// tmp[len - 1] = 0;
// }
// // Foreach character, take the value from TMP as I go.
// for(p = tmp + 1; *p; p++) {
// if(*p == '/' || *p == '\\') {
// *p = 0;
// _mkdir(tmp);
// *p = '/';
// }
// }
// _mkdir(tmp);
// }
int main(int argc, char *argv[]) {
FILE *file;
char path[FILENAME_MAX + 1];
@ -110,7 +70,6 @@ int main(int argc, char *argv[]) {
}
// Write texture
printf("Size %i x %i @ %i\n", x, y, channels);
fwrite(data, sizeof(unsigned char), x*y*channels, file);
// Cleanup

13
tools/utils/common.h Normal file
View File

@ -0,0 +1,13 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

50
tools/utils/file.c Normal file
View File

@ -0,0 +1,50 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "file.h"
void fileNormalizeSlashes(char *string) {
char c;
int i = 0;
while(c = string[i++]) {
if(c != '\\' && c != '/') continue;
string[i-1] = FILE_PATH_SEP;
}
}
void fileMkdirp(char *path) {
char buffer[FILENAME_MAX];
char c;
int i = 0;
bool inFile;
bool hasMore;
inFile = false;
hasMore = false;
while(c = path[i]) {
if((c == '\\' || c == '/') && i > 0) {
buffer[i] = '\0';
_mkdir(buffer);
inFile = false;
hasMore = false;
buffer[i] = FILE_PATH_SEP;
i++;
continue;
}
if(c == '.') inFile = true;
hasMore = true;
buffer[i] = c;
i++;
}
if(!inFile && hasMore) {
buffer[i] = '\0';
_mkdir(buffer);
}
}

View File

@ -6,10 +6,7 @@
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "common.h"
#if defined(_MSC_VER)
#include <direct.h>
@ -20,49 +17,6 @@
#define FILE_PATH_SEP '/'
#endif
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#endif
void fileNormalizeSlashes(char *string);
void fileNormalizeSlashes(char *string) {
char c;
int i = 0;
while(c = string[i++]) {
if(c != '\\' && c != '/') continue;
string[i-1] = FILE_PATH_SEP;
}
}
void fileMkdirp(char *path) {
char buffer[FILENAME_MAX];
char c;
int i = 0;
bool inFile;
bool hasMore;
inFile = false;
hasMore = false;
while(c = path[i]) {
if((c == '\\' || c == '/') && i > 0) {
buffer[i] = '\0';
_mkdir(buffer);
inFile = false;
hasMore = false;
buffer[i] = FILE_PATH_SEP;
i++;
continue;
}
if(c == '.') inFile = true;
hasMore = true;
buffer[i] = c;
i++;
}
if(!inFile && hasMore) {
buffer[i] = '\0';
_mkdir(buffer);
}
}
void fileMkdirp(char *path);