From ff576182c4da852bbdefa77c9a9a7ee7f5d23fcb Mon Sep 17 00:00:00 2001 From: Karl Date: Fri, 30 Apr 2021 22:40:42 +0200 Subject: [PATCH] Make Application create a GLFW Window --- cpp/Application.cpp | 13 +++++++--- cpp/RenderBackend.cpp | 44 ++++++++++++++++++++++++++++++++++ include/Gedeng/Input.h | 27 ++++++++++++++++++--- include/Gedeng/RenderBackend.h | 31 ++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 cpp/RenderBackend.cpp create mode 100644 include/Gedeng/RenderBackend.h diff --git a/cpp/Application.cpp b/cpp/Application.cpp index 2573ca7..723195b 100644 --- a/cpp/Application.cpp +++ b/cpp/Application.cpp @@ -1,21 +1,28 @@ #include "Gedeng/Application.h" -#include "Gedeng/Time.h" +#include "Gedeng/Input.h" #include "Gedeng/Logger.h" +#include "Gedeng/RenderBackend.h" +#include "Gedeng/Time.h" namespace Gedeng { void Application::run() { + // Setup Rendering + // FIXME: Make these parameters variable, maybe move this to a different function + RenderBackend::initialize_window(1920, 1080, String("Application")); + Input::initialize(RenderBackend::get_window()); + unsigned long previous_time_ms = Time::get_time_ms(); unsigned long time_until_fixed_update_ms = 0.0; - while (true) { + while (!RenderBackend::does_window_want_to_close()) { unsigned long current_time_ms = Time::get_time_ms(); unsigned long elapsed_time_ms = current_time_ms - previous_time_ms; previous_time_ms = current_time_ms; time_until_fixed_update_ms += elapsed_time_ms; - // Process Input + Input::poll_input(); // Update fixed time step while (time_until_fixed_update_ms >= MS_PER_UPDATE) { diff --git a/cpp/RenderBackend.cpp b/cpp/RenderBackend.cpp new file mode 100644 index 0000000..48bc929 --- /dev/null +++ b/cpp/RenderBackend.cpp @@ -0,0 +1,44 @@ +#include "Gedeng/RenderBackend.h" + +namespace Gedeng { + +void RenderBackend::initialize_window(unsigned int width, unsigned int height, String title) { + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + + window = glfwCreateWindow(width, height, title, NULL, NULL); + glfwMakeContextCurrent(window); + gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); +} + +bool RenderBackend::is_window_created() { + return window != nullptr; +} + +void RenderBackend::set_depth_test_enabled(bool enabled) { + if (enabled) { + glEnable(GL_DEPTH_TEST); + } else { + glDisable(GL_DEPTH_TEST); + } +} + +bool RenderBackend::does_window_want_to_close() { + return glfwWindowShouldClose(window); +} + +GLFWwindow *RenderBackend::get_window() { + return window; +} + +void RenderBackend::render() { + glfwSwapBuffers(window); +} + +} // namespace Gedeng \ No newline at end of file diff --git a/include/Gedeng/Input.h b/include/Gedeng/Input.h index 374ab37..b304615 100644 --- a/include/Gedeng/Input.h +++ b/include/Gedeng/Input.h @@ -1,13 +1,32 @@ #pragma once +// Must be the first include +#include + +// Other includes +#include + +#include "RenderBackend.h" #include namespace Gedeng { class Input { - public: - inline static std::map keys_down; + static void initialize(GLFWwindow *window) { + glfwSetKeyCallback(window, key_callback); + } + + // FIXME: Ignore warnings produced by these unused variables -- they're required for the callback to work + static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE); + + Input::set_key_down(key, action == GLFW_RELEASE ? false : true); + } + + static void poll_input() { + glfwPollEvents(); + } static void set_key_down(int key, bool down) { keys_down[key] = down; @@ -16,6 +35,8 @@ class Input { static bool is_key_down(int key) { return keys_down[key]; } + + inline static std::map keys_down; }; -} \ No newline at end of file +} // namespace Gedeng \ No newline at end of file diff --git a/include/Gedeng/RenderBackend.h b/include/Gedeng/RenderBackend.h new file mode 100644 index 0000000..ab05cd1 --- /dev/null +++ b/include/Gedeng/RenderBackend.h @@ -0,0 +1,31 @@ +#pragma once + +// Must be the first include +#include + +// Other includes +#include + +#include "Gedeng/String.h" + +namespace Gedeng { + +class RenderBackend { + private: + static inline GLFWwindow *window; + + public: + static void initialize_window(unsigned int width, unsigned int height, String title); + + static bool is_window_created(); + + static void set_depth_test_enabled(bool enabled); + + static bool does_window_want_to_close(); + + static GLFWwindow *get_window(); + + static void render(); +}; + +} // namespace Gedeng \ No newline at end of file