]> git.lizzy.rs Git - shadowclad.git/blob - src/engine/engine.c
Add copyright and license notices in source code
[shadowclad.git] / src / engine / engine.c
1 /**
2  * Copyright 2018-2020 Iwo 'Outfrost' Bujkiewicz
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8
9 #include "engine.h"
10
11 #include <stdlib.h>
12 #include <assimp/version.h>
13 #include <GL/glxew.h>
14 #include <GLFW/glfw3.h>
15
16 #include "_prelude.h"
17 #include "input.h"
18 #include "logger.h"
19 #include "performance.h"
20 #include "render.h"
21 #include "ui.h"
22
23 // static const int EXIT_OK = 0;
24 static const int EXIT_LIB_FAIL = 1;
25 static const int EXIT_CTX_FAIL = 2;
26
27 static GLFWwindow* window;
28
29 static void onGlfwError(int error, const char* description);
30
31
32
33 void init(EngineConfig config) {
34         if (window) {
35                 logError("init called more than once");
36                 return;
37         }
38
39         logInfo("Assimp %u.%u", aiGetVersionMajor(), aiGetVersionMinor());
40         logInfo("GLEW %s", (const char*) glewGetString(GLEW_VERSION));
41         logInfo("GLFW %s", glfwGetVersionString());
42
43         glfwSetErrorCallback(onGlfwError);
44
45         if (!glfwInit()) {
46                 logError("GLFW init failed");
47                 exit(EXIT_LIB_FAIL);
48         }
49         // glutInitContextVersion(4,5); TODO establish correct context
50         // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
51         // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
52
53         window = glfwCreateWindow(config.windowWidth,
54                                   config.windowHeight,
55                                   config.windowTitle.cstr,
56                                   NULL,
57                                   NULL);
58
59         if (!window) {
60                 logError("Window or context creation failed");
61                 glfwTerminate();
62                 exit(EXIT_CTX_FAIL);
63         }
64
65         glfwMakeContextCurrent(window);
66
67         logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION));
68         logInfo("GLSL %s", (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION));
69         logInfo("Renderer: %s", (const char*) glGetString(GL_RENDERER));
70
71         GLenum glewInitStatus = glewInit();
72         if (glewInitStatus != GLEW_OK) {
73                 logError("GLEW init failed: %s", (const char*) glewGetErrorString(glewInitStatus));
74                 exit(EXIT_LIB_FAIL);
75         }
76
77         logInfo("Setting swap interval: %d", config.swapInterval);
78         glfwSwapInterval(config.swapInterval);
79
80         int width, height;
81         glfwGetFramebufferSize(window, &width, &height);
82         resizeStage(window, width, height);
83
84         glfwSetFramebufferSizeCallback(window, resizeStage);
85         glfwSetKeyCallback(window, onKeyboardEvent);
86
87         initRender();
88         //initPerformanceMetering();
89 }
90
91 void run(void (*updateFn) (float)) {
92         if (!updateFn) {
93                 logError("No update function provided");
94                 return;
95         }
96
97         float lastTime = glfwGetTime();
98         float delta = 0.0f;
99
100         while (!glfwWindowShouldClose(window)) {
101                 float time = glfwGetTime();
102                 delta = time - lastTime;
103                 lastTime = time;
104
105                 updateFn(delta);
106
107                 renderFrame(window);
108                 glfwPollEvents();
109         }
110 }
111
112 void terminate() {
113         glfwTerminate();
114 }
115
116 EngineConfig defaultConfig() {
117         return (EngineConfig) { .windowWidth = 800,
118                                 .windowHeight = 600,
119                                 .windowTitle = newString(NULL),
120                                 .swapInterval = 1 };
121 }
122
123 static void onGlfwError(int error UNUSED, const char* description) {
124         logError("GLFW error: %s", description);
125 }