]> git.lizzy.rs Git - shadowclad.git/commitdiff
Refactor handling of imported assets a bit
authoroutfrost <kotlet.bahn@gmail.com>
Sun, 13 Jan 2019 19:32:25 +0000 (20:32 +0100)
committeroutfrost <kotlet.bahn@gmail.com>
Sun, 13 Jan 2019 19:32:25 +0000 (20:32 +0100)
assimp_types.h [new file with mode: 0644]
level.c
level.h
main.c
render.c

diff --git a/assimp_types.h b/assimp_types.h
new file mode 100644 (file)
index 0000000..5900963
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef ASSIMP_TYPES_H_
+#define ASSIMP_TYPES_H_
+
+#include <assimp/scene.h>
+
+typedef struct aiScene AiScene;
+typedef struct aiNode AiNode;
+typedef struct aiMesh AiMesh;
+typedef struct aiFace AiFace;
+
+#endif
diff --git a/level.c b/level.c
index 0b344fe1965bea8033b5ce13ba25f3e8ef1a84cc..cec2d042e4e07b4c58668dd7f83f6302109cab1e 100644 (file)
--- a/level.c
+++ b/level.c
@@ -1,30 +1,29 @@
 #include <GL/gl.h>
 #include <assimp/cimport.h>
-#include <assimp/scene.h>
 
-#include <stdlib.h>
 #include <stdio.h> // TODO remove
 
 #include "level.h"
-#include "tga.h"
 
-const Block BLOCK_EMPTY = 0;
-const Block BLOCK_WALL01 = 1;
+const AiScene* levelScene = NULL;
 
-TgaImage* levelImage = NULL;
+static const Block BLOCK_EMPTY = 0;
+static const Block BLOCK_WALL01 = 0xFF0000FF; // red
 
-Block getBlock(GLushort x, GLushort y) {
-       if (levelImage == NULL) {
-               return BLOCK_EMPTY;
-       }
-       return ((Block*) (*levelImage).bytes)[x * (*levelImage).header.imageWidth + y];
+static const AiScene* blockWall01 = NULL;
+
+static TgaImage* levelImage = NULL;
+
+void initLevel() {
+       blockWall01 = importScene("out/assets/wall01.3ds");
+       levelScene = blockWall01;
 }
 
 void setImage(TgaImage* image) {
        levelImage = image;
 }
 
-const struct aiScene* importModel(const char* path) {
+const AiScene* importScene(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
diff --git a/level.h b/level.h
index 96fcb7aa47b862de68d1378227bb4bf8e29940a2..b36c2bb88738b3550593d2126c518b5118279762 100644 (file)
--- a/level.h
+++ b/level.h
@@ -1,17 +1,18 @@
 #ifndef LEVEL_H_
 #define LEVEL_H_
 
-#include <GL/gl.h>
+#include <stdint.h>
+
+#include "assimp_types.h"
 
 #include "tga.h"
 
-typedef GLuint Block;
+typedef uint32_t Block;
 
-const Block BLOCK_EMPTY;
-const Block BLOCK_WALL01;
+const AiScene* levelScene;
 
-Block getBlock(GLushort x, GLushort y);
+void initLevel();
 void setImage(TgaImage* image);
-const struct aiScene* importModel(const char* path);
+const AiScene* importScene(const char* path);
 
 #endif
diff --git a/main.c b/main.c
index 34d7ec6f18d521a471e18baa31d8175dea0149ed..32f2c855d52e578c5754e30e02ef83704ed3e0ea 100644 (file)
--- a/main.c
+++ b/main.c
@@ -24,8 +24,8 @@ int main(int argc, char** argv) {
        
        initRender();
        initPerformanceMetering();
+       initLevel();
        
-       model = importModel("out/assets/wall01.3ds");
        /*
        fprintf(stderr, "*model = ");
        print_struct_aiScene(stderr, model);
index 807216d8267c364408e39dcd638b6d30cc255f03..c10f8f1f03a85b6faf56f69c468bb18c66255e95 100644 (file)
--- a/render.c
+++ b/render.c
@@ -1,6 +1,8 @@
 #include <GL/glut.h>
-#include <assimp/scene.h>
 
+#include "assimp_types.h"
+
+#include "level.h"
 #include "render.h"
 #include "typedefs.h"
 #include "performance.h"
@@ -19,7 +21,7 @@ void renderScene() {
        glEnable(GL_LIGHTING);
        
        glEnable(GL_LIGHT0);
-       drawModelRecursive(model, (*model).mRootNode);
+       drawModelRecursive(levelScene, levelScene->mRootNode);
        glDisable(GL_LIGHT0);
        
        glFlush();
@@ -55,15 +57,15 @@ void drawAxes() {
        glEnd();
 }
 
-void drawModelRecursive(const struct aiScene* model, const struct aiNode* node) {
+void drawModelRecursive(const AiScene* model, const AiNode* node) {
        if (((*model).mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
                return;
        }
        
        for (int i = 0; i < (*node).mNumMeshes; ++i) {
-               const struct aiMesh* mesh = (*model).mMeshes[(*node).mMeshes[i]];
+               const AiMesh* mesh = (*model).mMeshes[(*node).mMeshes[i]];
                for (int k = 0; k < (*mesh).mNumFaces; ++k) {
-                       const struct aiFace face = (*mesh).mFaces[k];
+                       const AiFace face = (*mesh).mFaces[k];
                        
                        GLenum faceMode;
                        switch (face.mNumIndices) {