I hate my life
This commit is contained in:
@ -7,13 +7,10 @@ cmake_minimum_required(VERSION 3.13)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
SET(TOOL_NAME texture_generation)
|
||||
set(TOOL_TEXTURE_GENERATION_SOURCE_DIR)
|
||||
|
||||
# Build Tool
|
||||
project(${TOOL_NAME} VERSION 1.0)
|
||||
add_executable(${TOOL_NAME})
|
||||
target_sources(${TOOL_NAME}
|
||||
project(texture_generation VERSION 1.0)
|
||||
add_executable(texture_generation)
|
||||
target_sources(texture_generation
|
||||
PRIVATE
|
||||
texture_generation.c
|
||||
../utils/file.c
|
||||
@ -22,7 +19,7 @@ target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/../
|
||||
)
|
||||
target_link_libraries(${TOOL_NAME}
|
||||
target_link_libraries(texture_generation
|
||||
PUBLIC
|
||||
stb
|
||||
)
|
||||
@ -30,8 +27,8 @@ target_link_libraries(${TOOL_NAME}
|
||||
# Function for creating the target
|
||||
function(tool_texture target in out)
|
||||
add_custom_target(${target}
|
||||
COMMAND texture_generation "${ROOT_DIR}/${ASSETS_DIR}/${in}" "${ASSETS_DIR}/${out}"
|
||||
COMMAND texture_generation "${in}" "${ASSETS_BUILD_DIR}/${out}"
|
||||
COMMENT "Generating texture ${target} from ${in}"
|
||||
SOURCES ${TOOL_NAME}
|
||||
DEPENDS texture_generation ${ARGN}
|
||||
)
|
||||
endfunction()
|
@ -5,8 +5,10 @@
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../utils/common.h"
|
||||
#include "../utils/file.h"
|
||||
#include "../utils/image.h"
|
||||
|
||||
#ifndef STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
@ -22,6 +24,7 @@ int RESIZE_SCALES[RESIZE_VARIANT_COUNT] = { 1, 2, 3, 4 };
|
||||
int main(int argc, char *argv[]) {
|
||||
FILE *file;
|
||||
char path[FILENAME_MAX + 1];
|
||||
char xml[2048];
|
||||
char *in;
|
||||
char *out;
|
||||
char pathSep;
|
||||
@ -38,7 +41,7 @@ int main(int argc, char *argv[]) {
|
||||
pathSep = FILE_PATH_SEP;
|
||||
in = argv[1];
|
||||
out = argv[2];
|
||||
|
||||
|
||||
// Normalize slashes
|
||||
fileNormalizeSlashes(in);
|
||||
fileNormalizeSlashes(out);
|
||||
@ -57,6 +60,13 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
// Begin XML buffering
|
||||
xml[0] = '\0';
|
||||
sprintf(xml,
|
||||
"<texture channels=\"%i\" width=\"%i\" height=\"%i\">",
|
||||
channels, w, h
|
||||
);
|
||||
|
||||
// For each scale.
|
||||
for(i = 0; i < RESIZE_VARIANT_COUNT; i++) {
|
||||
// Resize image
|
||||
@ -68,22 +78,15 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
// Determine output path
|
||||
path[0] = '\0';
|
||||
if(getcwd(path,sizeof(path)) == NULL) {
|
||||
printf("Failed to get current dir!\n");
|
||||
stbi_image_free(dataOriginal);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// Determine Output path
|
||||
sprintf(path, "%s%c%s_%i.texture", path, FILE_PATH_SEP, out, scale);
|
||||
printf("Writing %s\n", path);
|
||||
sprintf(path, "%s_%i.texture", out, scale);
|
||||
|
||||
// Open output file
|
||||
fileMkdirp(path);
|
||||
printf("OPOut %s\n", path);
|
||||
|
||||
file = fopen(path, "wb");
|
||||
if(file == NULL) {
|
||||
printf("Invalid file out!\n");
|
||||
printf("Invalid texture file out!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -93,10 +96,33 @@ int main(int argc, char *argv[]) {
|
||||
// Cleanup
|
||||
fclose(file);
|
||||
free(dataResized);
|
||||
|
||||
// Buffer XML info.
|
||||
sprintf(xml,
|
||||
"%s\n <texture-scale scale=\"%i\" width=\"%i\" height=\"%i\" asset=\"%s_%i.texture\" />",
|
||||
xml, scale, rw, rh, out, scale
|
||||
);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
stbi_image_free(dataOriginal);
|
||||
|
||||
// Finalize XML
|
||||
sprintf(xml, "%s\n</texture>", xml);
|
||||
|
||||
// Determine XML path
|
||||
path[0] = '\0';
|
||||
sprintf(path, "%s.xml", out);
|
||||
|
||||
// Write XML
|
||||
fileMkdirp(path);
|
||||
file = fopen(path, "w");
|
||||
if(file == NULL) {
|
||||
printf("Invalid XML File Out!\n");
|
||||
return 1;
|
||||
}
|
||||
fwrite(xml, sizeof(char), strlen(xml), file);
|
||||
fclose(file);
|
||||
|
||||
return 0;
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
|
||||
function(tool_assets args)
|
||||
add_custom_target(assets
|
||||
COMMAND tar -C ./assets -czvf assets.tar.gz *
|
||||
COMMAND tar -C ${ASSETS_BUILD_DIR} -czvf assets.tar.gz *
|
||||
DEPENDS ${ARGV}
|
||||
COMMENT "Compressing Assets"
|
||||
)
|
||||
@ -23,7 +23,7 @@ function(tool_copy target)
|
||||
LIST(GET ARGV ${indexnext} to)
|
||||
LIST(APPEND LOOP_DEPENDENCIES ${LOOP_TARGET})
|
||||
add_custom_command(OUTPUT ${LOOP_TARGET}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy "${ROOT_DIR}/${ASSETS_DIR}/${from}" "${ASSETS_DIR}/${to}"
|
||||
COMMAND ${CMAKE_COMMAND} -E copy "${from}" "${ASSETS_BUILD_DIR}/${to}"
|
||||
COMMENT "Copying ${from} => ${to}"
|
||||
)
|
||||
endforeach()
|
||||
|
@ -47,4 +47,12 @@ void fileMkdirp(char *path) {
|
||||
buffer[i] = '\0';
|
||||
_mkdir(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void assetReadString(FILE *file, char *buffer) {
|
||||
size_t length;
|
||||
fseek(file, 0, SEEK_END);// Seek to the end
|
||||
length = ftell(file);// Get our current position (the end)
|
||||
fseek(file, 0, SEEK_SET);// Reset the seek
|
||||
fread(buffer, 1, length, file);// Read all the bytes
|
||||
}
|
@ -19,4 +19,6 @@
|
||||
|
||||
void fileNormalizeSlashes(char *string);
|
||||
|
||||
void fileMkdirp(char *path);
|
||||
void fileMkdirp(char *path);
|
||||
|
||||
void assetReadString(FILE *file, char *buffer);
|
8
tools/utils/image.h
Normal file
8
tools/utils/image.h
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
94
tools/utils/xml.c
Normal file
94
tools/utils/xml.c
Normal file
@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "xml.h"
|
||||
|
||||
void xmlParseElement(xmlnode_t *node, char *string) {
|
||||
char c;
|
||||
int32_t i, j;
|
||||
uint8_t state;
|
||||
|
||||
node->attributeCount = 0;
|
||||
|
||||
i = 0;
|
||||
state = XML_STATE_NOTHING;
|
||||
while(c = string[i++]) {
|
||||
switch(state) {
|
||||
case XML_STATE_NOTHING:
|
||||
if(c != '<') continue;
|
||||
node->start = string + (i - 1);
|
||||
state = XML_STATE_PARSING_NAME;
|
||||
break;
|
||||
|
||||
case XML_STATE_PARSING_NAME:
|
||||
if(c == ' ' || c == '\n' || c == '\r') continue;
|
||||
|
||||
j = i - 1;
|
||||
while(c = string[j++]) {
|
||||
if(c == ' ') break;
|
||||
node->name[j] = c;
|
||||
}
|
||||
i = j;
|
||||
state = XML_STATE_PARSING_ATTRIBUTES;
|
||||
break;
|
||||
|
||||
case XML_STATE_PARSING_ATTRIBUTES:
|
||||
if(c == ' ' || c == '\n' || c == '\r') continue;
|
||||
if(c == '>') {
|
||||
node->internal = string + i;
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse Name
|
||||
node->attributeNames[node->attributeCount] = string + (i - 1);
|
||||
node->attributeNameLengths[node->attributeCount] = 0;
|
||||
while(c == ' ' && c == '\n' && c == '\r' && c != '>' && c != '=' && c != '\0') {
|
||||
c = string[i++];
|
||||
node->attributeNameLengths[node->attributeCount]++;
|
||||
}
|
||||
|
||||
if(c == '>') {
|
||||
i--;
|
||||
node->attributeValues[node->attributeCount] = NULL;
|
||||
node->attributeValueLengths[node->attributeCount] = 0;
|
||||
node->attributeCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Wait for = sign
|
||||
while(c == ' ' || c == '\n' || c == '\r') c = string[i++];
|
||||
|
||||
// Handle booleans
|
||||
if(c != '=') {
|
||||
node->attributeValues[node->attributeCount] = NULL;
|
||||
node->attributeValueLengths[node->attributeCount] = 0;
|
||||
node->attributeCount++;
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
|
||||
node->attributeValues[node->attributeCount] = string + i;
|
||||
node->attributeValueLengths[node->attributeCount] = 0;
|
||||
do {
|
||||
c = string[i++];
|
||||
node->attributeNameLengths[node->attributeCount]++;
|
||||
if(c == '\0' || c == '"') break;
|
||||
if(c == '\\') {
|
||||
i++;
|
||||
node->attributeNameLengths[node->attributeCount]++;
|
||||
}
|
||||
} while(c);
|
||||
|
||||
node->attributeCount++;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
34
tools/utils/xml.h
Normal file
34
tools/utils/xml.h
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
#include "file.h"
|
||||
|
||||
#define XML_NODE_CHILD_MAX 32
|
||||
#define XML_NODE_NAME_MAX 32
|
||||
#define XML_NODE_ATTRIBUTES_MAX 32
|
||||
|
||||
#define XML_STATE_NOTHING 0x00
|
||||
#define XML_STATE_PARSING_NAME 0x01
|
||||
#define XML_STATE_PARSING_ATTRIBUTES 0x02
|
||||
|
||||
typedef struct {
|
||||
char *start;
|
||||
char *internal;
|
||||
char name[XML_NODE_NAME_MAX];
|
||||
|
||||
char *attributeNames[XML_NODE_ATTRIBUTES_MAX];
|
||||
uint8_t attributeNameLengths[XML_NODE_ATTRIBUTES_MAX];
|
||||
char *attributeValues[XML_NODE_ATTRIBUTES_MAX];
|
||||
uint8_t attributeValueLengths[XML_NODE_ATTRIBUTES_MAX];
|
||||
uint8_t attributeCount;
|
||||
} xmlnode_t;
|
||||
|
||||
|
||||
|
||||
void xmlParseElement(xmlnode_t *node, char *string);
|
@ -3,20 +3,38 @@
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
function(tool_vn_character DEP_NAME IN OUT)
|
||||
add_custom_command(
|
||||
OUTPUT ${TEMP_DIR}/vn/${DEP_NAME}.c ${TEMP_DIR}/vn/${DEP_NAME}.h
|
||||
COMMAND node ${TOOLS_DIR}/vn/character-sheet-generator.js --assets="${ASSETS_DIR}" --root="${ROOT_DIR}" --temp="${TEMP_DIR}" --in="${IN}" --out="${OUT}" --dep="${DEP_NAME}"
|
||||
COMMENT "Generating VN Character ${DEP_NAME}"
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
# Build Tool
|
||||
project(character_generator VERSION 1.0)
|
||||
add_executable(character_generator)
|
||||
target_sources(character_generator
|
||||
PRIVATE
|
||||
character_generator.c
|
||||
../utils/file.c
|
||||
../utils/xml.c
|
||||
)
|
||||
target_include_directories(character_generator
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/../
|
||||
)
|
||||
target_link_libraries(character_generator
|
||||
PUBLIC
|
||||
stb
|
||||
)
|
||||
|
||||
# Function Target
|
||||
function(tool_vn_character target in out)
|
||||
add_custom_target(vn_character_${target}
|
||||
COMMAND character_generator "${in}" "${TEMP_DIR}/${out}"
|
||||
COMMENT "Generating character ${target} from ${in}"
|
||||
DEPENDS character_generator ${ARGN}
|
||||
)
|
||||
|
||||
tool_texture(${target}
|
||||
${TEMP_DIR}/${out}.png ${out}
|
||||
vn_character_${target}
|
||||
)
|
||||
|
||||
# target_sources(${PROJECT_NAME}
|
||||
# PRIVATE
|
||||
# ${CMAKE_CURRENT_BINARY_DIR}/${TEMP_DIR}/vn/${DEP_NAME}.c
|
||||
# ${CMAKE_CURRENT_BINARY_DIR}/${TEMP_DIR}/vn/${DEP_NAME}.h
|
||||
# )
|
||||
# target_include_directories(${PROJECT_NAME}
|
||||
# PUBLIC
|
||||
# ${CMAKE_CURRENT_BINARY_DIR}/${TEMP_DIR}/vn/
|
||||
# )
|
||||
endfunction()
|
@ -6,9 +6,7 @@ const { args } = require('./../utils/args');
|
||||
const { mkdirp } = require('../utils/file');
|
||||
|
||||
// Parse Args
|
||||
if(!args.root) throw new Error(`Missing root argument`);
|
||||
if(!args.assets) throw new Error(`Missing assets argument`);
|
||||
if(!args.temp) throw new Error(`Missing temp argument`);
|
||||
// if(!args.temp) throw new Error(`Missing temp argument`);
|
||||
if(!args.in) throw new Error(`Missing in argument`);
|
||||
if(!args.out) throw new Error(`Missing out argument`);
|
||||
if(!args.dep) throw new Error(`Missing dep argument`);
|
||||
@ -16,14 +14,16 @@ if(!args.in.endsWith('xml')) throw new Error(`Invalid in XML`);
|
||||
if(!args.out.endsWith('png')) throw new Error(`Invalid out PNG`);
|
||||
|
||||
// Determine in and out.
|
||||
const root = path.resolve(args.root);
|
||||
const file = path.resolve(args.root, args.assets, args.in);
|
||||
const outFile = path.resolve(args.assets, args.out);
|
||||
const file = path.resolve(args.in);
|
||||
const outFile = path.resolve(args.out);
|
||||
|
||||
const cOut = path.resolve(args.temp, 'vn', `${args.dep}.c`);
|
||||
const hOut = path.resolve(args.temp, 'vn', `${args.dep}.h`);
|
||||
console.log(outFile);
|
||||
|
||||
// const cOut = path.resolve(args.temp, 'vn', `${args.dep}.c`);
|
||||
// const hOut = path.resolve(args.temp, 'vn', `${args.dep}.h`);
|
||||
if(!fs.existsSync(file)) throw new Error(`Could not find ${file}`);
|
||||
if(fs.existsSync(outFile) && fs.existsSync(cOut) && fs.existsSync(hOut)) return;
|
||||
// if(fs.existsSync(outFile) && fs.existsSync(cOut) && fs.existsSync(hOut)) return;
|
||||
if(fs.existsSync(outFile)) return;
|
||||
|
||||
// Load XML
|
||||
const data = xml.xml2js(fs.readFileSync(file, 'utf-8'));
|
||||
@ -31,7 +31,7 @@ const [ character ] = data.elements;
|
||||
|
||||
// Validate file.
|
||||
if(!character.attributes.context) throw new Error(`Missing context`)
|
||||
const dir = path.resolve(root, args.assets, character.attributes.context);
|
||||
const dir = path.resolve(path.dirname(file), character.attributes.context);
|
||||
|
||||
// Parse base and layers
|
||||
const base = character.elements.find(e => e.name == 'base').attributes;
|
||||
@ -107,33 +107,33 @@ const layers = character.elements
|
||||
mkdirp(outFile);
|
||||
await imageWrite(out, outFile);
|
||||
|
||||
mkdirp(cOut);
|
||||
let name = character.attributes.name || args.name || args.dep;
|
||||
|
||||
// mkdirp(cOut);
|
||||
// fs.writeFileSync(cOut, `
|
||||
// #include "${args.dep}.h"
|
||||
|
||||
fs.writeFileSync(cOut, `
|
||||
#include "${args.dep}.h"
|
||||
// void vnCharacter${name}Init(vncharacter_t *vnc, texture_t *texture) {
|
||||
// assetTextureLoad(texture, VN_CHARACTER_${name.toUpperCase()}_TEXTURE);
|
||||
// vnCharacterInit(vnc, texture);
|
||||
|
||||
void vnCharacter${name}Init(vncharacter_t *vnc, texture_t *texture) {
|
||||
assetTextureLoad(texture, VN_CHARACTER_${name.toUpperCase()}_TEXTURE);
|
||||
vnCharacterInit(vnc, texture);
|
||||
// // Base Layer
|
||||
// vnCharacterLayerAdd(vnc, 1, 0, 0, 0, 0, ${baseImage.width}, ${baseImage.height});
|
||||
|
||||
// Base Layer
|
||||
vnCharacterLayerAdd(vnc, 1, 0, 0, 0, 0, ${baseImage.width}, ${baseImage.height});
|
||||
// // Layers
|
||||
// ${strLayers}
|
||||
// }
|
||||
// `);
|
||||
|
||||
// Layers
|
||||
${strLayers}
|
||||
}
|
||||
`);
|
||||
// fs.writeFileSync(hOut, `
|
||||
// #pragma once
|
||||
// #include <libs.h>
|
||||
// #include <vn/vncharacter.h>
|
||||
// #include <display/texture.h>
|
||||
// #include <file/asset.h>
|
||||
|
||||
fs.writeFileSync(hOut, `
|
||||
#pragma once
|
||||
#include <libs.h>
|
||||
#include <vn/vncharacter.h>
|
||||
#include <display/texture.h>
|
||||
#include <file/asset.h>
|
||||
|
||||
#define VN_CHARACTER_${name.toUpperCase()}_TEXTURE "${args.out}"
|
||||
// #define VN_CHARACTER_${name.toUpperCase()}_TEXTURE "${args.out}"
|
||||
|
||||
void vnCharacter${name}Init(vncharacter_t *vnc, texture_t *texture);
|
||||
`);
|
||||
// void vnCharacter${name}Init(vncharacter_t *vnc, texture_t *texture);
|
||||
// `);
|
||||
})().catch(console.error);
|
46
tools/vn/character_generator.c
Normal file
46
tools/vn/character_generator.c
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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"
|
||||
#include "../utils/xml.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
FILE *file;
|
||||
char *in;
|
||||
char *out;
|
||||
char xmlBuffer[2048];
|
||||
|
||||
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);
|
||||
|
||||
|
||||
// Read in XML file
|
||||
file = fopen(in, "rb");
|
||||
if(file == NULL) {
|
||||
printf("Failed to open file!\n");
|
||||
return 1;
|
||||
}
|
||||
assetReadString(file, xmlBuffer);
|
||||
|
||||
xmlnode_t node;
|
||||
xmlParseElement(&node, xmlBuffer);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user