]> git.lizzy.rs Git - shadowclad.git/blob - level.c
Render the level's grid of blocks
[shadowclad.git] / level.c
1 #include <GL/gl.h>
2 #include <assimp/cimport.h>
3 #include <stdlib.h>
4
5 #include "level.h"
6 #include "logger.h"
7
8 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
9                             .sceneData = NULL,
10                             .textureIds = NULL };
11 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
12                              .sceneData = NULL,
13                              .textureIds = NULL };
14
15 static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
16                                 &blockEmpty, &blockEmpty, &blockEmpty,
17                                 &blockWall01, &blockEmpty, &blockWall01 };
18
19 BlockGrid levelGrid = { .width = 3,
20                         .depth = 3,
21                         .blocks = testBlocks };
22
23 //static TgaImage* levelImage = NULL;
24
25 static const char* replaceFileExtension(const AiString path, const char* ext);
26
27 void initLevel() {
28         const AiScene* sceneData = importScene("out/assets/wall01.3ds");
29         blockWall01.sceneData = sceneData;
30         if (sceneData != NULL) {
31                 const unsigned int numTextures = sceneData->mNumMeshes;
32                 
33                 blockWall01.textureIds = malloc(numTextures * sizeof(GLuint));
34                 glGenTextures(numTextures, blockWall01.textureIds);
35                 
36                 for (unsigned int i = 0; i < numTextures; ++i) {
37                         glBindTexture(GL_TEXTURE_2D, blockWall01.textureIds[i]);
38                         
39                         AiString originalTexturePath;
40                         if (aiGetMaterialTexture(sceneData->mMaterials[sceneData->mMeshes[i]->mMaterialIndex],
41                                                  aiTextureType_DIFFUSE,
42                                                  0,
43                                                  &originalTexturePath,
44                                                  NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
45                                 const char* textureFile = replaceFileExtension(originalTexturePath, ".tga");
46                                 size_t textureFileLength = strlen(textureFile);
47                                 char* texturePath = malloc(strlen("out/assets/") + textureFileLength + 1);
48                                 strcpy(texturePath, "out/assets/");
49                                 strncat(texturePath, textureFile, textureFileLength);
50                                 TgaImage* textureImage = readTga(texturePath);
51                                 if (textureImage == NULL) {
52                                         logError("Asset texture file not found: %s", texturePath);
53                                 }
54                                 else {
55                                         glTexImage2D(GL_TEXTURE_2D,
56                                                      0,
57                                                      textureImage->imageComponents,
58                                                      textureImage->header.imageWidth,
59                                                      textureImage->header.imageHeight,
60                                                      0,
61                                                      textureImage->imageFormat,
62                                                      GL_UNSIGNED_BYTE,
63                                                      textureImage->bytes);
64                                         free(textureImage->bytes);
65                                 }
66                         }
67                 }
68                 
69                 glBindTexture(GL_TEXTURE_2D, 0);
70         }
71 }
72
73 void buildLevelFromImage(TgaImage* image) {
74         if (image->header.imageBpp != 32) {
75                 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
76                 return;
77         }
78 }
79
80 const AiScene* importScene(const char* path) {
81         const AiScene* scene = aiImportFile(path, 0u);
82         if (scene == NULL) {
83                 logError("Failed to import asset from %s", path);
84         }
85         else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
86                 logError("Incomplete scene imported from %s", path);
87                 aiReleaseImport(scene);
88                 scene = NULL;
89         }
90         return scene;
91         // TODO aiReleaseImport(scene);
92 }
93
94 /** BUGS
95  * The following function will not work properly with texture
96  * file names (excluding directory part) beginning with '.'
97  */
98 static const char* replaceFileExtension(const AiString path, const char* ext) {
99                 size_t lengthToCopy = path.length;
100                 
101                 char* lastDotSubstr = strrchr(path.data, '.');
102                 if (lastDotSubstr != NULL) {
103                         if (strpbrk(lastDotSubstr, "\\/") == NULL) {
104                                 lengthToCopy = lastDotSubstr - path.data;
105                         }
106                 }
107                 
108                 size_t extLength = strlen(ext) + 1;
109                 char* newPath = malloc(lengthToCopy + extLength);
110                 strncpy(newPath, path.data, lengthToCopy);
111                 strncpy(newPath + lengthToCopy, ext, extLength);
112                 
113                 return newPath;
114 }