main
Brett 2023-11-28 01:56:10 -05:00
parent dfdfd08cd8
commit 20748a8e60
3 changed files with 134 additions and 2 deletions

View File

@ -10,6 +10,53 @@
#include <glad/gl.h> #include <glad/gl.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <functional>
#include <cstdint>
#include <string>
#include <utility>
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<void()> init;
std::function<void(std::int32_t, std::int32_t)> update;
window_context context{};
std::int32_t sync_interval = 0;
window_data(std::string title, std::function<void()> init, std::function<void(std::int32_t, std::int32_t)> 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 #endif //BLT_WITH_GRAPHICS_TEMPLATE_WINDOW_H

View File

@ -4,3 +4,75 @@
* See LICENSE file for license detail * See LICENSE file for license detail
*/ */
#include <blt/gfx/window.h> #include <blt/gfx/window.h>
#include <blt/std/assert.h>
#include <blt/std/logging.h>
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();
}
}

View File

@ -1,7 +1,20 @@
#include <iostream> #include <iostream>
#include <blt/gfx/window.h>
void init()
{
}
void update(std::int32_t width, std::int32_t height)
{
}
int main() 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; return 0;
} }