]> git.lizzy.rs Git - shadowclad.git/commitdiff
Implement building levels from TGA images
authoroutfrost <kotlet.bahn@gmail.com>
Tue, 29 Jan 2019 22:01:22 +0000 (23:01 +0100)
committeroutfrost <kotlet.bahn@gmail.com>
Tue, 29 Jan 2019 22:01:22 +0000 (23:01 +0100)
level.c
level.h
render.c
ui.c

diff --git a/level.c b/level.c
index edcaa23384695e864d841bd2e2928f926880e21e..1048f86c7150fad1015e129c2065f38e95c2487c 100644 (file)
--- a/level.c
+++ b/level.c
@@ -70,13 +70,44 @@ void initLevel() {
                
                glBindTexture(GL_TEXTURE_2D, 0);
        }
+       
+       buildLevelFromImage(readTga("out/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;
        }
+       
+       BlockGrid newGrid = { .width = image->header.imageWidth,
+                             .depth = image->header.imageHeight,
+                             .blocks = malloc(image->header.imageWidth
+                                              * image->header.imageHeight
+                                              * sizeof(Block*)) };
+       
+       for (int z = 0; z < newGrid.depth; ++z) {
+               for (int x = 0; x < newGrid.width; ++x) {
+                       uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(z * newGrid.width) + x];
+                       Block* block;
+                       switch (pixelColorARGB) {
+                               case 0xFFFF0000:
+                                       block = &blockWall01;
+                                       break;
+                               default:
+                                       block = &blockEmpty;
+                                       break;
+                       }
+                       setBlockInGrid(newGrid, x, z, block);
+               }
+       }
+       
+       levelGrid = newGrid;
 }
 
 const AiScene* importScene(const char* path) {
diff --git a/level.h b/level.h
index c2e5c1395b0ce227d2a78603eb43b6fe310381c7..ea43b400676d5084b580641d75ccfad5be8c3d8a 100644 (file)
--- a/level.h
+++ b/level.h
@@ -38,4 +38,8 @@ static inline Block* getBlockFromGrid(BlockGrid grid, int x, int z) {
        return grid.blocks[(z * grid.width) + x];
 }
 
+static inline void setBlockInGrid(BlockGrid grid, int x, int z, Block* block) {
+       grid.blocks[(z * grid.width) + x] = block;
+}
+
 #endif
index c1775c87e803500ef2ad68e43686496c705cb495..a8bebf2d4c8c52ad3123a200c13a3491e53a9679 100644 (file)
--- a/render.c
+++ b/render.c
@@ -19,7 +19,7 @@ void initRender() {
        GLfloat light0_ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
        GLfloat light0_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
        GLfloat light0_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
-       GLfloat light0_position[] = {5.0f, 10.0f, 5.0f, 1.0f};
+       GLfloat light0_position[] = {5.0f, 2.0f, 5.0f, 1.0f};
        
        glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
diff --git a/ui.c b/ui.c
index 410a2a085efa21f0ed31175debd175efe5cda2d8..71effdbcb53e5c4f6f1fbacf7bd6d07f561ff438 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -13,7 +13,7 @@ void resizeStage(GLsizei width, GLsizei height) {
        
        GLfloat aspectRatio = (GLfloat) width / (GLfloat) height;
        
-       glOrtho(-8.0, 8.0, -8.0/aspectRatio, 8.0/aspectRatio, -128.0, 128.0);
+       glOrtho(-8.0, 24.0, -16.0/aspectRatio, 16.0/aspectRatio, -128.0, 128.0);
        
        glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
        glRotatef(45.0f, 0.0f, 1.0f, 0.0f);