From 554bbe6567a0351d959e9d9843e9aa8f2f0cda5c Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Wed, 16 Oct 2024 15:19:32 +0200 Subject: [PATCH] Commit before texture adding --- README.md | 2 +- src/bakara.h | 18 ++- src/bakara.pch | 20 ---- src/bakara/core/application.cpp | 19 +-- src/bakara/core/application.h | 10 +- src/bakara/core/deltatime.h | 17 +++ src/bakara/core/entry.cpp | 6 +- src/bakara/core/layer.h | 8 +- src/bakara/core/layer_stack.h | 2 +- src/bakara/core/window.h | 25 ++-- src/bakara/events/dispacher.h | 3 +- src/bakara/events/event.h | 27 +---- src/bakara/events/key_event.h | 19 ++- src/bakara/events/mouse_event.h | 14 +-- src/bakara/imgui/imgui_layer.cpp | 6 - src/bakara/io/controller.h | 18 +++ src/bakara/io/controller_codes.h | 16 ++- src/bakara/io/key_codes.h | 8 +- src/bakara/io/keyboard.cpp | 39 ------- src/bakara/io/keyboard.h | 21 +--- src/bakara/io/mouse.cpp | 101 ---------------- src/bakara/io/mouse.h | 44 +------ src/bakara/io/mouse_codes.h | 9 +- src/bakara/math/types.h | 6 + src/bakara/renderer/buffer.cpp | 25 ++++ src/bakara/renderer/buffer.h | 27 +++++ src/bakara/renderer/buffer_layout.cpp | 56 +++++++++ src/bakara/renderer/buffer_layout.h | 70 ++++++++++++ src/bakara/renderer/cameras/ortho_camera.cpp | 23 ++++ src/bakara/renderer/cameras/ortho_camera.h | 30 +++++ src/bakara/renderer/graphics_context.h | 2 + src/bakara/renderer/render_command.cpp | 8 ++ src/bakara/renderer/render_command.h | 18 +++ src/bakara/renderer/renderer.cpp | 27 +++++ src/bakara/renderer/renderer.h | 34 ++++++ src/bakara/renderer/renderer_api.h | 14 +++ src/bakara/renderer/shader.cpp | 16 +++ src/bakara/renderer/shader.h | 28 +++++ .../{io/joystick.h => renderer/texture.cpp} | 0 .../renderer/texture.h} | 0 src/bakara/renderer/vertex_array.cpp | 16 +++ src/bakara/renderer/vertex_array.h | 21 ++++ src/platforms/glfw/glfw_controller.cpp | 4 + src/platforms/glfw/glfw_controller.h | 35 ++++++ src/platforms/glfw/glfw_joystock.cpp | 0 src/platforms/glfw/glfw_keyboard.cpp | 27 +++++ src/platforms/glfw/glfw_mouse.cpp | 35 ++++++ src/platforms/glfw/glfw_window.cpp | 61 ++++++---- src/platforms/glfw/glfw_window.h | 37 +++--- src/platforms/opengl/opengl_buffer.cpp | 56 +++++++++ src/platforms/opengl/opengl_buffer.h | 35 ++++++ src/platforms/opengl/opengl_context.cpp | 13 ++- src/platforms/opengl/opengl_context.h | 4 +- src/platforms/opengl/opengl_renderer_api.cpp | 16 +++ src/platforms/opengl/opengl_renderer_api.h | 14 +++ src/platforms/opengl/opengl_shader.cpp | 108 ++++++++++++++++++ src/platforms/opengl/opengl_shader.h | 29 +++++ src/platforms/opengl/opengl_vertex_array.cpp | 76 ++++++++++++ src/platforms/opengl/opengl_vertex_array.h | 28 +++++ 59 files changed, 1091 insertions(+), 360 deletions(-) delete mode 100644 src/bakara.pch create mode 100644 src/bakara/core/deltatime.h create mode 100644 src/bakara/io/controller.h delete mode 100644 src/bakara/io/keyboard.cpp delete mode 100644 src/bakara/io/mouse.cpp create mode 100644 src/bakara/renderer/buffer.cpp create mode 100644 src/bakara/renderer/buffer.h create mode 100644 src/bakara/renderer/buffer_layout.cpp create mode 100644 src/bakara/renderer/buffer_layout.h create mode 100644 src/bakara/renderer/cameras/ortho_camera.cpp create mode 100644 src/bakara/renderer/cameras/ortho_camera.h create mode 100644 src/bakara/renderer/render_command.cpp create mode 100644 src/bakara/renderer/render_command.h create mode 100644 src/bakara/renderer/renderer.cpp create mode 100644 src/bakara/renderer/renderer.h create mode 100644 src/bakara/renderer/renderer_api.h create mode 100644 src/bakara/renderer/shader.cpp create mode 100644 src/bakara/renderer/shader.h rename src/bakara/{io/joystick.h => renderer/texture.cpp} (100%) rename src/{platforms/glfw/glfw_joystick.h => bakara/renderer/texture.h} (100%) create mode 100644 src/bakara/renderer/vertex_array.cpp create mode 100644 src/bakara/renderer/vertex_array.h create mode 100644 src/platforms/glfw/glfw_controller.cpp create mode 100644 src/platforms/glfw/glfw_controller.h delete mode 100644 src/platforms/glfw/glfw_joystock.cpp create mode 100644 src/platforms/glfw/glfw_keyboard.cpp create mode 100644 src/platforms/glfw/glfw_mouse.cpp create mode 100644 src/platforms/opengl/opengl_buffer.cpp create mode 100644 src/platforms/opengl/opengl_buffer.h create mode 100644 src/platforms/opengl/opengl_renderer_api.cpp create mode 100644 src/platforms/opengl/opengl_renderer_api.h create mode 100644 src/platforms/opengl/opengl_shader.cpp create mode 100644 src/platforms/opengl/opengl_shader.h create mode 100644 src/platforms/opengl/opengl_vertex_array.cpp create mode 100644 src/platforms/opengl/opengl_vertex_array.h diff --git a/README.md b/README.md index 9b5ac9d..ddca767 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Baka graphic module +# Baka Rasterazed Artwork ## Prerequiste * A video/graphics card that supports OpenGL. diff --git a/src/bakara.h b/src/bakara.h index 97064f0..0a85790 100644 --- a/src/bakara.h +++ b/src/bakara.h @@ -14,9 +14,23 @@ Plaform namespace doc */ namespace Bk::Plaform {} +namespace Baka = Bk; + #include -#include +#include +#include +#include #include #include -#include \ No newline at end of file +#include + +#include + +#include +#include + +#include +#include +#include +#include \ No newline at end of file diff --git a/src/bakara.pch b/src/bakara.pch deleted file mode 100644 index f941037..0000000 --- a/src/bakara.pch +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -/*! \file bkpch.h -Precompiled headers communly used in bakara. -*/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include \ No newline at end of file diff --git a/src/bakara/core/application.cpp b/src/bakara/core/application.cpp index 920f7b1..f8cb161 100644 --- a/src/bakara/core/application.cpp +++ b/src/bakara/core/application.cpp @@ -1,13 +1,16 @@ #include "application.h" +#include "bakara/core/window.h" +#include "bakatools/logging/assert.h" +#include "bakara/events/dispacher.h" namespace Bk { Application* Application::p_instance = nullptr; - Application::Application() + Application::Application(std::string title, u32 width, u32 height) { BK_CORE_MSG_ASSERT(p_instance == nullptr, "Application already exists, can not create two application.") Application::p_instance = this; - h_window = std::unique_ptr(Window::CreateWindow()); + h_window = std::unique_ptr(Window::CreateWindow(WindowProps(title, width, height))); imgui_layer = new ImguiLayer(); PushOverlay(imgui_layer); h_window->SetEventCallback(BK_BIND_EVENT_FN(OnEvent)); @@ -19,12 +22,6 @@ namespace Bk { void Application::OnEvent(Event& e) { EventDispatcher dispatcher(e); - dispatcher.dispatch(BK_BIND_DISPACHER_FN(MouseButtonPressEvent, Mouse::ButtonCallback)); - dispatcher.dispatch(BK_BIND_DISPACHER_FN(MouseButtonReleaseEvent, Mouse::ButtonCallback)); - dispatcher.dispatch(BK_BIND_DISPACHER_FN(MouseScrollEvent, Mouse::WheelCallback)); - dispatcher.dispatch(BK_BIND_DISPACHER_FN(MouseMoveEvent, Mouse::CursorCallback)); - dispatcher.dispatch(BK_BIND_DISPACHER_FN(KeyPressEvent, Keyboard::KeyCallback)); - dispatcher.dispatch(BK_BIND_DISPACHER_FN(KeyReleaseEvent, Keyboard::KeyCallback)); if (!(dispatcher.dispatch(BK_BIND_DISPACHER_FN(WindowCloseEvent, OnWindowClose)) || dispatcher.dispatch(BK_BIND_DISPACHER_FN(WindowResizeEvent, OnWindowResize)))) { @@ -57,15 +54,11 @@ namespace Bk { while (p_running) { for (Layer* layer : p_layer_stack) - layer->OnUpdate(); - + layer->OnUpdate(h_window->GetTime()); imgui_layer->Begin(); for (Layer* layer : p_layer_stack) layer->ImguiRender(); imgui_layer->End(); - - - h_window->OnUpdate(); } } diff --git a/src/bakara/core/application.h b/src/bakara/core/application.h index 6f16a50..1f08473 100644 --- a/src/bakara/core/application.h +++ b/src/bakara/core/application.h @@ -4,11 +4,9 @@ This file contains the main app abstraction. */ -#include "bakara.pch" +#include "bakara/events/window_event.h" +#include "bakatools/container/types.h" #include "bakara/core/window.h" -#include "bakara/events/events.h" -#include "bakara/io/mouse.h" -#include "bakara/io/keyboard.h" #include "bakara/core/layer_stack.h" #include "bakara/imgui/imgui_layer.h" @@ -23,7 +21,7 @@ namespace Bk { /*! \fn Bk::Application Initializes the main window and binds the event callback */ - Application(); + Application(std::string name = "BakaraEngine", u32 width = 1000, u32 height = 800); /*! \fn Bk::Application::~Application Virtual destructor enables subclasses to cleanup on termination */ @@ -93,5 +91,5 @@ namespace Bk { Must be defined in the user program @return User defined application */ - std::unique_ptr CreateApp(); + Application* CreateApp(int argc, char** argv); } \ No newline at end of file diff --git a/src/bakara/core/deltatime.h b/src/bakara/core/deltatime.h new file mode 100644 index 0000000..e2f9e97 --- /dev/null +++ b/src/bakara/core/deltatime.h @@ -0,0 +1,17 @@ +#pragma once + +namespace Bk +{ + class DeltaTime + { + public: + DeltaTime(float time = 0.0f) : time(time) + {} + operator float() { return time; } + float GetSeconds() { return time; } + float GetMilliseconds() { return time *1000.0f; } + + private: + float time; + }; +} \ No newline at end of file diff --git a/src/bakara/core/entry.cpp b/src/bakara/core/entry.cpp index a2230b9..b81cc2c 100644 --- a/src/bakara/core/entry.cpp +++ b/src/bakara/core/entry.cpp @@ -1,4 +1,3 @@ -#include "bakara.pch" #include "application.h" /*! \file entry.cpp @@ -8,14 +7,15 @@ /*! \fn std::unique_ptr Bk::CreateApp() External function implemented client side. */ -extern std::unique_ptr Bk::CreateApp(); +extern Bk::Application* Bk::CreateApp(int argc, char** argv); /*! \fn int main(int argc, char** argv) Entry of the program. */ int main(int argc, char** argv) { Bk::Log::Init("Bakara"); - std::unique_ptr app = Bk::CreateApp(); + Bk::Application* app = Bk::CreateApp(argc, argv); app->Run(); + delete app; return 0; } \ No newline at end of file diff --git a/src/bakara/core/layer.h b/src/bakara/core/layer.h index eea541f..6a8c653 100644 --- a/src/bakara/core/layer.h +++ b/src/bakara/core/layer.h @@ -1,6 +1,8 @@ #pragma once -#include "bakara.pch" -#include + +#include "bakara/core/deltatime.h" +#include "bakara/events/event.h" +#include namespace Bk { class Layer @@ -12,7 +14,7 @@ namespace Bk { virtual void OnAttach() {} virtual void OnDetach() {} virtual void OnEvent(Event& e) {} - virtual void OnUpdate() {} + virtual void OnUpdate(DeltaTime dt) {} virtual void ImguiRender() {} const std::string ToString() const { return name; } protected: diff --git a/src/bakara/core/layer_stack.h b/src/bakara/core/layer_stack.h index e819852..b200bcb 100644 --- a/src/bakara/core/layer_stack.h +++ b/src/bakara/core/layer_stack.h @@ -1,7 +1,7 @@ #pragma once -#include "bakara.pch" #include "layer.h" +#include namespace Bk { class LayerStack diff --git a/src/bakara/core/window.h b/src/bakara/core/window.h index 204c7e6..a3a6707 100644 --- a/src/bakara/core/window.h +++ b/src/bakara/core/window.h @@ -5,8 +5,11 @@ This file contiens all the interfaces to create a window. Implementation possible with GLFW, Win32, etc. */ -#include "bakara.pch" -#include +#include "bakara/core/deltatime.h" +#include "bakara/events/event.h" +#include "bakatools/container/types.h" +#include +#include namespace Bk { @@ -25,10 +28,16 @@ namespace Bk { @param width : Width of the window @param height : Height of the window */ - WindowProps(std::string title = "Bakara engine", uint width = 1000, uint height = 800) + WindowProps(std::string title, u32 width, u32 height) : title(title), width(width), height(height) {} }; + /*! \typedef Bk::Window::EventCallback + The EventCallback is a function pointer that takes a Event as parameter + and that reponses to them. + */ + using EventCallback = std::function; + /*! \class Bk::Window Window is an interface to abstract witch window api is used. This enable compilation with multiple api, so that it's possible to use the native api of the platform or to implement a graphics api's with more ease. @@ -36,12 +45,6 @@ namespace Bk { class Window { public: - /*! \typedef Bk::Window::EventCallback - The EventCallback is a function pointer that takes a Event as parameter - and that reponses to them. - */ - using EventCallback = std::function; - /*! \fn Bk::Window::~Window Virtual destructor enables subclasses to cleanup on termination. */ @@ -93,11 +96,13 @@ namespace Bk { virtual void* GetNativeWindow() = 0; + virtual DeltaTime GetTime() = 0; + /*! \fn Bk::Window::CreateWindow() Static function implemented in the api window class to get a window specifique api. If no parameter is specified, creates the window with the default settings. @param props : Window information */ - static Window* CreateWindow(const WindowProps& props = WindowProps()); + static Window* CreateWindow(const WindowProps& props); }; } \ No newline at end of file diff --git a/src/bakara/events/dispacher.h b/src/bakara/events/dispacher.h index 6a58dc4..7f90473 100644 --- a/src/bakara/events/dispacher.h +++ b/src/bakara/events/dispacher.h @@ -1,7 +1,6 @@ #pragma once -#include "bakara.pch" -#include "event.h" +#include "bakara/events/event.h" namespace Bk { class EventDispatcher diff --git a/src/bakara/events/event.h b/src/bakara/events/event.h index 6bb0fa5..8725db3 100644 --- a/src/bakara/events/event.h +++ b/src/bakara/events/event.h @@ -1,6 +1,8 @@ #pragma once -#include "bakara.pch" +#include "bakatools/base.h" +#include "bakatools/string/format.h" +#include #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); } @@ -67,27 +69,4 @@ namespace Bk { #endif bool is_in_category(EventCategory category) { return get_category_flags() & category; } }; - - 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/key_event.h b/src/bakara/events/key_event.h index 4ad8cc8..08e67ce 100644 --- a/src/bakara/events/key_event.h +++ b/src/bakara/events/key_event.h @@ -1,35 +1,34 @@ #pragma once - -#include +#include "bakara/io/key_codes.h" #include "event.h" namespace Bk { class KeyEvent : public Event { public: - KeyCode get_key() const { return m_key; } + Code get_key() const { return m_key; } EVENT_CLASS_CATEGORY(KeyboardCategory | InputCategory) protected: - KeyEvent(const KeyCode key) + KeyEvent(const Code key) : m_key(key) {} - KeyCode m_key; + Code m_key; }; class KeyPressEvent : public KeyEvent { public: - KeyPressEvent(KeyCode key, bool is_repeated = false) - : KeyEvent(key), p_is_repeated(is_repeated) {} + KeyPressEvent(Code key, bool IsRepeated = false) + : KeyEvent(key), p_is_repeated(IsRepeated) {} EVENT_CLASS_TYPE(KeyPress) EVENT_STRINGIFY("KeyPressEvent: %d repeat : %b", m_key, p_is_repeated) - bool is_repeated() { return p_is_repeated; } + bool IsRepeated() { return p_is_repeated; } private: bool p_is_repeated; @@ -38,7 +37,7 @@ namespace Bk { class KeyReleaseEvent : public KeyEvent { public: - KeyReleaseEvent(KeyCode key) + KeyReleaseEvent(Code key) : KeyEvent(key) {} EVENT_CLASS_TYPE(KeyRelease) @@ -49,7 +48,7 @@ namespace Bk { class KeyTypedEvent : public KeyEvent { public: - KeyTypedEvent(const KeyCode keycode) + KeyTypedEvent(const Code keycode) : KeyEvent(keycode) {} EVENT_CLASS_TYPE(KeyTyped) diff --git a/src/bakara/events/mouse_event.h b/src/bakara/events/mouse_event.h index 6733a28..5e4979f 100644 --- a/src/bakara/events/mouse_event.h +++ b/src/bakara/events/mouse_event.h @@ -1,8 +1,8 @@ #pragma once +#include "bakara/io/mouse_codes.h" #include "event.h" -#include -#include +#include "bakara/math/types.h" namespace Bk { @@ -48,19 +48,19 @@ namespace Bk class MouseButtonEvent : public Event { public: - MouseCode get_btn() { return m_btn; } + Code get_btn() { return m_btn; } EVENT_CLASS_CATEGORY(MouseCategory | InputCategory) protected: - MouseButtonEvent(MouseCode btn) + MouseButtonEvent(Code btn) : m_btn(btn) {} - MouseCode m_btn; + Code m_btn; }; class MouseButtonPressEvent : public MouseButtonEvent { public: - MouseButtonPressEvent(MouseCode btn) + MouseButtonPressEvent(Code btn) : MouseButtonEvent(btn) {} EVENT_STRINGIFY("MouseButtonPressEvent %d", m_btn) @@ -70,7 +70,7 @@ namespace Bk class MouseButtonReleaseEvent : public MouseButtonEvent { public: - MouseButtonReleaseEvent(MouseCode btn) + MouseButtonReleaseEvent(Code btn) : MouseButtonEvent(btn) {} EVENT_STRINGIFY("MouseButtonReleaseEvent %d", m_btn) diff --git a/src/bakara/imgui/imgui_layer.cpp b/src/bakara/imgui/imgui_layer.cpp index 6a5184b..0b8a19c 100644 --- a/src/bakara/imgui/imgui_layer.cpp +++ b/src/bakara/imgui/imgui_layer.cpp @@ -1,15 +1,9 @@ #include "imgui_layer.h" - -#include "bakara.pch" - #include "bakara/core/application.h" - #include #include - #include #include - #include #include diff --git a/src/bakara/io/controller.h b/src/bakara/io/controller.h new file mode 100644 index 0000000..f0b7a32 --- /dev/null +++ b/src/bakara/io/controller.h @@ -0,0 +1,18 @@ +#pragma once + +#include "controller_codes.h" + +namespace Bk +{ + class Controller { + public: + virtual void Update() = 0; + virtual float Axes(Code axis) = 0; + virtual unsigned char Button(Code button) = 0; + virtual int GetAxesCount() = 0; + virtual int GetButtonCount() = 0; + virtual bool IsPresent() = 0; + virtual const char* GetName() = 0; + }; + +} \ No newline at end of file diff --git a/src/bakara/io/controller_codes.h b/src/bakara/io/controller_codes.h index e62ebab..e2fb945 100644 --- a/src/bakara/io/controller_codes.h +++ b/src/bakara/io/controller_codes.h @@ -1,18 +1,16 @@ #pragma once + +#include + namespace Bk { /*! \typedef Bk::ControllerCode Unsigned integer representing a unique controller button */ - using ControllerCode = uint16_t; - /*! \typedef Bk::ControllerAxesCode - Unsigned integer representing a unique controller axe - */ - using ControllerAxes = uint16_t; - + using Code = uint16_t; /*! \enum Bk::ControllerButton Enumerator made to bind every controller buttons with a Bk::ControllerCode */ - typedef enum : ControllerCode + typedef enum : Code { BTN_DOWN = 0, //!< Cross, A BTN_LEFT = 1, //!< Square, X @@ -37,7 +35,7 @@ namespace Bk { /*! \enum Bk::ControllerJoystick Enum made to bind every controller axes with a Bk::ControllerAxesCode */ - typedef enum : ControllerAxes + typedef enum : Code { AXES_LEFT_STICK_X = 0, AXES_LEFT_STICK_Y = 1, @@ -45,5 +43,5 @@ namespace Bk { AXES_LEFT_TRIGGER = 3, AXES_RIGHT_TRIGGER = 4, AXES_RIGHT_STICK_Y = 5, - } ControllerJoystick; + } ControllerAxes; } \ No newline at end of file diff --git a/src/bakara/io/key_codes.h b/src/bakara/io/key_codes.h index 7c67dc6..e14075d 100644 --- a/src/bakara/io/key_codes.h +++ b/src/bakara/io/key_codes.h @@ -1,15 +1,17 @@ #pragma once +#include + namespace Bk { /*! \typedef Bk::KeyCode Unsigned integer representing a unique keyboard key */ - using KeyCode = uint16_t; + using Code = uint16_t; /*! \enum Bk::KeyCodes Enumerator made to bind every keyboard keys with a Bk::KeyCode */ - typedef enum : KeyCode + typedef enum : Code { Space = 32, /*!< _ */ Apostrophe = 39, /*!< ' */ @@ -139,5 +141,5 @@ namespace Bk { RightAlt = 346, RightSuper = 347, Menu = 348 - } KeyCodes; + } KeyCode; } \ No newline at end of file diff --git a/src/bakara/io/keyboard.cpp b/src/bakara/io/keyboard.cpp deleted file mode 100644 index 1a6cc6e..0000000 --- a/src/bakara/io/keyboard.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "keyboard.h" - -namespace Bk -{ - std::vector Keyboard::keys; - std::vector Keyboard::keysChanged; - - bool Keyboard::KeyCallback(KeyEvent& e) - { - //If the button is pressed or down then key is true - if (e.get_name() == "KeyPress" || e.get_name() == "KeyRelease") keys[e.get_key()] = !keys[e.get_key()]; - else keys[e.get_key()] = false; - //If the button is not down then keyChanged is true - keysChanged[e.get_key()] = e.get_name() == "KeyPress" || e.get_name() == "KeyRelease"; - return false; - } - - bool Keyboard::KeyDown(KeyCode key) - { - return keys[key]; - } - - bool Keyboard::KeyWentUp(KeyCode key) - { - return !keys[key] && keyChanged(key); - } - - bool Keyboard::KeyWentDown(KeyCode key) - { - return keys[key] && keyChanged(key); - } - - bool Keyboard::keyChanged(KeyCode key) - { - bool ret = keysChanged[key]; - keysChanged[key] = false; - return ret; - } -} \ No newline at end of file diff --git a/src/bakara/io/keyboard.h b/src/bakara/io/keyboard.h index 747e7b5..a9917fa 100644 --- a/src/bakara/io/keyboard.h +++ b/src/bakara/io/keyboard.h @@ -1,25 +1,14 @@ #pragma once -#include "bakara.pch" #include "key_codes.h" -#include "bakara/events/key_event.h" namespace Bk { - class Keyboard { + class Keyboard + { public: - - static bool KeyCallback(KeyEvent& e); - //Assesors - static bool KeyDown(KeyCode key); - static bool KeyWentUp(KeyCode key); - static bool KeyWentDown(KeyCode key); - - private: - //Keys state - static std::vector keys; - static std::vector keysChanged; - //Assesor - static bool keyChanged(KeyCode key); + static bool KeyRepeat(Code key); + static bool KeyUp(Code key); + static bool KeyDown(Code key); }; } \ No newline at end of file diff --git a/src/bakara/io/mouse.cpp b/src/bakara/io/mouse.cpp deleted file mode 100644 index e03c8bb..0000000 --- a/src/bakara/io/mouse.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#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::CursorCallback(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::ButtonCallback(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::WheelCallback(MouseScrollEvent& e) { - scrollDX = e.get_dx(); - scrollDY = e.get_dy(); - return false; - } - - Vec2 Mouse::GetPosition() { - return Vec2(x, y); - } - - double Mouse::GetDX() { - double _dX = dX; - dX = 0; - return _dX; - } - - double Mouse::GetDY() { - double _dY = dY; - dY = 0; - return _dY; - } - - double Mouse::GetScrollDX() { - double _scrollDX = scrollDX; - scrollDX = 0; - return _scrollDX; - } - - double Mouse::GetScrollDY() { - double _scrollDY = scrollDY; - scrollDY = 0; - return _scrollDY; - } - - bool Mouse::Button(MouseCode button) { - return buttons[button]; - } - - bool Mouse::ButtonUp(MouseCode button) { - return !buttons[button] && buttonChanged(button); - } - - bool Mouse::ButtonDown(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 index f86100e..c1fe8cc 100644 --- a/src/bakara/io/mouse.h +++ b/src/bakara/io/mouse.h @@ -1,49 +1,15 @@ #pragma once -#include "bakara.pch" +#include "bakara/math/types.h" #include "mouse_codes.h" -#include "bakara/events/mouse_event.h" + namespace Bk { class Mouse { public: - //GLFW callback function - static bool CursorCallback(MouseMoveEvent& e); - static bool ButtonCallback(MouseButtonEvent& e); - static bool WheelCallback(MouseScrollEvent& e); - - //Mouse position assesors static Vec2 GetPosition(); - - //Mouse scroll assesors - static double GetDX(); - static double GetDY(); - static double GetScrollDX(); - static double GetScrollDY(); - - //Mouse buttons assesors - static bool Button(MouseCode button); - static bool ButtonUp(MouseCode button); - static bool ButtonDown(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); + static bool ButtonUp(Code button); + static bool ButtonDown(Code button); + static bool ButtonRepeat(Code button); }; } \ No newline at end of file diff --git a/src/bakara/io/mouse_codes.h b/src/bakara/io/mouse_codes.h index f17a6d2..94b5a43 100644 --- a/src/bakara/io/mouse_codes.h +++ b/src/bakara/io/mouse_codes.h @@ -1,14 +1,17 @@ #pragma once + +#include + namespace Bk { /*! \typedef Bk::MouseCode Unsigned integer representing a unique mouse button */ - using MouseCode = uint16_t; + using Code = uint16_t; /*! \enum Bk::MouseCodes Enumerator made to bind every mouse buttons with a Bk::MouseCode */ - typedef enum : MouseCode + typedef enum : Code { Button0 = 0, /*!< Button left */ Button1 = 1, /*!< Button right */ @@ -23,5 +26,5 @@ namespace Bk { ButtonLeft = Button0, ButtonRight = Button1, ButtonMiddle = Button2 - } MouseCodes; + } MouseCode; } \ No newline at end of file diff --git a/src/bakara/math/types.h b/src/bakara/math/types.h index 1bc6c80..ff4f21c 100644 --- a/src/bakara/math/types.h +++ b/src/bakara/math/types.h @@ -5,11 +5,17 @@ Math types alias file used to abstract math types and not depend on any library. As long as it's implemented here. */ +#include "glm/fwd.hpp" #include #include +#include namespace Bk { + namespace Math = glm; + + using Quaterion = glm::quat; + /*! \typedef Bk::Vec2 Wrapper around glm::vec2 */ diff --git a/src/bakara/renderer/buffer.cpp b/src/bakara/renderer/buffer.cpp new file mode 100644 index 0000000..ea69da9 --- /dev/null +++ b/src/bakara/renderer/buffer.cpp @@ -0,0 +1,25 @@ +#include "buffer.h" +#include "bakara/renderer/renderer.h" +#include "bakatools/logging/assert.h" +#include "platforms/opengl/opengl_buffer.h" + +namespace Bk +{ + VertexBuffer* VertexBuffer::Create(float *vertices, u32 size) + { + switch (Renderer::GetAPI()) + { + case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; + case Renderer::API::Opengl: return new Platform::OpenglVertexBuffer(vertices, size); + } + } + + IndexBuffer* IndexBuffer::Create(u32* indices, u32 count) + { + switch (Renderer::GetAPI()) + { + case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; + case Renderer::API::Opengl: return new Platform::OpenglIndexBuffer(indices, count); + } + } +} \ No newline at end of file diff --git a/src/bakara/renderer/buffer.h b/src/bakara/renderer/buffer.h new file mode 100644 index 0000000..60b3822 --- /dev/null +++ b/src/bakara/renderer/buffer.h @@ -0,0 +1,27 @@ +#pragma once +#include "bakara/renderer/buffer_layout.h" +#include "bakatools/container/types.h" + +namespace Bk +{ + class VertexBuffer + { + public: + virtual ~VertexBuffer() {} + virtual void Bind() = 0; + virtual void Unbind() = 0; + virtual void SetLayout(BufferLayout layout) = 0; + virtual BufferLayout& GetLayout() = 0; + static VertexBuffer* Create(float* vertices, u32 size); + }; + + class IndexBuffer + { + public: + virtual ~IndexBuffer() {} + virtual void Bind() = 0; + virtual void Unbind() = 0; + virtual u32 GetCount() = 0; + static IndexBuffer* Create(u32* indices, u32 count); + }; +} \ No newline at end of file diff --git a/src/bakara/renderer/buffer_layout.cpp b/src/bakara/renderer/buffer_layout.cpp new file mode 100644 index 0000000..fd3d2f7 --- /dev/null +++ b/src/bakara/renderer/buffer_layout.cpp @@ -0,0 +1,56 @@ +#include "buffer_layout.h" + +namespace Bk +{ + u32 ShaderType::Size() + { + switch (type) + { + case None: return 0; + case Bool: return 1; + case Int: return 4; + case Int2: return 4 * 2; + case Int3: return 4 * 3; + case Int4: return 4 * 4; + case Float: return 4; + case Float2: return 4 * 2; + case Float3: return 4 * 3; + case Float4: return 4 * 4; + case Mat3: return 4 * 3 * 3; + case Mat4: return 4 * 4 * 4; + } + } + + u32 ShaderType::Count() + { + switch (type) + { + case None: return 0; + case Bool: return 1; + case Int: return 1; + case Int2: return 2; + case Int3: return 3; + case Int4: return 4; + case Float: return 1; + case Float2: return 2; + case Float3: return 3; + case Float4: return 4; + case Mat3: return 3 * 3; + case Mat4: return 4 * 4; + } + } + + void BufferLayout::CalculteOffsetAndStride() + { + u32 offset = 0; + stride = 0; + for(auto& element : elements) + { + element.offset = offset; + offset += element.type.Size(); + stride += element.type.Size(); + + } + + } +} \ No newline at end of file diff --git a/src/bakara/renderer/buffer_layout.h b/src/bakara/renderer/buffer_layout.h new file mode 100644 index 0000000..243c995 --- /dev/null +++ b/src/bakara/renderer/buffer_layout.h @@ -0,0 +1,70 @@ +#pragma once + +#include "bakatools/container/types.h" +#include +#include +#include + +namespace Bk +{ + class ShaderType + { + public: + enum Type : u8 + { + None = 0, + Bool, Int, Int2, Int3, Int4, + Float, Float2, Float3, Float4, + Mat3, Mat4 + }; + ShaderType() : type(None) {} + ShaderType(Type type) : type(type) {} + + ShaderType& operator=(Type value) { type = value; return *this; } + operator Type() const { return type; } + bool operator==(Type type) { return type == this->type; } + bool operator!=(Type type) { return type != this->type; } + bool operator==(ShaderType value) { return value.type == this->type; } + bool operator!=(ShaderType value) { return value.type != this->type; } + + u32 Size(); + u32 Count(); + private: + Type type; + }; + + struct BufferElement + { + std::string name; + u32 offset; + ShaderType type; + bool normelized; + + BufferElement() = default; + + BufferElement(std::string name, ShaderType type, bool normelized = false) + : name(name), type(type) {} + + BufferElement(ShaderType type, std::string name, bool normelized = false) + : name(name), type(type), normelized(normelized) {} + }; + + class BufferLayout + { + public: + BufferLayout() = default; + + BufferLayout(const std::initializer_list& elements) + : elements(elements) { CalculteOffsetAndStride(); } + + inline u32 GetStride() { return stride; } + inline std::vector& GetElements() { return elements; } + + std::vector::iterator begin() { return elements.begin(); } + std::vector::iterator end() { return elements.end(); } + private: + void CalculteOffsetAndStride(); + std::vector elements; + u32 stride; + }; +} \ No newline at end of file diff --git a/src/bakara/renderer/cameras/ortho_camera.cpp b/src/bakara/renderer/cameras/ortho_camera.cpp new file mode 100644 index 0000000..ac025e8 --- /dev/null +++ b/src/bakara/renderer/cameras/ortho_camera.cpp @@ -0,0 +1,23 @@ +#include "ortho_camera.h" +#include "bakara/math/types.h" +#include "glm/ext/matrix_clip_space.hpp" +#include "glm/ext/matrix_transform.hpp" +#include "glm/matrix.hpp" +#include "glm/trigonometric.hpp" + +namespace Bk +{ + OrthographicCamera::OrthographicCamera(float left, float right, float bottom, float top) + : projectionMatrix(Math::ortho(left, right, bottom, top, -1.0f, 1.0f)) + { + RecalculViewMatrix(); + } + + void OrthographicCamera::RecalculViewMatrix() + { + Mat4 transform = Math::translate(Mat4(1.0f), position); + transform = Math::rotate(transform, Math::radians(rotation) , Vec3(0.0f, 0.0f, 1.0f)); + viewMatrix = Math::inverse(transform); + viewProjectionMatrix = projectionMatrix * viewMatrix; + } +} \ No newline at end of file diff --git a/src/bakara/renderer/cameras/ortho_camera.h b/src/bakara/renderer/cameras/ortho_camera.h new file mode 100644 index 0000000..75e2b76 --- /dev/null +++ b/src/bakara/renderer/cameras/ortho_camera.h @@ -0,0 +1,30 @@ +#pragma once + +#include "bakara/math/types.h" + +namespace Bk +{ + class OrthographicCamera + { + public: + OrthographicCamera(float left, float right, float bottom, float top); + + void SetPosition(Vec3 position) { this->position = position; RecalculViewMatrix(); } + Vec3 GetPosition() { return position; } + void SetRotation(float rotation) { this->rotation = rotation; RecalculViewMatrix(); } + float GetRotaion() { return rotation; } + + Mat4 GetViewMatrix() { return viewMatrix; } + Mat4 GetProjectionMatrix() { return projectionMatrix; } + Mat4 GetViewProjectionMatrix() { return viewProjectionMatrix; } + + private: + void RecalculViewMatrix(); + Mat4 viewMatrix; + Mat4 projectionMatrix; + Mat4 viewProjectionMatrix; + + Vec3 position = { 0.0f, 0.0f, 0.0f }; + float rotation = 0.0f; + }; +} \ No newline at end of file diff --git a/src/bakara/renderer/graphics_context.h b/src/bakara/renderer/graphics_context.h index 5f9e538..76dfec1 100644 --- a/src/bakara/renderer/graphics_context.h +++ b/src/bakara/renderer/graphics_context.h @@ -5,7 +5,9 @@ namespace Bk class GraphicsContext { public: + virtual ~GraphicsContext() {} virtual void Init() = 0; virtual void SwapBuffers() = 0; + virtual void SetViewport(int width, int height) = 0; }; } \ No newline at end of file diff --git a/src/bakara/renderer/render_command.cpp b/src/bakara/renderer/render_command.cpp new file mode 100644 index 0000000..3fab55d --- /dev/null +++ b/src/bakara/renderer/render_command.cpp @@ -0,0 +1,8 @@ +#include "render_command.h" +#include "bakara/renderer/renderer_api.h" +#include "platforms/opengl/opengl_renderer_api.h" + +namespace Bk +{ + RendererAPI* RenderCommand::rendererAPI = new OpenglRendererAPI(); +} \ No newline at end of file diff --git a/src/bakara/renderer/render_command.h b/src/bakara/renderer/render_command.h new file mode 100644 index 0000000..6ae2f29 --- /dev/null +++ b/src/bakara/renderer/render_command.h @@ -0,0 +1,18 @@ +#pragma once + +#include "bakara/math/types.h" +#include "bakara/renderer/renderer_api.h" +#include "bakara/renderer/vertex_array.h" + +namespace Bk +{ + class RenderCommand + { + public: + static inline void Clear(Vec4 color) { rendererAPI->Clear(color.r, color.g, color.b, color.a); } + static inline void Clear(float red, float green, float blue, float alpha) { rendererAPI->Clear(red, green, blue, alpha); } + static inline void DrawIndexed(Ref va) { rendererAPI->DrawIndexed(va); } + private: + static RendererAPI* rendererAPI; + }; +} \ No newline at end of file diff --git a/src/bakara/renderer/renderer.cpp b/src/bakara/renderer/renderer.cpp new file mode 100644 index 0000000..3184153 --- /dev/null +++ b/src/bakara/renderer/renderer.cpp @@ -0,0 +1,27 @@ +#include "renderer.h" +#include "bakara/renderer/cameras/ortho_camera.h" +#include "bakara/renderer/render_command.h" +namespace Bk +{ + Renderer::API Renderer::s_RenderAPI = Renderer::API::Opengl; + Renderer::SceneData* Renderer::sceneData = new Renderer::SceneData(); + + void Renderer::BeginScene(OrthographicCamera camera) + { + sceneData->VPMatrix = camera.GetViewProjectionMatrix(); + } + + void Renderer::EndScene() + { + + } + + void Renderer::Submit(Ref va, Ref shader, Mat4 transform) + { + va->Bind(); + shader->Bind(); + shader->Set("viewProjection", sceneData->VPMatrix); + shader->Set("transform", transform); + RenderCommand::DrawIndexed(va); + } +} \ No newline at end of file diff --git a/src/bakara/renderer/renderer.h b/src/bakara/renderer/renderer.h new file mode 100644 index 0000000..70d7877 --- /dev/null +++ b/src/bakara/renderer/renderer.h @@ -0,0 +1,34 @@ +#pragma once + +#include "bakara/math/types.h" +#include "bakara/renderer/cameras/ortho_camera.h" +#include "bakara/renderer/shader.h" +#include "bakara/renderer/vertex_array.h" +#include "bakatools/container/types.h" + +namespace Bk +{ + class Renderer + { + public: + enum class API + { + None = 0, + Opengl = 1, + }; + + static API GetAPI() { return s_RenderAPI; } + + static void BeginScene(OrthographicCamera camera); + static void EndScene(); + + static void Submit(Ref va, Ref shader, Mat4 transform = Mat4(1.0f)); + private: + struct SceneData + { + Mat4 VPMatrix; + }; + static SceneData* sceneData; + static API s_RenderAPI; + }; +} \ No newline at end of file diff --git a/src/bakara/renderer/renderer_api.h b/src/bakara/renderer/renderer_api.h new file mode 100644 index 0000000..c91a4cb --- /dev/null +++ b/src/bakara/renderer/renderer_api.h @@ -0,0 +1,14 @@ +#pragma once + +#include "bakara/renderer/vertex_array.h" + +namespace Bk +{ + class RendererAPI + { + public: + virtual ~RendererAPI() {} + virtual void Clear(float red, float green, float blue, float alpha) = 0; + virtual void DrawIndexed(Ref va) = 0; + }; +} \ No newline at end of file diff --git a/src/bakara/renderer/shader.cpp b/src/bakara/renderer/shader.cpp new file mode 100644 index 0000000..ecbce97 --- /dev/null +++ b/src/bakara/renderer/shader.cpp @@ -0,0 +1,16 @@ +#include "shader.h" +#include "bakara/renderer/renderer.h" +#include "bakatools/logging/assert.h" +#include "platforms/opengl/opengl_shader.h" + +namespace Bk +{ + Shader* Shader::Create(std::string vertexSrc, std::string fragSrc) + { + switch(Renderer::GetAPI()) + { + case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; + case Renderer::API::Opengl: return new Platform::OpenglShader(vertexSrc, fragSrc); + } + } +} \ No newline at end of file diff --git a/src/bakara/renderer/shader.h b/src/bakara/renderer/shader.h new file mode 100644 index 0000000..884c6e5 --- /dev/null +++ b/src/bakara/renderer/shader.h @@ -0,0 +1,28 @@ +#pragma once + +#include "bakara/math/types.h" +#include + +namespace Bk +{ + class Shader + { + public: + virtual ~Shader() {} + + virtual void Bind() = 0; + virtual void Unbind() = 0; + + virtual void Set(const std::string name, int val) = 0; + virtual void Set(const std::string name, bool val) = 0; + virtual void Set(const std::string name, float val) = 0; + virtual void Set(const std::string name, float v1, float v2, float v3) = 0; + virtual void Set(const std::string name, Vec3 v) = 0; + virtual void Set(const std::string name, float v1, float v2, float v3, float v4) = 0; + virtual void Set(const std::string name, Vec4 v) = 0; + virtual void Set(const std::string name, Mat3 val) = 0; + virtual void Set(const std::string name, Mat4 val) = 0; + + static Shader* Create(std::string vertexSrc, std::string fragSrc); + }; +} \ No newline at end of file diff --git a/src/bakara/io/joystick.h b/src/bakara/renderer/texture.cpp similarity index 100% rename from src/bakara/io/joystick.h rename to src/bakara/renderer/texture.cpp diff --git a/src/platforms/glfw/glfw_joystick.h b/src/bakara/renderer/texture.h similarity index 100% rename from src/platforms/glfw/glfw_joystick.h rename to src/bakara/renderer/texture.h diff --git a/src/bakara/renderer/vertex_array.cpp b/src/bakara/renderer/vertex_array.cpp new file mode 100644 index 0000000..ddf4764 --- /dev/null +++ b/src/bakara/renderer/vertex_array.cpp @@ -0,0 +1,16 @@ +#include "bakara/renderer/renderer.h" +#include "bakatools/logging/assert.h" +#include "vertex_array.h" +#include "platforms/opengl/opengl_vertex_array.h" + +namespace Bk +{ + VertexArray* VertexArray::Create() + { + switch (Renderer::GetAPI()) + { + case Renderer::API::None: BK_MSG_ASSERT(false, "API not supported"); return nullptr; + case Renderer::API::Opengl: return new Platform::OpenglVertexArray(); + } + } +} \ No newline at end of file diff --git a/src/bakara/renderer/vertex_array.h b/src/bakara/renderer/vertex_array.h new file mode 100644 index 0000000..35aa4cd --- /dev/null +++ b/src/bakara/renderer/vertex_array.h @@ -0,0 +1,21 @@ +#pragma once + +#include "bakara/renderer/buffer.h" +#include "bakatools/container/types.h" + +namespace Bk +{ + class VertexArray + { + public: + virtual ~VertexArray() {} + virtual void Bind() = 0; + virtual void Unbind() = 0; + virtual void AddVertexBuffer(Ref buf) = 0; + virtual void SetIndexbuffer(Ref buf) = 0; + + virtual std::vector> GetVertexBuffers() = 0; + virtual Ref GetIndexbuffer() = 0; + static VertexArray* Create(); + }; +} \ No newline at end of file diff --git a/src/platforms/glfw/glfw_controller.cpp b/src/platforms/glfw/glfw_controller.cpp new file mode 100644 index 0000000..9b2879d --- /dev/null +++ b/src/platforms/glfw/glfw_controller.cpp @@ -0,0 +1,4 @@ +#include "glfw_controller.h" + +#include + diff --git a/src/platforms/glfw/glfw_controller.h b/src/platforms/glfw/glfw_controller.h new file mode 100644 index 0000000..a63a1ee --- /dev/null +++ b/src/platforms/glfw/glfw_controller.h @@ -0,0 +1,35 @@ +#pragma once + +#include "bakara/io/controller.h" + +namespace Bk +{ + namespace Platform + { + class ControllerGLFW : public Controller + { + public: + ControllerGLFW() = delete; + ControllerGLFW(int i); + + void Update() override; + float Axes(Code axis) override; + unsigned char Button(Code button) override; + int GetAxesCount() override; + int GetButtonCount() override; + bool IsPresent() override; + const char* GetName() override; + + private: + int present; + int id; + const char* name; + + int axesCount; + const float* axes; + + int buttonCount; + const unsigned char* buttons; + }; + } +} \ No newline at end of file diff --git a/src/platforms/glfw/glfw_joystock.cpp b/src/platforms/glfw/glfw_joystock.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/platforms/glfw/glfw_keyboard.cpp b/src/platforms/glfw/glfw_keyboard.cpp new file mode 100644 index 0000000..18d9c27 --- /dev/null +++ b/src/platforms/glfw/glfw_keyboard.cpp @@ -0,0 +1,27 @@ +#include "bakara/io/keyboard.h" +#include "bakara/core/application.h" +#include + +namespace Bk +{ + bool Keyboard::KeyDown(Code key) + { + auto* window = static_cast(Application::Get().GetWindow().GetNativeWindow()); + auto state = glfwGetKey(window, static_cast(key)); + return state == GLFW_PRESS; + } + + bool Keyboard::KeyUp(Code key) + { + auto* window = static_cast(Application::Get().GetWindow().GetNativeWindow()); + auto state = glfwGetKey(window, static_cast(key)); + return state == GLFW_RELEASE; + } + + bool Keyboard::KeyRepeat(Code key) + { + auto* window = static_cast(Application::Get().GetWindow().GetNativeWindow()); + auto state = glfwGetKey(window, static_cast(key)); + return state == GLFW_REPEAT; + } +} \ No newline at end of file diff --git a/src/platforms/glfw/glfw_mouse.cpp b/src/platforms/glfw/glfw_mouse.cpp new file mode 100644 index 0000000..5e40279 --- /dev/null +++ b/src/platforms/glfw/glfw_mouse.cpp @@ -0,0 +1,35 @@ +#include "bakara/io/mouse.h" +#include "bakara/core/application.h" +#include + +namespace Bk +{ + Vec2 Mouse::GetPosition() + { + auto* window = static_cast(Application::Get().GetWindow().GetNativeWindow()); + double xpos, ypos; + glfwGetCursorPos(window, &xpos, &ypos); + return { (float)xpos, (float)ypos }; + } + + bool Mouse::ButtonUp(Code button) + { + auto* window = static_cast(Application::Get().GetWindow().GetNativeWindow()); + auto state = glfwGetMouseButton(window, static_cast(button)); + return state == GLFW_RELEASE; + } + + bool Mouse::ButtonDown(Code button) + { + auto* window = static_cast(Application::Get().GetWindow().GetNativeWindow()); + auto state = glfwGetMouseButton(window, static_cast(button)); + return state == GLFW_PRESS; + } + + bool Mouse::ButtonRepeat(Code button) + { + auto* window = static_cast(Application::Get().GetWindow().GetNativeWindow()); + auto state = glfwGetMouseButton(window, static_cast(button)); + return state == GLFW_REPEAT; + } +} \ No newline at end of file diff --git a/src/platforms/glfw/glfw_window.cpp b/src/platforms/glfw/glfw_window.cpp index f32661c..e89f008 100644 --- a/src/platforms/glfw/glfw_window.cpp +++ b/src/platforms/glfw/glfw_window.cpp @@ -1,16 +1,24 @@ #include "glfw_window.h" +#include "bakara/core/deltatime.h" +#include "bakara/events/key_event.h" +#include "bakara/events/mouse_event.h" +#include "bakara/events/window_event.h" +#include "bakatools/logging/assert.h" -#include #include -#include namespace Bk { Window* Window::CreateWindow(const WindowProps& props) { - return new Platform::WinGLFW(props); + return new Platform::GlfwWindow(props); } namespace Platform { + GlfwWindowData::~GlfwWindowData() + { + delete context; + } + static uint p_glfw_Initialized = 0; static void glfw_error_callback(int error, const char* description) @@ -18,7 +26,7 @@ namespace Bk { BK_CORE_CRITICAL("GLFW Error ({0}) {1}", error, description); } - WinGLFW::WinGLFW(const WindowProps& props) + GlfwWindow::GlfwWindow(const WindowProps& props) { p_data.title = props.title; p_data.width = props.width; @@ -26,13 +34,12 @@ namespace Bk { Init(); } - WinGLFW::~WinGLFW() + GlfwWindow::~GlfwWindow() { - delete context; Close(); } - void WinGLFW::Init() + void GlfwWindow::Init() { h_IsOpen = true; BK_CORE_INFO("Creating window : {0} ({1}, {2})", p_data.title, p_data.width, p_data.height); @@ -43,31 +50,33 @@ namespace Bk { glfwSetErrorCallback(glfw_error_callback); } p_window = glfwCreateWindow((int)p_data.width, (int)p_data.height, p_data.title.c_str(), nullptr, nullptr); - context = new OpenglContext(p_window); - context->Init(); + p_data.context = new OpenglContext(p_window); + p_data.context->Init(); + p_data.context->SetViewport(p_data.width, p_data.height); glfwSetWindowUserPointer(p_window, &p_data); SetVsync(true); InitEventCallbacks(); } - void WinGLFW::InitEventCallbacks() + void GlfwWindow::InitEventCallbacks() { glfwSetFramebufferSizeCallback(p_window, [](GLFWwindow* window, int width, int height) { - WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window); + GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window); + data.context->SetViewport(width, height); WindowResizeEvent e(data.width = (uint)width, data.height = (uint)height); data.callback(e); }); glfwSetWindowCloseCallback(p_window, [](GLFWwindow* window) { - WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window); + GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window); WindowCloseEvent e; data.callback(e); }); glfwSetKeyCallback(p_window, [](GLFWwindow* window, int key, int scancode, int action, int mods) { - WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window); + GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window); switch(action) { case GLFW_PRESS: @@ -92,7 +101,7 @@ namespace Bk { }); glfwSetMouseButtonCallback(p_window, [](GLFWwindow* window, int button, int action, int mods) { - WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window); + GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window); switch(action) { case GLFW_PRESS: @@ -111,36 +120,38 @@ namespace Bk { }); glfwSetScrollCallback(p_window, [](GLFWwindow* window, double offset_x, double offset_y) { - - WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window); + GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window); MouseScrollEvent e((float)offset_x, (float)offset_y); data.callback(e); }); glfwSetCursorPosCallback(p_window, [](GLFWwindow* window, double pos_x, double pos_y) { - WindowData& data = *(WindowData*)glfwGetWindowUserPointer(window); + GlfwWindowData& data = *(GlfwWindowData*)glfwGetWindowUserPointer(window); MouseMoveEvent e((float)pos_x, (float)pos_y); data.callback(e); }); } - void WinGLFW::OnUpdate() + void GlfwWindow::OnUpdate() { + float time = glfwGetTime(); + dt = DeltaTime(time - lastFrameTime); + lastFrameTime = time; glfwPollEvents(); - context->SwapBuffers(); + p_data.context->SwapBuffers(); if (h_IsOpen) { if (p_Shutdown && h_IsOpen) { Shutdown(); } } } - void WinGLFW::SetEventCallback(const EventCallback callback) + void GlfwWindow::SetEventCallback(const EventCallback callback) { p_data.callback = callback; } - void WinGLFW::SetVsync(bool enable) + void GlfwWindow::SetVsync(bool enable) { if (h_IsOpen) { @@ -150,24 +161,24 @@ namespace Bk { } } - bool WinGLFW::IsVsync() const + bool GlfwWindow::IsVsync() const { return p_data.vsync; } - void WinGLFW::Shutdown() + void GlfwWindow::Shutdown() { h_IsOpen = false; p_Shutdown = false; glfwDestroyWindow(p_window); } - void WinGLFW::Close() + void GlfwWindow::Close() { p_Shutdown = true; } - void WinGLFW::Open() + void GlfwWindow::Open() { if (!h_IsOpen) { diff --git a/src/platforms/glfw/glfw_window.h b/src/platforms/glfw/glfw_window.h index 68774f8..8acce36 100644 --- a/src/platforms/glfw/glfw_window.h +++ b/src/platforms/glfw/glfw_window.h @@ -1,17 +1,28 @@ #pragma once -#include "bakara.pch" + +#include "bakara/core/deltatime.h" #include "bakara/core/window.h" -#include "bakara/events/events.h" #include "platforms/opengl/opengl_context.h" struct GLFWwindow; namespace Bk::Platform { - class WinGLFW : public Window + struct GlfwWindowData + { + std::string title; + uint width; + uint height; + bool vsync; + EventCallback callback; + GraphicsContext* context; + ~GlfwWindowData(); + }; + + class GlfwWindow : public Window { public: - WinGLFW(const WindowProps& props); - virtual ~WinGLFW(); + GlfwWindow(const WindowProps& props); + virtual ~GlfwWindow(); inline uint GetWidth() const override { return p_data.width; } inline uint GetHeight() const override { return p_data.height; } @@ -31,21 +42,17 @@ namespace Bk::Platform { @return Open flag */ bool IsOpen() override { return h_IsOpen; } + + virtual DeltaTime GetTime() override { return dt; } private: bool h_IsOpen; //!< indicaste if the window is Opened or not bool p_Shutdown; GLFWwindow* p_window; - OpenglContext* context; - struct WindowData - { - std::string title; - uint width; - uint height; - bool vsync; - EventCallback callback; - }; - WindowData p_data; + GlfwWindowData p_data; + + float lastFrameTime = 0.0f; + DeltaTime dt = { 0.0f }; void InitEventCallbacks(); void Init(); diff --git a/src/platforms/opengl/opengl_buffer.cpp b/src/platforms/opengl/opengl_buffer.cpp new file mode 100644 index 0000000..511e5b5 --- /dev/null +++ b/src/platforms/opengl/opengl_buffer.cpp @@ -0,0 +1,56 @@ +#include "opengl_buffer.h" +#include + +namespace Bk::Platform +{ + OpenglVertexBuffer::OpenglVertexBuffer(float* vertices, u32 size) + { + glGenBuffers(1, &id); + glBindBuffer(GL_ARRAY_BUFFER, id); + glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW); + } + + OpenglVertexBuffer::~OpenglVertexBuffer() + { + glDeleteBuffers(1, &id); + } + + void OpenglVertexBuffer::Bind() + { + glBindBuffer(GL_ARRAY_BUFFER, id); + } + + void OpenglVertexBuffer::Unbind() + { + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + + + OpenglIndexBuffer::OpenglIndexBuffer(u32* indices, u32 count) + : count(count) + { + glGenBuffers(1, &id); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(u32), indices, GL_STATIC_DRAW); + } + + OpenglIndexBuffer::~OpenglIndexBuffer() + { + glDeleteBuffers(1, &id); + } + + void OpenglIndexBuffer::Bind() + { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); + } + + void OpenglIndexBuffer::Unbind() + { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + + u32 OpenglIndexBuffer::GetCount() + { + return count; + } +} \ No newline at end of file diff --git a/src/platforms/opengl/opengl_buffer.h b/src/platforms/opengl/opengl_buffer.h new file mode 100644 index 0000000..ba6a415 --- /dev/null +++ b/src/platforms/opengl/opengl_buffer.h @@ -0,0 +1,35 @@ +#pragma once + +#include "bakara/renderer/buffer.h" +#include "bakara/renderer/buffer_layout.h" + +namespace Bk::Platform +{ + class OpenglVertexBuffer : public VertexBuffer + { + public: + OpenglVertexBuffer(float* vertices, u32 size); + virtual ~OpenglVertexBuffer(); + virtual void Bind() override; + virtual void Unbind() override; + virtual void SetLayout(BufferLayout layout) override { this->layout = layout; } + virtual BufferLayout& GetLayout() override { return layout; } + private: + BufferLayout layout; + u32 id; + }; + + class OpenglIndexBuffer : public IndexBuffer + { + public: + OpenglIndexBuffer(u32* vertices, u32 count); + virtual ~OpenglIndexBuffer(); + virtual void Bind() override; + virtual void Unbind() override; + virtual u32 GetCount() override; + + private: + u32 id; + u32 count; + }; +} \ No newline at end of file diff --git a/src/platforms/opengl/opengl_context.cpp b/src/platforms/opengl/opengl_context.cpp index b472d98..e410ece 100644 --- a/src/platforms/opengl/opengl_context.cpp +++ b/src/platforms/opengl/opengl_context.cpp @@ -1,3 +1,5 @@ +#include "bakatools/logging/assert.h" +#include "bakatools/logging/log.h" #include "opengl_context.h" #include #include @@ -16,7 +18,12 @@ namespace Bk::Platform GLint majVers = 0, minVers = 0; glGetIntegerv(GL_MAJOR_VERSION, &majVers); glGetIntegerv(GL_MINOR_VERSION, &minVers); - BK_CORE_INFO("Opengl Version : {0}.{1}", majVers, minVers); + BK_CORE_INFO("Hardware info "); + BK_CORE_INFO(" - Opengl Version {0}.{1}", majVers, minVers); + BK_CORE_INFO(" - GPU Vendor {0}", reinterpret_cast(glGetString(GL_VENDOR))); + BK_CORE_INFO(" - Renderer {0}", reinterpret_cast(glGetString(GL_RENDERER))); + BK_CORE_INFO(" - Renderer Version {0}", reinterpret_cast(glGetString(GL_VERSION))); + BK_CORE_MSG_ASSERT(majVers > 4 || (majVers == 4 && minVers >= 2), "Bakara only support Opengl versions 4.2 or more.") } void OpenglContext::SwapBuffers() @@ -24,4 +31,8 @@ namespace Bk::Platform glfwSwapBuffers(window_handle); } + void OpenglContext::SetViewport(int width, int height) + { + glViewport(0, 0, width, height); + } } \ No newline at end of file diff --git a/src/platforms/opengl/opengl_context.h b/src/platforms/opengl/opengl_context.h index 383e37f..eb47e1f 100644 --- a/src/platforms/opengl/opengl_context.h +++ b/src/platforms/opengl/opengl_context.h @@ -1,6 +1,6 @@ #pragma once -#include "bakara.pch" + #include "bakara/renderer/graphics_context.h" struct GLFWwindow; @@ -11,8 +11,10 @@ namespace Bk::Platform { public: OpenglContext(GLFWwindow* window_handle); + virtual ~OpenglContext() override { } void Init() override; void SwapBuffers() override; + void SetViewport(int width, int height) override; private: GLFWwindow* window_handle; }; diff --git a/src/platforms/opengl/opengl_renderer_api.cpp b/src/platforms/opengl/opengl_renderer_api.cpp new file mode 100644 index 0000000..2f9ed4a --- /dev/null +++ b/src/platforms/opengl/opengl_renderer_api.cpp @@ -0,0 +1,16 @@ +#include "opengl_renderer_api.h" +#include + +namespace Bk +{ + void OpenglRendererAPI::Clear(float red, float green, float blue, float alpha) + { + glClearColor(red, green, blue, alpha); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + void OpenglRendererAPI::DrawIndexed(Ref va) + { + glDrawElements(GL_TRIANGLES, va->GetIndexbuffer()->GetCount(), GL_UNSIGNED_INT, nullptr); + } +} \ No newline at end of file diff --git a/src/platforms/opengl/opengl_renderer_api.h b/src/platforms/opengl/opengl_renderer_api.h new file mode 100644 index 0000000..731a33f --- /dev/null +++ b/src/platforms/opengl/opengl_renderer_api.h @@ -0,0 +1,14 @@ +#pragma once + +#include "bakara/renderer/renderer_api.h" +#include "bakara/renderer/vertex_array.h" + +namespace Bk +{ + class OpenglRendererAPI : public RendererAPI + { + public: + virtual void Clear(float red, float green, float blue, float alpha) override; + virtual void DrawIndexed(Ref va) override; + }; +} \ No newline at end of file diff --git a/src/platforms/opengl/opengl_shader.cpp b/src/platforms/opengl/opengl_shader.cpp new file mode 100644 index 0000000..9c34505 --- /dev/null +++ b/src/platforms/opengl/opengl_shader.cpp @@ -0,0 +1,108 @@ +#include "opengl_shader.h" +#include "bakatools/logging/log.h" +#include "bakara/math/types.h" +#include + +namespace Bk::Platform +{ + OpenglShader::OpenglShader(std::string vertexSrc, std::string fragSrc) + { + int success; + char infoLog[512]; + //Creating a shader program + id = glCreateProgram(); + //Compile shader + GLuint ret = glCreateShader(GL_VERTEX_SHADER); + const GLchar* shader = vertexSrc.c_str(); + glShaderSource(ret, 1, &shader, NULL); + glCompileShader(ret); + glGetShaderiv(ret, GL_COMPILE_STATUS, &success); + if(!success) { + glGetShaderInfoLog(ret, 512, NULL, infoLog); + BK_INFO("Error with fragment shader compilating : {0}", infoLog); + } + glAttachShader(id, ret); + + GLuint ret1 = glCreateShader(GL_FRAGMENT_SHADER); + shader = fragSrc.c_str(); + glShaderSource(ret1, 1, &shader, NULL); + glCompileShader(ret1); + glGetShaderiv(ret1, GL_COMPILE_STATUS, &success); + if(!success) { + glGetShaderInfoLog(ret1, 512, NULL, infoLog); + BK_INFO("Error with fragment shader compilating : {0}", infoLog); + } + glAttachShader(id, ret1); + glLinkProgram(id); + + glGetProgramiv(id, GL_LINK_STATUS, &success); + if(!success) { + glGetProgramInfoLog(id, 512, NULL, infoLog); + BK_INFO("Error when link sharder in a program : {0}", infoLog); + } + + glDeleteShader(ret); + glDeleteShader(ret1); + } + + OpenglShader::~OpenglShader() + { + glDeleteProgram(id); + } + + void OpenglShader::Bind() + { + glUseProgram(id); + } + + void OpenglShader::Unbind() + { + glUseProgram(0); + } + + void OpenglShader::Set(const std::string name, int val) + { + glUniform1i(glGetUniformLocation(id, name.c_str()), val); + } + + void OpenglShader::Set(const std::string name, bool value) + { + glUniform1i(glGetUniformLocation(id, name.c_str()), (int)value); + } + + void OpenglShader::Set(const std::string name, float val) + { + glUniform1f(glGetUniformLocation(this->id, name.c_str()), val); + } + + void OpenglShader::Set(const std::string name, float v1, float v2, float v3) + { + glUniform3f(glGetUniformLocation(id, name.c_str()), v1, v2, v3); + } + + void OpenglShader::Set(const std::string name, Vec3 v) + { + this->Set(name, v.x, v.y, v.z); + } + + void OpenglShader::Set(const std::string name, float v1, float v2, float v3, float v4) + { + glUniform4f(glGetUniformLocation(id, name.c_str()), v1, v2, v3, v4); + } + + void OpenglShader::Set(const std::string name, Vec4 v) + { + glUniformMatrix3fv(glGetUniformLocation(this->id, name.c_str()), 1, GL_FALSE, Math::value_ptr(v)); + } + + void OpenglShader::Set(const std::string name, Mat3 val) + { + glUniformMatrix3fv(glGetUniformLocation(this->id, name.c_str()), 1, GL_FALSE, glm::value_ptr(val)); + } + + void OpenglShader::Set(const std::string name, Mat4 val) + { + glUniformMatrix4fv(glGetUniformLocation(this->id, name.c_str()), 1, GL_FALSE, glm::value_ptr(val)); + } + +} \ No newline at end of file diff --git a/src/platforms/opengl/opengl_shader.h b/src/platforms/opengl/opengl_shader.h new file mode 100644 index 0000000..033a403 --- /dev/null +++ b/src/platforms/opengl/opengl_shader.h @@ -0,0 +1,29 @@ +#pragma once + +#include "bakara/renderer/shader.h" + +namespace Bk::Platform +{ + class OpenglShader : public Shader + { + public: + OpenglShader(std::string vertexSrc, std::string fragSrc); + virtual ~OpenglShader(); + + virtual void Bind() override; + virtual void Unbind() override; + + virtual void Set(const std::string name, int val) override; + virtual void Set(const std::string name, bool val) override; + virtual void Set(const std::string name, float val) override; + virtual void Set(const std::string name, float v1, float v2, float v3) override; + virtual void Set(const std::string name, Vec3 v) override; + virtual void Set(const std::string name, float v1, float v2, float v3, float v4) override; + virtual void Set(const std::string name, Vec4 v) override; + virtual void Set(const std::string name, Mat3 val) override; + virtual void Set(const std::string name, Mat4 val) override; + + private: + unsigned int id; + }; +} \ No newline at end of file diff --git a/src/platforms/opengl/opengl_vertex_array.cpp b/src/platforms/opengl/opengl_vertex_array.cpp new file mode 100644 index 0000000..e57e663 --- /dev/null +++ b/src/platforms/opengl/opengl_vertex_array.cpp @@ -0,0 +1,76 @@ +#include "platforms/opengl/opengl_vertex_array.h" +#include "bakara/renderer/buffer.h" +#include "bakara/renderer/buffer_layout.h" +#include "bakatools/logging/assert.h" +#include + +namespace Bk::Platform +{ + static GLenum OpenglTypeFromShaderType(ShaderType type) + { + switch(type) + { + case ShaderType::None: BK_MSG_ASSERT(false, "Shader type not supported"); return GL_NONE; + case ShaderType::Float: return GL_FLOAT; + case ShaderType::Float2: return GL_FLOAT; + case ShaderType::Float3: return GL_FLOAT; + case ShaderType::Float4: return GL_FLOAT; + case ShaderType::Mat3: return GL_FLOAT; + case ShaderType::Mat4: return GL_FLOAT; + case ShaderType::Int: return GL_INT; + case ShaderType::Int2: return GL_INT; + case ShaderType::Int3: return GL_INT; + case ShaderType::Int4: return GL_INT; + case ShaderType::Bool: return GL_BOOL; + } + } + + OpenglVertexArray::OpenglVertexArray() + { + glGenVertexArrays(1, &id); + } + + OpenglVertexArray::~OpenglVertexArray() + { + glDeleteVertexArrays(1, &id); + } + + void OpenglVertexArray::Bind() + { + glBindVertexArray(id); + } + + void OpenglVertexArray::Unbind() + { + glBindVertexArray(0); + } + + void OpenglVertexArray::AddVertexBuffer(Ref buf) + { + BK_MSG_ASSERT(!buf->GetLayout().GetElements().empty(), "Vertex Buffer layout empty"); + glBindVertexArray(id); + u32 index = 0; + u32 stride = buf->GetLayout().GetStride(); + for(auto& element : buf->GetLayout()) + { + glEnableVertexAttribArray(index); + glVertexAttribPointer(index, + element.type.Count(), + OpenglTypeFromShaderType(element.type), + element.normelized ? GL_TRUE : GL_FALSE, + stride, + (const void*)(u64)element.offset); + index++; + } + vBuffers.push_back(buf); + glBindVertexArray(0); + } + + void OpenglVertexArray::SetIndexbuffer(Ref buf) + { + glBindVertexArray(id); + buf->Bind(); + iBuffer = buf; + glBindVertexArray(0); + } +} \ No newline at end of file diff --git a/src/platforms/opengl/opengl_vertex_array.h b/src/platforms/opengl/opengl_vertex_array.h new file mode 100644 index 0000000..24e27b3 --- /dev/null +++ b/src/platforms/opengl/opengl_vertex_array.h @@ -0,0 +1,28 @@ +#pragma once + +#include "bakara/renderer/buffer.h" +#include "bakara/renderer/vertex_array.h" +#include "bakatools/container/types.h" +#include + +namespace Bk::Platform +{ + + class OpenglVertexArray : public VertexArray + { + public: + OpenglVertexArray(); + virtual ~OpenglVertexArray(); + virtual void Bind() override; + virtual void Unbind() override; + virtual void AddVertexBuffer(Ref buf) override; + virtual void SetIndexbuffer(Ref buf) override; + virtual std::vector> GetVertexBuffers() override { return vBuffers; } + virtual Ref GetIndexbuffer() override { return iBuffer; } + + private: + std::vector> vBuffers; + Ref iBuffer; + u32 id; + }; +} \ No newline at end of file