Create OpenGL context

This commit is contained in:
Derek Christ
2020-08-31 00:56:04 +02:00
parent cab3b9272d
commit 22eca24033
13 changed files with 261 additions and 11 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,2 @@
build
.directory
.vscode

29
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Starten",
"type": "cppdbg",
"request": "launch",
"program": "build/Fall-Fever",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "Build",
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

6
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,6 @@
{
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"files.associations": {
"ostream": "cpp"
}
}

20
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,20 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "cmake --build . -j4",
"options": {
"cwd": "build"
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -10,7 +10,12 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib/glad)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(Fall-Fever main.cpp)
add_executable(Fall-Fever
main.cpp
Controller.cpp
Window.cpp
EventHandler.cpp
)
target_link_libraries(
Fall-Fever PRIVATE

75
Controller.cpp Normal file
View File

@@ -0,0 +1,75 @@
#include <stdlib.h>
#include <iostream>
#ifdef __linux__
#include <unistd.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
#include "Controller.h"
Controller::Controller() {
if(!glfwInit()) exit(-1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwSetErrorCallback(error_callback);
gameWindow = new Window();
gameEventHandler = new EventHandler();
}
Controller::~Controller() {
delete gameWindow;
delete gameEventHandler;
glfwTerminate();
}
void Controller::run() {
bool running = 1;
while(running) {
// Timing
limit_framerate();
std::cout << 1/deltaTime << std::endl;
// Update game
// Render and buffer swap
glfwSwapBuffers(gameWindow->getGLFWwindow());
// Check events, handle input
gameEventHandler->handleEvents(gameWindow->getGLFWwindow());
if(gameEventHandler->gameShouldTerminate) running = 0;
}
}
void Controller::error_callback(int error, const char* description) {
(void)error;
fprintf(stderr, "Error: %s\n", description);
}
void Controller::limit_framerate() {
static double startingTime = 0.0;
static double lastTime = 0.0;
lastTime = glfwGetTime() - startingTime;
double frameTime = 1/(double)MAX_FPS;
if(frameTime > lastTime) {
#ifdef __linux__
usleep((frameTime - lastTime) * 1000000);
#endif
#ifdef _WIN32
Sleep((frameTime - lastTime) * 1000);
#endif
}
deltaTime = glfwGetTime() - startingTime;
startingTime = glfwGetTime();
}

29
Controller.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <GLFW/glfw3.h>
#include "Window.h"
#include "EventHandler.h"
class Controller {
public:
Controller();
~Controller();
void run();
static void error_callback(int error, const char* description);
private:
void limit_framerate();
Window *gameWindow;
EventHandler *gameEventHandler;
const uint16_t MAX_FPS = 60;
double deltaTime;
};

10
EventHandler.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "EventHandler.h"
void EventHandler::handleEvents(GLFWwindow *window) {
glfwPollEvents();
gameShouldTerminate = glfwWindowShouldClose(window);
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}

15
EventHandler.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include <GLFW/glfw3.h>
class EventHandler {
public:
void handleEvents(GLFWwindow *window);
bool gameShouldTerminate;
private:
};

35
Window.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <glad/glad.h>
#include "Window.h"
Window::Window() {
width = 800; height = 600;
window = glfwCreateWindow(width, height, "Fall-Fever", NULL, NULL);
if(!window) {
std::cout << "Failed to create window" << std::endl;
}
// Create OpenGL context
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
exit(-1);
}
glViewport(0, 0, width, height);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
}
Window::~Window() {
glfwDestroyWindow(window);
}
void Window::framebuffer_size_callback(GLFWwindow* window, int width, int height) {
(void)window;
glViewport(0, 0, width, height);
}

22
Window.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include <GLFW/glfw3.h>
class Window {
public:
Window();
~Window();
GLFWwindow * getGLFWwindow() { return window; }
static void framebuffer_size_callback(GLFWwindow* window, int width, int height);
private:
GLFWwindow *window;
int width, height;
};

View File

@@ -4,3 +4,5 @@ add_library(
)
target_include_directories(glad PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(glad dl)

View File

@@ -1,15 +1,18 @@
#include <iostream>
//#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "Controller.h"
int main(int argc, char** argv) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "GLFW-Window", NULL, NULL);
while(1){}
// Suppress warning about unused variable
(void)argc; (void)argv;
// Create window
Controller *mainController = new Controller();
mainController->run();
delete mainController;
return 0;
}