]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/engine.c
Tweak info prints
[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(EngineConfig config) {
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(config.windowWidth,
45                                   config.windowHeight,
46                                   config.windowTitle.cstr,
47                                   NULL,
48                                   NULL);
49
50         if (!window) {
51                 logError("Window or context creation failed");
52                 glfwTerminate();
53                 exit(EXIT_CTX_FAIL);
54         }
55
56         glfwMakeContextCurrent(window);
57
58         logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION));
59         logInfo("GLSL %s", (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION));
60         logInfo("Renderer: %s", (const char*) glGetString(GL_RENDERER));
61
62         GLenum glewInitStatus = glewInit();
63         if (glewInitStatus != GLEW_OK) {
64                 logError("GLEW init failed: %s", (const char*) glewGetErrorString(glewInitStatus));
65                 exit(EXIT_LIB_FAIL);
66         }
67
68         logInfo("Setting swap interval: %d", config.swapInterval);
69         glfwSwapInterval(config.swapInterval);
70
71         int width, height;
72         glfwGetFramebufferSize(window, &width, &height);
73         resizeStage(window, width, height);
74
75         glfwSetFramebufferSizeCallback(window, resizeStage);
76         glfwSetKeyCallback(window, onKeyboardEvent);
77
78         initRender();
79         //initPerformanceMetering();
80 }
81
82 void run(void (*updateFn) (float)) {
83         if (!updateFn) {
84                 logError("No update function provided");
85                 return;
86         }
87
88         float lastTime = glfwGetTime();
89         float delta = 0.0f;
90
91         while (!glfwWindowShouldClose(window)) {
92                 float time = glfwGetTime();
93                 delta = time - lastTime;
94                 lastTime = time;
95
96                 updateFn(delta);
97
98                 renderFrame(window);
99                 glfwPollEvents();
100         }
101 }
102
103 void terminate() {
104         glfwTerminate();
105 }
106
107 EngineConfig defaultConfig() {
108         return (EngineConfig) { .windowWidth = 800,
109                                 .windowHeight = 600,
110                                 .windowTitle = newString(NULL),
111                                 .swapInterval = 1 };
112 }
113
114 static void onGlfwError(int error, const char* description) {
115         logError("GLFW error: %s", description);
116 }