]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/engine.c
Restructure keyboard input handling
[shadowclad.git] / src / engine / engine.c
1 #include "engine.h"
2
3 #include <stdlib.h>
4 #include <assimp/version.h>
5 #include <GL/glxew.h>
6 #include <GLFW/glfw3.h>
7
8 #include "input.h"
9 #include "logger.h"
10 #include "performance.h"
11 #include "render.h"
12 #include "ui.h"
13
14 // static const int EXIT_OK = 0;
15 static const int EXIT_LIB_FAIL = 1;
16 static const int EXIT_CTX_FAIL = 2;
17
18 static GLFWwindow* window;
19
20 static void onGlfwError(int error, const char* description);
21
22
23
24 void init() {
25         if (window) {
26                 logError("init called more than once");
27                 return;
28         }
29
30         logInfo("Assimp %u.%u", aiGetVersionMajor(), aiGetVersionMinor());
31         logInfo("GLEW %s", (const char*) glewGetString(GLEW_VERSION));
32         logInfo("GLFW %s", glfwGetVersionString());
33
34         glfwSetErrorCallback(onGlfwError);
35
36         if (!glfwInit()) {
37                 logError("GLFW init failed");
38                 exit(EXIT_LIB_FAIL);
39         }
40         // glutInitContextVersion(4,5); TODO establish correct context
41         // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
42         // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
43
44         window = glfwCreateWindow(1280, 720, "shadowclad", NULL, NULL);
45         if (!window) {
46                 logError("Window or context creation failed");
47                 glfwTerminate();
48                 exit(EXIT_CTX_FAIL);
49         }
50
51         glfwMakeContextCurrent(window);
52
53         logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION));
54         logInfo("GLSL %s", (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION));
55         logInfo("%s", (const char*) glGetString(GL_RENDERER));
56
57         GLenum glewInitStatus = glewInit();
58         if (glewInitStatus != GLEW_OK) {
59                 logError("GLEW init failed: %s", (const char*) glewGetErrorString(glewInitStatus));
60                 exit(EXIT_LIB_FAIL);
61         }
62
63         logInfo("Setting swap interval to 1");
64         glfwSwapInterval(1);
65
66         int width, height;
67         glfwGetFramebufferSize(window, &width, &height);
68         resizeStage(window, width, height);
69
70         glfwSetFramebufferSizeCallback(window, resizeStage);
71         glfwSetKeyCallback(window, onKeyboardEvent);
72
73         initRender();
74         //initPerformanceMetering();
75 }
76
77 void run(void (*updateFn) (float)) {
78         if (!updateFn) {
79                 logError("No update function provided");
80                 return;
81         }
82
83         float lastTime = glfwGetTime();
84         float delta = 0.0f;
85
86         while (!glfwWindowShouldClose(window)) {
87                 float time = glfwGetTime();
88                 delta = time - lastTime;
89                 lastTime = time;
90
91                 updateFn(delta);
92
93                 renderFrame(window);
94                 glfwPollEvents();
95         }
96 }
97
98 void terminate() {
99         glfwTerminate();
100 }
101
102 static void onGlfwError(int error, const char* description) {
103         logError("GLFW error: %s", description);
104 }