From 5ad3cdb713a2ea4dec1ea010805d6144e9055271 Mon Sep 17 00:00:00 2001 From: outfrost Date: Thu, 31 Jan 2019 03:54:34 +0100 Subject: [PATCH] Add the concept of a player character and load its 3D model Also, a bit of code cleanup --- Makefile | 4 ++-- asset.c | 20 ++++++++++++++++++++ asset.h | 8 ++++++++ assimp_types.h | 5 +++++ level.c | 29 ++++++++++++----------------- level.h | 2 +- logger.c | 2 ++ main.c | 2 -- performance.c | 2 ++ player.c | 20 ++++++++++++++++++++ player.h | 15 +++++++++++++++ render.c | 2 ++ 12 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 asset.c create mode 100644 asset.h create mode 100644 player.c create mode 100644 player.h diff --git a/Makefile b/Makefile index 28a753a..6dcf104 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,8 @@ LDLIBS ::= -L/usr/local/lib -lGL -lGLEW -lglut -lassimp $(LDLIBS) # Prefix all object file names with the compilation directory objects ::= $(addprefix out/, \ - main.o level.o logger.o \ - performance.o render.o tga.o ui.o) + main.o asset.o level.o logger.o \ + performance.o player.o render.o tga.o ui.o) # Set executable extension for the platform ifeq ($(OS),Windows_NT) diff --git a/asset.c b/asset.c new file mode 100644 index 0000000..ff7c3db --- /dev/null +++ b/asset.c @@ -0,0 +1,20 @@ +#include +#include + +#include "assimp_types.h" + +#include "logger.h" + +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; + } + return scene; + // TODO aiReleaseImport(scene); +} diff --git a/asset.h b/asset.h new file mode 100644 index 0000000..03e268f --- /dev/null +++ b/asset.h @@ -0,0 +1,8 @@ +#ifndef ASSET_H_ +#define ASSET_H_ + +#include "assimp_types.h" + +const AiScene* importScene(const char* path); + +#endif diff --git a/assimp_types.h b/assimp_types.h index 5869c32..5e58279 100644 --- a/assimp_types.h +++ b/assimp_types.h @@ -3,6 +3,11 @@ #include +#ifdef ASSIMP_DOUBLE_PRECISION + #error "ASSIMP_DOUBLE_PRECISION is defined" + #error "shadowclad relies on ai_real defined as a single precision float" +#endif + typedef struct aiScene AiScene; typedef struct aiNode AiNode; typedef struct aiMesh AiMesh; diff --git a/level.c b/level.c index 1048f86..1a1778b 100644 --- a/level.c +++ b/level.c @@ -1,10 +1,10 @@ #include -#include -#include #include +#include "asset.h" #include "level.h" #include "logger.h" +#include "player.h" static Block blockEmpty = { .type = BLOCKTYPE_SPACE, .sceneData = NULL, @@ -21,10 +21,13 @@ 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"); blockWall01.sceneData = sceneData; @@ -90,6 +93,7 @@ void buildLevelFromImage(TgaImage* image) { .blocks = malloc(image->header.imageWidth * image->header.imageHeight * sizeof(Block*)) }; + playerSpawnPos = (AiVector3D) DEFAULT_PLAYER_SPAWN_POS; for (int z = 0; z < newGrid.depth; ++z) { for (int x = 0; x < newGrid.width; ++x) { @@ -99,6 +103,10 @@ void buildLevelFromImage(TgaImage* image) { 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; @@ -108,20 +116,7 @@ void buildLevelFromImage(TgaImage* image) { } levelGrid = newGrid; -} - -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; - } - return scene; - // TODO aiReleaseImport(scene); + spawnPlayer(); } /** BUGS diff --git a/level.h b/level.h index ea43b40..83fefd9 100644 --- a/level.h +++ b/level.h @@ -29,10 +29,10 @@ typedef struct { #define BLOCKGRID_CELL_SIZE 2.5f BlockGrid levelGrid; +AiVector3D playerSpawnPos; void initLevel(); void buildLevelFromImage(TgaImage* image); -const AiScene* importScene(const char* path); static inline Block* getBlockFromGrid(BlockGrid grid, int x, int z) { return grid.blocks[(z * grid.width) + x]; diff --git a/logger.c b/logger.c index 23b0dd8..373df63 100644 --- a/logger.c +++ b/logger.c @@ -5,6 +5,8 @@ LogLevel logLevel = LOGLEVEL_DEBUG; + + void logMessage(LogLevel msgLevel, const char* file, int line, const char* message, ...) { if (msgLevel > logLevel) { return; diff --git a/main.c b/main.c index 527a936..8076c54 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,6 @@ #include #include -//#include "debugutil.h" #include "level.h" #include "logger.h" #include "performance.h" @@ -17,7 +16,6 @@ int main(int argc, char** argv) { glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutCreateWindow(NULL); - // glutSetWindowTitle(getGlInfoString()); glutSetWindowTitle("shadowclad"); logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION)); diff --git a/performance.c b/performance.c index 916517b..443652c 100644 --- a/performance.c +++ b/performance.c @@ -14,6 +14,8 @@ static Timepoint lastDisplayTime; static int frames = 0; static bool meteringEnabled = false; + + void initPerformanceMetering() { if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) { logWarning("Clock read failed, performance metering unavailable"); diff --git a/player.c b/player.c new file mode 100644 index 0000000..dfe41f3 --- /dev/null +++ b/player.c @@ -0,0 +1,20 @@ +#include "asset.h" +#include "level.h" +#include "logger.h" +#include "player.h" + +PlayerCharacter playerCharacter = { .sceneData = NULL, + .textureIds = NULL }; + +static AiVector3D playerPos; + + + +void initPlayer() { + playerCharacter.sceneData = importScene("out/assets/playercharacter.3ds"); + // TODO import textures +} + +void spawnPlayer() { + playerPos = playerSpawnPos; +} diff --git a/player.h b/player.h new file mode 100644 index 0000000..f480c45 --- /dev/null +++ b/player.h @@ -0,0 +1,15 @@ +#ifndef PLAYER_H_ +#define PLAYER_H_ + +#include + +#include "assimp_types.h" + +typedef struct { + const AiScene* sceneData; + GLuint* textureIds; +} PlayerCharacter; + +void spawnPlayer(); + +#endif diff --git a/render.c b/render.c index ce77a0f..87acb69 100644 --- a/render.c +++ b/render.c @@ -13,6 +13,8 @@ static void drawAxes(); static void renderBlockGrid(const BlockGrid grid); static void drawBlock(const Block* block); + + void initRender() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); -- 2.44.0