Make strings dynamically generated.
Before Width: | Height: | Size: 175 B After Width: | Height: | Size: 175 B |
Before Width: | Height: | Size: 737 B After Width: | Height: | Size: 737 B |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 119 B After Width: | Height: | Size: 119 B |
5
assets/strings.js
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
const GAME_STRINGS = {
|
||||||
|
'HELLO': 'Hello World!\nHow are you today?'
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = { GAME_STRINGS };
|
@@ -2,7 +2,9 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const process = require('process');
|
const process = require('process');
|
||||||
const { spawnSync, execSync } = require('child_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_BUILD = path.resolve('build');
|
||||||
const DIR_GENERATED = path.resolve(DIR_BUILD, 'generated');
|
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 FILE_LINKFILE = path.join(DIR_BUILD, `linkflile.lk`);
|
||||||
|
|
||||||
const LCC = path.join(DIR_GBDK, 'bin', 'lcc');
|
const LCC = path.join(DIR_GBDK, 'bin', 'lcc');
|
||||||
const LCCFLAGS = '';
|
const LCCFLAGS = `-I${DIR_GENERATED} -I${DIR_SRC}`;
|
||||||
|
|
||||||
|
const compiledSources = [];
|
||||||
|
|
||||||
// Create build dirs
|
// Create build dirs
|
||||||
[
|
[
|
||||||
@@ -35,9 +39,7 @@ const buildSourceFiles = directory => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(file.endsWith('.c')) {
|
if(file.endsWith('.c')) sources.push(fullPath);
|
||||||
sources.push(file.split(DIR_SRC).join(''));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return sources;
|
return sources;
|
||||||
@@ -55,31 +57,43 @@ const logOut = (out, buffer) => {
|
|||||||
buffer(str);
|
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.
|
compiledSources.push(path.join(fileNameOut));
|
||||||
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(sourceRelativePath, fileNameOut));
|
if(fs.existsSync(fileOut)) return;
|
||||||
|
|
||||||
if(fs.existsSync(fileOut)) continue;
|
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
try {
|
try {
|
||||||
result = execSync(`${LCC} ${LCCFLAGS} -c -o ${fileOut} ${fileIn}`);
|
result = execSync(`${LCC} ${LCCFLAGS} -c -o ${fileOut} ${cFile}`);
|
||||||
logOut(result, console.log);
|
logOut(result, console.log);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
logOut(e, console.error);
|
logOut(e, e => {
|
||||||
process.exit(1);
|
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.
|
// Generate a linkfile.
|
||||||
fs.writeFileSync(FILE_LINKFILE, compiledSources.map(cs=>{
|
fs.writeFileSync(FILE_LINKFILE, compiledSources.map(cs=>{
|
||||||
return path.join(DIR_OBJ, cs);
|
return path.join(DIR_OBJ, cs);
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
const { arrayToString } = require('./util');
|
const { arrayToString } = require('./util');
|
||||||
|
|
||||||
const FONT_CHARACTER_FIRST = 33;
|
const FONT_CHARACTER_FIRST = 33;
|
||||||
@@ -10,18 +11,25 @@ const getCodeFrom = l => {
|
|||||||
return cc - FONT_CHARACTER_FIRST + FONT_DATA_POSITION
|
return cc - FONT_CHARACTER_FIRST + FONT_DATA_POSITION
|
||||||
}
|
}
|
||||||
|
|
||||||
const string2gb = (string, name) => {
|
const string2gb = (string, name, dirOut) => {
|
||||||
const letters = [];
|
const letters = [];
|
||||||
for(let i = 0; i < string.length; i++) {
|
for(let i = 0; i < string.length; i++) {
|
||||||
letters.push(getCodeFrom(string[i]));
|
letters.push(getCodeFrom(string[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
out = '#include "libs.h"\n\n';
|
let outH = '#include "libs.h"\n\n';
|
||||||
out += `#define STR_${name}_LENGTH ${string.length}\n`;
|
outH += `#define STR_${name}_LENGTH ${string.length}\n`;
|
||||||
out += `extern const uint8_t STR_${name}_DATA[];\n\n\n`
|
outH += `extern const uint8_t STR_${name}_DATA[];`;
|
||||||
out += `const uint8_t STR_${name}_DATA[] = {\n` + arrayToString(letters) + `\n};`;
|
|
||||||
|
|
||||||
fs.writeFileSync('out.c', out);
|
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(fileH, outH);
|
||||||
|
fs.writeFileSync(fileC, outC);
|
||||||
|
return { fileH, fileC };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@@ -10,7 +10,7 @@
|
|||||||
#include "time.h"
|
#include "time.h"
|
||||||
#include "common_tiles.h"
|
#include "common_tiles.h"
|
||||||
#include "penny.h"
|
#include "penny.h"
|
||||||
#include "strings.h"
|
#include "HELLO.h"
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
int16_t j;
|
int16_t j;
|
||||||
|
@@ -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,
|
|
||||||
};
|
|
@@ -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[];
|
|