Started updating tools to C++

This commit is contained in:
2023-02-06 20:28:55 -08:00
parent 28e7b1e079
commit 392817d852
7 changed files with 447 additions and 441 deletions

Binary file not shown.

View File

@ -1,24 +1,24 @@
# Copyright (c) 2021 Dominic Msters # Copyright (c) 2021 Dominic Msters
# #
# This software is released under the MIT License. # This software is released under the MIT License.
# https://opensource.org/licenses/MIT # https://opensource.org/licenses/MIT
# Texture Build Tool # Texture Build Tool
project(texturegen VERSION 1.0) project(texturegen VERSION 1.0)
add_executable(texturegen) add_executable(texturegen)
target_sources(texturegen target_sources(texturegen
PRIVATE PRIVATE
main.c main.cpp
../../utils/file.c ../../utils/file.c
../../utils/image.c ../../utils/image.c
) )
target_include_directories(texturegen target_include_directories(texturegen
PUBLIC PUBLIC
${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR}/../../
${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}
) )
target_link_libraries(texturegen target_link_libraries(texturegen
PUBLIC PUBLIC
${DAWN_BUILD_HOST_LIBS} ${DAWN_BUILD_HOST_LIBS}
stb stb
) )

View File

@ -1,87 +1,89 @@
/** /**
* Copyright (c) 2021 Dominic Masters * Copyright (c) 2021 Dominic Masters
* *
* This software is released under the MIT License. * This software is released under the MIT License.
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#include "../../utils/common.h" extern "C" {
#include "../../utils/file.h" #include "../../utils/common.h"
#include "../../utils/image.h" #include "../../utils/file.h"
#include "../../utils/image.h"
int main(int argc, char *argv[]) { }
FILE *file;
char path[FILENAME_MAX + 1]; int main(int argc, char *argv[]) {
uint8_t headerBuffer[32]; FILE *file;
char *in; char path[FILENAME_MAX + 1];
char *out; uint8_t headerBuffer[32];
int w, h, channels, headerBufferLength; char *in;
stbi_uc *dataImageRaw, dataIn; char *out;
float *dataImage; int w, h, channels, headerBufferLength;
size_t i, len; stbi_uc *dataImageRaw, dataIn;
float *dataImage;
if(argc != 3) { size_t i, len;
printf("Invalid number of arguments\n");
return 1; if(argc != 3) {
} printf("Invalid number of arguments\n");
return 1;
// Set up strings }
in = argv[1];
out = argv[2]; // Set up strings
in = argv[1];
// Normalize slashes out = argv[2];
fileNormalizeSlashes(in);
fileNormalizeSlashes(out); // Normalize slashes
fileNormalizeSlashes(in);
// Check the output doesn't already exist fileNormalizeSlashes(out);
sprintf(path, "%s.texture", out);
file = fopen(path, "rb"); // Check the output doesn't already exist
if(file != NULL) { sprintf(path, "%s.texture", out);
fclose(file); file = fopen(path, "rb");
return 0; if(file != NULL) {
} fclose(file);
return 0;
// Read in original texture }
file = fopen(in, "rb");
if(file == NULL) { // Read in original texture
printf("Failed to open file!\n"); file = fopen(in, "rb");
return 1; if(file == NULL) {
} printf("Failed to open file!\n");
return 1;
dataImageRaw = stbi_load_from_file(file, &w, &h, &channels, STBI_rgb_alpha); }
if(dataImageRaw == NULL) {
printf("Failed to load input texture!\n"); dataImageRaw = stbi_load_from_file(file, &w, &h, &channels, STBI_rgb_alpha);
return 1; if(dataImageRaw == NULL) {
} printf("Failed to load input texture!\n");
fclose(file); return 1;
}
// Convert to floating points fclose(file);
len = STBI_rgb_alpha * w * h;
dataImage = malloc(sizeof(float) * len); // Convert to floating points
for(i = 0; i < len; i++) { len = STBI_rgb_alpha * w * h;
dataIn = dataImageRaw[i]; dataImage = (float *)malloc(sizeof(float) * len);
dataImage[i] = ((float)dataIn) / 255.0f; for(i = 0; i < len; i++) {
} dataIn = dataImageRaw[i];
stbi_image_free(dataImageRaw); dataImage[i] = ((float)dataIn) / 255.0f;
}
// Open output file stbi_image_free(dataImageRaw);
fileMkdirp(path);
file = fopen(path, "wb"); // Open output file
if(file == NULL) { fileMkdirp(path);
printf("Invalid texture file out!\n"); file = fopen(path, "wb");
return 1; if(file == NULL) {
} printf("Invalid texture file out!\n");
return 1;
// Write info }
headerBufferLength = sprintf(headerBuffer, "%i|%i|", w, h);
fwrite(headerBuffer, sizeof(uint8_t), headerBufferLength, file); // Write info
headerBufferLength = sprintf((char *)headerBuffer, "%i|%i|", w, h);
// Write texture fwrite(headerBuffer, sizeof(uint8_t), headerBufferLength, file);
fwrite(dataImage, sizeof(float), len, file);
// Write texture
// Cleanup fwrite(dataImage, sizeof(float), len, file);
fclose(file);
free(dataImage); // Cleanup
fclose(file);
return 0; free(dataImage);
return 0;
} }

View File

@ -1,24 +1,24 @@
# Copyright (c) 2021 Dominic Msters # Copyright (c) 2021 Dominic Msters
# #
# This software is released under the MIT License. # This software is released under the MIT License.
# https://opensource.org/licenses/MIT # https://opensource.org/licenses/MIT
# Texture Build Tool # Texture Build Tool
project(tilesetgen VERSION 1.0) project(tilesetgen VERSION 1.0)
add_executable(tilesetgen) add_executable(tilesetgen)
target_sources(tilesetgen target_sources(tilesetgen
PRIVATE PRIVATE
main.c main.cpp
../../utils/file.c ../../utils/file.c
../../utils/image.c ../../utils/image.c
) )
target_include_directories(tilesetgen target_include_directories(tilesetgen
PUBLIC PUBLIC
${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR}/../../
${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}
) )
target_link_libraries(tilesetgen target_link_libraries(tilesetgen
PUBLIC PUBLIC
${DAWN_BUILD_HOST_LIBS} ${DAWN_BUILD_HOST_LIBS}
stb stb
) )

View File

@ -1,147 +1,149 @@
/** /**
* Copyright (c) 2022 Dominic Masters * Copyright (c) 2022 Dominic Masters
* *
* This software is released under the MIT License. * This software is released under the MIT License.
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#include "../../utils/common.h" extern "C" {
#include "../../utils/file.h" #include "../../utils/common.h"
#include "../../utils/image.h" #include "../../utils/file.h"
#include "../../utils/image.h"
int main(int argc, char *argv[]) { }
char *in;
char *out; int main(int argc, char *argv[]) {
FILE *file; char *in;
char path[FILENAME_MAX + 1]; char *out;
int w, h, channels, cols, rows; FILE *file;
stbi_uc *dataImageRaw; char path[FILENAME_MAX + 1];
int gapX, gapY, borderX, borderY; int w, h, channels, cols, rows;
stbi_uc *dataImageRaw;
if(argc < 5) { int gapX, gapY, borderX, borderY;
printf("Invalid number of arguments");
return 1; if(argc < 5) {
} printf("Invalid number of arguments");
return 1;
in = argv[1]; }
out = argv[2];
gapX = 0, gapY = 0, borderX = 0, borderY = 0; in = argv[1];
out = argv[2];
cols = atoi(argv[3]); gapX = 0, gapY = 0, borderX = 0, borderY = 0;
if(cols <= 0) {
printf("Columns is invalid\n"); cols = atoi(argv[3]);
return 1; if(cols <= 0) {
} printf("Columns is invalid\n");
return 1;
rows = atoi(argv[4]); }
if(rows <= 0) {
printf("Rows is invalid"); rows = atoi(argv[4]);
return 1; if(rows <= 0) {
} printf("Rows is invalid");
return 1;
if(argc >= 6) { }
gapX = atoi(argv[5]);
if(gapX <= 0) { if(argc >= 6) {
printf("Gap X is invalid\n"); gapX = atoi(argv[5]);
return 1; if(gapX <= 0) {
} printf("Gap X is invalid\n");
} return 1;
}
if(argc >= 7) { }
gapY = atoi(argv[6]);
if(gapY <= 0) { if(argc >= 7) {
printf("Gap Y is invalid\n"); gapY = atoi(argv[6]);
return 1; if(gapY <= 0) {
} printf("Gap Y is invalid\n");
} return 1;
}
if(argc >= 8) { }
borderX = atoi(argv[7]);
if(borderX <= 0) { if(argc >= 8) {
printf("Border X is invalid\n"); borderX = atoi(argv[7]);
return 1; if(borderX <= 0) {
} printf("Border X is invalid\n");
} return 1;
}
if(argc >= 9) { }
borderY = atoi(argv[8]);
if(borderY <= 0) { if(argc >= 9) {
printf("Border Y is invalid\n"); borderY = atoi(argv[8]);
return 1; if(borderY <= 0) {
} printf("Border Y is invalid\n");
} return 1;
}
// Normalize slashes }
fileNormalizeSlashes(in);
fileNormalizeSlashes(out); // Normalize slashes
fileNormalizeSlashes(in);
// Check the output doesn't already exist fileNormalizeSlashes(out);
sprintf(path, "%s.tileset", out);
file = fopen(path, "rb"); // Check the output doesn't already exist
if(file != NULL) { sprintf(path, "%s.tileset", out);
fclose(file); file = fopen(path, "rb");
return 0; if(file != NULL) {
} fclose(file);
return 0;
// Read in the original texture }
file = fopen(in, "rb");
if(file == NULL) { // Read in the original texture
printf("Failed to open file!\n"); file = fopen(in, "rb");
return 1; if(file == NULL) {
} printf("Failed to open file!\n");
return 1;
// Read image data }
dataImageRaw = stbi_load_from_file(file, &w, &h, &channels, STBI_rgb_alpha);
if(dataImageRaw == NULL) { // Read image data
printf("Failed to load input texture!\n"); dataImageRaw = stbi_load_from_file(file, &w, &h, &channels, STBI_rgb_alpha);
return 1; if(dataImageRaw == NULL) {
} printf("Failed to load input texture!\n");
fclose(file); return 1;
free(dataImageRaw); }
fclose(file);
if(w <= 0 || h <= 0) { free(dataImageRaw);
printf("Reading image failed (corrupted?)\n");
return 1; if(w <= 0 || h <= 0) {
} printf("Reading image failed (corrupted?)\n");
return 1;
// Calculate division sizes (pixels) }
int divX = (w - (borderX * 2) - (gapX * (cols - 1))) / cols;
int divY = (h - (borderY * 2) - (gapY * (rows - 1))) / rows; // Calculate division sizes (pixels)
int divX = (w - (borderX * 2) - (gapX * (cols - 1))) / cols;
// Calculate the division sizes (units) int divY = (h - (borderY * 2) - (gapY * (rows - 1))) / rows;
float tdivX = (float)divX / (float)w;
float tdivY = (float)divY / (float)h; // Calculate the division sizes (units)
float tdivX = (float)divX / (float)w;
// Output buffer prep float tdivY = (float)divY / (float)h;
char *buffer = malloc(sizeof(char) * (cols * rows * 48 + 48 + 48));
buffer[0] = '\0'; // Output buffer prep
char *buffer = (char *)malloc(sizeof(char) * (cols * rows * 48 + 48 + 48));
sprintf(buffer, "%i|%i|%i|%i|", cols, rows, divX, divY); buffer[0] = '\0';
// Now prep tileset. sprintf(buffer, "%i|%i|%i|%i|", cols, rows, divX, divY);
for(float y = 0; y < rows; y++) {
for(float x = 0; x < cols; x++) { // Now prep tileset.
float ux0 = ((float)borderX + ((float)divX * x) + ((float)gapX * x)) / (float)w; for(float y = 0; y < rows; y++) {
float ux1 = ux0 + tdivX; for(float x = 0; x < cols; x++) {
float uy0 = ((float)borderY + ((float)divY * y) + ((float)gapY * y)) / (float)h; float ux0 = ((float)borderX + ((float)divX * x) + ((float)gapX * x)) / (float)w;
float uy1 = uy0 + tdivY; float ux1 = ux0 + tdivX;
sprintf(buffer, "%s%f,%f,%f,%f|", buffer, ux0, ux1, uy0, uy1); float uy0 = ((float)borderY + ((float)divY * y) + ((float)gapY * y)) / (float)h;
} float uy1 = uy0 + tdivY;
} sprintf(buffer, "%s%f,%f,%f,%f|", buffer, ux0, ux1, uy0, uy1);
}
// Open output file }
fileMkdirp(path);
file = fopen(path, "wb"); // Open output file
if(file == NULL) { fileMkdirp(path);
free(buffer); file = fopen(path, "wb");
printf("Invalid tileset file out!\n"); if(file == NULL) {
return 1; free(buffer);
} printf("Invalid tileset file out!\n");
return 1;
// Write and close }
fwrite(buffer, sizeof(char), strlen(buffer), file);
fclose(file); // Write and close
free(buffer); fwrite(buffer, sizeof(char), strlen(buffer), file);
fclose(file);
return 0; free(buffer);
return 0;
} }

View File

@ -1,24 +1,24 @@
# Copyright (c) 2021 Dominic Msters # Copyright (c) 2021 Dominic Msters
# #
# This software is released under the MIT License. # This software is released under the MIT License.
# https://opensource.org/licenses/MIT # https://opensource.org/licenses/MIT
# Texture Build Tool # Texture Build Tool
project(truetypegen VERSION 1.0) project(truetypegen VERSION 1.0)
add_executable(truetypegen) add_executable(truetypegen)
target_sources(truetypegen target_sources(truetypegen
PRIVATE PRIVATE
main.c main.cpp
../../utils/file.c ../../utils/file.c
../../utils/image.c ../../utils/image.c
) )
target_include_directories(truetypegen target_include_directories(truetypegen
PUBLIC PUBLIC
${CMAKE_CURRENT_LIST_DIR}/../../ ${CMAKE_CURRENT_LIST_DIR}/../../
${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}
) )
target_link_libraries(truetypegen target_link_libraries(truetypegen
PUBLIC PUBLIC
${DAWN_BUILD_HOST_LIBS} ${DAWN_BUILD_HOST_LIBS}
stb stb
) )

View File

@ -1,141 +1,143 @@
/** /**
* Copyright (c) 2021 Dominic Masters * Copyright (c) 2021 Dominic Masters
* *
* This software is released under the MIT License. * This software is released under the MIT License.
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#include "../../utils/common.h" extern "C" {
#include "../../utils/file.h" #include "../../utils/common.h"
#include "../../utils/image.h" #include "../../utils/file.h"
#ifndef STB_TRUETYPE_IMPLEMENTATION #include "../../utils/image.h"
#define STB_TRUETYPE_IMPLEMENTATION #ifndef STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h> #define STB_TRUETYPE_IMPLEMENTATION
#endif #include <stb_truetype.h>
#endif
#define TRUETYPE_FIRST_CHAR 32 }
#define TRUETYPE_NUM_CHARS 96
#define TRUETYPE_FIRST_CHAR 32
int main(int argc, char *args[]) { #define TRUETYPE_NUM_CHARS 96
FILE *file;
char path[FILENAME_MAX + 1]; int main(int argc, char *args[]) {
int32_t width, height, fontSize, textureSize; FILE *file;
char path[FILENAME_MAX + 1];
/* int32_t width, height, fontSize, textureSize;
args0 - PATH
args1 - Input Filename (TTF file) /*
args2 - Output Filename args0 - PATH
args3 - Width of the output texture (Resolution X) args1 - Input Filename (TTF file)
args4 - Height of the output texture (Resolution Y) args2 - Output Filename
args5 - Font size to draw args3 - Width of the output texture (Resolution X)
*/ args4 - Height of the output texture (Resolution Y)
args5 - Font size to draw
if(argc != 6) { */
printf("Invalid number of arguments\n");
return 1; if(argc != 6) {
} printf("Invalid number of arguments\n");
return 1;
char *fileIn = args[1]; }
char *fileOut = args[2];
char *strWidth = args[3]; char *fileIn = args[1];
char *strHeight = args[4]; char *fileOut = args[2];
char *strFontSize = args[5]; char *strWidth = args[3];
char *strHeight = args[4];
// Normalize slashes char *strFontSize = args[5];
fileNormalizeSlashes(fileIn);
fileNormalizeSlashes(fileOut); // Normalize slashes
fileNormalizeSlashes(fileIn);
// Check the output doesn't already exist fileNormalizeSlashes(fileOut);
sprintf(path, "%s.truetype", fileOut);
file = fopen(path, "rb"); // Check the output doesn't already exist
if(file != NULL) { sprintf(path, "%s.truetype", fileOut);
fclose(file); file = fopen(path, "rb");
return 0; if(file != NULL) {
} fclose(file);
return 0;
width = atoi(strWidth); }
if(width <= 0) {
printf("Width is invalid.\n"); width = atoi(strWidth);
return 1; if(width <= 0) {
} printf("Width is invalid.\n");
return 1;
height = atoi(strHeight); }
if(height <= 0) {
printf("Height is invalid.\n"); height = atoi(strHeight);
return 1; if(height <= 0) {
} printf("Height is invalid.\n");
return 1;
fontSize = atoi(strFontSize); }
if(fontSize <= 0) {
printf("Font size is invalid.\n"); fontSize = atoi(strFontSize);
return 1; if(fontSize <= 0) {
} printf("Font size is invalid.\n");
return 1;
// Read in the TTF data }
file = fopen(fileIn, "rb");
if(file == NULL) { // Read in the TTF data
printf("Failed to open input TTF file.\n"); file = fopen(fileIn, "rb");
return 1; if(file == NULL) {
} printf("Failed to open input TTF file.\n");
return 1;
// Seek to end, get length, seek back to start. }
fseek(file, 0, SEEK_END);
size_t fileSize = ftell(file); // Seek to end, get length, seek back to start.
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_END);
size_t fileSize = ftell(file);
// Read in all data fseek(file, 0, SEEK_SET);
char *ttfData = malloc(sizeof(char) * fileSize);
size_t readSize = fread(ttfData, 1, fileSize, file); // Read in all data
fclose(file); char *ttfData = (char*)malloc(sizeof(char) * fileSize);
if(readSize < fileSize) { size_t readSize = fread(ttfData, 1, fileSize, file);
free(ttfData); fclose(file);
printf("Failed to read all data form TTF\n"); if(readSize < fileSize) {
return 1; free(ttfData);
} printf("Failed to read all data form TTF\n");
return 1;
// Create bitmap data. This is a single channel color (alpha). }
textureSize = width * height;
stbi_uc *bitmapData = malloc(sizeof(stbi_uc) * textureSize); // Create bitmap data. This is a single channel color (alpha).
stbtt_bakedchar characterData[TRUETYPE_NUM_CHARS]; textureSize = width * height;
stbi_uc *bitmapData = (stbi_uc*)malloc(sizeof(stbi_uc) * textureSize);
// Now parse the TTF itself. stbtt_bakedchar characterData[TRUETYPE_NUM_CHARS];
stbtt_BakeFontBitmap(
ttfData, 0, (float)fontSize, bitmapData, // Now parse the TTF itself.
width, height, stbtt_BakeFontBitmap(
TRUETYPE_FIRST_CHAR, TRUETYPE_NUM_CHARS, (uint8_t*)ttfData, 0, (float)fontSize, bitmapData,
characterData width, height,
); TRUETYPE_FIRST_CHAR, TRUETYPE_NUM_CHARS,
characterData
// Prepare output file for writing. );
sprintf(path, "%s.truetype", fileOut);
fileMkdirp(path); // Prepare output file for writing.
file = fopen(path, "wb"); sprintf(path, "%s.truetype", fileOut);
if(file == NULL) { fileMkdirp(path);
printf("Failed to create output TTF file\n"); file = fopen(path, "wb");
return 1; if(file == NULL) {
} printf("Failed to create output TTF file\n");
return 1;
// Now prepare output data. }
char headerBuffer[64];
int32_t headerBufferLength = sprintf( // Now prepare output data.
headerBuffer, "%i|%i|%i|", width, height, fontSize char headerBuffer[64];
); int32_t headerBufferLength = sprintf(
fwrite(headerBuffer, sizeof(char), headerBufferLength, file); headerBuffer, "%i|%i|%i|", width, height, fontSize
);
// Write output pixels. fwrite(headerBuffer, sizeof(char), headerBufferLength, file);
float outputPixels[4];
for(int32_t i = 0; i < textureSize; i++) { // Write output pixels.
outputPixels[0] = 1.0f; float outputPixels[4];
outputPixels[1] = 1.0f; for(int32_t i = 0; i < textureSize; i++) {
outputPixels[2] = 1.0f; outputPixels[0] = 1.0f;
outputPixels[3] = ((float)bitmapData[i]) / 255.0f; outputPixels[1] = 1.0f;
fwrite(outputPixels, sizeof(float), 4, file); outputPixels[2] = 1.0f;
} outputPixels[3] = ((float)bitmapData[i]) / 255.0f;
fwrite(outputPixels, sizeof(float), 4, file);
// Now write output quads data. }
fwrite(characterData, sizeof(stbtt_bakedchar), TRUETYPE_NUM_CHARS, file);
fclose(file); // Now write output quads data.
free(bitmapData); fwrite(characterData, sizeof(stbtt_bakedchar), TRUETYPE_NUM_CHARS, file);
fclose(file);
return 0; free(bitmapData);
return 0;
} }