4 #include "engine/logger.h"
9 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
11 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
14 static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
15 &blockEmpty, &blockEmpty, &blockEmpty,
16 &blockWall01, &blockEmpty, &blockWall01 };
18 BlockGrid levelGrid = { .width = 3,
20 .blocks = testBlocks };
22 #define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
23 Vector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
28 blockWall01.asset3D = importAsset("assets/wall01.3ds");
30 buildLevelFromImage(readTga("assets/level01.tga"));
33 void buildLevelFromImage(TgaImage* image) {
35 logError("Null image received, cannot build level");
39 if (image->header.imageBpp != 32) {
40 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
44 BlockGrid newGrid = { .width = image->header.imageWidth,
45 .depth = image->header.imageHeight,
46 .blocks = malloc(image->header.imageWidth
47 * image->header.imageHeight
49 playerSpawnPos = (Vector3D) DEFAULT_PLAYER_SPAWN_POS;
51 for (size_t row = 0; row < newGrid.depth; ++row) {
52 for (size_t x = 0; x < newGrid.width; ++x) {
53 // Flip the image vertically due to (0, 0) being bottom left
54 size_t z = newGrid.depth - row - 1;
56 uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
58 switch (pixelColorARGB) {
64 playerSpawnPos = (Vector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
70 setBlockInGrid(newGrid, x, z, block);