Add Input class

- Add Input class used to poll mouse and key button states
- Add additional example systems to the Scene
This commit is contained in:
Thraix
2023-05-26 23:02:03 +02:00
parent d817c3084d
commit 5a615ecc4e
16 changed files with 505 additions and 40 deletions
+7
View File
@@ -190,6 +190,7 @@
<ClCompile Include="src\copium\event\Event.cpp" />
<ClCompile Include="src\copium\event\EventDispatcher.cpp" />
<ClCompile Include="src\copium\event\EventSignal.cpp" />
<ClCompile Include="src\copium\event\Input.cpp" />
<ClCompile Include="src\copium\event\KeyPressEvent.cpp" />
<ClCompile Include="src\copium\event\KeyReleaseEvent.cpp" />
<ClCompile Include="src\copium\event\MouseMoveEvent.cpp" />
@@ -265,6 +266,8 @@
<ClInclude Include="src\copium\event\EventResult.h" />
<ClInclude Include="src\copium\event\EventSignal.h" />
<ClInclude Include="src\copium\event\EventType.h" />
<ClInclude Include="src\copium\event\Input.h" />
<ClInclude Include="src\copium\event\InputCode.h" />
<ClInclude Include="src\copium\event\KeyPressEvent.h" />
<ClInclude Include="src\copium\event\KeyReleaseEvent.h" />
<ClInclude Include="src\copium\event\MouseMoveEvent.h" />
@@ -273,8 +276,12 @@
<ClInclude Include="src\copium\event\MouseScrollEvent.h" />
<ClInclude Include="src\copium\event\WindowFocusEvent.h" />
<ClInclude Include="src\copium\event\WindowResizeEvent.h" />
<ClInclude Include="src\copium\example\CameraFollowPlayerSystem.h" />
<ClInclude Include="src\copium\example\CameraUpdateSystem.h" />
<ClInclude Include="src\copium\example\Components.h" />
<ClInclude Include="src\copium\example\FrameCountSystem.h" />
<ClInclude Include="src\copium\example\PhysicsSystem.h" />
<ClInclude Include="src\copium\example\PlayerControllerSystem.h" />
<ClInclude Include="src\copium\example\RenderSystem.h" />
<ClInclude Include="src\copium\example\MouseFollowSystem.h" />
<ClInclude Include="src\copium\mesh\Mesh.h" />
+21
View File
@@ -216,6 +216,9 @@
<ClCompile Include="src\copium\ecs\Signal.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\event\Input.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\copium\sampler\DepthAttachment.h">
@@ -464,5 +467,23 @@
<ClInclude Include="src\copium\event\EventSignal.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\CameraUpdateSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\PlayerControllerSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\PhysicsSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\event\Input.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\event\InputCode.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\example\CameraFollowPlayerSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
+26 -7
View File
@@ -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 <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
@@ -23,9 +27,14 @@ namespace Copium
renderer = std::make_unique<Renderer>();
descriptorSetRenderer = renderer->GetGraphicsPipeline().CreateDescriptorSet(descriptorPool, 1);
ecs = std::make_unique<ECSManager>();
ecs->AddSystem<RenderSystem>(renderer.get(), descriptorSetRenderer.get(), &commandBuffer); // better way to store the RenderSystem data?
ecs->AddSystem<FrameCountSystem>().Before<FrameCountSystem>();
ecs->AddSystem<MouseFollowSystem>();
ecs->AddSystem<PlayerControllerSystem>();
ecs->AddSystem<PhysicsSystem>();
ecs->AddSystem<CameraFollowPlayerSystem>();
ecs->AddSystem<CameraUpdateSystem>(&viewMatrix, &projectionMatrix, &invPvMatrix);
ecs->AddSystem<MouseFollowSystem>(&invPvMatrix);
ecs->AddSystem<FrameCountSystem>();
ecs->AddSystem<RenderSystem>(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<TransformC>(glm::vec2{-1 + x * 0.2 + 0.05, -1 + y * 0.2 + 0.05}, glm::vec2{0.1, 0.1});
entity.AddComponent<TransformC>(glm::vec2{-10.0f + x * 1.6f + 0.4f, -10.0f + y * 1.6 + 0.4f}, glm::vec2{0.8f, 0.8f});
entity.AddComponent<ColorC>(glm::vec3{x * 0.1f, y * 0.1f, 1.0f});
}
}
@@ -53,9 +62,19 @@ namespace Copium
entityMouse.AddComponent<MouseFollowC>();
Entity entityText = Entity::Create(ecs.get());
entityText.AddComponent<TransformC>(glm::vec2{-aspect + 0.01, 0.94}, glm::vec2{1.0});
entityText.AddComponent<TextC>(AssetRef{AssetManager::LoadAsset("font.meta")}, std::to_string(0) + " fps", 0.06f);
entityText.AddComponent<TransformC>(glm::vec2{-aspect * 10.0f + 0.1f, 9.4f}, glm::vec2{1.0});
entityText.AddComponent<TextC>(AssetRef{AssetManager::LoadAsset("font.meta")}, std::to_string(0) + " fps", 0.6f);
entityText.AddComponent<FrameCountC>();
Entity entityCamera = Entity::Create(ecs.get());
entityCamera.AddComponent<CameraC>(BoundingBox(-aspect, -1.0f, aspect, 1.0f), false);
entityCamera.AddComponent<TransformC>(glm::vec2{0.0f}, glm::vec2{4.0f});
Entity entityPlayer = Entity::Create(ecs.get());
entityPlayer.AddComponent<PlayerC>(entityCamera);
entityPlayer.AddComponent<PhysicsC>(0.1f, glm::vec2{0.0f, 0.0f}, glm::vec2{0.0f, 0.0f});
entityPlayer.AddComponent<TransformC>(glm::vec2{0.0f}, glm::vec2{1.0f});
entityPlayer.AddComponent<TextureC>(AssetRef{AssetManager::LoadAsset("fox2.meta")}, glm::vec2{0.0f, 0.0f}, glm::vec2{1.0f, 1.0f});
}
void Scene::Update()
+3 -2
View File
@@ -15,8 +15,9 @@ namespace Copium
std::unique_ptr<Renderer> renderer;
std::unique_ptr<ECSManager> ecs;
std::unique_ptr<DescriptorSet> descriptorSetRenderer;
int frameCounter = 0;
int fps = 0;
glm::mat4 projectionMatrix;
glm::mat4 viewMatrix;
glm::mat4 invPvMatrix;
public:
Scene(CommandBuffer& commandBuffer, DescriptorPool& descriptorPool);
void Update();
+34 -9
View File
@@ -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)
+89
View File
@@ -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));
}
}
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <vector>
#include "copium/event/InputCode.h"
#include <glm/glm.hpp>
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();
};
}
+124
View File
@@ -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
@@ -0,0 +1,18 @@
#pragma once
#include "copium/ecs/System.h"
#include "copium/example/Components.h"
namespace Copium
{
class CameraFollowPlayerSystem : public System<PlayerC, TransformC>
{
public:
void RunEntity(Entity entity, PlayerC& player, TransformC& transform) override
{
TransformC& cameraTransform = player.camera.GetComponent<TransformC>();
glm::vec2 wantedPos = transform.position + transform.size * 0.5f;
cameraTransform.position -= (cameraTransform.position - wantedPos) * 0.10f;
}
};
}
@@ -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 <glm/gtc/matrix_transform.hpp>
namespace Copium
{
class CameraUpdateSystem : public System<CameraC, TransformC>
{
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<const EventSignal&>(signal);
switch (eventSignal.GetEvent().GetType())
{
case EventType::WindowResize:
{
const WindowResizeEvent& windowResizeEvent = static_cast<const WindowResizeEvent&>(eventSignal.GetEvent());
float aspect = windowResizeEvent.GetWidth() / (float)windowResizeEvent.GetHeight();
camera.projection.r = aspect;
camera.projection.l = -aspect;
break;
}
}
}
};
}
@@ -1,6 +1,7 @@
#pragma once
#include "copium/asset/AssetRef.h"
#include "copium/ecs/Entity.h"
#include <string>
@@ -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;
};
}
@@ -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 <glm/gtc/matrix_transform.hpp>
@@ -13,21 +9,17 @@ namespace Copium
{
class MouseFollowSystem : public System<MouseFollowC, TransformC>
{
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<const EventSignal&>(signal);
if (eventSignal.GetEvent().GetType() == EventType::MouseMove)
{
const MouseMoveEvent& mouseMoveEvent = static_cast<const MouseMoveEvent&>(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};
}
};
}
@@ -0,0 +1,20 @@
#pragma once
#include "copium/ecs/System.h"
#include "copium/example/Components.h"
namespace Copium
{
class PhysicsSystem : public System<PhysicsC, TransformC>
{
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};
}
};
}
@@ -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<PlayerC, PhysicsC>
{
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;
}
};
}
@@ -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);
+3
View File
@@ -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();