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
+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)