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