From 20748a8e608315e4b1b58e1b736714b577e4d6e0 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 28 Nov 2023 01:56:10 -0500 Subject: [PATCH] window --- include/blt/gfx/window.h | 47 +++++++++++++++++++++++++ src/blt/gfx/window.cpp | 74 +++++++++++++++++++++++++++++++++++++++- src/main.cpp | 15 +++++++- 3 files changed, 134 insertions(+), 2 deletions(-) diff --git a/include/blt/gfx/window.h b/include/blt/gfx/window.h index 643f91b..9b2be84 100644 --- a/include/blt/gfx/window.h +++ b/include/blt/gfx/window.h @@ -10,6 +10,53 @@ #include #include +#include +#include +#include +#include +namespace blt::gfx +{ + struct window_context + { + std::int32_t GL_MAJOR = 4; + std::int32_t GL_MINOR = 6; + std::int32_t DOUBLE_BUFFER = GLFW_TRUE; + std::int32_t GL_PROFILE = GLFW_OPENGL_CORE_PROFILE; + }; + + struct window_data + { + std::string title; + std::int32_t width; + std::int32_t height; + std::function init; + std::function update; + + window_context context{}; + std::int32_t sync_interval = 0; + + window_data(std::string title, std::function init, std::function update, std::int32_t width = 640, + std::int32_t height = 480): + title(std::move(title)), width(width), height(height), init(std::move(init)), update(std::move(update)) + {} + + window_data& setContext(const window_context& ctx) + { + context = ctx; + return *this; + } + + window_data& setSyncInterval(std::int32_t sync) + { + sync_interval = sync; + return *this; + } + }; + + void init(const window_data& data); + + void cleanup(); +} #endif //BLT_WITH_GRAPHICS_TEMPLATE_WINDOW_H diff --git a/src/blt/gfx/window.cpp b/src/blt/gfx/window.cpp index 6e6cde9..f67ce87 100644 --- a/src/blt/gfx/window.cpp +++ b/src/blt/gfx/window.cpp @@ -3,4 +3,76 @@ * Licensed under GNU General Public License V3.0 * See LICENSE file for license detail */ -#include \ No newline at end of file +#include +#include +#include + +void error_callback(int error, const char* description) +{ + BLT_ERROR("GLFW Error (%d): %s", error, description); + std::abort(); +} + +namespace blt::gfx +{ + // because we aren't meant to have multiple GLFW windows (especially with GLAD) we will keep the window as global state.1 + struct + { + GLFWwindow* window; + std::int32_t width; + std::int32_t height; + } window_state; + + void create_callbacks() + { + + } + + void init(const window_data& data) + { + /* -- Setup Error Callback -- */ + glfwSetErrorCallback(error_callback); + BLT_ASSERT(glfwInit() && "Unable to init GLFW. Aborting."); + + /* -- Setup Window Context -- */ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, data.context.GL_MAJOR); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, data.context.GL_MINOR); + glfwWindowHint(GLFW_DOUBLEBUFFER, data.context.DOUBLE_BUFFER); + glfwWindowHint(GLFW_OPENGL_PROFILE, data.context.GL_PROFILE); + + /* -- Create the Window -- */ + window_state.window = glfwCreateWindow(data.width, data.height, data.title.c_str(), nullptr, nullptr); + BLT_ASSERT(window_state.window && "Unable to create GLFW window."); + + /* -- Set Window Specifics + OpenGL -- */ + glfwMakeContextCurrent(window_state.window); + glfwSwapInterval(data.sync_interval); + gladLoadGL(glfwGetProcAddress); + + create_callbacks(); + + /* -- Call User Provided post-window-init function -- */ + data.init(); + + /* -- General Loop -- */ + while (!glfwWindowShouldClose(window_state.window)) + { + /* -- Get the current framebuffer size, update the global width/height state, along with OpenGL viewport -- */ + glfwGetFramebufferSize(window_state.window, &window_state.width, &window_state.height); + glViewport(0, 0, window_state.width, window_state.height); + + /* -- Call user update function -- */ + data.update(window_state.width, window_state.height); + + /* -- Update GLFW state -- */ + glfwSwapBuffers(window_state.window); + glfwPollEvents(); + } + } + + void cleanup() + { + glfwDestroyWindow(window_state.window); + glfwTerminate(); + } +} diff --git a/src/main.cpp b/src/main.cpp index 4f9de56..bc7c634 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,20 @@ #include +#include + +void init() +{ + +} + +void update(std::int32_t width, std::int32_t height) +{ + +} + int main() { - std::cout << "Hello, World!" << std::endl; + blt::gfx::init(blt::gfx::window_data{"My Sexy Window", init, update}.setSyncInterval(1)); + blt::gfx::cleanup(); return 0; }