Added tileset loading.

This commit is contained in:
2022-12-05 13:57:18 -08:00
parent bcdb0742f3
commit 8059158bae
10 changed files with 314 additions and 4 deletions

View File

@ -4,6 +4,7 @@
# https://opensource.org/licenses/MIT
add_subdirectory(texturegen)
add_subdirectory(tilesetgen)
add_subdirectory(truetypegen)
# Texture Tool
@ -15,6 +16,16 @@ function(tool_texture target in)
)
endfunction()
# Tileset Tool
function(tool_tileset targetTileset targetTexture in cols rows)
tool_texture(${targetTexture} ${in})
add_custom_target(${targetTileset}
COMMAND tilesetgen "${DAWN_ASSETS_SOURCE_DIR}/${in}" "${DAWN_ASSETS_BUILD_DIR}/${targetTileset}" "${cols}" "${rows}"
COMMENT "Generating tileset ${target} from ${in}"
DEPENDS tilesetgen ${targetTexture}
)
endfunction()
# TrueType Tool
function(tool_truetype target in out width height fontSize)
add_custom_target(${target}

View File

@ -0,0 +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
)

View File

@ -0,0 +1,153 @@
/**
* 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)
float divX = (
(float)w - ((float)borderX * 2.0f) -
((float)gapX * ((float)cols - 1))
) / cols;
float divY = (
(float)h - ((float)borderY * 2.0f) -
((float)gapY * ((float)rows - 1))
) / rows;
// Calculate the division sizes (units)
float tdivX = divX / (float)w;
float tdivY = divY / (float)h;
// Output buffer prep
char *buffer = malloc(sizeof(char) * (cols * rows * 48 + 48 + 48));
buffer[0] = '\0';
sprintf(buffer, "%i|%i|", cols, rows);
// Now prep tileset.
for(int y = 0; y < rows; y++) {
for(int x = 0; x < cols; x++) {
float ux0 = (borderX + (divX * x) + (gapX * x)) / w;
float ux1 = ux0 + tdivX;
float uy0 = (borderY + (divY * y) + (gapY * y)) / 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;
}