]> git.lizzy.rs Git - shadowclad.git/blobdiff - level.c
Move assets into assets/ directory
[shadowclad.git] / level.c
diff --git a/level.c b/level.c
index edcaa23384695e864d841bd2e2928f926880e21e..0917319801ec62f309d12ac5ef08f96502c84da6 100644 (file)
--- a/level.c
+++ b/level.c
@@ -1,10 +1,10 @@
 #include <GL/gl.h>
-#include <assimp/cimport.h>
-#include <assimp/postprocess.h>
 #include <stdlib.h>
 
+#include "asset.h"
 #include "level.h"
 #include "logger.h"
+#include "player.h"
 
 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
                             .sceneData = NULL,
@@ -21,12 +21,15 @@ BlockGrid levelGrid = { .width = 3,
                         .depth = 3,
                         .blocks = testBlocks };
 
-//static TgaImage* levelImage = NULL;
+#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");
+       const AiScene* sceneData = importScene("assets/wall01.3ds");
        blockWall01.sceneData = sceneData;
        if (sceneData != NULL) {
                const unsigned int numTextures = sceneData->mNumMeshes;
@@ -46,8 +49,8 @@ void initLevel() {
                                                 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/");
+                               char* texturePath = malloc(strlen("assets/") + textureFileLength + 1);
+                               strcpy(texturePath, "assets/");
                                strncat(texturePath, textureFile, textureFileLength);
                                TgaImage* textureImage = readTga(texturePath);
                                if (textureImage == NULL) {
@@ -70,27 +73,53 @@ void initLevel() {
                
                glBindTexture(GL_TEXTURE_2D, 0);
        }
+       
+       buildLevelFromImage(readTga("assets/level01.tga"));
 }
 
 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;
        }
-}
-
-const AiScene* importScene(const char* path) {
-       const AiScene* scene = aiImportFile(path, aiProcess_PreTransformVertices);
-       if (scene == NULL) {
-               logError("Failed to import asset from %s", path);
-       }
-       else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
-               logError("Incomplete scene imported from %s", path);
-               aiReleaseImport(scene);
-               scene = NULL;
+       
+       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);
+               }
        }
-       return scene;
-       // TODO aiReleaseImport(scene);
+       
+       levelGrid = newGrid;
+       spawnPlayer();
 }
 
 /** BUGS