]> git.lizzy.rs Git - shadowclad.git/blob - src/game/level.c
Fix includes and build instructions
[shadowclad.git] / src / game / level.c
1 #include <GL/gl.h>
2 #include <stdlib.h>
3
4 #include "engine/logger.h"
5
6 #include "level.h"
7 #include "player.h"
8
9 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
10                             .asset3D = NULL };
11 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
12                              .asset3D = NULL };
13
14 static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
15                                 &blockEmpty, &blockEmpty, &blockEmpty,
16                                 &blockWall01, &blockEmpty, &blockWall01 };
17
18 BlockGrid levelGrid = { .width = 3,
19                         .depth = 3,
20                         .blocks = testBlocks };
21
22 #define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
23 Vector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
24
25
26
27 void initLevel() {
28         blockWall01.asset3D = importAsset("assets/wall01.3ds");
29         
30         buildLevelFromImage(readTga("assets/level01.tga"));
31 }
32
33 void buildLevelFromImage(TgaImage* image) {
34         if (image == NULL) {
35                 logError("Null image received, cannot build level");
36                 return;
37         }
38         
39         if (image->header.imageBpp != 32) {
40                 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
41                 return;
42         }
43         
44         BlockGrid newGrid = { .width = image->header.imageWidth,
45                               .depth = image->header.imageHeight,
46                               .blocks = malloc(image->header.imageWidth
47                                                * image->header.imageHeight
48                                                * sizeof(Block*)) };
49         playerSpawnPos = (Vector3D) DEFAULT_PLAYER_SPAWN_POS;
50         
51         for (int row = 0; row < newGrid.depth; ++row) {
52                 for (int x = 0; x < newGrid.width; ++x) {
53                         // Flip the image vertically due to (0, 0) being bottom left
54                         int z = newGrid.depth - row - 1;
55                         
56                         uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
57                         Block* block;
58                         switch (pixelColorARGB) {
59                                 case 0xFFFF0000:
60                                         block = &blockWall01;
61                                         break;
62                                 case 0xFF00FFFF:
63                                         block = &blockEmpty;
64                                         playerSpawnPos = (Vector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
65                                         break;
66                                 default:
67                                         block = &blockEmpty;
68                                         break;
69                         }
70                         setBlockInGrid(newGrid, x, z, block);
71                 }
72         }
73         
74         levelGrid = newGrid;
75         spawnPlayer();
76 }