Quellcode durchsuchen

Split window code over functions

Kajetan Johannes Hammerle vor 3 Tagen
Ursprung
Commit
92dcc7339e
3 geänderte Dateien mit 51 neuen und 28 gelöschten Zeilen
  1. 12 1
      src/Main.c
  2. 26 26
      src/Window.c
  3. 13 1
      src/Window.h

+ 12 - 1
src/Main.c

@@ -12,7 +12,18 @@
 int main(int argCount, const char** args) {
 int main(int argCount, const char** args) {
     if(argCount < 2) {
     if(argCount < 2) {
         return 0;
         return 0;
-    } else if(strcmp(args[1], "w") == 0 && windowTest()) {
+    } else if(strcmp(args[1], "w") == 0) {
+        WindowSettings ws = {.width = 400, .height = 300, .title = "Example"};
+        Error e = windowInit(&ws);
+        if(hasError(&e)) {
+            puts(e.text);
+            return 0;
+        }
+        while(!windowShouldClose()) {
+            windowNextFrame();
+        }
+        puts("Stop window");
+        windowDestroy();
         return 0;
         return 0;
     }
     }
 
 

+ 26 - 26
src/Window.c

@@ -3,39 +3,39 @@
 #include <GLFW/glfw3.h>
 #include <GLFW/glfw3.h>
 #include <stdio.h>
 #include <stdio.h>
 
 
-bool windowTest() {
-    GLFWwindow* window;
+#define SET_ERROR(format, ...)                                           \
+    snprintf(e.text, sizeof(e.text), format __VA_OPT__(, ) __VA_ARGS__);
 
 
-    /* Initialize the library */
+static GLFWwindow* window = nullptr;
+
+Error windowInit(const WindowSettings* ws) {
+    Error e = {};
     if(!glfwInit()) {
     if(!glfwInit()) {
-        printf("init failed\n");
-        return true;
+        SET_ERROR("Init window failed");
+        return e;
     }
     }
-
-    /* Create a windowed mode window and its OpenGL context */
-    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
-    if(!window) {
+    window =
+        glfwCreateWindow(ws->width, ws->height, ws->title, nullptr, nullptr);
+    if(window == nullptr) {
         glfwTerminate();
         glfwTerminate();
-        printf("create window failed\n");
-        return true;
+        SET_ERROR("Create window failed\n");
+        return e;
     }
     }
-
-    /* Make the window's context current */
     glfwMakeContextCurrent(window);
     glfwMakeContextCurrent(window);
+    return e;
+}
 
 
-    /* Loop until the user closes the window */
-    while(!glfwWindowShouldClose(window)) {
-        /* Render here */
-        glClear(GL_COLOR_BUFFER_BIT);
-
-        /* Swap front and back buffers */
-        glfwSwapBuffers(window);
+void windowDestroy() {
+    glfwDestroyWindow(window);
+    glfwTerminate();
+}
 
 
-        /* Poll for and process events */
-        glfwPollEvents();
-    }
-    printf("YES\n");
+void windowNextFrame() {
+    glClear(GL_COLOR_BUFFER_BIT);
+    glfwSwapBuffers(window);
+    glfwPollEvents();
+}
 
 
-    glfwTerminate();
-    return true;
+bool windowShouldClose() {
+    return glfwWindowShouldClose(window);
 }
 }

+ 13 - 1
src/Window.h

@@ -1,6 +1,18 @@
 #ifndef BASIC_WINDOW_H
 #ifndef BASIC_WINDOW_H
 #define BASIC_WINDOW_H
 #define BASIC_WINDOW_H
 
 
-bool windowTest();
+#include "Error.h"
+
+typedef struct {
+    int width;
+    int height;
+    char title[64];
+} WindowSettings;
+
+Error windowInit(const WindowSettings* ws);
+void windowDestroy();
+
+void windowNextFrame();
+bool windowShouldClose();
 
 
 #endif
 #endif