Skip to content

Commit

Permalink
Merge pull request #3 from timmypidashev/main
Browse files Browse the repository at this point in the history
Merge latest work :)
  • Loading branch information
timmypidashev authored Nov 1, 2021
2 parents 9b2af97 + 9748bc7 commit c423148
Show file tree
Hide file tree
Showing 19 changed files with 661 additions and 9 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: gitsubmodule
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
labels:
- External (3rd party)
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "External/spdlog"]
path = External/spdlog
url = https://github.com/gabime/spdlog
[submodule "External/glad"]
path = External/glad
url = https://github.com/Dav1dde/glad
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

"cmake.configureSettings": {
"BUILD_EXAMPLES": "OFF"
},
}
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Set options
# ----------------------------------
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(BUILD_EXAMPLES "Build examples" OFF)

# Configure Header Files
Expand Down
32 changes: 30 additions & 2 deletions Core/Application/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
#define GLFW_INCLUDE_NONE

#include <Iridpch.h>

#include "Logger/Log.h"

#include "Application.h"

#include <glad/glad.h>

#include "GLFW/glfw3.h"

namespace Iridescent {

#define BIND_EVENT_FUNCTION(x) std::bind(&Application::x, this, std::placeholders::_1)

Application::Application()
{
m_Window = std::unique_ptr<Window>(Window::Create());
m_Window->SetEventCallback(BIND_EVENT_FUNCTION(OnEvent));
}


Application::~Application()
{
}

void Application::OnEvent(Event& e)
{
EventDispatcher dispatcher(e);
dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FUNCTION(OnWindowClose));

IRID_CORE_TRACE("{0}", e);
}

void Application::Run()
{
while (true);
while (m_Running)
{
m_Window->OnUpdate();
}
}

bool Application::OnWindowClose(WindowCloseEvent& e)
{
m_Running = false;
return true;
}

}
11 changes: 11 additions & 0 deletions Core/Application/Application.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include "Exports.h"
#include "Events/Event.h"
#include "Events/ApplicationEvents.h"
#include "Window.h"

