]> git.lizzy.rs Git - shadowclad.git/blobdiff - level.c
Implement custom Asset3D structure and refactor to use it
[shadowclad.git] / level.c
diff --git a/level.c b/level.c
index 3469736ca5d3770fe8dafea3abbe55dd9b589da0..a16061ab0b3e4167a14569366499f8ace8f2c6be 100644 (file)
--- a/level.c
+++ b/level.c
@@ -1,32 +1,75 @@
 #include <GL/gl.h>
-#include <assimp/cimport.h>
+#include <stdlib.h>
 
 #include "level.h"
 #include "logger.h"
+#include "player.h"
 
-const AiScene* levelScene = NULL;
+static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
+                            .asset3D = NULL };
+static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
+                             .asset3D = NULL };
 
-static const Block BLOCK_EMPTY = 0;
-static const Block BLOCK_WALL01 = 0xFF0000FF; // red
+static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
+                                &blockEmpty, &blockEmpty, &blockEmpty,
+                                &blockWall01, &blockEmpty, &blockWall01 };
 
-static const AiScene* blockWall01 = NULL;
+BlockGrid levelGrid = { .width = 3,
+                        .depth = 3,
+                        .blocks = testBlocks };
+
+#define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
+Vector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
 
-static TgaImage* levelImage = NULL;
 
-void initLevel() {
-       blockWall01 = importScene("out/assets/wall01.3ds");
-       levelScene = blockWall01;
-}
 
-void setImage(TgaImage* image) {
-       levelImage = image;
+void initLevel() {
+       blockWall01.asset3D = importAsset("assets/wall01.3ds");
+       
+       buildLevelFromImage(readTga("assets/level01.tga"));
 }
 
-const AiScene* importScene(const char* path) {
-       const AiScene* scene = aiImportFile(path, 0u);
-       if (scene == NULL) {
-               logError("Asset import failed (file: %s)", path);
+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 = (Vector3D) 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 = (Vector3D) { 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();
 }