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