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