Started updating tools to C++
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							| @@ -1,24 +1,24 @@ | ||||
| # Copyright (c) 2021 Dominic Msters | ||||
| #  | ||||
| # This software is released under the MIT License. | ||||
| # https://opensource.org/licenses/MIT | ||||
|  | ||||
| # Texture Build Tool | ||||
| project(texturegen VERSION 1.0) | ||||
| add_executable(texturegen) | ||||
| target_sources(texturegen | ||||
|   PRIVATE | ||||
|     main.c | ||||
|     ../../utils/file.c | ||||
|     ../../utils/image.c | ||||
| ) | ||||
| target_include_directories(texturegen | ||||
|   PUBLIC | ||||
|     ${CMAKE_CURRENT_LIST_DIR}/../../ | ||||
|     ${CMAKE_CURRENT_LIST_DIR} | ||||
| ) | ||||
| target_link_libraries(texturegen | ||||
|   PUBLIC | ||||
|     ${DAWN_BUILD_HOST_LIBS} | ||||
|     stb | ||||
| # Copyright (c) 2021 Dominic Msters | ||||
| #  | ||||
| # This software is released under the MIT License. | ||||
| # https://opensource.org/licenses/MIT | ||||
|  | ||||
| # Texture Build Tool | ||||
| project(texturegen VERSION 1.0) | ||||
| add_executable(texturegen) | ||||
| target_sources(texturegen | ||||
|   PRIVATE | ||||
|     main.cpp | ||||
|     ../../utils/file.c | ||||
|     ../../utils/image.c | ||||
| ) | ||||
| target_include_directories(texturegen | ||||
|   PUBLIC | ||||
|     ${CMAKE_CURRENT_LIST_DIR}/../../ | ||||
|     ${CMAKE_CURRENT_LIST_DIR} | ||||
| ) | ||||
| target_link_libraries(texturegen | ||||
|   PUBLIC | ||||
|     ${DAWN_BUILD_HOST_LIBS} | ||||
|     stb | ||||
| ) | ||||
| @@ -1,87 +1,89 @@ | ||||
| /**
 | ||||
|  * Copyright (c) 2021 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT
 | ||||
|  */ | ||||
| 
 | ||||
| #include "../../utils/common.h" | ||||
| #include "../../utils/file.h" | ||||
| #include "../../utils/image.h" | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
|   FILE *file; | ||||
|   char path[FILENAME_MAX + 1]; | ||||
|   uint8_t headerBuffer[32]; | ||||
|   char *in; | ||||
|   char *out; | ||||
|   int w, h, channels, headerBufferLength; | ||||
|   stbi_uc *dataImageRaw, dataIn; | ||||
|   float *dataImage; | ||||
|   size_t i, len; | ||||
| 
 | ||||
|   if(argc != 3) { | ||||
|     printf("Invalid number of arguments\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   // Set up strings
 | ||||
|   in = argv[1]; | ||||
|   out = argv[2]; | ||||
|    | ||||
|   // Normalize slashes
 | ||||
|   fileNormalizeSlashes(in); | ||||
|   fileNormalizeSlashes(out); | ||||
|    | ||||
|   // Check the output doesn't already exist
 | ||||
|   sprintf(path, "%s.texture", out); | ||||
|   file = fopen(path, "rb"); | ||||
|   if(file != NULL) { | ||||
|     fclose(file); | ||||
|     return 0; | ||||
|   } | ||||
| 
 | ||||
|   // Read in original texture
 | ||||
|   file = fopen(in, "rb"); | ||||
|   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"); | ||||
|     return 1; | ||||
|   } | ||||
|   fclose(file); | ||||
| 
 | ||||
|   // Convert to floating points
 | ||||
|   len = STBI_rgb_alpha * w * h; | ||||
|   dataImage = malloc(sizeof(float) * len); | ||||
|   for(i = 0; i < len; i++) { | ||||
|     dataIn = dataImageRaw[i]; | ||||
|     dataImage[i] = ((float)dataIn) / 255.0f; | ||||
|   } | ||||
|   stbi_image_free(dataImageRaw); | ||||
| 
 | ||||
|   // Open output file
 | ||||
|   fileMkdirp(path); | ||||
|   file = fopen(path, "wb"); | ||||
|   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 texture
 | ||||
|   fwrite(dataImage, sizeof(float), len, file); | ||||
| 
 | ||||
|   // Cleanup
 | ||||
|   fclose(file); | ||||
|   free(dataImage); | ||||
| 
 | ||||
|   return 0; | ||||
| /**
 | ||||
|  * Copyright (c) 2021 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT
 | ||||
|  */ | ||||
| 
 | ||||
| extern "C" { | ||||
|   #include "../../utils/common.h" | ||||
|   #include "../../utils/file.h" | ||||
|   #include "../../utils/image.h" | ||||
| } | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
|   FILE *file; | ||||
|   char path[FILENAME_MAX + 1]; | ||||
|   uint8_t headerBuffer[32]; | ||||
|   char *in; | ||||
|   char *out; | ||||
|   int w, h, channels, headerBufferLength; | ||||
|   stbi_uc *dataImageRaw, dataIn; | ||||
|   float *dataImage; | ||||
|   size_t i, len; | ||||
| 
 | ||||
|   if(argc != 3) { | ||||
|     printf("Invalid number of arguments\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   // Set up strings
 | ||||
|   in = argv[1]; | ||||
|   out = argv[2]; | ||||
|    | ||||
|   // Normalize slashes
 | ||||
|   fileNormalizeSlashes(in); | ||||
|   fileNormalizeSlashes(out); | ||||
|    | ||||
|   // Check the output doesn't already exist
 | ||||
|   sprintf(path, "%s.texture", out); | ||||
|   file = fopen(path, "rb"); | ||||
|   if(file != NULL) { | ||||
|     fclose(file); | ||||
|     return 0; | ||||
|   } | ||||
| 
 | ||||
|   // Read in original texture
 | ||||
|   file = fopen(in, "rb"); | ||||
|   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"); | ||||
|     return 1; | ||||
|   } | ||||
|   fclose(file); | ||||
| 
 | ||||
|   // Convert to floating points
 | ||||
|   len = STBI_rgb_alpha * w * h; | ||||
|   dataImage = (float *)malloc(sizeof(float) * len); | ||||
|   for(i = 0; i < len; i++) { | ||||
|     dataIn = dataImageRaw[i]; | ||||
|     dataImage[i] = ((float)dataIn) / 255.0f; | ||||
|   } | ||||
|   stbi_image_free(dataImageRaw); | ||||
| 
 | ||||
|   // Open output file
 | ||||
|   fileMkdirp(path); | ||||
|   file = fopen(path, "wb"); | ||||
|   if(file == NULL) { | ||||
|     printf("Invalid texture file out!\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   // Write info
 | ||||
|   headerBufferLength = sprintf((char *)headerBuffer, "%i|%i|", w, h); | ||||
|   fwrite(headerBuffer, sizeof(uint8_t), headerBufferLength, file); | ||||
| 
 | ||||
|   // Write texture
 | ||||
|   fwrite(dataImage, sizeof(float), len, file); | ||||
| 
 | ||||
|   // Cleanup
 | ||||
|   fclose(file); | ||||
|   free(dataImage); | ||||
| 
 | ||||
|   return 0; | ||||
| } | ||||
| @@ -1,24 +1,24 @@ | ||||
| # Copyright (c) 2021 Dominic Msters | ||||
| #  | ||||
| # This software is released under the MIT License. | ||||
| # https://opensource.org/licenses/MIT | ||||
|  | ||||
| # Texture Build Tool | ||||
| project(tilesetgen VERSION 1.0) | ||||
| add_executable(tilesetgen) | ||||
| target_sources(tilesetgen | ||||
|   PRIVATE | ||||
|     main.c | ||||
|     ../../utils/file.c | ||||
|     ../../utils/image.c | ||||
| ) | ||||
| target_include_directories(tilesetgen | ||||
|   PUBLIC | ||||
|     ${CMAKE_CURRENT_LIST_DIR}/../../ | ||||
|     ${CMAKE_CURRENT_LIST_DIR} | ||||
| ) | ||||
| target_link_libraries(tilesetgen | ||||
|   PUBLIC | ||||
|     ${DAWN_BUILD_HOST_LIBS} | ||||
|     stb | ||||
| # Copyright (c) 2021 Dominic Msters | ||||
| #  | ||||
| # This software is released under the MIT License. | ||||
| # https://opensource.org/licenses/MIT | ||||
|  | ||||
| # Texture Build Tool | ||||
| project(tilesetgen VERSION 1.0) | ||||
| add_executable(tilesetgen) | ||||
| target_sources(tilesetgen | ||||
|   PRIVATE | ||||
|     main.cpp | ||||
|     ../../utils/file.c | ||||
|     ../../utils/image.c | ||||
| ) | ||||
| target_include_directories(tilesetgen | ||||
|   PUBLIC | ||||
|     ${CMAKE_CURRENT_LIST_DIR}/../../ | ||||
|     ${CMAKE_CURRENT_LIST_DIR} | ||||
| ) | ||||
| target_link_libraries(tilesetgen | ||||
|   PUBLIC | ||||
|     ${DAWN_BUILD_HOST_LIBS} | ||||
|     stb | ||||
| ) | ||||
| @@ -1,147 +1,149 @@ | ||||
| /**
 | ||||
|  * Copyright (c) 2022 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT
 | ||||
|  */ | ||||
| 
 | ||||
| #include "../../utils/common.h" | ||||
| #include "../../utils/file.h" | ||||
| #include "../../utils/image.h" | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
|   char *in; | ||||
|   char *out; | ||||
|   FILE *file; | ||||
|   char path[FILENAME_MAX + 1]; | ||||
|   int w, h, channels, cols, rows; | ||||
|   stbi_uc *dataImageRaw; | ||||
|   int gapX, gapY, borderX, borderY; | ||||
| 
 | ||||
|   if(argc < 5) { | ||||
|     printf("Invalid number of arguments"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   in = argv[1]; | ||||
|   out = argv[2]; | ||||
|   gapX = 0, gapY = 0, borderX = 0, borderY = 0; | ||||
| 
 | ||||
|   cols = atoi(argv[3]); | ||||
|   if(cols <= 0) { | ||||
|     printf("Columns is invalid\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   rows = atoi(argv[4]); | ||||
|   if(rows <= 0) { | ||||
|     printf("Rows is invalid"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   if(argc >= 6) { | ||||
|     gapX = atoi(argv[5]); | ||||
|     if(gapX <= 0) { | ||||
|       printf("Gap X is invalid\n"); | ||||
|       return 1; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   if(argc >= 7) { | ||||
|     gapY = atoi(argv[6]); | ||||
|     if(gapY <= 0) { | ||||
|       printf("Gap Y is invalid\n"); | ||||
|       return 1; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   if(argc >= 8) { | ||||
|     borderX = atoi(argv[7]); | ||||
|     if(borderX <= 0) { | ||||
|       printf("Border X is invalid\n"); | ||||
|       return 1; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   if(argc >= 9) { | ||||
|     borderY = atoi(argv[8]); | ||||
|     if(borderY <= 0) { | ||||
|       printf("Border Y is invalid\n"); | ||||
|       return 1; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   // Normalize slashes
 | ||||
|   fileNormalizeSlashes(in); | ||||
|   fileNormalizeSlashes(out); | ||||
| 
 | ||||
|   // Check the output doesn't already exist
 | ||||
|   sprintf(path, "%s.tileset", out); | ||||
|   file = fopen(path, "rb"); | ||||
|   if(file != NULL) { | ||||
|     fclose(file); | ||||
|     return 0; | ||||
|   } | ||||
| 
 | ||||
|   // Read in the original texture
 | ||||
|   file = fopen(in, "rb"); | ||||
|   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) { | ||||
|     printf("Failed to load input texture!\n"); | ||||
|     return 1; | ||||
|   } | ||||
|   fclose(file); | ||||
|   free(dataImageRaw); | ||||
| 
 | ||||
|   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 the division sizes (units)
 | ||||
|   float tdivX = (float)divX / (float)w; | ||||
|   float tdivY = (float)divY / (float)h; | ||||
| 
 | ||||
|   // Output buffer prep
 | ||||
|   char *buffer = malloc(sizeof(char) * (cols * rows * 48 + 48 + 48)); | ||||
|   buffer[0] = '\0'; | ||||
| 
 | ||||
|   sprintf(buffer, "%i|%i|%i|%i|", cols, rows, divX, divY); | ||||
| 
 | ||||
|   // Now prep tileset.
 | ||||
|   for(float y = 0; y < rows; y++) { | ||||
|     for(float x = 0; x < cols; x++) { | ||||
|       float ux0 = ((float)borderX + ((float)divX * x) + ((float)gapX * x)) / (float)w; | ||||
|       float ux1 = ux0 + tdivX; | ||||
|       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"); | ||||
|   if(file == NULL) { | ||||
|     free(buffer); | ||||
|     printf("Invalid tileset file out!\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   // Write and close
 | ||||
|   fwrite(buffer, sizeof(char), strlen(buffer), file); | ||||
|   fclose(file); | ||||
|   free(buffer); | ||||
| 
 | ||||
|   return 0; | ||||
| /**
 | ||||
|  * Copyright (c) 2022 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT
 | ||||
|  */ | ||||
| 
 | ||||
| extern "C" { | ||||
|   #include "../../utils/common.h" | ||||
|   #include "../../utils/file.h" | ||||
|   #include "../../utils/image.h" | ||||
| } | ||||
| 
 | ||||
| int main(int argc, char *argv[]) { | ||||
|   char *in; | ||||
|   char *out; | ||||
|   FILE *file; | ||||
|   char path[FILENAME_MAX + 1]; | ||||
|   int w, h, channels, cols, rows; | ||||
|   stbi_uc *dataImageRaw; | ||||
|   int gapX, gapY, borderX, borderY; | ||||
| 
 | ||||
|   if(argc < 5) { | ||||
|     printf("Invalid number of arguments"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   in = argv[1]; | ||||
|   out = argv[2]; | ||||
|   gapX = 0, gapY = 0, borderX = 0, borderY = 0; | ||||
| 
 | ||||
|   cols = atoi(argv[3]); | ||||
|   if(cols <= 0) { | ||||
|     printf("Columns is invalid\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   rows = atoi(argv[4]); | ||||
|   if(rows <= 0) { | ||||
|     printf("Rows is invalid"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   if(argc >= 6) { | ||||
|     gapX = atoi(argv[5]); | ||||
|     if(gapX <= 0) { | ||||
|       printf("Gap X is invalid\n"); | ||||
|       return 1; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   if(argc >= 7) { | ||||
|     gapY = atoi(argv[6]); | ||||
|     if(gapY <= 0) { | ||||
|       printf("Gap Y is invalid\n"); | ||||
|       return 1; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   if(argc >= 8) { | ||||
|     borderX = atoi(argv[7]); | ||||
|     if(borderX <= 0) { | ||||
|       printf("Border X is invalid\n"); | ||||
|       return 1; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   if(argc >= 9) { | ||||
|     borderY = atoi(argv[8]); | ||||
|     if(borderY <= 0) { | ||||
|       printf("Border Y is invalid\n"); | ||||
|       return 1; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   // Normalize slashes
 | ||||
|   fileNormalizeSlashes(in); | ||||
|   fileNormalizeSlashes(out); | ||||
| 
 | ||||
|   // Check the output doesn't already exist
 | ||||
|   sprintf(path, "%s.tileset", out); | ||||
|   file = fopen(path, "rb"); | ||||
|   if(file != NULL) { | ||||
|     fclose(file); | ||||
|     return 0; | ||||
|   } | ||||
| 
 | ||||
|   // Read in the original texture
 | ||||
|   file = fopen(in, "rb"); | ||||
|   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) { | ||||
|     printf("Failed to load input texture!\n"); | ||||
|     return 1; | ||||
|   } | ||||
|   fclose(file); | ||||
|   free(dataImageRaw); | ||||
| 
 | ||||
|   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 the division sizes (units)
 | ||||
|   float tdivX = (float)divX / (float)w; | ||||
|   float tdivY = (float)divY / (float)h; | ||||
| 
 | ||||
|   // Output buffer prep
 | ||||
|   char *buffer = (char *)malloc(sizeof(char) * (cols * rows * 48 + 48 + 48)); | ||||
|   buffer[0] = '\0'; | ||||
| 
 | ||||
|   sprintf(buffer, "%i|%i|%i|%i|", cols, rows, divX, divY); | ||||
| 
 | ||||
|   // Now prep tileset.
 | ||||
|   for(float y = 0; y < rows; y++) { | ||||
|     for(float x = 0; x < cols; x++) { | ||||
|       float ux0 = ((float)borderX + ((float)divX * x) + ((float)gapX * x)) / (float)w; | ||||
|       float ux1 = ux0 + tdivX; | ||||
|       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"); | ||||
|   if(file == NULL) { | ||||
|     free(buffer); | ||||
|     printf("Invalid tileset file out!\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   // Write and close
 | ||||
|   fwrite(buffer, sizeof(char), strlen(buffer), file); | ||||
|   fclose(file); | ||||
|   free(buffer); | ||||
| 
 | ||||
|   return 0; | ||||
| } | ||||
| @@ -1,24 +1,24 @@ | ||||
| # Copyright (c) 2021 Dominic Msters | ||||
| #  | ||||
| # This software is released under the MIT License. | ||||
| # https://opensource.org/licenses/MIT | ||||
|  | ||||
| # Texture Build Tool | ||||
| project(truetypegen VERSION 1.0) | ||||
| add_executable(truetypegen) | ||||
| target_sources(truetypegen | ||||
|   PRIVATE | ||||
|     main.c | ||||
|     ../../utils/file.c | ||||
|     ../../utils/image.c | ||||
| ) | ||||
| target_include_directories(truetypegen | ||||
|   PUBLIC | ||||
|     ${CMAKE_CURRENT_LIST_DIR}/../../ | ||||
|     ${CMAKE_CURRENT_LIST_DIR} | ||||
| ) | ||||
| target_link_libraries(truetypegen | ||||
|   PUBLIC | ||||
|     ${DAWN_BUILD_HOST_LIBS} | ||||
|     stb | ||||
| # Copyright (c) 2021 Dominic Msters | ||||
| #  | ||||
| # This software is released under the MIT License. | ||||
| # https://opensource.org/licenses/MIT | ||||
|  | ||||
| # Texture Build Tool | ||||
| project(truetypegen VERSION 1.0) | ||||
| add_executable(truetypegen) | ||||
| target_sources(truetypegen | ||||
|   PRIVATE | ||||
|     main.cpp | ||||
|     ../../utils/file.c | ||||
|     ../../utils/image.c | ||||
| ) | ||||
| target_include_directories(truetypegen | ||||
|   PUBLIC | ||||
|     ${CMAKE_CURRENT_LIST_DIR}/../../ | ||||
|     ${CMAKE_CURRENT_LIST_DIR} | ||||
| ) | ||||
| target_link_libraries(truetypegen | ||||
|   PUBLIC | ||||
|     ${DAWN_BUILD_HOST_LIBS} | ||||
|     stb | ||||
| ) | ||||
| @@ -1,141 +1,143 @@ | ||||
| /**
 | ||||
|  * Copyright (c) 2021 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT
 | ||||
|  */ | ||||
| 
 | ||||
| #include "../../utils/common.h" | ||||
| #include "../../utils/file.h" | ||||
| #include "../../utils/image.h" | ||||
| #ifndef  STB_TRUETYPE_IMPLEMENTATION | ||||
|   #define STB_TRUETYPE_IMPLEMENTATION | ||||
|   #include <stb_truetype.h> | ||||
| #endif | ||||
| 
 | ||||
| #define TRUETYPE_FIRST_CHAR 32 | ||||
| #define TRUETYPE_NUM_CHARS 96 | ||||
| 
 | ||||
| int main(int argc, char *args[]) { | ||||
|   FILE *file; | ||||
|   char path[FILENAME_MAX + 1]; | ||||
|   int32_t width, height, fontSize, textureSize; | ||||
| 
 | ||||
|   /*
 | ||||
|     args0 - PATH | ||||
|     args1 - Input Filename (TTF file) | ||||
|     args2 - Output Filename | ||||
|     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; | ||||
|   } | ||||
| 
 | ||||
|   char *fileIn = args[1]; | ||||
|   char *fileOut = args[2]; | ||||
|   char *strWidth = args[3]; | ||||
|   char *strHeight = args[4]; | ||||
|   char *strFontSize = args[5]; | ||||
| 
 | ||||
|   // Normalize slashes
 | ||||
|   fileNormalizeSlashes(fileIn); | ||||
|   fileNormalizeSlashes(fileOut); | ||||
|    | ||||
|   // Check the output doesn't already exist
 | ||||
|   sprintf(path, "%s.truetype", fileOut); | ||||
|   file = fopen(path, "rb"); | ||||
|   if(file != NULL) { | ||||
|     fclose(file); | ||||
|     return 0; | ||||
|   } | ||||
| 
 | ||||
|   width = atoi(strWidth); | ||||
|   if(width <= 0) { | ||||
|     printf("Width is invalid.\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   height = atoi(strHeight); | ||||
|   if(height <= 0) { | ||||
|     printf("Height is invalid.\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   fontSize = atoi(strFontSize); | ||||
|   if(fontSize <= 0) { | ||||
|     printf("Font size is invalid.\n"); | ||||
|     return 1; | ||||
|   } | ||||
|    | ||||
|   // Read in the TTF data
 | ||||
|   file = fopen(fileIn, "rb"); | ||||
|   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); | ||||
|   fseek(file, 0, SEEK_SET); | ||||
| 
 | ||||
|   // Read in all data
 | ||||
|   char *ttfData = malloc(sizeof(char) * fileSize); | ||||
|   size_t readSize = fread(ttfData, 1, fileSize, file); | ||||
|   fclose(file); | ||||
|   if(readSize < fileSize) { | ||||
|     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); | ||||
|   stbtt_bakedchar characterData[TRUETYPE_NUM_CHARS]; | ||||
| 
 | ||||
|   // Now parse the TTF itself.
 | ||||
|   stbtt_BakeFontBitmap( | ||||
|     ttfData, 0, (float)fontSize, bitmapData, | ||||
|     width, height, | ||||
|     TRUETYPE_FIRST_CHAR, TRUETYPE_NUM_CHARS, | ||||
|     characterData | ||||
|   ); | ||||
| 
 | ||||
|   // Prepare output file for writing.
 | ||||
|   sprintf(path, "%s.truetype", fileOut); | ||||
|   fileMkdirp(path); | ||||
|   file = fopen(path, "wb"); | ||||
|   if(file == NULL) { | ||||
|     printf("Failed to create output TTF file\n"); | ||||
|     return 1; | ||||
|   } | ||||
|    | ||||
|   // Now prepare output data.
 | ||||
|   char headerBuffer[64]; | ||||
|   int32_t headerBufferLength = sprintf( | ||||
|     headerBuffer, "%i|%i|%i|", width, height, fontSize | ||||
|   ); | ||||
|   fwrite(headerBuffer, sizeof(char), headerBufferLength, file); | ||||
| 
 | ||||
|   // Write output pixels.
 | ||||
|   float outputPixels[4]; | ||||
|   for(int32_t i = 0; i < textureSize; i++) { | ||||
|     outputPixels[0] = 1.0f; | ||||
|     outputPixels[1] = 1.0f; | ||||
|     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); | ||||
|   free(bitmapData); | ||||
| 
 | ||||
|   return 0; | ||||
| /**
 | ||||
|  * Copyright (c) 2021 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT
 | ||||
|  */ | ||||
| 
 | ||||
| extern "C" { | ||||
|   #include "../../utils/common.h" | ||||
|   #include "../../utils/file.h" | ||||
|   #include "../../utils/image.h" | ||||
|   #ifndef  STB_TRUETYPE_IMPLEMENTATION | ||||
|     #define STB_TRUETYPE_IMPLEMENTATION | ||||
|     #include <stb_truetype.h> | ||||
|   #endif | ||||
| } | ||||
| 
 | ||||
| #define TRUETYPE_FIRST_CHAR 32 | ||||
| #define TRUETYPE_NUM_CHARS 96 | ||||
| 
 | ||||
| int main(int argc, char *args[]) { | ||||
|   FILE *file; | ||||
|   char path[FILENAME_MAX + 1]; | ||||
|   int32_t width, height, fontSize, textureSize; | ||||
| 
 | ||||
|   /*
 | ||||
|     args0 - PATH | ||||
|     args1 - Input Filename (TTF file) | ||||
|     args2 - Output Filename | ||||
|     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; | ||||
|   } | ||||
| 
 | ||||
|   char *fileIn = args[1]; | ||||
|   char *fileOut = args[2]; | ||||
|   char *strWidth = args[3]; | ||||
|   char *strHeight = args[4]; | ||||
|   char *strFontSize = args[5]; | ||||
| 
 | ||||
|   // Normalize slashes
 | ||||
|   fileNormalizeSlashes(fileIn); | ||||
|   fileNormalizeSlashes(fileOut); | ||||
|    | ||||
|   // Check the output doesn't already exist
 | ||||
|   sprintf(path, "%s.truetype", fileOut); | ||||
|   file = fopen(path, "rb"); | ||||
|   if(file != NULL) { | ||||
|     fclose(file); | ||||
|     return 0; | ||||
|   } | ||||
| 
 | ||||
|   width = atoi(strWidth); | ||||
|   if(width <= 0) { | ||||
|     printf("Width is invalid.\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   height = atoi(strHeight); | ||||
|   if(height <= 0) { | ||||
|     printf("Height is invalid.\n"); | ||||
|     return 1; | ||||
|   } | ||||
| 
 | ||||
|   fontSize = atoi(strFontSize); | ||||
|   if(fontSize <= 0) { | ||||
|     printf("Font size is invalid.\n"); | ||||
|     return 1; | ||||
|   } | ||||
|    | ||||
|   // Read in the TTF data
 | ||||
|   file = fopen(fileIn, "rb"); | ||||
|   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); | ||||
|   fseek(file, 0, SEEK_SET); | ||||
| 
 | ||||
|   // Read in all data
 | ||||
|   char *ttfData = (char*)malloc(sizeof(char) * fileSize); | ||||
|   size_t readSize = fread(ttfData, 1, fileSize, file); | ||||
|   fclose(file); | ||||
|   if(readSize < fileSize) { | ||||
|     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 = (stbi_uc*)malloc(sizeof(stbi_uc) * textureSize); | ||||
|   stbtt_bakedchar characterData[TRUETYPE_NUM_CHARS]; | ||||
| 
 | ||||
|   // Now parse the TTF itself.
 | ||||
|   stbtt_BakeFontBitmap( | ||||
|     (uint8_t*)ttfData, 0, (float)fontSize, bitmapData, | ||||
|     width, height, | ||||
|     TRUETYPE_FIRST_CHAR, TRUETYPE_NUM_CHARS, | ||||
|     characterData | ||||
|   ); | ||||
| 
 | ||||
|   // Prepare output file for writing.
 | ||||
|   sprintf(path, "%s.truetype", fileOut); | ||||
|   fileMkdirp(path); | ||||
|   file = fopen(path, "wb"); | ||||
|   if(file == NULL) { | ||||
|     printf("Failed to create output TTF file\n"); | ||||
|     return 1; | ||||
|   } | ||||
|    | ||||
|   // Now prepare output data.
 | ||||
|   char headerBuffer[64]; | ||||
|   int32_t headerBufferLength = sprintf( | ||||
|     headerBuffer, "%i|%i|%i|", width, height, fontSize | ||||
|   ); | ||||
|   fwrite(headerBuffer, sizeof(char), headerBufferLength, file); | ||||
| 
 | ||||
|   // Write output pixels.
 | ||||
|   float outputPixels[4]; | ||||
|   for(int32_t i = 0; i < textureSize; i++) { | ||||
|     outputPixels[0] = 1.0f; | ||||
|     outputPixels[1] = 1.0f; | ||||
|     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); | ||||
|   free(bitmapData); | ||||
| 
 | ||||
|   return 0; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user