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