diff --git a/bakara/src/bakara/plaforms/window/glfw/win_glfw.cpp b/bakara/src/bakara/plaforms/window/glfw/win_glfw.cpp new file mode 100644 index 0000000..876dce3 --- /dev/null +++ b/bakara/src/bakara/plaforms/window/glfw/win_glfw.cpp @@ -0,0 +1,68 @@ +#include "win_glfw.h" +namespace Bk { + std::unique_ptr Window::create_window(const WindowPros& props) + { + return std::unique_ptr(new Plaform::WinGLFW(props)); + } + + namespace Plaform { + static uint p_glfw_initialized = 0; + + static void glfw_error_callback(int error, const char* description) + { + BK_CORE_CRITICAL("GLFW Error ({0}) {1}", error, description); + } + + WinGLFW::WinGLFW(const WindowPros& props) + { + p_data.title = props.title; + p_data.width = props.width; + p_data.height = props.height; + + BK_CORE_INFO("Creating window : {0} ({1}, {2})", props.title, props.width, props.height); + if (!p_glfw_initialized++) + { + int success = glfwInit(); + BK_MSG_ASSERT(success, "Couldn't initialize glfw!") + glfwSetErrorCallback(glfw_error_callback); + } + p_window = glfwCreateWindow((int)props.width, (int)props.height, props.title.c_str(), nullptr, nullptr); + glfwMakeContextCurrent(p_window); + glfwSetWindowUserPointer(p_window, &p_data); + set_vsync(true); + } + + WinGLFW::~WinGLFW() + { + shutdown(); + } + + void WinGLFW::on_update() + { + glfwPollEvents(); + glfwSwapBuffers(p_window); + } + + void WinGLFW::set_event_callback(const EventCallback callback) + { + p_data.callback = callback; + } + + void WinGLFW::set_vsync(bool enable) + { + if (enable) { glfwSwapInterval(1); } + else { glfwSwapInterval(0); } + p_data.vsync = enable; + } + + bool WinGLFW::is_vsync() const + { + return p_data.vsync; + } + + void WinGLFW::shutdown() + { + glfwDestroyWindow(p_window); + } + } +} diff --git a/bakara/src/bakara/plaforms/window/glfw/win_glfw.h b/bakara/src/bakara/plaforms/window/glfw/win_glfw.h new file mode 100644 index 0000000..4b74195 --- /dev/null +++ b/bakara/src/bakara/plaforms/window/glfw/win_glfw.h @@ -0,0 +1,37 @@ +#pragma once +#include +#include +#include + +namespace Bk::Plaform { + class WinGLFW : public Window + { + public: + WinGLFW(const WindowPros& props); + virtual ~WinGLFW(); + + inline uint get_width() const override { return p_data.width; } + inline uint get_height() const override { return p_data.height; } + + void on_update() override; + void set_event_callback(const EventCallback callback) override; + + void set_vsync(bool enable) override; + bool is_vsync() const override; + + private: + GLFWwindow* p_window; + + struct WindowData + { + std::string title; + uint width; + uint height; + bool vsync; + EventCallback callback; + }; + WindowData p_data; + + void shutdown(); + }; +} \ No newline at end of file