Reduced some number crunching in the generator

This commit is contained in:
2023-07-31 02:05:21 +10:00
parent b3179d96f2
commit 577aa47fbd
5 changed files with 50 additions and 35 deletions

View File

@@ -132,9 +132,6 @@
xcopy "$(SDL2ImageDir)\lib\x64\SDL2_image.dll" "$(OutDir)" /Y /I /E xcopy "$(SDL2ImageDir)\lib\x64\SDL2_image.dll" "$(OutDir)" /Y /I /E
xcopy "$(SDL2MixerDir)\lib\x64\SDL2_mixer.dll" "$(OutDir)" /Y /I /E xcopy "$(SDL2MixerDir)\lib\x64\SDL2_mixer.dll" "$(OutDir)" /Y /I /E
xcopy "$(SDL2TTFDir)\lib\x64\SDL2_ttf.dll" "$(OutDir)" /Y /I /E xcopy "$(SDL2TTFDir)\lib\x64\SDL2_ttf.dll" "$(OutDir)" /Y /I /E
del "$(OutDir)$(ProjectName).exp"
del "$(OutDir)$(ProjectName).pdb"
rmdir "$(OutDir)optional"
xcopy "$(ProjectDir)assets" "$(Outdir)assets" /Y /I /E</Command> xcopy "$(ProjectDir)assets" "$(Outdir)assets" /Y /I /E</Command>
</PostBuildEvent> </PostBuildEvent>
<Bscmake> <Bscmake>
@@ -168,9 +165,6 @@ xcopy "$(ProjectDir)assets" "$(Outdir)assets" /Y /I /E</Command>
xcopy "$(SDL2ImageDir)\lib\x64\SDL2_image.dll" "$(OutDir)" /Y /I /E xcopy "$(SDL2ImageDir)\lib\x64\SDL2_image.dll" "$(OutDir)" /Y /I /E
xcopy "$(SDL2MixerDir)\lib\x64\SDL2_mixer.dll" "$(OutDir)" /Y /I /E xcopy "$(SDL2MixerDir)\lib\x64\SDL2_mixer.dll" "$(OutDir)" /Y /I /E
xcopy "$(SDL2TTFDir)\lib\x64\SDL2_ttf.dll" "$(OutDir)" /Y /I /E xcopy "$(SDL2TTFDir)\lib\x64\SDL2_ttf.dll" "$(OutDir)" /Y /I /E
del "$(OutDir)$(ProjectName).exp"
del "$(OutDir)$(ProjectName).pdb"
rmdir "$(OutDir)optional"
xcopy "$(ProjectDir)assets" "$(Outdir)assets" /Y /I /E</Command> xcopy "$(ProjectDir)assets" "$(Outdir)assets" /Y /I /E</Command>
</PostBuildEvent> </PostBuildEvent>
<Bscmake> <Bscmake>

2
Box

Submodule Box updated: 4297e15959...cdfc42cf84

View File

