Added texture, assets, tools and texture loading.
This commit is contained in:
25
src/dawntools/display/CMakeLists.txt
Normal file
25
src/dawntools/display/CMakeLists.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
# Copyright (c) 2021 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
add_subdirectory(texturegen)
|
||||
add_subdirectory(truetypegen)
|
||||
|
||||
# Texture Tool
|
||||
function(tool_texture target in out)
|
||||
add_custom_target(${target}
|
||||
COMMAND texturegen "${DAWN_ASSETS_SOURCE_DIR}/${in}" "${DAWN_ASSETS_BUILD_DIR}/${out}"
|
||||
COMMENT "Generating texture ${target} from ${in}"
|
||||
DEPENDS texturegen
|
||||
)
|
||||
endfunction()
|
||||
|
||||
# TrueType Tool
|
||||
function(tool_truetype target in out width height fontSize)
|
||||
add_custom_target(${target}
|
||||
COMMAND truetypegen "${in}" "${DAWN_ASSETS_BUILD_DIR}/${out}" "${width}" "${height}" "${fontSize}"
|
||||
COMMENT "Generating truetype ${target} from ${in}"
|
||||
DEPENDS truetypegen
|
||||
)
|
||||
endfunction()
|
24
src/dawntools/display/texturegen/CMakeLists.txt
Normal file
24
src/dawntools/display/texturegen/CMakeLists.txt
Normal 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(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
|
||||
${LIBS_PLATFORM}
|
||||
stb
|
||||
)
|
87
src/dawntools/display/texturegen/main.c
Normal file
87
src/dawntools/display/texturegen/main.c
Normal file
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
24
src/dawntools/display/truetypegen/CMakeLists.txt
Normal file
24
src/dawntools/display/truetypegen/CMakeLists.txt
Normal 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(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
|
||||
${LIBS_PLATFORM}
|
||||
stb
|
||||
)
|
140
src/dawntools/display/truetypegen/main.c
Normal file
140
src/dawntools/display/truetypegen/main.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* 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) {
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user