Skip to content

Commit

Permalink
Bug fixes with event system (#53)
Browse files Browse the repository at this point in the history
I made a few changes to the event system to ensure they work with our current windowing system.

Added more comments, especially to the event system. While making a bit few changes to the Windows API by removing one of the update functions and replacing that with the `InputPoll::UpdateEvents()` function instead to ensure all events being handled are successfully updated
  • Loading branch information
SpinnerX authored Oct 9, 2024
1 parent 1a99c86 commit d4b78b0
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Editor/Editor/Editor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "Editor.hpp"
#include "Core/Event/InputPoll.hpp"
#include "Core/Event/KeyCodes.hpp"
#include <engine3d/Core/EngineLogger.hpp>
#include <engine3d/Core/Timestep.hpp>
namespace engine3d{
Expand Down
1 change: 0 additions & 1 deletion engine3d/Core/Event/Event.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
// #include <Core/Core.hpp>
#include <Core/Core.hpp>
#include <string>

Expand Down
15 changes: 12 additions & 3 deletions engine3d/Core/Event/InputPoll.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#pragma once
#include <Event/KeyCodes.hpp>
#include <Event/MouseCodes.hpp>
#include <Core/Event/KeyCodes.hpp>
#include <Core/Event/MouseCodes.hpp>
#include <glm/glm.hpp>

namespace engine3d{
/**
* @name InputPoll.hpp
* @note Actual input polling system to poll in differeny sets of key/mouse actions
* @param UpdateEvents handles making sure that all of our events we handle have been successfully updated.
* @param GetMousePos just returns the position of our mouse cursor
*/
class InputPoll{
public:

Expand All @@ -13,10 +19,13 @@ namespace engine3d{
static bool IsMousePressed(MouseCode mouseCode);

//! @note Mouse Position
static glm::vec2 GetMousePos();
static glm::vec2 GetMousePosition();

static float GetMouseX();

static float GetMouseY();

//! @note Assuring that our events are correctly being updated
static void UpdateEvents();
};
};
4 changes: 4 additions & 0 deletions engine3d/Core/Event/KeyEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

namespace engine3d{

/**
* @name KeyEvent.hpp
* @note Defines different key events and how different keycodes define the action corresponding to those keys
*/
class ENGINE_API KeyEvent : public Event{
public:

Expand Down
4 changes: 4 additions & 0 deletions engine3d/Core/Event/MouseEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <sstream>

namespace engine3d{
/**
* @name MouseEvent.hpp
* @note defines our mouse movement and being to control when this specific event occurs
*/
class ENGINE_API MouseMovedEvent : public Event{
public:
MouseMovedEvent(float x, float y) : mouseX(x), mouseY(y) {};
Expand Down
18 changes: 12 additions & 6 deletions engine3d/Core/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@

class GLFWwindow;
namespace engine3d{
/**
* @name
* @note Window-agnostic implementation
* @note Having a window abstracted interface for having users define their own windows.
* @param GetVkSurface returns the current vulkan surface of the currently active window
* @param GetNativeWindow returns a pointer to the actual glfw window
* @param IsActive returns a boolean that is used for checking our window is active before use.
* @param OnUpdateAllFrames is used for displaying in the last stages of the swapchain of images ready for to be on the actual screen.
*/
class Window{
public:
// Window(uint32_t p_Width, uint32_t p_Height, const std::string& p_Title);
static Window* Create(uint32_t p_Width=900, uint32_t p_Height=600, const std::string& p_Title="Engine3D");

/**
Expand All @@ -26,11 +34,11 @@ namespace engine3d{
uint32_t GetHeight() const;
std::string GetTitle() const;

void OnUpdatePerTick();

void UpdateFrames();
void OnUpdateAllFrames();

protected:
//! @note These are all implementation details not exposed to the public API's
//! @note Internal implementations the developer of the window should know about.
virtual uint32_t Width() const = 0;
virtual uint32_t Height() const = 0;
virtual std::string Title() const = 0;
Expand All @@ -43,8 +51,6 @@ namespace engine3d{
virtual GLFWwindow* NativeWindow() = 0;

//! @note Update surface rendering every frame.
virtual void UpdatePerTick() = 0;

virtual void Presentation() = 0;
};
};
2 changes: 0 additions & 2 deletions engine3d/Core/internal/VulkanCpp/VulkanWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ namespace engine3d{
GLFWwindow* NativeWindow() override;

//! @note Update surface rendering every frame.
void UpdatePerTick() override;

void Presentation() override;

private:
Expand Down
12 changes: 12 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(ENGINE_INCLUDE_NAME ../engine3d)
set(ENGINE_INTERNAL_INCLUDE ${ENGINE_INCLUDE_NAME}/Core/internal)
set(ENGINE_SRC_DIR engine3d)
set(VULKAN_INCLUDE_DIR ${ENGINE_INCLUDE_NAME}/Core/internal/VulkanCpp)
set(VULKAN_SRC_DIR ${ENGINE_SRC_DIR}/Core/internal/VulkanCpp)
Expand All @@ -11,6 +12,15 @@ set(
${ENGINE_INCLUDE_NAME}/Core/EngineLogger.hpp
${ENGINE_INCLUDE_NAME}/Core/Window.hpp

# Event system includes
${ENGINE_INCLUDE_NAME}/Core/Event/KeyCodes.hpp
${ENGINE_INCLUDE_NAME}/Core/Event/MouseCodes.hpp
${ENGINE_INCLUDE_NAME}/Core/Event/Event.hpp
${ENGINE_INCLUDE_NAME}/Core/Event/InputPoll.hpp
${ENGINE_INCLUDE_NAME}/Core/Event/KeyEvent.hpp
${ENGINE_INCLUDE_NAME}/Core/Event/MouseEvent.hpp


${VULKAN_INCLUDE_DIR}/Vulkan.hpp
${VULKAN_INCLUDE_DIR}/VulkanWindow.hpp
${VULKAN_INCLUDE_DIR}/VulkanDevice.hpp
Expand All @@ -33,6 +43,8 @@ set(
${ENGINE_SRC_DIR}/Core/Window.cpp
${ENGINE_SRC_DIR}/Core/EngineLogger.cpp

${ENGINE_SRC_DIR}/Core/Event/InputPoll.cpp

${VULKAN_SRC_DIR}/Vulkan.cpp
${VULKAN_SRC_DIR}/VulkanWindow.cpp
${VULKAN_SRC_DIR}/VulkanDevice.cpp
Expand Down
7 changes: 6 additions & 1 deletion src/engine3d/Core/ApplicationInstance.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// #include <Core/internal/FrameTimer.hpp>
#include "Event/InputPoll.hpp"
#include <Core/ApplicationInstance.hpp>
#include <Core/Timestep.hpp>
// #include <Core/Renderer/Renderer.hpp>
Expand All @@ -24,7 +25,11 @@ namespace engine3d{
// FrameTimer::UpdateFrameTimer(); // give us the frames in flight.

// Renderer::Presentation();
m_Window->OnUpdatePerTick();

UpdateCurrentApplicationInstance();
m_Window->OnUpdateAllFrames();

InputPoll::UpdateEvents();
}

//! @note Cleaning up imgui
Expand Down
41 changes: 41 additions & 0 deletions src/engine3d/Core/Event/InputPoll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <Core/ApplicationInstance.hpp>
#include <Core/internal/VulkanCpp/Vulkan.hpp>
#include <GLFW/glfw3.h>
#include <Core/Event/InputPoll.hpp>


namespace engine3d{
bool InputPoll::IsKeyPressed(KeyCode keycode){
// auto window = ApplicationInstance::Get().GetCurrentWindow();
auto window = ApplicationInstance::GetWindow().GetNativeWindow();

auto state = glfwGetKey(window, static_cast<int32_t>(keycode));
return state == GLFW_PRESS || state == GLFW_REPEAT;
}

bool InputPoll::IsMousePressed(MouseCode mouseCode){
auto window = ApplicationInstance::GetWindow().GetNativeWindow();
auto state = glfwGetMouseButton(window, static_cast<int32_t>(mouseCode));
return state == GLFW_PRESS;
}

glm::vec2 InputPoll::GetMousePosition(){
auto window = ApplicationInstance::GetWindow().GetNativeWindow();
double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);

return {xPos, yPos};
}

float InputPoll::GetMouseX(){
return GetMousePosition().x;
}

float InputPoll::GetMouseY(){
return GetMousePosition().y;
}

void InputPoll::UpdateEvents(){
glfwPollEvents();
}
};
6 changes: 1 addition & 5 deletions src/engine3d/Core/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ namespace engine3d{
}


void Window::OnUpdatePerTick(){
UpdatePerTick();
}

void Window::UpdateFrames(){
void Window::OnUpdateAllFrames(){
Presentation();
}
};
4 changes: 0 additions & 4 deletions src/engine3d/Core/internal/VulkanCpp/VulkanWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ namespace engine3d::vk{
m_IsCurrentWindowActive = true;
}

void VulkanWindow::UpdatePerTick(){
glfwPollEvents();
}

void VulkanWindow::Presentation(){
// m_CurrentWindowSwapchain->Presentation();
}
Expand Down

0 comments on commit d4b78b0

Please sign in to comment.