]> git.lizzy.rs Git - shadowclad.git/commitdiff
Separate init, main loop, and game update, cleanup main
authoroutfrost <kotlet.bahn@gmail.com>
Fri, 26 Jun 2020 23:48:24 +0000 (01:48 +0200)
committeroutfrost <kotlet.bahn@gmail.com>
Fri, 26 Jun 2020 23:48:24 +0000 (01:48 +0200)
Makefile
src/engine/engine.c [new file with mode: 0644]
src/engine/engine.h [new file with mode: 0644]
src/game/game.c [new file with mode: 0644]
src/game/game.h [new file with mode: 0644]
src/main.c

index f2da15281288788738d2ab8e702c30e93fe87ff2..61c5b433f5450eec9bbc2d43be203693f023250b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ LDLIBS ::= -lm -lGL -lGLEW -lglfw -lassimp $(LDLIBS)
 
 sources ::= main.c \
             engine/asset.c \
+            engine/engine.c \
             engine/geometry.c \
             engine/logger.c \
             engine/performance.c \
@@ -23,6 +24,7 @@ sources ::= main.c \
             engine/string.c \
             engine/tga.c \
             engine/ui.c \
+            game/game.c \
             game/input.c \
             game/level.c \
             game/player.c
diff --git a/src/engine/engine.c b/src/engine/engine.c
new file mode 100644 (file)
index 0000000..81abb23
--- /dev/null
@@ -0,0 +1,106 @@
+#include "engine.h"
+
+#include <stdlib.h>
+#include <assimp/version.h>
+#include <GL/glxew.h>
+#include <GLFW/glfw3.h>
+
+#include "logger.h"
+#include "performance.h"
+#include "render.h"
+#include "ui.h"
+
+// static const int EXIT_OK = 0;
+static const int EXIT_LIB_FAIL = 1;
+static const int EXIT_CTX_FAIL = 2;
+
+static GLFWwindow* window;
+
+static void onGlfwError(int error, const char* description);
+
+
+
+void init() {
+       if (window) {
+               logError("init called more than once");
+               return;
+       }
+
+       logInfo("Assimp %u.%u", aiGetVersionMajor(), aiGetVersionMinor());
+       logInfo("GLEW %s", (const char*) glewGetString(GLEW_VERSION));
+       logInfo("GLFW %s", glfwGetVersionString());
+
+       glfwSetErrorCallback(onGlfwError);
+
+       if (!glfwInit()) {
+               logError("GLFW init failed");
+               exit(EXIT_LIB_FAIL);
+       }
+       // glutInitContextVersion(4,5); TODO establish correct context
+       // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
+       // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
+
+       window = glfwCreateWindow(1280, 720, "shadowclad", NULL, NULL);
+       if (!window) {
+               logError("Window or context creation failed");
+               glfwTerminate();
+               exit(EXIT_CTX_FAIL);
+       }
+
+       glfwMakeContextCurrent(window);
+
+       logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION));
+       logInfo("GLSL %s", (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION));
+       logInfo("%s", (const char*) glGetString(GL_RENDERER));
+
+       GLenum glewInitStatus = glewInit();
+       if (glewInitStatus != GLEW_OK) {
+               logError("GLEW init failed: %s", (const char*) glewGetErrorString(glewInitStatus));
+               exit(EXIT_LIB_FAIL);
+       }
+
+       logInfo("Setting swap interval to 1");
+       glfwSwapInterval(1);
+
+       int width, height;
+       glfwGetFramebufferSize(window, &width, &height);
+       resizeStage(window, width, height);
+
+       glfwSetFramebufferSizeCallback(window, resizeStage);
+
+       initRender();
+       //initPerformanceMetering();
+}
+
+void run(void (*updateFn) (float)) {
+       if (!updateFn) {
+               logError("No update function provided");
+               return;
+       }
+
+       float lastTime = glfwGetTime();
+       float delta = 0.0f;
+
+       while (!glfwWindowShouldClose(window)) {
+               float time = glfwGetTime();
+               delta = time - lastTime;
+               lastTime = time;
+
+               updateFn(delta);
+
+               renderFrame(window);
+               glfwPollEvents();
+       }
+}
+
+void terminate() {
+       glfwTerminate();
+}
+
+void setKeyboardEventCallback(void (*keyboardEventCallback) (GLFWwindow*, int, int, int, int)) {
+       glfwSetKeyCallback(window, keyboardEventCallback);
+}
+
+static void onGlfwError(int error, const char* description) {
+       logError("GLFW error: %s", description);
+}
diff --git a/src/engine/engine.h b/src/engine/engine.h
new file mode 100644 (file)
index 0000000..da5f963
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef ENGINE_H_
+#define ENGINE_H_
+
+typedef struct GLFWwindow GLFWwindow;
+
+void init();
+void run(void (*updateFn) (float));
+void terminate();
+void setKeyboardEventCallback(void (*) (GLFWwindow*, int, int, int, int));
+
+#endif // ENGINE_H_
diff --git a/src/game/game.c b/src/game/game.c
new file mode 100644 (file)
index 0000000..a1a1da2
--- /dev/null
@@ -0,0 +1,19 @@
+#include "game.h"
+
+#include "engine/engine.h"
+
+#include "input.h"
+#include "level.h"
+#include "player.h"
+
+void initGame() {
+       initLevel();
+       initPlayer();
+       startLevel();
+
+       setKeyboardEventCallback(onKeyboardEvent);
+}
+
+void update(float delta) {
+       updatePlayer(delta);
+}
diff --git a/src/game/game.h b/src/game/game.h
new file mode 100644 (file)
index 0000000..7742846
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef GAME_H_
+#define GAME_H_
+
+void initGame();
+void update(float delta);
+
+#endif // GAME_H_
index c99553975127536e616760c5f0ef63cc80c78307..167d3b427d50ba3be4e4a73bbb0b0cd7e85ca8eb 100644 (file)
@@ -1,85 +1,17 @@
-#include <assimp/version.h>
-#include <GL/glxew.h>
-#include <GLFW/glfw3.h>
+#include "engine/engine.h"
 
