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

Before

Width:  |  Height:  |  Size: 175 B

After

Width:  |  Height:  |  Size: 175 B

View File

Before

Width:  |  Height:  |  Size: 737 B

After

Width:  |  Height:  |  Size: 737 B

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

Before

Width:  |  Height:  |  Size: 119 B

After

Width:  |  Height:  |  Size: 119 B

5
assets/strings.js Normal file
View File

@@ -0,0 +1,5 @@
const GAME_STRINGS = {
'HELLO': 'Hello World!\nHow are you today?'
};
module.exports = { GAME_STRINGS };

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

View File

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

View File

@@ -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,
};

View File

@@ -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[];