small changes for debugging and error messages
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 13 Oct 2020 16:29:42 +0000 (11:29 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 13 Oct 2020 16:29:42 +0000 (11:29 -0500)
Makefile
doc/plan [new file with mode: 0644]
src/sim.c

index 731481fb9a75af9c93326b4d7d738131915cf9ab..56b386b56072ea1c4994040d12a5baf58ca11f41 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ endif
 endif
 
 INCLUDES=-I./include
-LIBS=-lGL -lglfw
+LIBS=-lGL -lglfw -lm
 TARGET=./sim
 
 ifeq ($(RELEASE), 1)
diff --git a/doc/plan b/doc/plan
new file mode 100644 (file)
index 0000000..e69de29
index fbd700ca6d4ecc72b357de5e24c15dc4c160e454..a5dc7083cb78d03326513191e1287136a983964d 100644 (file)
--- a/src/sim.c
+++ b/src/sim.c
@@ -1,3 +1,5 @@
+#define DEBUG
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <malloc.h>
@@ -11,7 +13,7 @@
 #define WINDOW_HEIGHT          900
 #define WINDOW_TITLE           "N-Body Simulation"
 
-void __attribute__((noreturn)) panic_and_die(char* msg, ...) {
+void __attribute__((noreturn)) panic_and_die(const char* msg, ...) {
        puts("************ PANIC ************");
 
        va_list va;
@@ -19,6 +21,11 @@ void __attribute__((noreturn)) panic_and_die(char* msg, ...) {
        vprintf(msg, va);
        va_end(va);
 
+#ifdef DEBUG
+       // NOTE: This allows for a debugger to stop here.
+       __asm("int $3");
+#endif
+
        exit(1);
 }
 
@@ -27,20 +34,35 @@ void glfw_key_handler(GLFWwindow* window, int key, int scancode, int action, int
                glfwSetWindowShouldClose(window, 1);
 }
 
+void glfw_resize_handler(GLFWwindow* window, int width, int height) {
+       glViewport(0, 0, width, height);
+}
+
+void glfw_error_handler(int error, const char* desc) {
+       panic_and_die("GLFW Error (%d): %s\n", error, desc);
+}
+
 GLFWwindow* window;
 void init_opengl() {
        if (!glfwInit()) panic_and_die("Failed to initalize GLFW.");
+       glfwSetErrorCallback(glfw_error_handler);
 
        window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, NULL, NULL);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
        glfwMakeContextCurrent(window);
 
+       glfwSwapInterval(1);
        glfwSetKeyCallback(window, glfw_key_handler);
+       glfwSetFramebufferSizeCallback(window, glfw_resize_handler);
 }
 
 void deinit_opengl() {
        glfwDestroyWindow(window);
+       glfwTerminate();
+}
+
+void update() {
 }
 
 void draw() {
@@ -53,6 +75,7 @@ void loop() {
        while (!glfwWindowShouldClose(window)) {
                glfwPollEvents();
 
+               update();
                draw();
        }
 }
@@ -61,4 +84,6 @@ int main(int argc, char* argv[]) {
        init_opengl();
        loop();
        deinit_opengl();
+
+       return 0;
 }
\ No newline at end of file