@@ -61,8 +61,8 @@ fn generateTilemapData(rng: opaque) {
//generate a grid filled with only empty tiles, as a starting point //generate a grid filled with only empty tiles, as a starting point
tilemap = []; tilemap = [];
for (var j: int = 0; j < CELL_COUNT_Y * CELL_HEIGHT; j++) { for (var j: int = 0; j < MAP_GRID_HEIGHT; j++) {
for (var i: int = 0; i < CELL_COUNT_X * CELL_WIDTH; i++) { for (var i: int = 0; i < MAP_GRID_WIDTH; i++) {
tilemap.push(-1); //x tilemap.push(-1); //x
tilemap.push(-1); //y tilemap.push(-1); //y
tilemap.push(0); //collision tilemap.push(0); //collision
@@ -148,10 +148,12 @@ fn etchRoom(rng: opaque, metadata: [string: any]) {
//etch the floor-space //etch the floor-space
for (var j: int = y; j < y + h; j++) { for (var j: int = y; j < y + h; j++) {
for (var i: int = x; i < x + w; i++) { for (var i: int = x; i < x + w; i++) {
var floorIndex = rng.generateRandomNumber() % 4; //NOTE: there might not always be only 4 floor sprites var ITERATION: int const = j * MAP_GRID_WIDTH * 3 + i * 3;
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3 + 0] = tileset[theme + "-floor-" + string floorIndex][0]; var r = rng.generateRandomNumber() % 4; //BUG: see Toy#67
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3 + 1] = tileset[theme + "-floor-" + string floorIndex][1]; var floorIndex: string const = theme + "-floor-" + string r; //NOTE: there might not always be only 4 floor sprites
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3 + 2] = tileset[theme + "-floor-" + string floorIndex][2]; tilemap[ITERATION + 0] = tileset[floorIndex][0];
tilemap[ITERATION + 1] = tileset[floorIndex][1];
tilemap[ITERATION + 2] = tileset[floorIndex][2];
} }
} }
} }
@@ -356,10 +358,12 @@ fn etchLine(x: int, y: int, xLength: int, yLength: int, theme: string, rng: opaq
//lengths can be negative, so handle it //lengths can be negative, so handle it
while (abs(xLength) > 0 || abs(yLength) > 0) { while (abs(xLength) > 0 || abs(yLength) > 0) {
//etch floor at this position //etch floor at this position
var floorIndex = rng.generateRandomNumber() % 4; //NOTE: there might not always be only 4 floor sprites var ITERATION: int const = y * MAP_GRID_WIDTH * 3 + x * 3;
tilemap[y * CELL_WIDTH * CELL_COUNT_X * 3 + x * 3 + 0] = tileset[theme + "-floor-" + string floorIndex][0]; var r = rng.generateRandomNumber() % 4;
tilemap[y * CELL_WIDTH * CELL_COUNT_X * 3 + x * 3 + 1] = tileset[theme + "-floor-" + string floorIndex][1]; var floorIndex: string const = theme + "-floor-" + string r; //NOTE: there might not always be only 4 floor sprites
tilemap[y * CELL_WIDTH * CELL_COUNT_X * 3 + x * 3 + 2] = tileset[theme + "-floor-" + string floorIndex][2]; tilemap[ITERATION + 0] = tileset[floorIndex][0];
tilemap[ITERATION + 1] = tileset[floorIndex][1];
tilemap[ITERATION + 2] = tileset[floorIndex][2];
//tick down //tick down
if (abs(xLength) > 0) { if (abs(xLength) > 0) {
@@ -398,10 +402,13 @@ fn etchWalls(roomData) {
continue; continue;
} }
var ITERATION: int const = j * MAP_GRID_WIDTH * 3 + i * 3;
var theme = roomData[floor(i / CELL_WIDTH)][floor(j / CELL_HEIGHT)]["theme"]; var theme = roomData[floor(i / CELL_WIDTH)][floor(j / CELL_HEIGHT)]["theme"];
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3 + 0] = tileset[theme + "-" + signals[j * MAP_GRID_WIDTH + i]][0]; var index: string const = theme + "-" + signals[j * MAP_GRID_WIDTH + i];
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3 + 1] = tileset[theme + "-" + signals[j * MAP_GRID_WIDTH + i]][1];
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3 + 2] = tileset[theme + "-" + signals[j * MAP_GRID_WIDTH + i]][2]; tilemap[ITERATION + 0] = tileset[index][0];
tilemap[ITERATION + 1] = tileset[index][1];
tilemap[ITERATION + 2] = tileset[index][2];
} }
} }
} }
@@ -499,7 +506,7 @@ var marchingFilterResult: [string] const = [
//if walkable is below, then return top, etc. //if walkable is below, then return top, etc.
fn parseTilemapAt(x: int, y: int) { fn parseTilemapAt(x: int, y: int) {
//parse based on walkability, for now //parse based on walkability, for now
if (tilemap[y * CELL_WIDTH * CELL_COUNT_X * 3 + x * 3 + 2] != 0) { if (tilemap[y * MAP_GRID_WIDTH * 3 + x * 3 + 2] != 0) {
return ""; //empty return ""; //empty
} }
@@ -510,9 +517,9 @@ fn parseTilemapAt(x: int, y: int) {
for (var i: int = 0; i < 12 -2; i++) { for (var i: int = 0; i < 12 -2; i++) {
for (var j: int = 0; j < 12 -2; j++) { for (var j: int = 0; j < 12 -2; j++) {
if ( if (
marchingFilter[(j + 0) * 12 + (i + 0)] == snapshot[0 * 3 + 0] && marchingFilter[j * 12 + (i + 0)] == snapshot[0 * 3 + 0] &&
marchingFilter[(j + 0) * 12 + (i + 1)] == snapshot[0 * 3 + 1] && marchingFilter[j * 12 + (i + 1)] == snapshot[0 * 3 + 1] &&
marchingFilter[(j + 0) * 12 + (i + 2)] == snapshot[0 * 3 + 2] && marchingFilter[j * 12 + (i + 2)] == snapshot[0 * 3 + 2] &&
marchingFilter[(j + 1) * 12 + (i + 0)] == snapshot[1 * 3 + 0] && marchingFilter[(j + 1) * 12 + (i + 0)] == snapshot[1 * 3 + 0] &&
marchingFilter[(j + 1) * 12 + (i + 1)] == snapshot[1 * 3 + 1] && marchingFilter[(j + 1) * 12 + (i + 1)] == snapshot[1 * 3 + 1] &&
@@ -540,6 +547,7 @@ fn parseTilemapAt(x: int, y: int) {
return ""; return "";
} }
//TODO: can this be optimized out?
fn generateSnapshotAt(x: int, y: int) { fn generateSnapshotAt(x: int, y: int) {
var result: [int] = []; var result: [int] = [];
@@ -549,7 +557,7 @@ fn generateSnapshotAt(x: int, y: int) {
result.push(0); result.push(0);
} }
else { else {
result.push(tilemap[(y+j) * CELL_WIDTH * CELL_COUNT_X * 3 + (x+i) * 3 + 2] != 0 ? 1 : 0); //walkable result.push(tilemap[(y+j) * MAP_GRID_WIDTH * 3 + (x+i) * 3 + 2] != 0 ? 1 : 0); //walkable
} }
} }
} }

View File

@@ -19,6 +19,9 @@ var CELL_HEIGHT: int const = 16;
var CELL_COUNT_X: int const = 3; var CELL_COUNT_X: int const = 3;
var CELL_COUNT_Y: int const = 3; var CELL_COUNT_Y: int const = 3;
var MAP_GRID_WIDTH: int const = CELL_WIDTH * CELL_COUNT_X;
var MAP_GRID_HEIGHT: int const = CELL_HEIGHT * CELL_COUNT_Y;
//lifecycle functions //lifecycle functions
fn onLoad(node: opaque) { fn onLoad(node: opaque) {
@@ -27,29 +30,31 @@ fn onLoad(node: opaque) {
//create a child as a render target //create a child as a render target
var child: opaque = node.loadChildNode("scripts:/tilemap/renderer-child.toy"); var child: opaque = node.loadChildNode("scripts:/tilemap/renderer-child.toy");
child.createNodeTexture(CELL_WIDTH * CELL_COUNT_X * TILE_PIXEL_WIDTH, CELL_HEIGHT * CELL_COUNT_Y * TILE_PIXEL_HEIGHT); child.createNodeTexture(MAP_GRID_WIDTH * TILE_PIXEL_WIDTH, MAP_GRID_HEIGHT * TILE_PIXEL_HEIGHT);
} }
//TODO: lazily render
fn setTilemap(node: opaque, tilemap: [int]) { fn setTilemap(node: opaque, tilemap: [int]) {
assert tilemap, "provided tilemap is null (in setTilemap)"; assert tilemap, "provided tilemap is null (in setTilemap)";
print "start setting tilemap";
var child: opaque = node.getChildNode(0); var child: opaque = node.getChildNode(0);
setRenderTarget(child); setRenderTarget(child);
//draw the tilemap to the child //draw the tilemap to the child
for (var j = 0; j < CELL_HEIGHT * CELL_COUNT_Y; j++) { for (var j = 0; j < MAP_GRID_HEIGHT; j++) {
for (var i = 0; i < CELL_WIDTH * CELL_COUNT_X; i++) { for (var i = 0; i < MAP_GRID_WIDTH; i++) {
//don't recalculate this every time
var ITERATION: int const = j * MAP_GRID_WIDTH * 3 + i * 3;
//don't render empty tiles //don't render empty tiles
if (tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3] < 0) { if (tilemap[ITERATION] < 0 || tilemap[ITERATION + 1] < 0) {
continue; continue;
} }
//set the rect of the node on the tilesheet - the "tilemap" var is a single blob of data //set the rect of the node on the tilesheet - the "tilemap" var is a single blob of data
node.setNodeRect( node.setNodeRect(
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3] * TILE_PIXEL_WIDTH, tilemap[ITERATION] * TILE_PIXEL_WIDTH,
tilemap[j * CELL_WIDTH * CELL_COUNT_X * 3 + i * 3 + 1] * TILE_PIXEL_HEIGHT, tilemap[ITERATION + 1] * TILE_PIXEL_HEIGHT,
TILE_PIXEL_WIDTH, TILE_PIXEL_HEIGHT TILE_PIXEL_WIDTH, TILE_PIXEL_HEIGHT
); );
@@ -65,6 +70,4 @@ fn setTilemap(node: opaque, tilemap: [int]) {
//reset the render target to the screen //reset the render target to the screen
setRenderTarget(null); setRenderTarget(null);
print "finished setting tilemap";
} }

View File

@@ -2,6 +2,13 @@
#include "drive_system.h" #include "drive_system.h"
#include "toy_memory.h"
//modified allocator
void* speedyMemoryAllocator(void* pointer, size_t oldSize, size_t newSize) {
return realloc(pointer, newSize);
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
//debugging tools //debugging tools
#ifdef _DEBUG #ifdef _DEBUG
@@ -9,6 +16,9 @@ int main(int argc, char* argv[]) {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif//win32 && debug #endif//win32 && debug
//set the memory allocator
Toy_setMemoryAllocator(speedyMemoryAllocator);
//the drive system maps filepaths to "drives", which specifies which folders can be accessed by the scripts //the drive system maps filepaths to "drives", which specifies which folders can be accessed by the scripts
Toy_initDriveSystem(); Toy_initDriveSystem();