Skip to content

Commit

Permalink
added event system into this commit (#52)
Browse files Browse the repository at this point in the history
Added clear comments on specific aspects of Vulkan abstraction layers, modified header extensions to hpp, including adding in the event-system
  • Loading branch information
SpinnerX authored Oct 9, 2024
1 parent cf32304 commit 1a99c86
Show file tree
Hide file tree
Showing 42 changed files with 775 additions and 113 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ endif(LINUX)

if(APPLE)
find_package(Vulkan REQUIRED)
# find_package(VulkanHeaders REQUIRED)
endif(APPLE)

find_package(glm REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion Editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(Editor CXX)

set(
all_src
Editor/Editor.h
Editor/Editor.hpp
Editor/Editor.cpp
)

Expand Down
8 changes: 3 additions & 5 deletions Editor/Editor/Editor.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "Editor.h"
// #include <engine3d/Core/Event/InputPoll.h>
#include "engine3d/Core/EngineLogger.h"
// #include "engine3d/Core/Renderer/Renderer.h"
#include <engine3d/Core/Timestep.h>
#include "Editor.hpp"
#include <engine3d/Core/EngineLogger.hpp>
#include <engine3d/Core/Timestep.hpp>
namespace engine3d{

EditorApplication::EditorApplication(const std::string& p_DebugName) : ApplicationInstance(p_DebugName) {
Expand Down
2 changes: 1 addition & 1 deletion Editor/Editor/Editor.h → Editor/Editor/Editor.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <engine3d/Core/ApplicationInstance.h>
#include <engine3d/Core/ApplicationInstance.hpp>

namespace engine3d{

Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def build(self):

def package(self):
copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
copy(self, pattern="*.h", src=os.path.join(self.source_folder, "engine3d"), dst=os.path.join(self.package_folder, "engine3d"))
copy(self, pattern="*.hpp", src=os.path.join(self.source_folder, "engine3d"), dst=os.path.join(self.package_folder, "engine3d"))
copy(self, pattern="*.a", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
copy(self, pattern="*.so", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
copy(self, pattern="*.lib", src=self.build_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include <string>
#include <Core/Core.h>
#include "internal/API.h"
#include <Core/Window.h>
#include <Core/Core.hpp>
#include "internal/API.hpp"
#include <Core/Window.hpp>

namespace engine3d{
/**
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <memory>
#include <Core/Core.h>
#include <Core/Core.hpp>
#include <fmt/os.h>
#include <fmt/ostream.h>
#include <spdlog/spdlog.h>
Expand Down
104 changes: 104 additions & 0 deletions engine3d/Core/Event/Event.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#pragma once
// #include <Core/Core.hpp>
#include <Core/Core.hpp>
#include <string>

namespace engine3d{
enum class EventType{
None=0,
WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
KeyPressed, KeyReleased, KeyTyped,
MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled
};

// EventCategories
// May want to filter out certain events
// In other words,receiving all events from application into some kind of
// receiving events from my application into some event class.
// Only carying about the keyboard events
// Example Scenario - If I wanted to log in mouse events, and do we really
// want to check to see if the event type is pressed, released, or scrolled
// So, instead we can just say "give me all the mouse events"
// Hence why we have EventCategoryEnum
enum EventCategory{
None = 0,
EventCategoryApplication = bit(0),
EventCategoryInput = bit(1),
EventCategoryKeyboard = bit(2),
EventCategoryMouse = bit(3),
EventCategoryMouseButton = bit(4)
};

class ENGINE_API Event{
friend class EventDispatcher; //! @note EventDispatcher accessing Event private variables
public:

std::string toString() const{
return str();
}

//! @note Checking if the type of category we are searching have been set.
inline bool InCategory(EventCategory category) {
return GetCategoryFlags() & category;
}

inline bool IsEventHandled() const { return isEventHandled; }

bool operator|=(bool value) {
isEventHandled |= value;
return isEventHandled;
}

private:
//! @note Type of Events that are going to be handled
//! @note Name of Event being handled
virtual EventType GetEventType() const = 0;
// virtual const char* GetName() const = 0;
virtual int GetCategoryFlags() const = 0;

//! @note Will be overrided by different types of Event
//! @note Returns the name as a string (or any other information related to those events)
virtual std::string str() const{ return "Event Default str()"; }

private:
bool isEventHandled = false;
};

class EventDispatcher{
public:
EventDispatcher(Event& e) : event(e) {}

//! @note Instead of passing in std::function<bool(T&)>
//! @note Lets just assume the type being passed in is going to be a lambda
/**@note Used like
EventDispatcher<WindowResizedEvent>([](WindowResizedEvent& event){
});
void onWindowResize(WindowResizedEvent& event){}
EventDispatcher.Dispatch<WindowResizedEvent>(onWindowResized);
*/

//! @param T is the type of event we accept
//! @param EventFunction is the function that we are submitting to our event dispatcher
//! @example dispatcher.Dispatch<MousePressedEvent>([](){});
//! @example dispatcher.Dispatch<KeyPressedEvent>(bind(this, &Instance::onKeyPressed))
//! @note Using bind(...) to bind our member function to dispatch based on that given event type
template<typename T, typename EventFunction>
bool Dispatch(EventFunction func){
//! @note Assuming that the type of given by user is an event
//! @note TODO - We should have this function know whether to infer function is a kind of event or not.
if(event.GetEventType() == T::GetStaticType()){
event.isEventHandled = func(*(T *)&event);
return true;
}
return false;
}

private:
Event& event;
};

};
22 changes: 22 additions & 0 deletions engine3d/Core/Event/InputPoll.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <Event/KeyCodes.hpp>
#include <Event/MouseCodes.hpp>
#include <glm/glm.hpp>

namespace engine3d{
class InputPoll{
public:

//! @note Key/Mouse event pressed!
static bool IsKeyPressed(KeyCode keycode);

static bool IsMousePressed(MouseCode mouseCode);

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

static float GetMouseX();

static float GetMouseY();
};
};
Loading

0 comments on commit 1a99c86

Please sign in to comment.