parent
0ef47947fb
commit
b17c794999
25 changed files with 303 additions and 86 deletions
@ -0,0 +1,29 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara.pch" |
||||
#include "event.h" |
||||
|
||||
namespace Bk { |
||||
class EventDispatcher |
||||
{ |
||||
public: |
||||
EventDispatcher(Event& event) |
||||
: p_event(event) {} |
||||
|
||||
// F will be deduced by the compiler
|
||||
template<typename T, typename F> |
||||
bool dispatch(const F& func) |
||||
{ |
||||
if (p_event.get_event_type() == T::get_static_type()) |
||||
{ |
||||
p_event.handled |= func(static_cast<T&>(p_event)); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
inline Event& get_event() { return p_event; } |
||||
private: |
||||
Event& p_event; |
||||
}; |
||||
} |
@ -1,20 +0,0 @@ |
||||
#pragma once |
||||
|
||||
#include "bakarapch.h" |
||||
#include "io_codes.h" |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class Input |
||||
{ |
||||
static bool key_down(KeyCode key); |
||||
static bool key_released(KeyCode key); |
||||
static bool key_pressed(KeyCode key); |
||||
|
||||
static bool mouse_button_down(MouseCode btn); |
||||
static bool mouse_button_released(MouseCode btn); |
||||
static bool mouse_button_pressed(MouseCode btn); |
||||
|
||||
static Vec2 mouse_position(); |
||||
}; |
||||
} |
@ -0,0 +1,101 @@ |
||||
#include "mouse.h" |
||||
namespace Bk |
||||
{ |
||||
double Mouse::x = 0; |
||||
double Mouse::y = 0; |
||||
|
||||
double Mouse::lastX = 0; |
||||
double Mouse::lastY = 0; |
||||
|
||||
double Mouse::dX = 0; |
||||
double Mouse::dY = 0; |
||||
|
||||
double Mouse::scrollDX = 0; |
||||
double Mouse::scrollDY = 0; |
||||
|
||||
bool Mouse::firstMouse; |
||||
|
||||
std::vector<bool> Mouse::buttons = { 0 }; |
||||
std::vector<bool> Mouse::buttonsChanged = { 0 }; |
||||
|
||||
bool Mouse::cursor_callback(MouseMoveEvent& e) { |
||||
x = e.get_x(); |
||||
y = e.get_y(); |
||||
|
||||
if(firstMouse) { |
||||
lastX = x; |
||||
lastY = y; |
||||
firstMouse = false; |
||||
} |
||||
dX = x - lastX; |
||||
dY = y -lastY; |
||||
lastX = x; |
||||
lastY = y; |
||||
return false; |
||||
} |
||||
|
||||
bool Mouse::button_callback(MouseButtonEvent& e) { |
||||
if (e.get_name() != "MouseButtonRelease") { |
||||
if(!buttons[e.get_btn()]) { |
||||
buttons[e.get_btn()] = true; |
||||
} |
||||
} else { |
||||
buttons[e.get_btn()] = false; |
||||
} |
||||
|
||||
buttonsChanged[e.get_btn()] = true; |
||||
return false; |
||||
} |
||||
|
||||
bool Mouse::wheel_callback(MouseScrollEvent& e) { |
||||
scrollDX = e.get_dx(); |
||||
scrollDY = e.get_dy(); |
||||
return false; |
||||
}
|
||||
|
||||
Vec2 Mouse::get_position() { |
||||
return Vec2(x, y); |
||||
} |
||||
|
||||
double Mouse::get_dx() { |
||||
double _dX = dX; |
||||
dX = 0; |
||||
return _dX; |
||||
} |
||||
|
||||
double Mouse::get_dy() { |
||||
double _dY = dY; |
||||
dY = 0; |
||||
return _dY; |
||||
} |
||||
|
||||
double Mouse::get_scroll_dx() { |
||||
double _scrollDX = scrollDX; |
||||
scrollDX = 0; |
||||
return _scrollDX; |
||||
} |
||||
|
||||
double Mouse::get_scroll_dy() { |
||||
double _scrollDY = scrollDY; |
||||
scrollDY = 0; |
||||
return _scrollDY; |
||||
}
|
||||
|
||||
bool Mouse::button(MouseCode button) { |
||||
return buttons[button]; |
||||
} |
||||
|
||||
bool Mouse::button_up(MouseCode button) { |
||||
return !buttons[button] && buttonChanged(button); |
||||
} |
||||
|
||||
bool Mouse::button_down(MouseCode button) { |
||||
return buttons[button] && buttonChanged(button); |
||||
} |
||||
|
||||
bool Mouse::buttonChanged(MouseCode button) { |
||||
bool ret = buttonsChanged[button]; |
||||
buttons[button] = false; |
||||
return ret; |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara.pch" |
||||
#include "mouse_codes.h" |
||||
#include "bakara/events/mouse_event.h" |
||||
namespace Bk
|
||||
{ |
||||
class Mouse { |
||||
public: |
||||
//GLFW callback function
|
||||
static bool cursor_callback(MouseMoveEvent& e); |
||||
static bool button_callback(MouseButtonEvent& e); |
||||
static bool wheel_callback(MouseScrollEvent& e); |
||||
|
||||
//Mouse position assesors
|
||||
static Vec2 get_position(); |
||||
|
||||
//Mouse scroll assesors
|
||||
static double get_dx(); |
||||
static double get_dy(); |
||||
static double get_scroll_dx(); |
||||
static double get_scroll_dy(); |
||||
|
||||
//Mouse buttons assesors
|
||||
static bool button(MouseCode button); |
||||
static bool button_up(MouseCode button); |
||||
static bool button_down(MouseCode button); |
||||
|
||||
private: |
||||
static double x; |
||||
static double y; |
||||
|
||||
static double lastX; |
||||
static double lastY; |
||||
|
||||
static double dX; |
||||
static double dY; |
||||
|
||||
static double scrollDX; |
||||
static double scrollDY; |
||||
|
||||
static bool firstMouse; |
||||
|
||||
static std::vector<bool> buttons; |
||||
static std::vector<bool> buttonsChanged; |
||||
|
||||
static bool buttonChanged(MouseCode button); |
||||
}; |
||||
} |
@ -0,0 +1,11 @@ |
||||
#pragma once |
||||
|
||||
namespace Bk
|
||||
{ |
||||
class GraphicsContext |
||||
{ |
||||
public: |
||||
virtual void init() = 0; |
||||
virtual void swap_buffers() = 0; |
||||
}; |
||||
} |
@ -1 +0,0 @@ |
||||
#include "bakarapch.h" |
@ -0,0 +1,27 @@ |
||||
#include "opengl_context.h" |
||||
#include <glad/glad.h> |
||||
#include <GLFW/glfw3.h> |
||||
|
||||
namespace Bk::Platform
|
||||
{ |
||||
OpenglContext::OpenglContext(GLFWwindow* window_handle) |
||||
: window_handle(window_handle) {} |
||||
|
||||
void OpenglContext::init()
|
||||
{ |
||||
glfwMakeContextCurrent(window_handle); |
||||
int success = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); |
||||
BK_CORE_MSG_ASSERT(success, "Couldn't load glad!") |
||||
|
||||
GLint majVers = 0, minVers = 0; |
||||
glGetIntegerv(GL_MAJOR_VERSION, &majVers); |
||||
glGetIntegerv(GL_MINOR_VERSION, &minVers); |
||||
BK_CORE_INFO("Opengl Version : {0}.{1}", majVers, minVers); |
||||
} |
||||
|
||||
void OpenglContext::swap_buffers()
|
||||
{ |
||||
glfwSwapBuffers(window_handle); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,19 @@ |
||||
#pragma once |
||||
|
||||
#include "bakara.pch" |
||||
#include "bakara/renderer/graphics_context.h" |
||||
|
||||
struct GLFWwindow; |
||||
|
||||
namespace Bk::Platform
|
||||
{ |
||||
class OpenglContext : public GraphicsContext |
||||
{ |
||||
public: |
||||
OpenglContext(GLFWwindow* window_handle); |
||||
void init() override; |
||||
void swap_buffers() override; |
||||
private: |
||||
GLFWwindow* window_handle; |
||||
}; |
||||
} |
Loading…
Reference in New Issue