From c9189c780774c48559d09de00e7edf057eccd2ce Mon Sep 17 00:00:00 2001 From: outfrost Date: Sun, 13 Jan 2019 20:32:25 +0100 Subject: [PATCH] Refactor handling of imported assets a bit --- assimp_types.h | 11 +++++++++++ level.c | 23 +++++++++++------------ level.h | 13 +++++++------ main.c | 2 +- render.c | 12 +++++++----- 5 files changed, 37 insertions(+), 24 deletions(-) create mode 100644 assimp_types.h diff --git a/assimp_types.h b/assimp_types.h new file mode 100644 index 0000000..5900963 --- /dev/null +++ b/assimp_types.h @@ -0,0 +1,11 @@ +#ifndef ASSIMP_TYPES_H_ +#define ASSIMP_TYPES_H_ + +#include + +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 0b344fe..cec2d04 100644 --- a/level.c +++ b/level.c @@ -1,30 +1,29 @@ #include #include -#include -#include #include // 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 96fcb7a..b36c2bb 100644 --- a/level.h +++ b/level.h @@ -1,17 +1,18 @@ #ifndef LEVEL_H_ #define LEVEL_H_ -#include +#include + +#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 34d7ec6..32f2c85 100644 --- 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); diff --git a/render.c b/render.c index 807216d..c10f8f1 100644 --- a/render.c +++ b/render.c @@ -1,6 +1,8 @@ #include -#include +#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) { -- 2.44.0