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