2 * Copyright 2018-2020 Iwo 'Outfrost' Bujkiewicz
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/.
12 #include <assimp/version.h>
14 #include <GLFW/glfw3.h>
19 #include "performance.h"
23 // static const int EXIT_OK = 0;
24 static const int EXIT_LIB_FAIL = 1;
25 static const int EXIT_CTX_FAIL = 2;
27 static GLFWwindow* window;
29 static void onGlfwError(int error, const char* description);
33 void init(EngineConfig config) {
35 logError("init called more than once");
39 logInfo("Assimp %u.%u", aiGetVersionMajor(), aiGetVersionMinor());
40 logInfo("GLEW %s", (const char*) glewGetString(GLEW_VERSION));
41 logInfo("GLFW %s", glfwGetVersionString());
43 glfwSetErrorCallback(onGlfwError);
46 logError("GLFW init failed");
49 // glutInitContextVersion(4,5); TODO establish correct context
50 // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
51 // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
53 window = glfwCreateWindow(config.windowWidth,
55 config.windowTitle.cstr,
60 logError("Window or context creation failed");
65 glfwMakeContextCurrent(window);
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));
71 GLenum glewInitStatus = glewInit();
72 if (glewInitStatus != GLEW_OK) {
73 logError("GLEW init failed: %s", (const char*) glewGetErrorString(glewInitStatus));
77 logInfo("Setting swap interval: %d", config.swapInterval);
78 glfwSwapInterval(config.swapInterval);
81 glfwGetFramebufferSize(window, &width, &height);
82 resizeStage(window, width, height);
84 glfwSetFramebufferSizeCallback(window, resizeStage);
85 glfwSetKeyCallback(window, onKeyboardEvent);
88 //initPerformanceMetering();
91 void run(void (*updateFn) (float)) {
93 logError("No update function provided");
97 float lastTime = glfwGetTime();
100 while (!glfwWindowShouldClose(window)) {
101 float time = glfwGetTime();
102 delta = time - lastTime;
116 EngineConfig defaultConfig() {
117 return (EngineConfig) { .windowWidth = 800,
119 .windowTitle = newString(NULL),
123 static void onGlfwError(int error UNUSED, const char* description) {
124 logError("GLFW error: %s", description);