]> git.lizzy.rs Git - shadowclad.git/blob - level.c
Remove reduntant glutSetWindowTitle()
[shadowclad.git] / level.c
1 #include <GL/gl.h>
2 #include <stdlib.h>
3
4 #include "asset.h"
5 #include "level.h"
6 #include "logger.h"
7 #include "player.h"
8
9 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
10                             .sceneData = NULL,
11                             .textureIds = NULL };
12 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
13                              .sceneData = NULL,
14                              .textureIds = NULL };
15
16 static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
17                                 &blockEmpty, &blockEmpty, &blockEmpty,
18                                 &blockWall01, &blockEmpty, &blockWall01 };
19
20 BlockGrid levelGrid = { .width = 3,
21                         .depth = 3,
22                         .blocks = testBlocks };
23
24 #define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
25 AiVector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
26
27 static const char* replaceFileExtension(const AiString path, const char* ext);
28
29
30
31 void initLevel() {
32         const AiScene* sceneData = importScene("out/assets/wall01.3ds");
33         blockWall01.sceneData = sceneData;
34         if (sceneData != NULL) {
35                 const unsigned int numTextures = sceneData->mNumMeshes;
36                 
37                 blockWall01.textureIds = malloc(numTextures * sizeof(GLuint));
38                 glGenTextures(numTextures, blockWall01.textureIds);
39                 
40                 for (unsigned int i = 0; i < numTextures; ++i) {
41                         glBindTexture(GL_TEXTURE_2D, blockWall01.textureIds[i]);
42                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
43                         
44                         AiString originalTexturePath;
45                         if (aiGetMaterialTexture(sceneData->mMaterials[sceneData->mMeshes[i]->mMaterialIndex],
46                                                  aiTextureType_DIFFUSE,
47                                                  0,
48                                                  &originalTexturePath,
49                                                  NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
50                                 const char* textureFile = replaceFileExtension(originalTexturePath, ".tga");
51                                 size_t textureFileLength = strlen(textureFile);
52                                 char* texturePath = malloc(strlen("out/assets/") + textureFileLength + 1);
53                                 strcpy(texturePath, "out/assets/");
54                                 strncat(texturePath, textureFile, textureFileLength);
55                                 TgaImage* textureImage = readTga(texturePath);
56                                 if (textureImage == NULL) {
57                                         logError("Asset texture file not found: %s", texturePath);
58                                 }
59                                 else {
60                                         glTexImage2D(GL_TEXTURE_2D,
61                                                      0,
62                                                      textureImage->imageComponents,
63                                                      textureImage->header.imageWidth,
64                                                      textureImage->header.imageHeight,
65                                                      0,
66                                                      textureImage->imageFormat,
67                                                      GL_UNSIGNED_BYTE,
68                                                      textureImage->bytes);
69                                         free(textureImage->bytes);
70                                 }
71                         }
72                 }
73                 
74                 glBindTexture(GL_TEXTURE_2D, 0);
75         }
76         
77         buildLevelFromImage(readTga("out/assets/level01.tga"));
78 }
79
80 void buildLevelFromImage(TgaImage* image) {
81         if (image == NULL) {
82                 logError("Null image received, cannot build level");
83                 return;
84         }
85         
86         if (image->header.imageBpp != 32) {
87                 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
88                 return;
89         }
90         
91         BlockGrid newGrid = { .width = image->header.imageWidth,
92                               .depth = image->header.imageHeight,
93                               .blocks = malloc(image->header.imageWidth
94                                                * image->header.imageHeight
95                                                * sizeof(Block*)) };
96         playerSpawnPos = (AiVector3D) DEFAULT_PLAYER_SPAWN_POS;
97         
98         for (int row = 0; row < newGrid.depth; ++row) {
99                 for (int x = 0; x < newGrid.width; ++x) {
100                         // Flip the image vertically due to (0, 0) being bottom left
101                         int z = newGrid.depth - row - 1;
102                         
103                         uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
104                         Block* block;
105                         switch (pixelColorARGB) {
106                                 case 0xFFFF0000:
107                                         block = &blockWall01;
108                                         break;
109                                 case 0xFF00FFFF:
110                                         block = &blockEmpty;
111                                         playerSpawnPos = (AiVector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
112                                         break;
113                                 default:
114                                         block = &blockEmpty;
115                                         break;
116                         }
117                         setBlockInGrid(newGrid, x, z, block);
118                 }
119         }
120         
121         levelGrid = newGrid;
122         spawnPlayer();
123 }
124
125 /** BUGS
126  * The following function will not work properly with texture
127  * file names (excluding directory part) beginning with '.'
128  */
129 static const char* replaceFileExtension(const AiString path, const char* ext) {
130                 size_t lengthToCopy = path.length;
131                 
132                 char* lastDotSubstr = strrchr(path.data, '.');
133                 if (lastDotSubstr != NULL) {
134                         if (strpbrk(lastDotSubstr, "\\/") == NULL) {
135                                 lengthToCopy = lastDotSubstr - path.data;
136                         }
137                 }
138                 
139                 size_t extLength = strlen(ext) + 1;
140                 char* newPath = malloc(lengthToCopy + extLength);
141                 strncpy(newPath, path.data, lengthToCopy);
142                 strncpy(newPath + lengthToCopy, ext, extLength);
143                 
144                 return newPath;
145 }