]> git.lizzy.rs Git - shadowclad.git/blob - src/game/level.c
Housekeeping
[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
9 #include "player.h"
10
11 BlockGrid levelGrid;
12
13 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
14                             .solid = NULL };
15 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
16                              .solid = NULL };
17
18 static Transform playerSpawnTransform;
19
20
21
22 void initLevel() {
23         playerSpawnTransform = identity();
24         translate(&playerSpawnTransform, (Vector3D) { .x = -BLOCKGRID_CELL_SIZE,
25                                                       .y = 0.0f,
26                                                       .z = -BLOCKGRID_CELL_SIZE });
27
28         blockWall01.solid = importSolid("assets/wall01.3ds");
29
30         buildLevelFromImage(readTga("assets/level01.tga"));
31
32         Scene* levelScene = newScene();
33
34         for (size_t z = 0; z < levelGrid.depth; ++z) {
35                 for (size_t x = 0; x < levelGrid.width; ++x) {
36                         Scene* blockScene = newScene();
37                         translate(&blockScene->transform, (Vector3D) { .x = x * BLOCKGRID_CELL_SIZE,
38                                                                        .y = 0.0f,
39                                                                        .z = z * BLOCKGRID_CELL_SIZE });
40                         blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
41                         insertChildScene(levelScene, blockScene);
42                 }
43         }
44
45         currentScene = levelScene;
46 }
47
48 void startLevel() {
49         spawnPlayer(playerSpawnTransform);
50 }
51
52 void buildLevelFromImage(TgaImage* image) {
53         if (image == NULL) {
54                 logError("Null image received, cannot build level");
55                 return;
56         }
57         
58         if (image->header.imageBpp != 32) {
59                 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
60                 return;
61         }
62         
63         BlockGrid newGrid = { .width = image->header.imageWidth,
64                               .depth = image->header.imageHeight,
65                               .blocks = malloc(image->header.imageWidth
66                                                * image->header.imageHeight
67                                                * sizeof(Block*)) };
68         
69         for (size_t row = 0; row < newGrid.depth; ++row) {
70                 for (size_t x = 0; x < newGrid.width; ++x) {
71                         // Flip the image vertically due to (0, 0) being bottom left
72                         size_t z = newGrid.depth - row - 1;
73                         
74                         uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
75                         Block* block;
76                         switch (pixelColorARGB) {
77                                 case 0xFFFF0000:
78                                         block = &blockWall01;
79                                         break;
80                                 case 0xFF00FFFF:
81                                         block = &blockEmpty;
82                                         playerSpawnTransform = identity();
83                                         translate(&playerSpawnTransform, (Vector3D) { .x = x * BLOCKGRID_CELL_SIZE,
84                                                                                       .y = 0.0f,
85                                                                                       .z = z * BLOCKGRID_CELL_SIZE });
86                                         break;
87                                 default:
88                                         block = &blockEmpty;
89                                         break;
90                         }
91                         setBlockInGrid(newGrid, x, z, block);
92                 }
93         }
94         
95         levelGrid = newGrid;
96 }