2 #include <assimp/cimport.h>
8 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
11 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
15 static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
16 &blockEmpty, &blockEmpty, &blockEmpty,
17 &blockWall01, &blockEmpty, &blockWall01 };
19 BlockGrid levelGrid = { .width = 3,
21 .blocks = testBlocks };
23 //static TgaImage* levelImage = NULL;
25 static const char* replaceFileExtension(const AiString path, const char* ext);
28 const AiScene* sceneData = importScene("out/assets/wall01.3ds");
29 blockWall01.sceneData = sceneData;
30 if (sceneData != NULL) {
31 const unsigned int numTextures = sceneData->mNumMeshes;
33 blockWall01.textureIds = malloc(numTextures * sizeof(GLuint));
34 glGenTextures(numTextures, blockWall01.textureIds);
36 for (unsigned int i = 0; i < numTextures; ++i) {
37 glBindTexture(GL_TEXTURE_2D, blockWall01.textureIds[i]);
39 AiString originalTexturePath;
40 if (aiGetMaterialTexture(sceneData->mMaterials[sceneData->mMeshes[i]->mMaterialIndex],
41 aiTextureType_DIFFUSE,
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);
55 glTexImage2D(GL_TEXTURE_2D,
57 textureImage->imageComponents,
58 textureImage->header.imageWidth,
59 textureImage->header.imageHeight,
61 textureImage->imageFormat,
64 free(textureImage->bytes);
69 glBindTexture(GL_TEXTURE_2D, 0);
73 void buildLevelFromImage(TgaImage* image) {
74 if (image->header.imageBpp != 32) {
75 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
80 const AiScene* importScene(const char* path) {
81 const AiScene* scene = aiImportFile(path, 0u);
83 logError("Failed to import asset from %s", path);
85 else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
86 logError("Incomplete scene imported from %s", path);
87 aiReleaseImport(scene);
91 // TODO aiReleaseImport(scene);
95 * The following function will not work properly with texture
96 * file names (excluding directory part) beginning with '.'
98 static const char* replaceFileExtension(const AiString path, const char* ext) {
99 size_t lengthToCopy = path.length;
101 char* lastDotSubstr = strrchr(path.data, '.');
102 if (lastDotSubstr != NULL) {
103 if (strpbrk(lastDotSubstr, "\\/") == NULL) {
104 lengthToCopy = lastDotSubstr - path.data;
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);