diff --git a/CopiumEngine/CopiumEngine.vcxproj b/CopiumEngine/CopiumEngine.vcxproj index ea8af2a..f0a2c9d 100644 --- a/CopiumEngine/CopiumEngine.vcxproj +++ b/CopiumEngine/CopiumEngine.vcxproj @@ -190,6 +190,7 @@ + @@ -265,6 +266,8 @@ + + @@ -273,8 +276,12 @@ + + + + diff --git a/CopiumEngine/CopiumEngine.vcxproj.filters b/CopiumEngine/CopiumEngine.vcxproj.filters index c7a2f53..f6736cb 100644 --- a/CopiumEngine/CopiumEngine.vcxproj.filters +++ b/CopiumEngine/CopiumEngine.vcxproj.filters @@ -216,6 +216,9 @@ Source Files + + Source Files + @@ -464,5 +467,23 @@ Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/CopiumEngine/src/copium/core/Scene.cpp b/CopiumEngine/src/copium/core/Scene.cpp index 097fdac..bbaba39 100644 --- a/CopiumEngine/src/copium/core/Scene.cpp +++ b/CopiumEngine/src/copium/core/Scene.cpp @@ -7,10 +7,14 @@ #include "copium/ecs/Entity.h" #include "copium/ecs/System.h" #include "copium/event/MouseMoveEvent.h" +#include "copium/example/CameraUpdateSystem.h" +#include "copium/example/Components.h" #include "copium/example/FrameCountSystem.h" #include "copium/example/MouseFollowSystem.h" #include "copium/example/RenderSystem.h" -#include "copium/example/Components.h" +#include "copium/example/PhysicsSystem.h" +#include "copium/example/PlayerControllerSystem.h" +#include "copium/example/CameraFollowPlayerSystem.h" #include #include @@ -23,9 +27,14 @@ namespace Copium renderer = std::make_unique(); descriptorSetRenderer = renderer->GetGraphicsPipeline().CreateDescriptorSet(descriptorPool, 1); ecs = std::make_unique(); - ecs->AddSystem(renderer.get(), descriptorSetRenderer.get(), &commandBuffer); // better way to store the RenderSystem data? - ecs->AddSystem().Before(); - ecs->AddSystem(); + + ecs->AddSystem(); + ecs->AddSystem(); + ecs->AddSystem(); + ecs->AddSystem(&viewMatrix, &projectionMatrix, &invPvMatrix); + ecs->AddSystem(&invPvMatrix); + ecs->AddSystem(); + ecs->AddSystem(renderer.get(), descriptorSetRenderer.get(), &commandBuffer, &viewMatrix, &projectionMatrix); // better way to store the RenderSystem data? // TODO: Load from scene file for (int y = 0; y < 10; y++) @@ -33,7 +42,7 @@ namespace Copium for (int x = 0; x < 10; x++) { Entity entity = Entity::Create(ecs.get()); - entity.AddComponent(glm::vec2{-1 + x * 0.2 + 0.05, -1 + y * 0.2 + 0.05}, glm::vec2{0.1, 0.1}); + entity.AddComponent(glm::vec2{-10.0f + x * 1.6f + 0.4f, -10.0f + y * 1.6 + 0.4f}, glm::vec2{0.8f, 0.8f}); entity.AddComponent(glm::vec3{x * 0.1f, y * 0.1f, 1.0f}); } } @@ -53,9 +62,19 @@ namespace Copium entityMouse.AddComponent(); Entity entityText = Entity::Create(ecs.get()); - entityText.AddComponent(glm::vec2{-aspect + 0.01, 0.94}, glm::vec2{1.0}); - entityText.AddComponent(AssetRef{AssetManager::LoadAsset("font.meta")}, std::to_string(0) + " fps", 0.06f); + entityText.AddComponent(glm::vec2{-aspect * 10.0f + 0.1f, 9.4f}, glm::vec2{1.0}); + entityText.AddComponent(AssetRef{AssetManager::LoadAsset("font.meta")}, std::to_string(0) + " fps", 0.6f); entityText.AddComponent(); + + Entity entityCamera = Entity::Create(ecs.get()); + entityCamera.AddComponent(BoundingBox(-aspect, -1.0f, aspect, 1.0f), false); + entityCamera.AddComponent(glm::vec2{0.0f}, glm::vec2{4.0f}); + + Entity entityPlayer = Entity::Create(ecs.get()); + entityPlayer.AddComponent(entityCamera); + entityPlayer.AddComponent(0.1f, glm::vec2{0.0f, 0.0f}, glm::vec2{0.0f, 0.0f}); + entityPlayer.AddComponent(glm::vec2{0.0f}, glm::vec2{1.0f}); + entityPlayer.AddComponent(AssetRef{AssetManager::LoadAsset("fox2.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f}); } void Scene::Update() diff --git a/CopiumEngine/src/copium/core/Scene.h b/CopiumEngine/src/copium/core/Scene.h index 4495054..fa31fe4 100644 --- a/CopiumEngine/src/copium/core/Scene.h +++ b/CopiumEngine/src/copium/core/Scene.h @@ -15,8 +15,9 @@ namespace Copium std::unique_ptr renderer; std::unique_ptr ecs; std::unique_ptr descriptorSetRenderer; - int frameCounter = 0; - int fps = 0; + glm::mat4 projectionMatrix; + glm::mat4 viewMatrix; + glm::mat4 invPvMatrix; public: Scene(CommandBuffer& commandBuffer, DescriptorPool& descriptorPool); void Update(); diff --git a/CopiumEngine/src/copium/core/Window.cpp b/CopiumEngine/src/copium/core/Window.cpp index f76666f..ef59fa3 100644 --- a/CopiumEngine/src/copium/core/Window.cpp +++ b/CopiumEngine/src/copium/core/Window.cpp @@ -2,6 +2,7 @@ #include "copium/core/Vulkan.h" #include "copium/event/EventDispatcher.h" +#include "copium/event/Input.h" #include "copium/event/KeyPressEvent.h" #include "copium/event/KeyReleaseEvent.h" #include "copium/event/MouseMoveEvent.h" @@ -99,24 +100,48 @@ namespace Copium void Window::KeyCallback(GLFWwindow* glfwWindow, int key, int scancode, int action, int mods) { - if (action == GLFW_PRESS) - EventDispatcher::QueueEvent(KeyPressEvent(key)); - else if (action == GLFW_RELEASE) - EventDispatcher::QueueEvent(KeyReleaseEvent(key)); + try + { + if (action == GLFW_PRESS) + { + Input::OnKey(key, true); + EventDispatcher::QueueEvent(KeyPressEvent(key)); + } + else if (action == GLFW_RELEASE) + { + Input::OnKey(key, false); + EventDispatcher::QueueEvent(KeyReleaseEvent(key)); + } + } + catch (RuntimeException& exception) + {} } void Window::MouseButtonCallback(GLFWwindow* glfwWindow, int button, int action, int mods) { - if (action == GLFW_PRESS) - EventDispatcher::QueueEvent(MousePressEvent{button}); - else if (action == GLFW_RELEASE) - EventDispatcher::QueueEvent(MouseReleaseEvent{button}); + try + { + if (action == GLFW_PRESS) + { + Input::OnMouse(button, true); + EventDispatcher::QueueEvent(MousePressEvent{button}); + } + else if (action == GLFW_RELEASE) + { + Input::OnMouse(button, false); + EventDispatcher::QueueEvent(MouseReleaseEvent{button}); + } + } + catch (RuntimeException& exception) + {} } void Window::MouseMoveCallback(GLFWwindow* glfwWindow, double xpos, double ypos) { Window* window = (Window*)glfwGetWindowUserPointer(glfwWindow); - EventDispatcher::QueueEvent(MouseMoveEvent{glm::vec2{xpos / window->width * 2.0 - 1.0, -(ypos / window->height * 2.0 - 1.0)}}); + glm::vec2 pos{xpos / window->width * 2.0 - 1.0, -(ypos / window->height * 2.0 - 1.0)}; + Input::OnMouseMove(pos); + EventDispatcher::QueueEvent(MouseMoveEvent{pos}); } void Window::WindowFocusCallback(GLFWwindow* glfwWindow, int focused) diff --git a/CopiumEngine/src/copium/event/Input.cpp b/CopiumEngine/src/copium/event/Input.cpp new file mode 100644 index 0000000..8a88c5b --- /dev/null +++ b/CopiumEngine/src/copium/event/Input.cpp @@ -0,0 +1,89 @@ +#include "copium/event/Input.h" + +#include "copium/util/Common.h" + +namespace Copium +{ + bool Input::keyDownList[MAX_NUM_KEYS]; + bool Input::keyEventList[MAX_NUM_KEYS]; + bool Input::mouseDownList[MAX_NUM_MOUSE_BUTTONS]; + bool Input::mouseEventList[MAX_NUM_MOUSE_BUTTONS]; + glm::vec2 Input::mousePos{0.0f}; + + bool Input::IsKeyPressed(int keyCode) + { + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range"); + return keyEventList[keyCode] && keyDownList[keyCode]; + } + + bool Input::IsKeyReleased(int keyCode) + { + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range"); + return keyEventList[keyCode] && !keyDownList[keyCode]; + } + + bool Input::IsKeyDown(int keyCode) + { + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range"); + return keyDownList[keyCode]; + } + + bool Input::IsKeyUp(int keyCode) + { + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range"); + return !keyDownList[keyCode]; + } + + bool Input::IsMousePressed(int button) + { + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range"); + return mouseEventList[button] && mouseDownList[button]; + } + + bool Input::IsMouseReleased(int button) + { + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range"); + return mouseEventList[button] && !mouseDownList[button]; + } + + bool Input::IsMouseDown(int button) + { + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range"); + return mouseDownList[button]; + } + + bool Input::IsMouseUp(int button) + { + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range"); + return !mouseDownList[button]; + } + + glm::vec2 Input::GetMousePos() + { + return mousePos; + } + + void Input::OnKey(int keyCode, bool pressed) + { + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range"); + keyDownList[keyCode] = pressed; + keyEventList[keyCode] = true; + } + + void Input::OnMouse(int button, bool pressed) + { + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range"); + mouseDownList[button] = pressed; + mouseEventList[button] = true; + } + + void Input::OnMouseMove(glm::vec2 mousePos) + { + Input::mousePos = mousePos; + } + + void Input::Update() + { + memset(keyEventList, false, sizeof(keyEventList)); + } +} diff --git a/CopiumEngine/src/copium/event/Input.h b/CopiumEngine/src/copium/event/Input.h new file mode 100644 index 0000000..05b284c --- /dev/null +++ b/CopiumEngine/src/copium/event/Input.h @@ -0,0 +1,46 @@ +#pragma once + +#include + +#include "copium/event/InputCode.h" + +#include + +namespace Copium +{ + class Input + { + private: + const static int MAX_NUM_KEYS = 1024; + const static int MAX_NUM_MOUSE_BUTTONS = 64; + + static bool keyDownList[MAX_NUM_KEYS]; + static bool keyEventList[MAX_NUM_KEYS]; + static bool mouseDownList[MAX_NUM_MOUSE_BUTTONS]; + static bool mouseEventList[MAX_NUM_MOUSE_BUTTONS]; + static glm::vec2 mousePos; + + public: + // Will only be true for a single frame after the KeyPressEvent/KeyReleaseEvent + static bool IsKeyPressed(int key); + static bool IsKeyReleased(int key); + + static bool IsKeyDown(int key); + static bool IsKeyUp(int key); + + // Will only be true for a single frame after the MousePressEvent/MouseReleaseEvent + static bool IsMousePressed(int button); + static bool IsMouseReleased(int button); + + static bool IsMouseDown(int button); + static bool IsMouseUp(int button); + + static glm::vec2 GetMousePos(); + + static void OnKey(int keyCode, bool pressed); + static void OnMouse(int buttion, bool pressed); + static void OnMouseMove(glm::vec2 mousePos); + + static void Update(); + }; +} diff --git a/CopiumEngine/src/copium/event/InputCode.h b/CopiumEngine/src/copium/event/InputCode.h new file mode 100644 index 0000000..142c7f1 --- /dev/null +++ b/CopiumEngine/src/copium/event/InputCode.h @@ -0,0 +1,124 @@ +#pragma once + +#include "copium/util/Enum.h" + +#define CP_KEY_SPACE 32 +#define CP_KEY_APOSTROPHE 39 +#define CP_KEY_COMMA 44 +#define CP_KEY_MINUS 45 +#define CP_KEY_PERIOD 46 +#define CP_KEY_SLASH 47 +#define CP_KEY_0 48 +#define CP_KEY_1 49 +#define CP_KEY_2 50 +#define CP_KEY_3 51 +#define CP_KEY_4 52 +#define CP_KEY_5 53 +#define CP_KEY_6 54 +#define CP_KEY_7 55 +#define CP_KEY_8 56 +#define CP_KEY_9 57 +#define CP_KEY_SEMICOLON 59 +#define CP_KEY_EQUAL 61 +#define CP_KEY_A 65 +#define CP_KEY_B 66 +#define CP_KEY_C 67 +#define CP_KEY_D 68 +#define CP_KEY_E 69 +#define CP_KEY_F 70 +#define CP_KEY_G 71 +#define CP_KEY_H 72 +#define CP_KEY_I 73 +#define CP_KEY_J 74 +#define CP_KEY_K 75 +#define CP_KEY_L 76 +#define CP_KEY_M 77 +#define CP_KEY_N 78 +#define CP_KEY_O 79 +#define CP_KEY_P 80 +#define CP_KEY_Q 81 +#define CP_KEY_R 82 +#define CP_KEY_S 83 +#define CP_KEY_T 84 +#define CP_KEY_U 85 +#define CP_KEY_V 86 +#define CP_KEY_W 87 +#define CP_KEY_X 88 +#define CP_KEY_Y 89 +#define CP_KEY_Z 90 +#define CP_KEY_LEFT_BRACKET 91 +#define CP_KEY_BACKSLASH 92 +#define CP_KEY_RIGHT_BRACKET 93 +#define CP_KEY_GRAVE_ACCENT 96 +#define CP_KEY_WORLD_1 161 +#define CP_KEY_WORLD_2 162 +#define CP_KEY_ESCAPE 256 +#define CP_KEY_ENTER 257 +#define CP_KEY_TAB 258 +#define CP_KEY_BACKSPACE 259 +#define CP_KEY_INSERT 260 +#define CP_KEY_DELETE 261 +#define CP_KEY_RIGHT 262 +#define CP_KEY_LEFT 263 +#define CP_KEY_DOWN 264 +#define CP_KEY_UP 265 +#define CP_KEY_PAGE_UP 266 +#define CP_KEY_PAGE_DOWN 267 +#define CP_KEY_HOME 268 +#define CP_KEY_END 269 +#define CP_KEY_CAPS_LOCK 280 +#define CP_KEY_SCROLL_LOCK 281 +#define CP_KEY_NUM_LOCK 282 +#define CP_KEY_PRINT_SCREEN 283 +#define CP_KEY_PAUSE 284 +#define CP_KEY_F1 290 +#define CP_KEY_F2 291 +#define CP_KEY_F3 292 +#define CP_KEY_F4 293 +#define CP_KEY_F5 294 +#define CP_KEY_F6 295 +#define CP_KEY_F7 296 +#define CP_KEY_F8 297 +#define CP_KEY_F9 298 +#define CP_KEY_F10 299 +#define CP_KEY_F11 300 +#define CP_KEY_F12 301 +#define CP_KEY_F13 302 +#define CP_KEY_F14 303 +#define CP_KEY_F15 304 +#define CP_KEY_F16 305 +#define CP_KEY_F17 306 +#define CP_KEY_F18 307 +#define CP_KEY_F19 308 +#define CP_KEY_F20 309 +#define CP_KEY_F21 310 +#define CP_KEY_F22 311 +#define CP_KEY_F23 312 +#define CP_KEY_F24 313 +#define CP_KEY_F25 314 +#define CP_KEY_KP_0 320 +#define CP_KEY_KP_1 321 +#define CP_KEY_KP_2 322 +#define CP_KEY_KP_3 323 +#define CP_KEY_KP_4 324 +#define CP_KEY_KP_5 325 +#define CP_KEY_KP_6 326 +#define CP_KEY_KP_7 327 +#define CP_KEY_KP_8 328 +#define CP_KEY_KP_9 329 +#define CP_KEY_KP_DECIMAL 330 +#define CP_KEY_KP_DIVIDE 331 +#define CP_KEY_KP_MULTIPLY 332 +#define CP_KEY_KP_SUBTRACT 333 +#define CP_KEY_KP_ADD 334 +#define CP_KEY_KP_ENTER 335 +#define CP_KEY_KP_EQUAL 336 +#define CP_KEY_LEFT_SHIFT 340 +#define CP_KEY_LEFT_CONTROL 341 +#define CP_KEY_LEFT_ALT 342 +#define CP_KEY_LEFT_SUPER 343 +#define CP_KEY_RIGHT_SHIFT 344 +#define CP_KEY_RIGHT_CONTROL 345 +#define CP_KEY_RIGHT_ALT 346 +#define CP_KEY_RIGHT_SUPER 347 +#define CP_KEY_MENU 348 diff --git a/CopiumEngine/src/copium/example/CameraFollowPlayerSystem.h b/CopiumEngine/src/copium/example/CameraFollowPlayerSystem.h new file mode 100644 index 0000000..5ce19dd --- /dev/null +++ b/CopiumEngine/src/copium/example/CameraFollowPlayerSystem.h @@ -0,0 +1,18 @@ +#pragma once + +#include "copium/ecs/System.h" +#include "copium/example/Components.h" + +namespace Copium +{ + class CameraFollowPlayerSystem : public System + { + public: + void RunEntity(Entity entity, PlayerC& player, TransformC& transform) override + { + TransformC& cameraTransform = player.camera.GetComponent(); + glm::vec2 wantedPos = transform.position + transform.size * 0.5f; + cameraTransform.position -= (cameraTransform.position - wantedPos) * 0.10f; + } + }; +} diff --git a/CopiumEngine/src/copium/example/CameraUpdateSystem.h b/CopiumEngine/src/copium/example/CameraUpdateSystem.h new file mode 100644 index 0000000..c8f23e7 --- /dev/null +++ b/CopiumEngine/src/copium/example/CameraUpdateSystem.h @@ -0,0 +1,52 @@ +#pragma once + +#include "copium/ecs/System.h" +#include "copium/example/Components.h" +#include "copium/event/EventSignal.h" +#include "copium/event/WindowResizeEvent.h" + +#include + +namespace Copium +{ + class CameraUpdateSystem : public System + { + private: + glm::mat4* viewMatrix; + glm::mat4* projectionMatrix; + glm::mat4* invPvMatrix; + public: + CameraUpdateSystem(glm::mat4* viewMatrix, glm::mat4* projectionMatrix, glm::mat4* invPvMatrix) + : viewMatrix{viewMatrix}, projectionMatrix{projectionMatrix}, invPvMatrix{invPvMatrix} + {} + + void RunEntity(Entity entity, CameraC& camera, TransformC& transform) + { + *projectionMatrix = glm::ortho(camera.projection.l, camera.projection.r, camera.projection.b, camera.projection.t); + *viewMatrix = glm::translate(glm::scale(glm::mat4{1}, glm::vec3{1.0f / transform.size.x, 1.0f / transform.size.y, 1.0f}), glm::vec3{-transform.position.x, -transform.position.y, 0.0f}); + *invPvMatrix = glm::inverse((*projectionMatrix) * (*viewMatrix)); + } + + void RunEntity(const Signal& signal, Entity entity, CameraC& camera, TransformC& transform) override + { + if (camera.staticBoundingBox) + return; + + if (signal.GetId() != EventSignal::GetIdStatic()) + return; + + const EventSignal& eventSignal = static_cast(signal); + switch (eventSignal.GetEvent().GetType()) + { + case EventType::WindowResize: + { + const WindowResizeEvent& windowResizeEvent = static_cast(eventSignal.GetEvent()); + float aspect = windowResizeEvent.GetWidth() / (float)windowResizeEvent.GetHeight(); + camera.projection.r = aspect; + camera.projection.l = -aspect; + break; + } + } + } + }; +} diff --git a/CopiumEngine/src/copium/example/Components.h b/CopiumEngine/src/copium/example/Components.h index 9afe5c4..7322a67 100644 --- a/CopiumEngine/src/copium/example/Components.h +++ b/CopiumEngine/src/copium/example/Components.h @@ -1,6 +1,7 @@ #pragma once #include "copium/asset/AssetRef.h" +#include "copium/ecs/Entity.h" #include @@ -38,4 +39,22 @@ namespace Copium struct FrameCountC {}; + + struct CameraC + { + BoundingBox projection; + bool staticBoundingBox; + }; + + struct PhysicsC + { + float mass; + glm::vec2 force; + glm::vec2 velocity; + }; + + struct PlayerC + { + Entity camera; + }; } \ No newline at end of file diff --git a/CopiumEngine/src/copium/example/MouseFollowSystem.h b/CopiumEngine/src/copium/example/MouseFollowSystem.h index 851aaa6..3cf3f98 100644 --- a/CopiumEngine/src/copium/example/MouseFollowSystem.h +++ b/CopiumEngine/src/copium/example/MouseFollowSystem.h @@ -1,11 +1,7 @@ #pragma once -#include "copium/core/Vulkan.h" #include "copium/ecs/System.h" -#include "copium/event/Event.h" -#include "copium/event/EventSignal.h" -#include "copium/event/MouseMoveEvent.h" -#include "copium/example/Components.h" +#include "copium/event/Input.h" #include @@ -13,21 +9,17 @@ namespace Copium { class MouseFollowSystem : public System { + private: + glm::mat4* invPvMatrix; public: - void RunEntity(const Signal& signal, Entity entity, MouseFollowC& mouseFollow, TransformC& transform) + MouseFollowSystem(glm::mat4* invPvMatrix) + : invPvMatrix{invPvMatrix} + {} + + void RunEntity(Entity entity, MouseFollowC& mouseFollow, TransformC& transform) { - if (signal.GetId() == EventSignal::GetIdStatic()) - { - const EventSignal& eventSignal = static_cast(signal); - if (eventSignal.GetEvent().GetType() == EventType::MouseMove) - { - const MouseMoveEvent& mouseMoveEvent = static_cast(eventSignal.GetEvent()); - float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height; - // TODO: Get projectionViewMatrix from camera - transform.position = glm::inverse(glm::ortho(-aspect, aspect, -1.0f, 1.0f)) * glm::vec4{mouseMoveEvent.GetPos().x, mouseMoveEvent.GetPos().y, 0.0f, 0.0f}; - transform.position -= transform.size * glm::vec2{0.5f}; - } - } + transform.position = (*invPvMatrix) * glm::vec4{Input::GetMousePos().x, Input::GetMousePos().y, 0.0f, 1.0f}; + transform.position -= transform.size * glm::vec2{0.5f}; } }; } diff --git a/CopiumEngine/src/copium/example/PhysicsSystem.h b/CopiumEngine/src/copium/example/PhysicsSystem.h new file mode 100644 index 0000000..f2f8a3f --- /dev/null +++ b/CopiumEngine/src/copium/example/PhysicsSystem.h @@ -0,0 +1,20 @@ +#pragma once + +#include "copium/ecs/System.h" +#include "copium/example/Components.h" + +namespace Copium +{ + class PhysicsSystem : public System + { + public: + void RunEntity(Entity entity, PhysicsC& physics, TransformC& transform) override + { + float timespan = 1 / 165.0f; // My main monitor refresh rate, should be based on the frame rate + physics.velocity += physics.force / physics.mass * timespan; + physics.velocity *= 0.7; // friction + transform.position += physics.velocity * timespan; + physics.force = glm::vec2{0.0f}; + } + }; +} diff --git a/CopiumEngine/src/copium/example/PlayerControllerSystem.h b/CopiumEngine/src/copium/example/PlayerControllerSystem.h new file mode 100644 index 0000000..e8917ec --- /dev/null +++ b/CopiumEngine/src/copium/example/PlayerControllerSystem.h @@ -0,0 +1,25 @@ +#pragma once + +#include "copium/ecs/System.h" +#include "copium/event/Input.h" +#include "copium/example/Components.h" + +namespace Copium +{ + class PlayerControllerSystem : public System + { + public: + void RunEntity(Entity entity, PlayerC& player, PhysicsC& physics) override + { + float magnitude = 200.0f; + glm::vec2 force{0.0f}; + if (Input::IsKeyDown(CP_KEY_W)) force.y += 1.0f; + if (Input::IsKeyDown(CP_KEY_S)) force.y -= 1.0f; + if (Input::IsKeyDown(CP_KEY_A)) force.x -= 1.0f; + if (Input::IsKeyDown(CP_KEY_D)) force.x += 1.0f; + + glm::normalize(force); + physics.force += force * magnitude; + } + }; +} diff --git a/CopiumEngine/src/copium/example/RenderSystem.h b/CopiumEngine/src/copium/example/RenderSystem.h index 45414d7..7b41452 100644 --- a/CopiumEngine/src/copium/example/RenderSystem.h +++ b/CopiumEngine/src/copium/example/RenderSystem.h @@ -17,11 +17,15 @@ namespace Copium Renderer* renderer; DescriptorSet* descriptorSet; CommandBuffer* commandBuffer; + glm::mat4* viewMatrix; + glm::mat4* projectionMatrix; public: - RenderSystem(Renderer* renderer, DescriptorSet* descriptorSet, CommandBuffer* commandBuffer) + RenderSystem(Renderer* renderer, DescriptorSet* descriptorSet, CommandBuffer* commandBuffer, glm::mat4* viewMatrix, glm::mat4* projectionMatrix) : renderer{renderer}, descriptorSet{descriptorSet}, - commandBuffer{commandBuffer} + commandBuffer{commandBuffer}, + viewMatrix{viewMatrix}, + projectionMatrix{projectionMatrix} {} void RunEntity(Entity entity, TransformC& transform) @@ -47,8 +51,8 @@ namespace Copium { float aspect = Vulkan::GetSwapChain().GetExtent().width / (float)Vulkan::GetSwapChain().GetExtent().height; UniformBuffer& uniformBuffer = descriptorSet->GetUniformBuffer("ubo"); - uniformBuffer.Set("projection", glm::ortho(-aspect, aspect, -1.0f, 1.0f)); - uniformBuffer.Set("view", glm::mat4(1)); + uniformBuffer.Set("projection", *projectionMatrix); + uniformBuffer.Set("view", *viewMatrix); uniformBuffer.Update(); renderer->SetDescriptorSet(*descriptorSet); diff --git a/CopiumEngine/src/copium/main.cpp b/CopiumEngine/src/copium/main.cpp index 06cf2db..a675239 100644 --- a/CopiumEngine/src/copium/main.cpp +++ b/CopiumEngine/src/copium/main.cpp @@ -1,6 +1,7 @@ #include "copium/core/Application.h" #include "copium/core/Vulkan.h" #include "copium/event/EventDispatcher.h" +#include "copium/event/Input.h" #include "copium/util/Common.h" #include "copium/util/Timer.h" @@ -16,7 +17,9 @@ int main(int argc, char** argv) while (application.Update()) { glfwPollEvents(); + Copium::EventDispatcher::Dispatch(); + Copium::Input::Update(); } } Copium::Vulkan::Destroy();