diff --git a/premake5.lua b/premake5.lua index d4b7d58..094e5d4 100644 --- a/premake5.lua +++ b/premake5.lua @@ -14,14 +14,12 @@ project "bakara" "src/bakara/**.cpp", "src/platforms/**.h", "src/platforms/**.cpp", - "src/*pch.*", } defines { "_CRT_SECURE_NO_WARNINGS", "GLFW_INCLUDE_NONE", - "BKMOD_ALL" } includedirs @@ -40,7 +38,7 @@ project "bakara" "bakatools", "GLFW", "GLAD", - "ImGuiDocking" + "ImGui" } filter "configurations:Debug" diff --git a/src/bakarapch.h b/src/bakara.pch similarity index 100% rename from src/bakarapch.h rename to src/bakara.pch diff --git a/src/bakara/core/application.cpp b/src/bakara/core/application.cpp index a19278a..420704d 100644 --- a/src/bakara/core/application.cpp +++ b/src/bakara/core/application.cpp @@ -19,8 +19,12 @@ namespace Bk { void Application::on_event(Event& e) { EventDispatcher dispatcher(e); - if (!(dispatcher.dispatch(BK_BIND_DISPACHER_FN(WindowCloseEvent, on_window_close)) || - dispatcher.dispatch(BK_BIND_DISPACHER_FN(WindowResizeEvent, on_window_resize)))) + dispatcher.dispatch(BK_BIND_DISPACHER_FN(MouseButtonPressEvent, Mouse::button_callback)); + dispatcher.dispatch(BK_BIND_DISPACHER_FN(MouseButtonReleaseEvent, Mouse::button_callback)); + dispatcher.dispatch(BK_BIND_DISPACHER_FN(MouseScrollEvent, Mouse::wheel_callback)); + dispatcher.dispatch(BK_BIND_DISPACHER_FN(MouseMoveEvent, Mouse::cursor_callback)); + if (!(dispatcher.dispatch(BK_BIND_DISPACHER_FN(WindowCloseEvent, on_window_close)) + || dispatcher.dispatch(BK_BIND_DISPACHER_FN(WindowResizeEvent, on_window_resize)))) { for(auto it = p_layer_stack.rbegin(); it != p_layer_stack.rend(); it++) { @@ -52,12 +56,14 @@ namespace Bk { { for (Layer* layer : p_layer_stack) layer->on_update(); - + imgui_layer->begin(); for (Layer* layer : p_layer_stack) layer->imgui_render(); imgui_layer->end(); + + h_window->on_update(); } } diff --git a/src/bakara/core/application.h b/src/bakara/core/application.h index 33c8f91..fa3186f 100644 --- a/src/bakara/core/application.h +++ b/src/bakara/core/application.h @@ -4,11 +4,12 @@ This file contains the main app abstraction. */ -#include -#include -#include -#include -#include +#include "bakara.pch" +#include "bakara/core/window.h" +#include "bakara/events/events.h" +#include "bakara/io/mouse.h" +#include "bakara/core/layer_stack.h" +#include "bakara/imgui/imgui_layer.h" namespace Bk { /*! \class Bk::Application diff --git a/src/bakara/core/entry.cpp b/src/bakara/core/entry.cpp index 0957b69..f3b7410 100644 --- a/src/bakara/core/entry.cpp +++ b/src/bakara/core/entry.cpp @@ -1,4 +1,4 @@ -#include +#include "bakara.pch" #include "application.h" /*! \file entry.cpp diff --git a/src/bakara/core/layer.h b/src/bakara/core/layer.h index 26841e4..4a55674 100644 --- a/src/bakara/core/layer.h +++ b/src/bakara/core/layer.h @@ -1,5 +1,5 @@ #pragma once -#include +#include "bakara.pch" #include namespace Bk { diff --git a/src/bakara/core/layer_stack.h b/src/bakara/core/layer_stack.h index e919de7..95f1434 100644 --- a/src/bakara/core/layer_stack.h +++ b/src/bakara/core/layer_stack.h @@ -1,6 +1,6 @@ #pragma once -#include +#include "bakara.pch" #include "layer.h" namespace Bk { diff --git a/src/bakara/core/window.h b/src/bakara/core/window.h index f29318f..29054d2 100644 --- a/src/bakara/core/window.h +++ b/src/bakara/core/window.h @@ -5,7 +5,7 @@ This file contiens all the interfaces to create a window. Implementation possible with GLFW, Win32, etc. */ -#include +#include "bakara.pch" #include namespace Bk { diff --git a/src/bakara/events/dispacher.h b/src/bakara/events/dispacher.h new file mode 100644 index 0000000..6a58dc4 --- /dev/null +++ b/src/bakara/events/dispacher.h @@ -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 + bool dispatch(const F& func) + { + if (p_event.get_event_type() == T::get_static_type()) + { + p_event.handled |= func(static_cast(p_event)); + return true; + } + return false; + } + + inline Event& get_event() { return p_event; } + private: + Event& p_event; + }; +} \ No newline at end of file diff --git a/src/bakara/events/event.h b/src/bakara/events/event.h index 474b5bc..b57c972 100644 --- a/src/bakara/events/event.h +++ b/src/bakara/events/event.h @@ -1,6 +1,6 @@ #pragma once -#include +#include "bakara.pch" #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); } diff --git a/src/bakara/imgui/imgui_layer.cpp b/src/bakara/imgui/imgui_layer.cpp index 36b36f6..f5137ec 100644 --- a/src/bakara/imgui/imgui_layer.cpp +++ b/src/bakara/imgui/imgui_layer.cpp @@ -1,6 +1,6 @@ #include "imgui_layer.h" -#include "bakarapch.h" +#include "bakara.pch" #include "bakara/core/application.h" @@ -21,24 +21,25 @@ namespace Bk { ImGuiIO& io = ImGui::GetIO(); io.IniFilename = NULL; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - //io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking - //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows - //io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoTaskBarIcons; - io.ConfigViewportsNoAutoMerge = true; + #if defined(BK_IMGUI_DOCKING) + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows + io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoTaskBarIcons; + io.ConfigViewportsNoAutoMerge = true; + // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones. + ImGuiStyle& style = ImGui::GetStyle(); + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + style.WindowRounding = 0.0f; + style.Colors[ImGuiCol_WindowBg].w = 1.0f; + } + #endif // Setup Dear ImGui style ImGui::StyleColorsDark(); //ImGui::StyleColorsClassic(); - // When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones. - ImGuiStyle& style = ImGui::GetStyle(); - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - style.WindowRounding = 0.0f; - style.Colors[ImGuiCol_WindowBg].w = 1.0f; - } - Application& app = Application::get(); GLFWwindow* window = static_cast(app.get_window().get_native_window()); @@ -56,6 +57,9 @@ namespace Bk { void ImguiLayer::begin() { + ImGuiIO& io = ImGui::GetIO(); + Application& app = Application::get(); + io.DisplaySize = ImVec2((float)app.get_window().get_width(), (float)app.get_window().get_height()); ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); @@ -63,20 +67,17 @@ namespace Bk { void ImguiLayer::end() { - ImGuiIO& io = ImGui::GetIO(); - Application& app = Application::get(); - io.DisplaySize = ImVec2((float)app.get_window().get_width(), (float)app.get_window().get_height()); - // Rendering ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - GLFWwindow* backup_current_context = glfwGetCurrentContext(); - ImGui::UpdatePlatformWindows(); - ImGui::RenderPlatformWindowsDefault(); - glfwMakeContextCurrent(backup_current_context); - } + #if defined(BK_IMGUI_DOCKING) + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + GLFWwindow* backup_current_context = glfwGetCurrentContext(); + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + glfwMakeContextCurrent(backup_current_context); + } + #endif } } \ No newline at end of file diff --git a/src/bakara/io/input.h b/src/bakara/io/input.h deleted file mode 100644 index f2164aa..0000000 --- a/src/bakara/io/input.h +++ /dev/null @@ -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(); - }; -} \ No newline at end of file diff --git a/src/bakara/io/joystick.h b/src/bakara/io/joystick.h new file mode 100644 index 0000000..e69de29 diff --git a/src/bakara/io/keyboard.cpp b/src/bakara/io/keyboard.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/bakara/io/keyboard.h b/src/bakara/io/keyboard.h new file mode 100644 index 0000000..e69de29 diff --git a/src/bakara/io/mouse.cpp b/src/bakara/io/mouse.cpp new file mode 100644 index 0000000..2ec70ce --- /dev/null +++ b/src/bakara/io/mouse.cpp @@ -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 Mouse::buttons = { 0 }; + std::vector 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; + } +} \ No newline at end of file diff --git a/src/bakara/io/mouse.h b/src/bakara/io/mouse.h new file mode 100644 index 0000000..f8bb200 --- /dev/null +++ b/src/bakara/io/mouse.h @@ -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 buttons; + static std::vector buttonsChanged; + + static bool buttonChanged(MouseCode button); + }; +} \ No newline at end of file diff --git a/src/bakara/renderer/graphics_context.h b/src/bakara/renderer/graphics_context.h new file mode 100644 index 0000000..c003724 --- /dev/null +++ b/src/bakara/renderer/graphics_context.h @@ -0,0 +1,11 @@ +#pragma once + +namespace Bk +{ + class GraphicsContext + { + public: + virtual void init() = 0; + virtual void swap_buffers() = 0; + }; +} \ No newline at end of file diff --git a/src/bakarapch.cpp b/src/bakarapch.cpp deleted file mode 100644 index 6c81d8d..0000000 --- a/src/bakarapch.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "bakarapch.h" \ No newline at end of file diff --git a/src/platforms/glfw/glfw_joystick.h b/src/platforms/glfw/glfw_joystick.h new file mode 100644 index 0000000..e69de29 diff --git a/src/platforms/glfw/glfw_joystock.cpp b/src/platforms/glfw/glfw_joystock.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/platforms/glfw/glfw_window.cpp b/src/platforms/glfw/glfw_window.cpp index b2ab8be..b0e8bf3 100644 --- a/src/platforms/glfw/glfw_window.cpp +++ b/src/platforms/glfw/glfw_window.cpp @@ -1,14 +1,16 @@ +#include "glfw_window.h" + #include +#include #include -#include "glfw_window.h" namespace Bk { Window* Window::create_window(const WindowProps& props) { - return new Plaform::WinGLFW(props); + return new Platform::WinGLFW(props); } - namespace Plaform { + namespace Platform { static uint p_glfw_initialized = 0; static void glfw_error_callback(int error, const char* description) @@ -26,6 +28,7 @@ namespace Bk { WinGLFW::~WinGLFW() { + delete context; close(); } @@ -40,16 +43,8 @@ namespace Bk { glfwSetErrorCallback(glfw_error_callback); } p_window = glfwCreateWindow((int)p_data.width, (int)p_data.height, p_data.title.c_str(), nullptr, nullptr); - glfwMakeContextCurrent(p_window); - - 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); + context = new OpenglContext(p_window); + context->init(); glfwSetWindowUserPointer(p_window, &p_data); set_vsync(true); @@ -132,12 +127,10 @@ namespace Bk { void WinGLFW::on_update() { + glfwPollEvents(); + context->swap_buffers(); if (h_is_open) { - glClearColor(1,0,0.5,1); - glClear(GL_COLOR_BUFFER_BIT); - glfwPollEvents(); - glfwSwapBuffers(p_window); if (p_shutdown && h_is_open) { shutdown(); } } } diff --git a/src/platforms/glfw/glfw_window.h b/src/platforms/glfw/glfw_window.h index 4c72ea8..90bf705 100644 --- a/src/platforms/glfw/glfw_window.h +++ b/src/platforms/glfw/glfw_window.h @@ -1,10 +1,12 @@ #pragma once -#include -#include -#include -#include +#include "bakara.pch" +#include "bakara/core/window.h" +#include "bakara/events/events.h" +#include "platforms/opengl/opengl_context.h" -namespace Bk::Plaform { +struct GLFWwindow; + +namespace Bk::Platform { class WinGLFW : public Window { public: @@ -31,9 +33,10 @@ namespace Bk::Plaform { bool is_open() override { return h_is_open; } private: bool h_is_open; //!< indicaste if the window is opened or not - GLFWwindow* p_window; bool p_shutdown; + GLFWwindow* p_window; + OpenglContext* context; struct WindowData { std::string title; diff --git a/src/platforms/opengl/opengl_context.cpp b/src/platforms/opengl/opengl_context.cpp new file mode 100644 index 0000000..36170e3 --- /dev/null +++ b/src/platforms/opengl/opengl_context.cpp @@ -0,0 +1,27 @@ +#include "opengl_context.h" +#include +#include + +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); + } + +} \ No newline at end of file diff --git a/src/platforms/opengl/opengl_context.h b/src/platforms/opengl/opengl_context.h new file mode 100644 index 0000000..94714a3 --- /dev/null +++ b/src/platforms/opengl/opengl_context.h @@ -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; + }; +} \ No newline at end of file