Make strings dynamically generated.

This commit is contained in:
2022-01-04 22:36:00 -08:00
parent 1cd4f76f50
commit d05252045a
10 changed files with 54 additions and 52 deletions

View File

@@ -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);

View File

@@ -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 = {