1
0
Fork 0
Univerxel/src/render/window.cpp

92 lines
2.6 KiB
C++

#include <stdio.h>
#include <stdlib.h>
#include "window.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define MIN_WIDTH 854
#define MIN_HEIGHT 480
void framebuffer_size_callback(GLFWwindow *, int width, int height) {
glViewport(0, 0, width, height);
}
GLFWwindow* createWindow(int samples) {
GLFWwindow *window;
// Initialise GLFW
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
getchar();
return NULL;
}
glfwWindowHint(GLFW_SAMPLES, samples);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#if FIXED_WINDOW
glfwWindowHint(GLFW_RESIZABLE, false); //Note: Dev-only: Force floating on i3
#endif
window = glfwCreateWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, "Univerxel", NULL, NULL);
if (window == NULL) {
fprintf(stderr, "Failed to open GLFW window.\n");
getchar();
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return NULL;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Hide the mouse and enable unlimited mouvement
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set the mouse at the center of the screen
glfwPollEvents();
glfwSetCursorPos(window, DEFAULT_WIDTH / 2, DEFAULT_HEIGHT / 2);
// Force aspect ratio
glfwSetWindowSizeLimits(window, MIN_WIDTH, MIN_HEIGHT, GLFW_DONT_CARE, GLFW_DONT_CARE);
glfwSetWindowAspectRatio(window, RATIO_WIDTH, RATIO_HEIGHT);
// Handle resize
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetWindowTitle(window, "Univerxel");
glfwSetWindowAttrib(window, GLFW_RESIZABLE, true);
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LEQUAL);
// Cull triangles which normal is not towards the camera
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
GLint smp;
glGetIntegerv(GL_SAMPLES, &smp);
if(smp > 0) {
glEnable(GL_MULTISAMPLE);
}
return window;
}