Utils example

This commit is contained in:
2021-10-17 11:08:40 -07:00
parent b2e99bb976
commit 3fd0efb4d8
12 changed files with 272 additions and 58 deletions

12
tools/vn/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
function(tool_vn_character DEP_NAME IN OUT)
add_custom_target(${DEP_NAME}
COMMAND node ${TOOLS_DIR}/vn/character-sheet-generator.js --root="${ROOT_DIR}" --in="${ROOT_DIR}/${IN}" --out="${OUT}"
COMMENT "Adding VN Character ${FILE_NAME}"
)
endfunction()

View File

@ -0,0 +1,93 @@
const path = require('path');
const { imageCreate, imageWrite, imageLoad, imageCopy } = require('./../utils/image');
const fs = require('fs');
const xml = require('xml-js');
const { args } = require('./../utils/args');
const { mkdirp } = require('../utils/file');
// Parse Args
if(!args.root) throw new Error(`Missing root argument`);
if(!args.in) throw new Error(`Missing in argument`);
if(!args.out) throw new Error(`Missing out argument`);
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.in);
if(!fs.existsSync(file)) throw new Error(`Could not find ${file}`);
const outFile = path.resolve(args.out);
if(fs.existsSync(outFile)) return;
// Load XML
const data = xml.xml2js(fs.readFileSync(file, 'utf-8'));
const [ character ] = data.elements;
// Validate file.
if(!character.attributes.context) throw new Error(`Missing context`)
const dir = path.resolve(root, 'assets', character.attributes.context);
// Parse base and layers
const base = character.elements.find(e => e.name == 'base').attributes;
if(!base) throw new Error(`Failed to find base`);
const layers = character.elements
.filter(e => e.name == 'layer')
.map(e => e.attributes)
.map(e => ({
...e,
x: parseInt(e.x),
y: parseInt(e.y),
width: parseInt(e.width),
height: parseInt(e.height)
}))
;
(async () => {
// Load the base
const baseImage = await imageLoad(path.join(dir, base.file));
let columnsMax = 0;
let widthMax = 0;
layers.forEach((layer,row) => {
if(!layer.width || !layer.height || !layer.x || !layer.y) {
throw new Error(`Missing layer info`);
}
const layerDir = path.join(dir, layer.directory);
const scan = fs.readdirSync(layerDir);
columnsMax = Math.max(scan.length, columnsMax);
widthMax = Math.max(widthMax, layer.width);
});
// Create the output buffer
const out = imageCreate(
baseImage.width + (columnsMax * widthMax),
baseImage.height
);
// Copy the base
imageCopy(out, baseImage, 0, 0);
// Now begin copying the children, row is defined by the directory
let y = 0;
for(let row = 0; row < layers.length; row++) {
const layer = layers[row];
const layerDir = path.join(dir, layer.directory);
const scan = fs.readdirSync(layerDir);
// Column defined by the file index
for(let col = 0; col < scan.length; col++) {
const img = await imageLoad(path.join(layerDir, scan[col]));
console.log('Copying', scan[col]);
imageCopy(out, img,
baseImage.width+(col*layer.width), y,
layer
);
}
y += layer.height;
}
mkdirp(outFile);
await imageWrite(out, outFile);
})().catch(console.error);

View File

@ -1,24 +0,0 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Definitions
# Libraries
# Sources
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
target_sources(${PROJECT_NAME}
PRIVATE
${SOURCES}
${HEADERS}
)
# Includes
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)

View File

@ -1,12 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "character-sheet-maker.h"
int32_t main(int32_t argc, char *argv[]) {
}

View File

@ -1,11 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
int32_t main(int32_t argc, char *argv[]);