4 #include "engine/logger.h"
5 #include "engine/scene.h"
10 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
12 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
15 static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
16 &blockEmpty, &blockEmpty, &blockEmpty,
17 &blockWall01, &blockEmpty, &blockWall01 };
19 BlockGrid levelGrid = { .width = 3,
21 .blocks = testBlocks };
23 #define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
24 Vector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
29 blockWall01.solid = importSolid("assets/wall01.3ds");
31 buildLevelFromImage(readTga("assets/level01.tga"));
33 Scene* levelScene = newScene();
35 for (size_t z = 0; z < levelGrid.depth; ++z) {
36 for (size_t x = 0; x < levelGrid.width; ++x) {
37 Scene* blockScene = newScene();
38 translate(&blockScene->transform, (Vector3D) { .x = x * BLOCKGRID_CELL_SIZE,
40 .z = z * BLOCKGRID_CELL_SIZE });
41 blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
42 insertChildScene(levelScene, blockScene);
46 currentScene = levelScene;
49 void buildLevelFromImage(TgaImage* image) {
51 logError("Null image received, cannot build level");
55 if (image->header.imageBpp != 32) {
56 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
60 BlockGrid newGrid = { .width = image->header.imageWidth,
61 .depth = image->header.imageHeight,
62 .blocks = malloc(image->header.imageWidth
63 * image->header.imageHeight
65 playerSpawnPos = (Vector3D) DEFAULT_PLAYER_SPAWN_POS;
67 for (size_t row = 0; row < newGrid.depth; ++row) {
68 for (size_t x = 0; x < newGrid.width; ++x) {
69 // Flip the image vertically due to (0, 0) being bottom left
70 size_t z = newGrid.depth - row - 1;
72 uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
74 switch (pixelColorARGB) {
80 playerSpawnPos = (Vector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
86 setBlockInGrid(newGrid, x, z, block);