Moved to a dynamic id system and increased id size to uint32_t

This commit is contained in:
KiritoDv 2024-12-29 00:46:19 -06:00 committed by Lywx
parent 4d28f402e5
commit 8462aee14e
8 changed files with 106 additions and 47 deletions

View File

@ -1782,7 +1782,7 @@ void Display_Update(void) {
#endif #endif
// @port: @event: Call DisplayPreUpdateEvent // @port: @event: Call DisplayPreUpdateEvent
EventSystem_CallEvent(DisplayPreUpdateEvent, NULL); CALL_EVENT(DisplayPreUpdateEvent);
Matrix_Push(&gGfxMatrix); Matrix_Push(&gGfxMatrix);
if ((gCurrentLevel == LEVEL_AQUAS) && (gPlayer[0].state == PLAYERSTATE_ACTIVE)) { if ((gCurrentLevel == LEVEL_AQUAS) && (gPlayer[0].state == PLAYERSTATE_ACTIVE)) {
@ -2016,5 +2016,5 @@ void Display_Update(void) {
Matrix_Pop(&gGfxMatrix); Matrix_Pop(&gGfxMatrix);
// @port: @event: Call DisplayPostUpdateEvent // @port: @event: Call DisplayPostUpdateEvent
EventSystem_CallEvent(DisplayPostUpdateEvent, NULL); CALL_EVENT(DisplayPostUpdateEvent);
} }

View File

@ -348,7 +348,7 @@ void Game_Update(void) {
u8 soundMode; u8 soundMode;
// @port: @event: Call GamePreUpdateEvent // @port: @event: Call GamePreUpdateEvent
EventSystem_CallEvent(GamePreUpdateEvent, NULL); CALL_EVENT(GamePreUpdateEvent);
Game_SetGameState(); Game_SetGameState();
if (gGameStandby) { if (gGameStandby) {
@ -606,7 +606,7 @@ void Game_Update(void) {
Audio_dummy_80016A50(); Audio_dummy_80016A50();
// @port: @event: Call GamePostUpdateEvent // @port: @event: Call GamePostUpdateEvent
EventSystem_CallEvent(GamePostUpdateEvent, NULL); CALL_EVENT(GamePostUpdateEvent);
} }
} }

View File

@ -2,6 +2,7 @@
#include "fox_hud.h" #include "fox_hud.h"
#include "prevent_bss_reordering.h" #include "prevent_bss_reordering.h"
#include "port/interpolation/FrameInterpolation.h" #include "port/interpolation/FrameInterpolation.h"
#include "port/hooks/Events.h"
Vec3f D_801616A0; Vec3f D_801616A0;
Vec3f D_801616B0; Vec3f D_801616B0;
@ -3612,27 +3613,40 @@ void HUD_VS_Radar(void) {
} }
void HUD_SinglePlayer(void) { void HUD_SinglePlayer(void) {
if (gPlayState != PLAY_PAUSE) { CALL_CANCELLABLE_EVENT(DrawRadarHUDEvent){
HUD_Radar(); if (gPlayState != PLAY_PAUSE) {
HUD_Radar();
}
} }
RCP_SetupDL_36(); RCP_SetupDL_36();
if ((gLevelMode != LEVELMODE_TURRET) && (D_hud_80161708 != 0)) { if ((gLevelMode != LEVELMODE_TURRET) && (D_hud_80161708 != 0)) {
HUD_BoostGauge_Draw(246.0f, 28.0f); CALL_CANCELLABLE_EVENT(DrawBoostGaugeHUDEvent) {
HUD_BombCounter_Draw(250.0f, 38.0f); HUD_BoostGauge_Draw(246.0f, 28.0f);
} }
CALL_CANCELLABLE_EVENT(DrawBombCounterHUDEvent) {
HUD_IncomingMsg(); HUD_BombCounter_Draw(250.0f, 38.0f);
if (D_hud_80161708 != 0) {
HUD_Shield_GoldRings_Score(24.0f, 30.0f);
if (gCurrentLevel != LEVEL_TRAINING) {
HUD_LivesCount2_Draw(248.0f, 11.0f, gLifeCount[gPlayerNum]);
} }
} }
if (gCurrentLevel == LEVEL_TRAINING) { CALL_CANCELLABLE_EVENT(DrawIncomingMsgHUDEvent) {
Training_RingPassCount_Draw(); HUD_IncomingMsg();
}
if (D_hud_80161708 != 0) {
CALL_CANCELLABLE_EVENT(DrawGoldRingsHUDEvent) {
HUD_Shield_GoldRings_Score(24.0f, 30.0f);
}
CALL_CANCELLABLE_EVENT(DrawLivesCounterHUDEvent) {
if (gCurrentLevel != LEVEL_TRAINING) {
HUD_LivesCount2_Draw(248.0f, 11.0f, gLifeCount[gPlayerNum]);
}
}
}
CALL_CANCELLABLE_EVENT(DrawTrainingRingPassCountHUDEvent) {
if (gCurrentLevel == LEVEL_TRAINING) {
Training_RingPassCount_Draw();
}
} }
} }

View File

@ -4,8 +4,16 @@
EventSystem* EventSystem::Instance = new EventSystem(); EventSystem* EventSystem::Instance = new EventSystem();
EventID EventSystem::RegisterEvent() {
return this->mInternalEventID++;
}
ListenerID EventSystem::RegisterListener(EventID id, EventCallback callback, EventPriority priority) { ListenerID EventSystem::RegisterListener(EventID id, EventCallback callback, EventPriority priority) {
auto& listeners = this->mEventListeners[(uint8_t)((id >> 16) & 0xFF)][(uint16_t)(id & 0xFFFF)]; if(id == -1) {
throw std::runtime_error("Trying to register listener for unregistered event");
}
auto& listeners = this->mEventListeners[id];
if(std::find_if(listeners.begin(), listeners.end(), [callback](EventListener listener) { if(std::find_if(listeners.begin(), listeners.end(), [callback](EventListener listener) {
return listener.function == callback; return listener.function == callback;
@ -24,23 +32,23 @@ ListenerID EventSystem::RegisterListener(EventID id, EventCallback callback, Eve
} }
void EventSystem::UnregisterListener(EventID id, ListenerID listenerId) { void EventSystem::UnregisterListener(EventID id, ListenerID listenerId) {
auto& listeners = this->mEventListeners[(uint8_t)((id >> 16) & 0xFF)][(uint16_t)(id & 0xFFFF)]; auto& listeners = this->mEventListeners[id];
listeners.erase(listeners.begin() + listenerId); listeners.erase(listeners.begin() + listenerId);
} }
void EventSystem::CallEvent(EventID id, IEvent* event) { void EventSystem::CallEvent(EventID id, IEvent* event) {
auto& listeners = this->mEventListeners[(uint8_t)((id >> 16) & 0xFF)][(uint16_t)(id & 0xFFFF)]; auto& listeners = this->mEventListeners[id];
if (listeners.empty()) {
return;
}
for (auto& listener : listeners) { for (auto& listener : listeners) {
listener.function(event); listener.function(event);
} }
} }
extern "C" EventID EventSystem_RegisterEvent() {
return EventSystem::Instance->RegisterEvent();
}
extern "C" size_t EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority) { extern "C" size_t EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority) {
return EventSystem::Instance->RegisterListener(id, callback, priority); return EventSystem::Instance->RegisterListener(id, callback, priority);
} }

View File

@ -4,15 +4,9 @@
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
typedef uint16_t EventID; typedef uint32_t EventID;
typedef uint16_t NamespaceID;
typedef uint32_t ListenerID; typedef uint32_t ListenerID;
typedef enum {
EVENT_TYPE_PRE,
EVENT_TYPE_POST
} EventType;
typedef enum { typedef enum {
EVENT_PRIORITY_LOW, EVENT_PRIORITY_LOW,
EVENT_PRIORITY_NORMAL, EVENT_PRIORITY_NORMAL,
@ -30,18 +24,21 @@ typedef struct {
EventCallback function; EventCallback function;
} EventListener; } EventListener;
// Namespace ID Type #ifdef INIT_EVENT_IDS
// 00000000XXXXXXXX 000000000000000 0 #define DECLARE_EVENT(eventName) \
#define EVENT_ID(namespace_, id_, type_) ((((uint32_t)(namespace_) & 0xFFFF) << 16) | (((uint32_t)(id_) & 0x7FFF) << 1) | ((uint32_t)(type_) & 0x1)) uint32_t eventName##ID = -1;
#else
#define DECLARE_EVENT(eventName) \
extern uint32_t eventName##ID;
#endif
#define INTERNAL_EVENT_ID(id, type) EVENT_ID(0, id, type) #define DEFINE_EVENT(eventName, ...) \
#define DEFINE_EVENT(id, eventName, type, ...) \
typedef struct { \ typedef struct { \
IEvent event; \ IEvent event; \
__VA_ARGS__ \ __VA_ARGS__ \
} eventName; \ } eventName; \
\ \
static uint32_t eventName##ID = INTERNAL_EVENT_ID(id, type); DECLARE_EVENT(eventName)
#define CALL_EVENT(eventType, ...) \ #define CALL_EVENT(eventType, ...) \
eventType eventType##_ = { {false}, __VA_ARGS__ }; \ eventType eventType##_ = { {false}, __VA_ARGS__ }; \
@ -52,6 +49,12 @@ typedef struct {
EventSystem_CallEvent(eventType##ID, &eventType##_); \ EventSystem_CallEvent(eventType##ID, &eventType##_); \
if (!eventType##_.event.cancelled) if (!eventType##_.event.cancelled)
#define REGISTER_EVENT(eventType) \
eventType##ID = EventSystem_RegisterEvent();
#define REGISTER_LISTENER(eventType, callback, priority) \
EventSystem_RegisterListener(eventType##ID, callback, priority);
#ifdef __cplusplus #ifdef __cplusplus
#include <array> #include <array>
#include <vector> #include <vector>
@ -60,13 +63,16 @@ typedef struct {
class EventSystem { class EventSystem {
public: public:
static EventSystem* Instance; static EventSystem* Instance;
EventID RegisterEvent();
ListenerID RegisterListener(EventID id, EventCallback callback, EventPriority priority = EVENT_PRIORITY_NORMAL); ListenerID RegisterListener(EventID id, EventCallback callback, EventPriority priority = EVENT_PRIORITY_NORMAL);
void UnregisterListener(EventID ev, ListenerID id); void UnregisterListener(EventID ev, ListenerID id);
void CallEvent(EventID id, IEvent* event); void CallEvent(EventID id, IEvent* event);
private: private:
std::unordered_map<NamespaceID, std::unordered_map<EventID, std::vector<EventListener>>> mEventListeners; std::unordered_map<EventID, std::vector<EventListener>> mEventListeners;
EventID mInternalEventID = 0;
}; };
#else #else
extern EventID EventSystem_RegisterEvent();
extern ListenerID EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority); extern ListenerID EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority);
extern void EventSystem_UnregisterListener(EventID ev, ListenerID id); extern void EventSystem_UnregisterListener(EventID ev, ListenerID id);
extern void EventSystem_CallEvent(EventID id, void* event); extern void EventSystem_CallEvent(EventID id, void* event);

View File

@ -2,8 +2,19 @@
#include "port/hooks/impl/EventSystem.h" #include "port/hooks/impl/EventSystem.h"
#define DisplayPreUpdateEvent INTERNAL_EVENT_ID(1, EVENT_TYPE_PRE) DEFINE_EVENT(DisplayPreUpdateEvent);
#define DisplayPostUpdateEvent INTERNAL_EVENT_ID(1, EVENT_TYPE_POST) DEFINE_EVENT(DisplayPostUpdateEvent);
#define GamePreUpdateEvent INTERNAL_EVENT_ID(2, EVENT_TYPE_PRE) DEFINE_EVENT(GamePreUpdateEvent);
#define GamePostUpdateEvent INTERNAL_EVENT_ID(2, EVENT_TYPE_POST) DEFINE_EVENT(GamePostUpdateEvent);
DEFINE_EVENT(DrawRadarHUDEvent);
DEFINE_EVENT(DrawBoostGaugeHUDEvent);
DEFINE_EVENT(DrawBombCounterHUDEvent);
DEFINE_EVENT(DrawIncomingMsgHUDEvent);
DEFINE_EVENT(DrawGoldRingsHUDEvent);
DEFINE_EVENT(DrawLivesCounterHUDEvent);
DEFINE_EVENT(DrawTrainingRingPassCountHUDEvent);
DEFINE_EVENT(DrawGlobalHUDPreEvent);
DEFINE_EVENT(DrawGlobalHUDPostEvent);

View File

@ -4,6 +4,6 @@
#include "sf64object.h" #include "sf64object.h"
#include "port/hooks/impl/EventSystem.h" #include "port/hooks/impl/EventSystem.h"
DEFINE_EVENT(3, ItemDropEvent, EVENT_TYPE_PRE, DEFINE_EVENT(ItemDropEvent,
Item* item; Item* item;
); );

View File

@ -1,9 +1,11 @@
#include "PortEnhancements.h" #include "PortEnhancements.h"
#include "port/hooks/Events.h"
#include "global.h" #include "global.h"
#include "hit64.h" #include "hit64.h"
#include "mods.h" #include "mods.h"
#define INIT_EVENT_IDS
#include "port/hooks/Events.h"
void OnDisplayUpdatePre(IEvent* event) { void OnDisplayUpdatePre(IEvent* event) {
#if DEBUG_BOSS_KILLER == 1 #if DEBUG_BOSS_KILLER == 1
KillBoss(); KillBoss();
@ -157,8 +159,26 @@ void OnGameUpdatePost(IEvent* event) {
void PortEnhancements_Init() { void PortEnhancements_Init() {
// Register event listeners // Register event listeners
EventSystem_RegisterListener(DisplayPreUpdateEvent, OnDisplayUpdatePre, EVENT_PRIORITY_NORMAL); REGISTER_EVENT(DisplayPreUpdateEvent);
EventSystem_RegisterListener(GamePostUpdateEvent, OnGameUpdatePost, EVENT_PRIORITY_NORMAL); REGISTER_EVENT(DisplayPostUpdateEvent);
REGISTER_EVENT(GamePreUpdateEvent);
REGISTER_EVENT(GamePostUpdateEvent);
REGISTER_EVENT(DrawRadarHUDEvent);
REGISTER_EVENT(DrawBoostGaugeHUDEvent);
REGISTER_EVENT(DrawBombCounterHUDEvent);
REGISTER_EVENT(DrawIncomingMsgHUDEvent);
REGISTER_EVENT(DrawGoldRingsHUDEvent);
REGISTER_EVENT(DrawLivesCounterHUDEvent);
REGISTER_EVENT(DrawTrainingRingPassCountHUDEvent);
REGISTER_EVENT(DrawGlobalHUDPreEvent);
REGISTER_EVENT(DrawGlobalHUDPostEvent);
REGISTER_EVENT(ItemDropEvent);
REGISTER_LISTENER(DisplayPreUpdateEvent, OnDisplayUpdatePre, EVENT_PRIORITY_NORMAL);
REGISTER_LISTENER(GamePostUpdateEvent, OnGameUpdatePost, EVENT_PRIORITY_NORMAL);
} }
void PortEnhancements_Exit() { void PortEnhancements_Exit() {