Implemented SDL event listener class

This commit is contained in:
Hyper 2024-11-09 20:28:03 +00:00
parent 05e09ba7e2
commit 33b6b09e5d
6 changed files with 68 additions and 1 deletions

View file

@ -59,6 +59,7 @@ set(SWA_HID_CXX_SOURCES
)
set(SWA_PATCHES_CXX_SOURCES
"patches/ui/frontend_listener.cpp"
"patches/fps_patches.cpp"
"patches/misc_patches.cpp"
"patches/object_patches.cpp"

View file

@ -0,0 +1,3 @@
#include "frontend_listener.h"
FrontendListener m_frontendListener;

View file

@ -0,0 +1,24 @@
#pragma once
#include "ui/window_listener.h"
class FrontendListener : public WindowListener
{
private:
bool m_isF8KeyDown = false;
public:
void OnKeyDown(SDL_Keycode key) override
{
if (key == SDLK_F8 && !m_isF8KeyDown)
{
printf("F8 pressed!\n");
m_isF8KeyDown = true;
}
}
void OnKeyUp(SDL_Keycode key) override
{
m_isF8KeyDown = key != SDLK_F8;
}
};

View file

@ -1,4 +1,5 @@
#include "window.h"
#include "window_listener.h"
#include <config.h>
#include <kernel/function.h>
#include <SDL_syswm.h>
@ -56,6 +57,9 @@ int Window_OnSDLEvent(void*, SDL_Event* event)
}
}
for (auto listener : Window::s_listeners)
listener->OnKeyDown(event->key.keysym.sym);
break;
}
@ -68,6 +72,9 @@ int Window_OnSDLEvent(void*, SDL_Event* event)
m_isFullscreenKeyReleased = true;
break;
}
for (auto listener : Window::s_listeners)
listener->OnKeyUp(event->key.keysym.sym);
}
case SDL_WINDOWEVENT:
@ -107,6 +114,9 @@ int Window_OnSDLEvent(void*, SDL_Event* event)
}
}
for (auto listener : Window::s_listeners)
listener->OnSDLEvent(event);
return 0;
}

View file

@ -7,7 +7,9 @@
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720
struct Window
class WindowListener;
class Window
{
public:
inline static SDL_Window* s_pWindow;
@ -20,6 +22,8 @@ public:
inline static bool s_isFocused;
inline static std::vector<std::shared_ptr<WindowListener>> s_listeners;
static SDL_Surface* GetIconSurface(void* pIconBmp = nullptr, size_t iconSize = 0)
{
auto rw = SDL_RWFromMem(pIconBmp ? pIconBmp : (void*)g_icon, pIconBmp ? iconSize : g_icon_size);

View file

@ -0,0 +1,25 @@
#pragma once
#include "ui/window.h"
class IWindowListener
{
public:
virtual ~IWindowListener() = default;
virtual void OnSDLEvent(SDL_Event* event) = 0;
virtual void OnKeyDown(SDL_Keycode key) = 0;
virtual void OnKeyUp(SDL_Keycode key) = 0;
};
class WindowListener : public IWindowListener
{
public:
WindowListener()
{
Window::s_listeners.emplace_back(this);
}
void OnSDLEvent(SDL_Event* event) override {}
void OnKeyDown(SDL_Keycode key) override {}
void OnKeyUp(SDL_Keycode key) override {}
};