From 22eca24033fe15077dcfef769331e3060b542d1d Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Mon, 31 Aug 2020 00:56:04 +0200 Subject: [PATCH] Create OpenGL context --- .gitignore | 1 - .vscode/launch.json | 29 ++++++++++++++++ .vscode/settings.json | 6 ++++ .vscode/tasks.json | 20 +++++++++++ CMakeLists.txt | 7 +++- Controller.cpp | 75 +++++++++++++++++++++++++++++++++++++++++ Controller.h | 29 ++++++++++++++++ EventHandler.cpp | 10 ++++++ EventHandler.h | 15 +++++++++ Window.cpp | 35 +++++++++++++++++++ Window.h | 22 ++++++++++++ lib/glad/CMakeLists.txt | 2 ++ main.cpp | 21 +++++++----- 13 files changed, 261 insertions(+), 11 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 Controller.cpp create mode 100644 Controller.h create mode 100644 EventHandler.cpp create mode 100644 EventHandler.h create mode 100644 Window.cpp create mode 100644 Window.h diff --git a/.gitignore b/.gitignore index 661c60c..f1e6669 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ build .directory -.vscode diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..335fb70 --- /dev/null +++ b/.vscode/launch.json @@ -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 + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..29fe99b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", + "files.associations": { + "ostream": "cpp" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..c1f3458 --- /dev/null +++ b/.vscode/tasks.json @@ -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 + } + } + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index d3ff029..5a29593 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/Controller.cpp b/Controller.cpp new file mode 100644 index 0000000..d95b431 --- /dev/null +++ b/Controller.cpp @@ -0,0 +1,75 @@ +#include +#include + +#ifdef __linux__ +#include +#endif +#ifdef _WIN32 +#include +#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(); +} diff --git a/Controller.h b/Controller.h new file mode 100644 index 0000000..7b4f4f8 --- /dev/null +++ b/Controller.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#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; + +}; diff --git a/EventHandler.cpp b/EventHandler.cpp new file mode 100644 index 0000000..5bd1df3 --- /dev/null +++ b/EventHandler.cpp @@ -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); +} + diff --git a/EventHandler.h b/EventHandler.h new file mode 100644 index 0000000..268be65 --- /dev/null +++ b/EventHandler.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +class EventHandler { + +public: + + void handleEvents(GLFWwindow *window); + bool gameShouldTerminate; + +private: + + +}; \ No newline at end of file diff --git a/Window.cpp b/Window.cpp new file mode 100644 index 0000000..879cf58 --- /dev/null +++ b/Window.cpp @@ -0,0 +1,35 @@ +#include +#include + +#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); +} diff --git a/Window.h b/Window.h new file mode 100644 index 0000000..d7dda19 --- /dev/null +++ b/Window.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +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; + +}; \ No newline at end of file diff --git a/lib/glad/CMakeLists.txt b/lib/glad/CMakeLists.txt index d33ce93..799adf0 100644 --- a/lib/glad/CMakeLists.txt +++ b/lib/glad/CMakeLists.txt @@ -4,3 +4,5 @@ add_library( ) target_include_directories(glad PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +target_link_libraries(glad dl) diff --git a/main.cpp b/main.cpp index 2ca7b3f..30fb5a7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,18 @@ -#include +//#include #include #include +#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; }