Migrate to GLFW 3

master
outfrost 2020-06-23 01:32:14 +02:00
parent edb5eba367
commit d82e6263e8
8 changed files with 83 additions and 30 deletions

View File

@ -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

View 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) {

View 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_

View 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;
}

View 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_

View 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;
}
}
}

View 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_

View 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"
@ -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);
}