Finished base of window system and layers

dev
anulax1225 ago%!(EXTRA string=1 year)
parent 5abba4f0b9
commit 809eccb231
  1. 20
      bakara/src/bakara/core/application.cpp
  2. 7
      bakara/src/bakara/core/application.h
  3. 2
      bakara/src/bakara/core/layer.cpp
  4. 0
      bakara/src/bakara/core/layer.h
  5. 0
      bakara/src/bakara/core/layer_stack.cpp
  6. 8
      bakara/src/bakara/core/layer_stack.h
  7. 4
      bakara/src/bakara/events/event.h
  8. 4
      bakara/src/bakara/io/window.h
  9. 55
      bakara/src/bakara/plaforms/window/glfw/win_glfw.cpp
  10. 11
      bakara/src/bakara/plaforms/window/glfw/win_glfw.h

@ -4,7 +4,7 @@ namespace Bk {
Application::Application()
{
p_window = Window::create_window();
p_window->set_event_callback(BK_BIND_EVENT_FN(Application::on_event));
p_window->set_event_callback(BK_BIND_EVENT_FN(on_event));
}
Application::~Application() { }
@ -12,19 +12,22 @@ namespace Bk {
void Application::on_event(Event& e)
{
EventDispatcher dispatcher(e);
dispatcher.dispatch<WindowCloseEvent>(BK_BIND_EVENT_FN(Application::on_window_close));
dispatcher.dispatch<WindowResizeEvent>(BK_BIND_EVENT_FN(Application::on_window_resize));
BK_CORE_INFO("Event : {0}", EVENT_STRING(e));
dispatcher.dispatch<WindowCloseEvent>(BK_BIND_DISPACHER_FN(WindowCloseEvent, on_window_close));
dispatcher.dispatch<WindowResizeEvent>(BK_BIND_DISPACHER_FN(WindowResizeEvent, on_window_resize));
BK_CORE_INFO("Event : {0}", GET_EVENT_STRING(e));
}
void Application::on_window_close(WindowCloseEvent& e)
bool Application::on_window_close(WindowCloseEvent& e)
{
p_window.close();
p_window->close();
return true;
}
void Application::on_window_resize(WindowResizeEvent& e)
bool Application::on_window_resize(WindowResizeEvent& e)
{
BK_CORE_INFO("Event : {0}", GET_EVENT_STRING(e));
return true;
}
void Application::run()
@ -32,6 +35,7 @@ namespace Bk {
while (p_running)
{
p_window->on_update();
if (!p_window->is_open()) p_window->open();
}
}
}

@ -6,7 +6,8 @@
namespace Bk {
#define BK_BIND_EVENT_FN(fn) std::bind(&fn, this, std::placeholders::_1)
#define BK_BIND_EVENT_FN(fn) [this](Event& e) { fn(e); }
#define BK_BIND_DISPACHER_FN(event, fn) [this](event& e) -> bool{ return fn(e); }
class Application
{
@ -14,8 +15,8 @@ namespace Bk {
Application();
virtual ~Application();
void on_event(Event& e);
void on_window_close(WindowCloseEvent& e);
void on_window_resize(WindowResizeEvent& e);
bool on_window_close(WindowCloseEvent& e);
bool on_window_resize(WindowResizeEvent& e);
void run();
private:
std::unique_ptr<Window> p_window;

@ -1,4 +1,4 @@
#include "Layer.h"
#include "layer.h"
namespace Bk {
Layer::Layer(const std::string& name)

@ -0,0 +1,8 @@
#pragma once
namespace BK {
class LayerStack
{
};
}

@ -44,11 +44,11 @@ namespace Bk {
#ifdef BK_DEBUG
#define EVENT_STRINGIFY(str, ...) std::string to_string() const override { return format(str, __VA_ARGS__); }
#define EVENT_STRING(event) event.to_string()
#define GET_EVENT_STRING(event) event.to_string()
#else
#define EVENT_STRINGIFY(str, ...)
#define EVENT_STRING(event)
#define GET_EVENT_STRING(event) ""
#endif
class Event

@ -30,7 +30,11 @@ namespace Bk {
virtual bool is_vsync() const = 0;
virtual void close() = 0;
virtual void open() = 0;
bool is_open() { return p_is_open; }
static std::unique_ptr<Window> create_window(const WindowPros& props = WindowPros());
protected:
bool p_is_open;
};
}

@ -16,7 +16,10 @@ namespace Bk {
WinGLFW::WinGLFW(const WindowPros& props)
{
init(props);
p_data.title = props.title;
p_data.width = props.width;
p_data.height = props.height;
init();
}
WinGLFW::~WinGLFW()
@ -24,20 +27,17 @@ namespace Bk {
close();
}
void WinGLFW::init(const WindowPros& props)
void WinGLFW::init()
{
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);
p_is_open = true;
BK_CORE_INFO("Creating window : {0} ({1}, {2})", p_data.title, p_data.width, p_data.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);
p_window = glfwCreateWindow((int)p_data.width, (int)p_data.height, p_data.title.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(p_window);
glfwSetWindowUserPointer(p_window, &p_data);
set_vsync(true);
@ -121,10 +121,14 @@ namespace Bk {
void WinGLFW::on_update()
{
glClearColor(1,0,0.5,1);
glClear(GL_COLOR_BUFFER_BIT);
glfwPollEvents();
glfwSwapBuffers(p_window);
if (p_is_open)
{
glClearColor(1,0,0.5,1);
glClear(GL_COLOR_BUFFER_BIT);
glfwPollEvents();
glfwSwapBuffers(p_window);
if (p_shutdown && p_is_open) { shutdown(); }
}
}
void WinGLFW::set_event_callback(const EventCallback callback)
@ -134,19 +138,38 @@ namespace Bk {
void WinGLFW::set_vsync(bool enable)
{
if (enable) { glfwSwapInterval(1); }
else { glfwSwapInterval(0); }
p_data.vsync = enable;
if (p_is_open)
{
if (enable) { glfwSwapInterval(1); }
else { glfwSwapInterval(0); }
p_data.vsync = enable;
}
}
bool WinGLFW::is_vsync() const
{
return p_data.vsync;
}
void WinGLFW::shutdown()
{
p_is_open = false;
p_shutdown = false;
glfwDestroyWindow(p_window);
}
void WinGLFW::close()
{
glfwDestroyWindow(p_window);
p_shutdown = true;
}
void WinGLFW::open()
{
if (!p_is_open)
{
init();
}
}
}
}

@ -1,10 +1,8 @@
#pragma once
#include <bkpch.h>
#include <bakara/io/window.h>
#include <bakara/events/mouse_event.h>
#include <bakara/events/key_event.h>
#include <bakara/events/window_event.h>
#include <bakara/events/event.h>
#include <bakara/events/events.h>
#include <mutex>
#include <GLFW/glfw3.h>
@ -25,9 +23,11 @@ namespace Bk::Plaform {
bool is_vsync() const override;
void close() override;
void open() override;
private:
GLFWwindow* p_window;
bool p_shutdown;
struct WindowData
{
@ -40,6 +40,7 @@ namespace Bk::Plaform {
WindowData p_data;
void init_event_callbacks();
void init(const WindowPros& props);
void init();
void shutdown();
};
}
Loading…
Cancel
Save