6 #include "engine/logger.h"
7 #include "engine/scene.h"
8 #include "engine/tga.h"
14 static const float BLOCKGRID_CELL_SIZE = 2.5f;
16 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
18 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
21 static Transform playerSpawnTransform;
23 static void buildLevelFromImage(const TgaImage* image);
28 playerSpawnTransform = identity();
29 translate(&playerSpawnTransform, (Vector) { -BLOCKGRID_CELL_SIZE,
31 -BLOCKGRID_CELL_SIZE });
33 blockWall01.solid = importSolid("assets/wall01.3ds");
35 buildLevelFromImage(readTga("assets/level01.tga"));
37 Scene* levelScene = newScene();
39 for (size_t z = 0; z < levelGrid.depth; ++z) {
40 for (size_t x = 0; x < levelGrid.width; ++x) {
41 Scene* blockScene = newScene();
42 translate(&blockScene->transform, (Vector) {
43 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
45 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
46 blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
47 insertChildScene(levelScene, blockScene);
51 currentScene = levelScene;
55 spawnPlayer(playerSpawnTransform);
58 static void buildLevelFromImage(const TgaImage* image) {
60 logError("Null image received, cannot build level");
64 if (image->header.imageBpp != 32) {
65 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
69 BlockGrid newGrid = { .width = image->header.imageWidth,
70 .depth = image->header.imageHeight,
71 .blocks = malloc(image->header.imageWidth
72 * image->header.imageHeight
75 for (size_t row = 0; row < newGrid.depth; ++row) {
76 for (size_t x = 0; x < newGrid.width; ++x) {
77 // Flip the image vertically due to (0, 0) being bottom left
78 size_t z = newGrid.depth - row - 1;
80 uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
82 switch (pixelColorARGB) {
88 playerSpawnTransform = identity();
89 translate(&playerSpawnTransform, (Vector) {
90 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
92 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
98 setBlockInGrid(newGrid, x, z, block);
105 static inline size_t nonNegative(long n) {
106 return n < 0 ? 0u : n;
109 GridLocation gridLocationFromTransform(Transform transform) {
110 Vector scaledPos = scaleVector(translationOf(transform), 1.0f / BLOCKGRID_CELL_SIZE);
111 return (GridLocation) { .x = nonNegative(scaledPos.x),
112 .z = nonNegative(scaledPos.z) };