]> git.lizzy.rs Git - shadowclad.git/commitdiff
Extract some configuration into EngineConfig
authoroutfrost <kotlet.bahn@gmail.com>
Sat, 27 Jun 2020 02:24:03 +0000 (04:24 +0200)
committeroutfrost <kotlet.bahn@gmail.com>
Sat, 27 Jun 2020 02:24:03 +0000 (04:24 +0200)
src/engine/engine.c
src/engine/engine.h
src/engine/string.c
src/engine/string.h
src/main.c

index 17155334fc8862a8b4ac1d052c3618b7ef2c6da1..aba8cd20373bf3f0214e2de476a493ef4d2efbe9 100644 (file)
@@ -21,7 +21,7 @@ static void onGlfwError(int error, const char* description);
 
 
 
-void init() {
+void init(EngineConfig config) {
        if (window) {
                logError("init called more than once");
                return;
@@ -41,7 +41,12 @@ void init() {
        // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
        // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
 
-       window = glfwCreateWindow(1280, 720, "shadowclad", NULL, NULL);
+       window = glfwCreateWindow(config.windowWidth,
+                                 config.windowHeight,
+                                 config.windowTitle.cstr,
+                                 NULL,
+                                 NULL);
+
        if (!window) {
                logError("Window or context creation failed");
                glfwTerminate();
@@ -60,8 +65,8 @@ void init() {
                exit(EXIT_LIB_FAIL);
        }
 
-       logInfo("Setting swap interval to 1");
-       glfwSwapInterval(1);
+       logInfo("Setting swap interval to %d", config.swapInterval);
+       glfwSwapInterval(config.swapInterval);
 
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
@@ -99,6 +104,13 @@ void terminate() {
        glfwTerminate();
 }
 
+EngineConfig defaultConfig() {
+       return (EngineConfig) { .windowWidth = 800,
+                               .windowHeight = 600,
+                               .windowTitle = newString(NULL),
+                               .swapInterval = 1 };
+}
+
 static void onGlfwError(int error, const char* description) {
        logError("GLFW error: %s", description);
 }
index f1ff1026b0a22743e7bd2c42d4b9480e97cac58d..1fdcb01ff84352ecaaeb4e9f6ebdbe9d5e2f2a57 100644 (file)
@@ -1,8 +1,21 @@
 #ifndef ENGINE_ENGINE_H_
 #define ENGINE_ENGINE_H_
 
-void init();
+#include "string.h"
+
+typedef struct EngineConfig EngineConfig;
+
+struct EngineConfig {
+       int windowWidth;
+       int windowHeight;
+       String windowTitle;
+       int swapInterval;
+};
+
+void init(EngineConfig);
 void run(void (*updateFn) (float));
 void terminate();
 
+EngineConfig defaultConfig();
+
 #endif // ENGINE_ENGINE_H_
index 05c93ccc9bc7fb7f9cc312c553208fe62a44d926..04ad0dbe1edee1418c66fc031739d9db5cd3c16d 100644 (file)
@@ -2,6 +2,22 @@
 
 #include <stdlib.h>
 
+String newString(const char* s) {
+       size_t len = 0u;
+       char* cstr;
+       if (s) {
+               len = strlen(s);
+               cstr = memcpy(malloc((len + 1) * sizeof(char)),
+                             s,
+                             len * sizeof(char));
+       }
+       else {
+               cstr = malloc(1 * sizeof(char));
+       }
+       cstr[len] = '\0';
+       return (String) { .length = len, .cstr = cstr };
+}
+
 String stringFromAiString(const struct aiString aistr) {
        char* cstr = memcpy(malloc((aistr.length + 1) * sizeof(char)),
                                aistr.data,
index edd6508e0d952196bde7aa9890fd5d676dd06211..92409c32bafe0416da1a9d86cb1f5504f381e7e6 100644 (file)
@@ -10,6 +10,7 @@ struct String {
        char* cstr;
 };
 
+String newString(const char* s);
 String stringFromAiString(const struct aiString aistr);
 void dropString(String str);
 
index 167d3b427d50ba3be4e4a73bbb0b0cd7e85ca8eb..6cd21f0077b962b69783a200b7dea9eae43a47fa 100644 (file)
@@ -3,8 +3,13 @@
 #include "game/game.h"
 
 int main(/*int argc, char** argv*/) {
+       EngineConfig cfg = { .windowWidth = 1280,
+                            .windowHeight = 720,
+                            .windowTitle = newString("shadowclad"),
+                            .swapInterval = 1 };
+
        // Engine startup
-       init();
+       init(cfg);
 
        initGame();