namespace Iridescent {

Expand All @@ -11,6 +14,14 @@ namespace Iridescent {
virtual ~Application();

void Run();

void OnEvent(Event& e);

private:
bool OnWindowClose(WindowCloseEvent& e);
std::unique_ptr<Window> m_Window;
bool m_Running = true;

};

// To be defined in CLIENT
Expand Down
2 changes: 2 additions & 0 deletions Core/Application/EntryPoint.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "config.h"

#ifdef IRID_EXPORT

extern Iridescent::Application* Iridescent::CreateApplication();
Expand Down
22 changes: 17 additions & 5 deletions Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ set(SOURCE_FILES
# Logger sources
Logger/Log.cpp
Logger/Log.h


# Events sources
Events/ApplicationEvents.h
Events/Event.h
Events/KeyEvent.h
Events/MouseEvent.h

# Window
Window/Windows.cpp
Window/Windows.h
Window.h
)

# Add library sources
Expand All @@ -34,12 +44,14 @@ target_precompile_headers(${PROJECT_NAME} PRIVATE Iridpch.h)

# Set Library Includes
# ----------------------------------
target_include_directories(${PROJECT_NAME}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

# Set target link libraries
# ----------------------------------
target_link_libraries(${PROJECT_NAME}
PUBLIC spdlog::spdlog
target_link_libraries(${PROJECT_NAME} PUBLIC
spdlog::spdlog
glfw
glad
)
66 changes: 66 additions & 0 deletions Core/Events/ApplicationEvents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include "Event.h"

#include <sstream>

namespace Iridescent {

class IRID_EXPORT WindowResizeEvent : public Event
{
public:
WindowResizeEvent(unsigned int width, unsigned int height)
: m_Width(width), m_Height(height) {}

inline unsigned int GetWidth() const { return m_Width; }
inline unsigned int GetHeight() const { return m_Height; }

std::string ToString() const override
{
std::stringstream ss;
ss << "WindowResizeEvent: " << m_Width << ", " << m_Height;
return ss.str();
}

EVENT_CLASS_TYPE(WindowResize)
EVENT_CLASS_CATEGORY(EventCategoryApplication)
private:
unsigned int m_Width, m_Height;
};

class IRID_EXPORT WindowCloseEvent : public Event
{
public:
WindowCloseEvent() {}

EVENT_CLASS_TYPE(WindowClose)
EVENT_CLASS_CATEGORY(EventCategoryApplication)
};

class IRID_EXPORT AppTickEvent : public Event
{
public:
AppTickEvent() {}

EVENT_CLASS_TYPE(AppTick)
EVENT_CLASS_CATEGORY(EventCategoryApplication)
};

class IRID_EXPORT AppUpdateEvent : public Event
{
public:
AppUpdateEvent() {}

EVENT_CLASS_TYPE(AppUpdate)
EVENT_CLASS_CATEGORY(EventCategoryApplication)
};

class IRID_EXPORT AppRenderEvent : public Event
{
public:
AppRenderEvent() {}

EVENT_CLASS_TYPE(AppRender)
EVENT_CLASS_CATEGORY(EventCategoryApplication)
};
}
85 changes: 85 additions & 0 deletions Core/Events/Event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

#include "Exports.h"

#include <string>
#include <functional>

namespace Iridescent {

// Events in Hazel are currently blocking, meaning when an event occurs it
// immediately gets dispatched and must be dealt with right then an there.
// For the future, a better strategy might be to buffer events in an event
// bus and process them during the "event" part of the update stage.

enum class EventType
{
None = 0,
WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
AppTick, AppUpdate, AppRender,
KeyPressed, KeyReleased,
MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled
};

enum EventCategory
{
None = 0,
EventCategoryApplication = BIT(0),
EventCategoryInput = BIT(1),
EventCategoryKeyboard = BIT(2),
EventCategoryMouse = BIT(3),
EventCategoryMouseButton = BIT(4)
};

#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::##type; }\
virtual EventType GetEventType() const override { return GetStaticType(); }\
virtual const char* GetName() const override { return #type; }

#define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; }

class IRID_EXPORT Event
{
friend class EventDispatcher;
public:
virtual EventType GetEventType() const = 0;
virtual const char* GetName() const = 0;
virtual int GetCategoryFlags() const = 0;
virtual std::string ToString() const { return GetName(); }

inline bool IsInCategory(EventCategory category)
{
return GetCategoryFlags() & category;
}
protected:
bool m_Handled = false;
};

class EventDispatcher
{
template<typename T>
using EventFn = std::function<bool(T&)>;
public:
EventDispatcher(Event& event)
: m_Event(event)
{
}

template<typename T>
bool Dispatch(EventFn<T> func)
{
if (m_Event.GetEventType() == T::GetStaticType())
{
m_Event.m_Handled = func(*(T*)&m_Event);
return true;
}
return false;
}
private:
Event& m_Event;
};

inline std::ostream& operator<<(std::ostream& os, const Event& e)
{
return os << e.ToString();
}
}
57 changes: 57 additions & 0 deletions Core/Events/KeyEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "Event.h"

#include <sstream>

namespace Iridescent {

class IRID_EXPORT KeyEvent : public Event
{
public:
inline int GetKeyCode() const { return m_KeyCode; }

EVENT_CLASS_CATEGORY(EventCategoryKeyboard | EventCategoryInput)
protected:
KeyEvent(int keycode)
: m_KeyCode(keycode) {}

int m_KeyCode;
};

class IRID_EXPORT KeyPressedEvent : public KeyEvent
{
public:
KeyPressedEvent(int keycode, int repeatCount)
: KeyEvent(keycode), m_RepeatCount(repeatCount) {}

inline int GetRepeatCount() const { return m_RepeatCount; }

std::string ToString() const override
{
std::stringstream ss;
ss << "KeyPressedEvent: " << m_KeyCode << " (" << m_RepeatCount << " repeats)";
return ss.str();
}

EVENT_CLASS_TYPE(KeyPressed)
private:
int m_RepeatCount;
};

class IRID_EXPORT KeyReleasedEvent : public KeyEvent
{
public:
KeyReleasedEvent(int keycode)
: KeyEvent(keycode) {}

std::string ToString() const override
{
std::stringstream ss;
ss << "KeyReleasedEvent: " << m_KeyCode;
return ss.str();
}

EVENT_CLASS_TYPE(KeyReleased)
};
}
Loading

0 comments on commit c423148

Please sign in to comment.