From d82e6263e8a33e37f0526b93f400664a7b7c734e Mon Sep 17 00:00:00 2001 From: outfrost Date: Tue, 23 Jun 2020 01:32:14 +0200 Subject: [PATCH] Migrate to GLFW 3 --- Makefile | 2 +- src/engine/render.c | 6 ++-- src/engine/render.h | 4 ++- src/engine/ui.c | 6 ++-- src/engine/ui.h | 4 ++- src/game/input.c | 21 ++++++++++++++ src/game/input.h | 3 ++ src/main.c | 67 +++++++++++++++++++++++++++++++-------------- 8 files changed, 83 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 42225a5..f2da152 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CPPFLAGS ::= -iquotesrc/ $(CPPFLAGS) CFLAGS ::= -g -std=c99 -Wall -Wextra -Wpedantic -Werror \ -Wno-error=unused-function -Wno-error=unused-parameter $(CFLAGS) LDFLAGS ::= $(LDFLAGS) -LDLIBS ::= -lm -lGL -lGLEW -lglut -lassimp $(LDLIBS) +LDLIBS ::= -lm -lGL -lGLEW -lglfw -lassimp $(LDLIBS) # ###### # Paths diff --git a/src/engine/render.c b/src/engine/render.c index 089c475..d09a1a5 100644 --- a/src/engine/render.c +++ b/src/engine/render.c @@ -1,7 +1,6 @@ #include "render.h" #include -#include #include "geometry.h" #include "performance.h" @@ -42,7 +41,7 @@ void initRender() { //glShadeModel(GL_FLAT); } -void renderFrame() { +void renderFrame(GLFWwindow* window) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_NORMALIZE); @@ -55,9 +54,8 @@ void renderFrame() { renderScene(currentScene, identity()); glFlush(); - glutSwapBuffers(); + glfwSwapBuffers(window); frameRendered(); - glutPostRedisplay(); } static void renderScene(const Scene* scene, const Transform baseTransform) { diff --git a/src/engine/render.h b/src/engine/render.h index 25ea9be..4b61320 100644 --- a/src/engine/render.h +++ b/src/engine/render.h @@ -1,12 +1,14 @@ #ifndef RENDER_H_ #define RENDER_H_ +#include + #include "scene.h" extern float viewportAspectRatio; extern const Scene* cameraAnchor; void initRender(); -void renderFrame(); +void renderFrame(GLFWwindow* window); #endif // RENDER_H_ diff --git a/src/engine/ui.c b/src/engine/ui.c index 4b06f0c..7a8da1d 100644 --- a/src/engine/ui.c +++ b/src/engine/ui.c @@ -2,11 +2,11 @@ #include "render.h" -void resizeStage(GLsizei width, GLsizei height) { +void resizeStage(GLFWwindow* window, int width, int height) { if (height == 0) height = 1; - + glViewport(0, 0, width, height); - + viewportAspectRatio = (float) width / (float) height; } diff --git a/src/engine/ui.h b/src/engine/ui.h index 1e1cedf..7ab1784 100644 --- a/src/engine/ui.h +++ b/src/engine/ui.h @@ -3,6 +3,8 @@ #include -void resizeStage(GLsizei width, GLsizei height); +typedef struct GLFWwindow GLFWwindow; + +void resizeStage(GLFWwindow* window, int width, int height); #endif // UI_H_ diff --git a/src/game/input.c b/src/game/input.c index 447e92e..a91fabc 100644 --- a/src/game/input.c +++ b/src/game/input.c @@ -20,3 +20,24 @@ void onKeyPressed(unsigned char key, int x, int y) { break; } } + +void onKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods) { + if (action == GLFW_PRESS) { + switch (key) { + case GLFW_KEY_W: + playerMovementInput(0.0f, 1.0f); + break; + case GLFW_KEY_S: + playerMovementInput(0.0f, -1.0f); + break; + case GLFW_KEY_A: + playerMovementInput(-1.0f, 0.0f); + break; + case GLFW_KEY_D: + playerMovementInput(1.0f, 0.0f); + break; + default: + break; + } + } +} diff --git a/src/game/input.h b/src/game/input.h index 0ae694f..d8b7f9d 100644 --- a/src/game/input.h +++ b/src/game/input.h @@ -1,6 +1,9 @@ #ifndef INPUT_H_ #define INPUT_H_ +#include + void onKeyPressed(unsigned char key, int x, int y); +void onKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods); #endif // INPUT_H_ diff --git a/src/main.c b/src/main.c index abfd8e9..a4be117 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ #include -#include +#include #include "engine/logger.h" #include "engine/performance.h" @@ -10,26 +10,40 @@ #include "game/level.h" #include "game/player.h" -int main(int argc, char** argv) { - glutInit(&argc, argv); +void onGlfwError(int error, const char* description); + +int main(/*int argc, char** argv*/) { + glfwSetErrorCallback(onGlfwError); + + if (!glfwInit()) { + logError("GLFW init failed"); + return 1; + } // glutInitContextVersion(4,5); TODO establish correct context - - glutInitWindowSize(1280, 720); - - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); - glutCreateWindow("shadowclad"); - + // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5); + + GLFWwindow* window = glfwCreateWindow(1280, 720, "shadowclad", NULL, NULL); + if (!window) { + logError("Window or context creation failed"); + glfwTerminate(); + return 2; + } + + glfwMakeContextCurrent(window); + //glutInitDisplayMode(//glut_DOUBLE | //glut_RGBA | //glut_DEPTH); + logInfo("OpenGL %s", (const char*) glGetString(GL_VERSION)); logInfo("GLSL %s", (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION)); logInfo("%s", (const char*) glGetString(GL_RENDERER)); - + GLenum glewInitStatus = glewInit(); if (glewInitStatus != GLEW_OK) { logError("GLEW init failed: %s", (const char*) glewGetErrorString(glewInitStatus)); return 1; } logInfo("GLEW %s", (const char*) glewGetString(GLEW_VERSION)); - +/* if (GLXEW_EXT_swap_control) { Display* display = glXGetCurrentDisplay(); GLXDrawable drawable = glXGetCurrentDrawable(); @@ -48,19 +62,32 @@ int main(int argc, char** argv) { else { logWarning("Could not enable vsync (extensions not supported)"); } - - glutDisplayFunc(renderFrame); - glutReshapeFunc(resizeStage); - glutKeyboardFunc(onKeyPressed); - //glutMouseFunc(mouse_button_event); - //glutMotionFunc(mouse_motion_event); - +*/ + logInfo("Setting swap interval to 1"); + glfwSwapInterval(1); + + int width, height; + glfwGetFramebufferSize(window, &width, &height); + resizeStage(window, width, height); + + glfwSetFramebufferSizeCallback(window, resizeStage); + glfwSetKeyCallback(window, onKeyboardEvent); + initRender(); //initPerformanceMetering(); initLevel(); initPlayer(); startLevel(); - - glutMainLoop(); + + while (!glfwWindowShouldClose(window)) { + renderFrame(window); + glfwPollEvents(); + } + + glfwTerminate(); return 0; } + +void onGlfwError(int error, const char* description) { + logError("GLFW error: %s", description); +} -- 2.44.0