-#include "engine/logger.h"
-#include "engine/performance.h"
-#include "engine/render.h"
-#include "engine/ui.h"
-
-#include "game/input.h"
-#include "game/level.h"
-#include "game/player.h"
-
-void onGlfwError(int error, const char* description);
+#include "game/game.h"
 
 int main(/*int argc, char** argv*/) {
-       logInfo("Assimp %u.%u", aiGetVersionMajor(), aiGetVersionMinor());
-       logInfo("GLEW %s", (const char*) glewGetString(GLEW_VERSION));
-       logInfo("GLFW %s", glfwGetVersionString());
-
-       glfwSetErrorCallback(onGlfwError);
-
-       if (!glfwInit()) {
-               logError("GLFW init failed");
-               return 1;
-       }
-       // glutInitContextVersion(4,5); TODO establish correct context
-       // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
-       // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
-
-       GLFWwindow* window = glfwCreateWindow(1280, 720, "shadowclad", NULL, NULL);
-       if (!window) {
-               logError("Window or context creation failed");
-               glfwTerminate();
-               return 2;
-       }
-
-       glfwMakeContextCurrent(window);
-
-       logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION));
-       logInfo("GLSL %s", (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION));
-       logInfo("%s", (const char*) glGetString(GL_RENDERER));
-
-       GLenum glewInitStatus = glewInit();
-       if (glewInitStatus != GLEW_OK) {
-               logError("GLEW init failed: %s", (const char*) glewGetErrorString(glewInitStatus));
-               return 1;
-       }
+       // Engine startup
+       init();
 
-       logInfo("Setting swap interval to 1");
-       glfwSwapInterval(1);
+       initGame();
 
-       int width, height;
-       glfwGetFramebufferSize(window, &width, &height);
-       resizeStage(window, width, height);
+       // Main update loop
+       run(update);
 
-       glfwSetFramebufferSizeCallback(window, resizeStage);
-       glfwSetKeyCallback(window, onKeyboardEvent);
-
-       initRender();
-       //initPerformanceMetering();
-       initLevel();
-       initPlayer();
-       startLevel();
-
-       float lastTime = glfwGetTime();
-       float delta = 0.0f;
-
-       while (!glfwWindowShouldClose(window)) {
-               float time = glfwGetTime();
-               delta = time - lastTime;
-               lastTime = time;
-
-               updatePlayer(delta);
-               renderFrame(window);
-               glfwPollEvents();
-       }
-
-       glfwTerminate();
+       // Shutdown
+       terminate();
        return 0;
 }
-
-void onGlfwError(int error, const char* description) {
-       logError("GLFW error: %s", description);
-}