From d05252045ae85f84ad04b0e8ae18e9b255d3cb0f Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 4 Jan 2022 22:36:00 -0800 Subject: [PATCH] Make strings dynamically generated. --- {images => assets/images}/border.png | Bin {images => assets/images}/font.png | Bin {images => assets/images}/sm.png | Bin {images => assets/images}/tileset.png | Bin assets/strings.js | 5 +++ scripts/build.js | 54 ++++++++++++++++---------- scripts/string2gb.js | 20 +++++++--- src/main.c | 2 +- src/strings.c | 13 ------- src/strings.h | 12 ------ 10 files changed, 54 insertions(+), 52 deletions(-) rename {images => assets/images}/border.png (100%) rename {images => assets/images}/font.png (100%) rename {images => assets/images}/sm.png (100%) rename {images => assets/images}/tileset.png (100%) create mode 100644 assets/strings.js delete mode 100644 src/strings.c delete mode 100644 src/strings.h diff --git a/images/border.png b/assets/images/border.png similarity index 100% rename from images/border.png rename to assets/images/border.png diff --git a/images/font.png b/assets/images/font.png similarity index 100% rename from images/font.png rename to assets/images/font.png diff --git a/images/sm.png b/assets/images/sm.png similarity index 100% rename from images/sm.png rename to assets/images/sm.png diff --git a/images/tileset.png b/assets/images/tileset.png similarity index 100% rename from images/tileset.png rename to assets/images/tileset.png diff --git a/assets/strings.js b/assets/strings.js new file mode 100644 index 0000000..a541052 --- /dev/null +++ b/assets/strings.js @@ -0,0 +1,5 @@ +const GAME_STRINGS = { + 'HELLO': 'Hello World!\nHow are you today?' +}; + +module.exports = { GAME_STRINGS }; \ No newline at end of file diff --git a/scripts/build.js b/scripts/build.js index 030b324..74880dc 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -2,7 +2,9 @@ const fs = require('fs'); const path = require('path'); const process = require('process'); const { spawnSync, execSync } = require('child_process'); -const rimraf = require('rimraf'); +const { png2gb } = require('./png2gb'); +const { string2gb } = require('./string2gb'); +const { GAME_STRINGS } = require('./../assets/strings'); const DIR_BUILD = path.resolve('build'); const DIR_GENERATED = path.resolve(DIR_BUILD, 'generated'); @@ -13,7 +15,9 @@ const FILE_OUT = path.resolve(DIR_BUILD, 'Penny.gb'); const FILE_LINKFILE = path.join(DIR_BUILD, `linkflile.lk`); const LCC = path.join(DIR_GBDK, 'bin', 'lcc'); -const LCCFLAGS = ''; +const LCCFLAGS = `-I${DIR_GENERATED} -I${DIR_SRC}`; + +const compiledSources = []; // Create build dirs [ @@ -35,9 +39,7 @@ const buildSourceFiles = directory => { return; } - if(file.endsWith('.c')) { - sources.push(file.split(DIR_SRC).join('')); - } + if(file.endsWith('.c')) sources.push(fullPath); }); return sources; @@ -55,31 +57,43 @@ const logOut = (out, buffer) => { buffer(str); } +const compileC = (cFile) => { + const fileNameOut = path.basename(cFile, '.c') + '.o'; + const fileOut = path.join(DIR_OBJ, fileNameOut); -// Get a list of sources and build each of them prior to linking. -const allSources = buildSourceFiles(DIR_SRC); -const compiledSources = []; -for(let i = 0; i < allSources.length; i++) { - const source = allSources[i]; - const fileIn = path.join(DIR_SRC, source); - const fileNameOut = path.basename(fileIn, '.c') + '.o'; - const sourceRelativePath = path.dirname(source).split(DIR_SRC).join(''); - const fileOut = path.join(DIR_OBJ, sourceRelativePath, fileNameOut); + compiledSources.push(path.join(fileNameOut)); - compiledSources.push(path.join(sourceRelativePath, fileNameOut)); - - if(fs.existsSync(fileOut)) continue; + if(fs.existsSync(fileOut)) return; let result; try { - result = execSync(`${LCC} ${LCCFLAGS} -c -o ${fileOut} ${fileIn}`); + result = execSync(`${LCC} ${LCCFLAGS} -c -o ${fileOut} ${cFile}`); logOut(result, console.log); } catch(e) { - logOut(e, console.error); - process.exit(1); + logOut(e, e => { + console.error(e); + process.exit(1); + }); + throw e; } } +// Generate strings +Object.entries(GAME_STRINGS).forEach(entry => { + const [ name, str ] = entry; + const { fileH, fileC } = string2gb(str, name, DIR_GENERATED); + compileC(fileC); +}) + +// Get a list of sources and build each of them prior to linking. +const allSources = buildSourceFiles(DIR_SRC); +for(let i = 0; i < allSources.length; i++) { + compileC(allSources[i]); +} + +// Gen imagery + + // Generate a linkfile. fs.writeFileSync(FILE_LINKFILE, compiledSources.map(cs=>{ return path.join(DIR_OBJ, cs); diff --git a/scripts/string2gb.js b/scripts/string2gb.js index 7667ac3..ff0cce8 100644 --- a/scripts/string2gb.js +++ b/scripts/string2gb.js @@ -1,4 +1,5 @@ const fs = require('fs'); +const path = require('path'); const { arrayToString } = require('./util'); const FONT_CHARACTER_FIRST = 33; @@ -10,18 +11,25 @@ const getCodeFrom = l => { return cc - FONT_CHARACTER_FIRST + FONT_DATA_POSITION } -const string2gb = (string, name) => { +const string2gb = (string, name, dirOut) => { const letters = []; for(let i = 0; i < string.length; i++) { letters.push(getCodeFrom(string[i])); } - out = '#include "libs.h"\n\n'; - out += `#define STR_${name}_LENGTH ${string.length}\n`; - out += `extern const uint8_t STR_${name}_DATA[];\n\n\n` - out += `const uint8_t STR_${name}_DATA[] = {\n` + arrayToString(letters) + `\n};`; + let outH = '#include "libs.h"\n\n'; + outH += `#define STR_${name}_LENGTH ${string.length}\n`; + outH += `extern const uint8_t STR_${name}_DATA[];`; + + let outC = `#include "${name}.h"\n\n`; + outC += `const uint8_t STR_${name}_DATA[] = {\n` + arrayToString(letters) + `\n};`; + + const fileH = path.join(dirOut, `${name}.h`); + const fileC = path.join(dirOut, `${name}.c`); - fs.writeFileSync('out.c', out); + fs.writeFileSync(fileH, outH); + fs.writeFileSync(fileC, outC); + return { fileH, fileC }; } module.exports = { diff --git a/src/main.c b/src/main.c index 337179f..8d310af 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ #include "time.h" #include "common_tiles.h" #include "penny.h" -#include "strings.h" +#include "HELLO.h" void main() { int16_t j; diff --git a/src/strings.c b/src/strings.c deleted file mode 100644 index 253fdcf..0000000 --- a/src/strings.c +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Copyright (c) 2022 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "strings.h" - -const uint8_t STR_HELLO_DATA[] = { - 0x2B,0x48,0x4F,0x4F,0x52,0x20,0x3A,0x52,0x55,0x4F,0x47,0x04,0x0A,0x2B,0x52,0x5A, - 0x20,0x44,0x55,0x48,0x20,0x5C,0x52,0x58,0x20,0x57,0x52,0x47,0x44,0x5C,0x22, -}; \ No newline at end of file diff --git a/src/strings.h b/src/strings.h deleted file mode 100644 index a7fef4b..0000000 --- a/src/strings.h +++ /dev/null @@ -1,12 +0,0 @@ -/** - * Copyright (c) 2022 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "libs.h" - -#define STR_HELLO_LENGTH 31 -extern const uint8_t STR_HELLO_DATA[];