4 #include "engine/logger.h"
5 #include "engine/scene.h"
12 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
14 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
17 static Transform playerSpawnTransform;
22 playerSpawnTransform = identity();
23 translate(&playerSpawnTransform, (Vector3D) { .x = -BLOCKGRID_CELL_SIZE,
25 .z = -BLOCKGRID_CELL_SIZE });
27 blockWall01.solid = importSolid("assets/wall01.3ds");
29 buildLevelFromImage(readTga("assets/level01.tga"));
31 Scene* levelScene = newScene();
33 for (size_t z = 0; z < levelGrid.depth; ++z) {
34 for (size_t x = 0; x < levelGrid.width; ++x) {
35 Scene* blockScene = newScene();
36 translate(&blockScene->transform, (Vector3D) { .x = x * BLOCKGRID_CELL_SIZE,
38 .z = z * BLOCKGRID_CELL_SIZE });
39 blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
40 insertChildScene(levelScene, blockScene);
44 currentScene = levelScene;
48 spawnPlayer(playerSpawnTransform);
51 void buildLevelFromImage(TgaImage* image) {
53 logError("Null image received, cannot build level");
57 if (image->header.imageBpp != 32) {
58 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
62 BlockGrid newGrid = { .width = image->header.imageWidth,
63 .depth = image->header.imageHeight,
64 .blocks = malloc(image->header.imageWidth
65 * image->header.imageHeight
68 for (size_t row = 0; row < newGrid.depth; ++row) {
69 for (size_t x = 0; x < newGrid.width; ++x) {
70 // Flip the image vertically due to (0, 0) being bottom left
71 size_t z = newGrid.depth - row - 1;
73 uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
75 switch (pixelColorARGB) {
81 playerSpawnTransform = identity();
82 translate(&playerSpawnTransform, (Vector3D) { .x = x * BLOCKGRID_CELL_SIZE,
84 .z = z * BLOCKGRID_CELL_SIZE });
90 setBlockInGrid(newGrid, x, z, block);