]> git.lizzy.rs Git - shadowclad.git/commitdiff
Migrate to GLFW 3
authoroutfrost <kotlet.bahn@gmail.com>
Mon, 22 Jun 2020 23:32:14 +0000 (01:32 +0200)
committeroutfrost <kotlet.bahn@gmail.com>
Mon, 22 Jun 2020 23:32:14 +0000 (01:32 +0200)
Makefile
src/engine/render.c
src/engine/render.h
src/engine/ui.c
src/engine/ui.h
src/game/input.c
src/game/input.h
src/main.c

index 42225a5a91e9b15e93d2ede59c6f2cf1763f9793..f2da15281288788738d2ab8e702c30e93fe87ff2 100644 (file)
--- 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
index 089c475809a974981eb805414df5f7f6509b3da4..d09a1a53d2ce7dc6e2115b787eaaa716fc6e9fb0 100644 (file)
@@ -1,7 +1,6 @@
 #include "render.h"
 
 #include <stdbool.h>
-#include <GL/glut.h>
 
 #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) {
index 25ea9beccbf548ef21a2a799e5221011d7f58f02..4b6132017fa6f60baef28c9fd57f13537cd928b4 100644 (file)
@@ -1,12 +1,14 @@
 #ifndef RENDER_H_
 #define RENDER_H_
 
+#include <GLFW/glfw3.h>
+
 #include "scene.h"
 
 extern float viewportAspectRatio;
 extern const Scene* cameraAnchor;
 
 void initRender();
-void renderFrame();
+void renderFrame(GLFWwindow* window);
 
 #endif // RENDER_H_
index 4b06f0ce62f0fe8b1ea90a0a928a1dde262c6591..7a8da1db5526a99527a69e13592825fccce90f3f 100644 (file)
@@ -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;
 }
index 1e1cedfa6a3ac6e5b10c083dee5b227f78ac8308..7ab1784102c6a5f7f9bc1f611175a13169725304 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <GL/gl.h>
 
-void resizeStage(GLsizei width, GLsizei height);
+typedef struct GLFWwindow GLFWwindow;
+
+void resizeStage(GLFWwindow* window, int width, int height);
 
 #endif // UI_H_
index 447e92e8d20f03cd761a9606f845026365fde75f..a91fabc25eb214971ea5b1eca5ca6251b75bf19c 100644 (file)
@@ -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;
+               }
+       }
+}
index 0ae694f6f8154ca313a42f301935a14a197eaf1c..d8b7f9d900d17b873b2c3ba363b0f915a9fc6aff 100644 (file)
@@ -1,6 +1,9 @@
 #ifndef INPUT_H_
 #define INPUT_H_
 
+#include <GLFW/glfw3.h>
+
 void onKeyPressed(unsigned char key, int x, int y);
+void onKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods);
 
 #endif // INPUT_H_
index abfd8e9fe214ebf156f71d6e4577c5f16af96e0d..a4be117dc79917e7d8533bda2f5483f6ec3a9c3f 100644 (file)
@@ -1,5 +1,5 @@
 #include <GL/glxew.h>
-#include <GL/glut.h>
+#include <GLFW/glfw3.h>
 
 #include "engine/logger.h"
 #include "engine/performance.h"
 #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);
+}