// Copyright (c) 2021 Dominic Msters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "glwfwplatform.h" GLFWwindow *window = NULL; game_t *runningGame = NULL; int32_t main() { // Attempt to init GLFW if(!glfwInit()) return NULL; window = glfwCreateWindow(WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT, "", NULL, NULL ); if(!window) { glfwTerminate(); return 1; } // Load GLAD glfwMakeContextCurrent(window); glfwSwapInterval(0); gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); // Setup window listeners glfwSetWindowSizeCallback(window, &glfwOnResize); glfwSetKeyCallback(window, &glfwOnKey); // Init the game if(gameInit()) { // Bind initial keys inputBind(INPUT_NULL, (inputsource_t)GLFW_KEY_ESCAPE); inputBind(INPUT_UP, (inputsource_t)GLFW_KEY_UP); inputBind(INPUT_DOWN, (inputsource_t)GLFW_KEY_DOWN); inputBind(INPUT_LEFT, (inputsource_t)GLFW_KEY_LEFT); inputBind(INPUT_RIGHT, (inputsource_t)GLFW_KEY_RIGHT); inputBind(INPUT_UP, (inputsource_t)GLFW_KEY_W); inputBind(INPUT_DOWN, (inputsource_t)GLFW_KEY_S); inputBind(INPUT_LEFT, (inputsource_t)GLFW_KEY_A); inputBind(INPUT_RIGHT, (inputsource_t)GLFW_KEY_D); // Init the render resolution renderSetResolution(WINDOW_WIDTH_DEFAULT, WINDOW_HEIGHT_DEFAULT); // Update the window title. glfwSetWindowTitle(window, GAME_STATE.name); double time = 0; // Main Render Loop while(!glfwWindowShouldClose(window)) { glfwPollEvents(); // Determine the delta. double newTime = glfwGetTime(); float fDelta = (float)(newTime - time); time = newTime; // Tick the engine. if(!gameUpdate(fDelta)) break; glfwSwapBuffers(window); sleep(0);//Fixes some weird high CPU bug, not actually necessary. } // Game has finished running, cleanup. gameDispose(); } // Terminate the GLFW context. glfwSetWindowSizeCallback(window, NULL); glfwTerminate(); return 0; } void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) { renderSetResolution(width, height); } void glfwOnKey(GLFWwindow *window, int32_t key, int32_t scancode, int32_t action, int32_t mods ) { if(action == GLFW_PRESS) { INPUT_STATE.buffer[key] = 1; } else if(action == GLFW_RELEASE) { INPUT_STATE.buffer[key] = 0; } }