]> git.lizzy.rs Git - shadowclad.git/blob - src/game/level.c
c814e4e48588f68b946712c5a035e08c5246f057
[shadowclad.git] / src / game / level.c
1 #include "level.h"
2
3 #include <stdlib.h>
4 #include <GL/gl.h>
5
6 #include "engine/logger.h"
7 #include "engine/scene.h"
8 #include "engine/tga.h"
9
10 #include "player.h"
11
12 BlockGrid levelGrid;
13
14 static const float BLOCKGRID_CELL_SIZE = 2.5f;
15
16 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
17                             .solid = NULL };
18 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
19                              .solid = NULL };
20
21 static Transform playerSpawnTransform;
22
23 static void buildLevelFromImage(const TgaImage* image);
24
25
26
27 void initLevel() {
28         playerSpawnTransform = identity();
29         translate(&playerSpawnTransform, (Vector) { -BLOCKGRID_CELL_SIZE,
30                                                     0.0f,
31                                                     -BLOCKGRID_CELL_SIZE });
32
33         blockWall01.solid = importSolid("assets/wall01.3ds");
34
35         buildLevelFromImage(readTga("assets/level01.tga"));
36
37         Scene* levelScene = newScene();
38
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),
44                                 0.0f,
45                                 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
46                         blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
47                         insertChildScene(levelScene, blockScene);
48                 }
49         }
50
51         currentScene = levelScene;
52 }
53
54 void startLevel() {
55         spawnPlayer(playerSpawnTransform);
56 }
57
58 static void buildLevelFromImage(const TgaImage* image) {
59         if (image == NULL) {
60                 logError("Null image received, cannot build level");
61                 return;
62         }
63
64         if (image->header.imageBpp != 32) {
65                 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
66                 return;
67         }
68
69         BlockGrid newGrid = { .width = image->header.imageWidth,
70                               .depth = image->header.imageHeight,
71                               .blocks = malloc(image->header.imageWidth
72                                                * image->header.imageHeight
73                                                * sizeof(Block*)) };
74
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;
79
80                         uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
81                         Block* block;
82                         switch (pixelColorARGB) {
83                                 case 0xFFFF0000:
84                                         block = &blockWall01;
85                                         break;
86                                 case 0xFF00FFFF:
87                                         block = &blockEmpty;
88                                         playerSpawnTransform = identity();
89                                         translate(&playerSpawnTransform, (Vector) {
90                                                 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
91                                                 0.0f,
92                                                 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
93                                         break;
94                                 default:
95                                         block = &blockEmpty;
96                                         break;
97                         }
98                         setBlockInGrid(newGrid, x, z, block);
99                 }
100         }
101
102         levelGrid = newGrid;
103 }
104
105 static inline size_t nonNegative(long n) {
106         return n < 0 ? 0u : n;
107 }
108
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) };
113 }