]> git.lizzy.rs Git - shadowclad.git/blobdiff - level.c
Remove reduntant glutSetWindowTitle()
[shadowclad.git] / level.c
diff --git a/level.c b/level.c
index 2c9dba2fc03dc7c43c253f5a4b3dcc805ca00a9e..31be493b2bd979f8d668f306a0499aa62b9fbef2 100644 (file)
--- a/level.c
+++ b/level.c
 #include <GL/gl.h>
 #include <stdlib.h>
-#include <stdio.h> // TODO remove
-#include <assimp/cimport.h>
-#include <assimp/scene.h>
 
+#include "asset.h"
 #include "level.h"
-#include "tga.h"
+#include "logger.h"
+#include "player.h"
 
-const Block BLOCK_EMPTY = 0;
-const Block BLOCK_WALL01 = 1;
+static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
+                            .sceneData = NULL,
+                            .textureIds = NULL };
+static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
+                             .sceneData = NULL,
+                             .textureIds = NULL };
 
-TGAimage* level_image = NULL;
+static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
+                                &blockEmpty, &blockEmpty, &blockEmpty,
+                                &blockWall01, &blockEmpty, &blockWall01 };
 
-Block get_block(GLushort x, GLushort y) {
-       if (level_image == NULL) {
-               return BLOCK_EMPTY;
+BlockGrid levelGrid = { .width = 3,
+                        .depth = 3,
+                        .blocks = testBlocks };
+
+#define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
+AiVector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
+
+static const char* replaceFileExtension(const AiString path, const char* ext);
+
+
+
+void initLevel() {
+       const AiScene* sceneData = importScene("out/assets/wall01.3ds");
+       blockWall01.sceneData = sceneData;
+       if (sceneData != NULL) {
+               const unsigned int numTextures = sceneData->mNumMeshes;
+               
+               blockWall01.textureIds = malloc(numTextures * sizeof(GLuint));
+               glGenTextures(numTextures, blockWall01.textureIds);
+               
+               for (unsigned int i = 0; i < numTextures; ++i) {
+                       glBindTexture(GL_TEXTURE_2D, blockWall01.textureIds[i]);
+                       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+                       
+                       AiString originalTexturePath;
+                       if (aiGetMaterialTexture(sceneData->mMaterials[sceneData->mMeshes[i]->mMaterialIndex],
+                                                aiTextureType_DIFFUSE,
+                                                0,
+                                                &originalTexturePath,
+                                                NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
+                               const char* textureFile = replaceFileExtension(originalTexturePath, ".tga");
+                               size_t textureFileLength = strlen(textureFile);
+                               char* texturePath = malloc(strlen("out/assets/") + textureFileLength + 1);
+                               strcpy(texturePath, "out/assets/");
+                               strncat(texturePath, textureFile, textureFileLength);
+                               TgaImage* textureImage = readTga(texturePath);
+                               if (textureImage == NULL) {
+                                       logError("Asset texture file not found: %s", texturePath);
+                               }
+                               else {
+                                       glTexImage2D(GL_TEXTURE_2D,
+                                                    0,
+                                                    textureImage->imageComponents,
+                                                    textureImage->header.imageWidth,
+                                                    textureImage->header.imageHeight,
+                                                    0,
+                                                    textureImage->imageFormat,
+                                                    GL_UNSIGNED_BYTE,
+                                                    textureImage->bytes);
+                                       free(textureImage->bytes);
+                               }
+                       }
+               }
+               
+               glBindTexture(GL_TEXTURE_2D, 0);
        }
-       return ((Block*) (*level_image).bytes)[x * (*level_image).header.image_width + y];
+       
+       buildLevelFromImage(readTga("out/assets/level01.tga"));
 }
 
-void set_image(TGAimage* image) {
-       level_image = image;
+void buildLevelFromImage(TgaImage* image) {
+       if (image == NULL) {
+               logError("Null image received, cannot build level");
+               return;
+       }
+       
+       if (image->header.imageBpp != 32) {
+               logError("Invalid level image format (%d bpp)", image->header.imageBpp);
+               return;
+       }
+       
+       BlockGrid newGrid = { .width = image->header.imageWidth,
+                             .depth = image->header.imageHeight,
+                             .blocks = malloc(image->header.imageWidth
+                                              * image->header.imageHeight
+                                              * sizeof(Block*)) };
+       playerSpawnPos = (AiVector3D) DEFAULT_PLAYER_SPAWN_POS;
+       
+       for (int row = 0; row < newGrid.depth; ++row) {
+               for (int x = 0; x < newGrid.width; ++x) {
+                       // Flip the image vertically due to (0, 0) being bottom left
+                       int z = newGrid.depth - row - 1;
+                       
+                       uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
+                       Block* block;
+                       switch (pixelColorARGB) {
+                               case 0xFFFF0000:
+                                       block = &blockWall01;
+                                       break;
+                               case 0xFF00FFFF:
+                                       block = &blockEmpty;
+                                       playerSpawnPos = (AiVector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
+                                       break;
+                               default:
+                                       block = &blockEmpty;
+                                       break;
+                       }
+                       setBlockInGrid(newGrid, x, z, block);
+               }
+       }
+       
+       levelGrid = newGrid;
+       spawnPlayer();
 }
 
-const struct aiScene* import_model(const char* path) {
-       const struct aiScene* scene = aiImportFile(path, 0u);
-       if (scene == NULL) {
-               fprintf(stderr, "Asset import failed at file %s\n", path); // TODO factor logging the heck outta here
-       }
-       return scene;
-       // TODO aiReleaseImport(scene);
+/** BUGS
+ * The following function will not work properly with texture
+ * file names (excluding directory part) beginning with '.'
+ */
+static const char* replaceFileExtension(const AiString path, const char* ext) {
+               size_t lengthToCopy = path.length;
+               
+               char* lastDotSubstr = strrchr(path.data, '.');
+               if (lastDotSubstr != NULL) {
+                       if (strpbrk(lastDotSubstr, "\\/") == NULL) {
+                               lengthToCopy = lastDotSubstr - path.data;
+                       }
+               }
+               
+               size_t extLength = strlen(ext) + 1;
+               char* newPath = malloc(lengthToCopy + extLength);
+               strncpy(newPath, path.data, lengthToCopy);
+               strncpy(newPath + lengthToCopy, ext, extLength);
+               
+               return newPath;
 }