Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xTimer: port stuff from el rato #343

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/SB/Core/x/xTimer.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@
#include "xTimer.h"
#include "xMath.h"

#include <types.h>

static U32 sPauseTimerHash[] =
{
0xBC345600, 0xBC345609,
0xBC345683, 0xBC34568C,
0xBC345706, 0xBC34570F,
0xBC345789, 0xBC345792,
0xBC34580C, 0xBC345815,
0xBC34588F, 0xBC345898,
0xBC345912, 0xBC34591B,
0xBC345995, 0xBC34599E,
0xBC345A18, 0xBC345A21,
0xBC345A9B, 0xBC345AA4,
};

F32 GetRandomizedTime(xTimerAsset* tasset)
{
U32 halfRangeMilli = 1000.0f * tasset->randomRange;
if (halfRangeMilli == 0) {
return tasset->seconds;
}

S32 offset = xrand() % (halfRangeMilli * 2) - halfRangeMilli;
F32 time = tasset->seconds + offset / 1000.0f;
return time;
}

void xTimerInit(void* b, void* tasset)
{
xTimerInit((xBase*)b, (xTimerAsset*)tasset);
}

S32 xTimer_ObjIDIsPauseTimer(U32 id)
{
if (id == 0xCB3F6340) return TRUE;
if (id >= 0x016FC9F0 && id <= 0x016FC9F9) return TRUE;

S32 foo = (id >= 0xBC345600);
S32 bar = (id <= 0xBC345AA4);
if (foo && bar) {
for (S32 i = 0; i < 10; i++) {
if (id >= sPauseTimerHash[i*2] && id <= sPauseTimerHash[i*2+1]) {
return TRUE;
}
}
}

return FALSE;
}

void xTimerInit(xBase* b, xTimerAsset* tasset)
{
xBaseInit(b, tasset);

xTimer* t = (xTimer*)b;

t->eventFunc = xTimerEventCB;
t->tasset = tasset;

if (t->linkCount) {
t->link = (xLinkAsset*)((U8*)t->tasset + sizeof(xTimerAsset));
} else {
t->link = NULL;
}

t->state = 0;
t->secondsLeft = GetRandomizedTime(tasset);
t->runsInPause = xTimer_ObjIDIsPauseTimer(b->id);
t->flags = 0;
}

void xTimerReset(xTimer* ent)
{
xBaseReset(ent, ent->tasset);
ent->state = 0;
ent->secondsLeft = GetRandomizedTime(ent->tasset);
ent->flags = 0;
}
3 changes: 3 additions & 0 deletions src/SB/Core/x/xTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ struct xTimer : xBase
struct xScene;

void xTimerInit(void* b, void* tasset);
void xTimerInit(xBase* b, xTimerAsset* tasset);
void xTimerReset(xTimer* ent);
void xTimerSave(xTimer* ent, xSerial* s);
void xTimerLoad(xTimer* ent, xSerial* s);
S32 xTimerEventCB(xBase*, xBase* to, U32 toEvent, const F32* toParam, xBase*);
void xTimerUpdate(xBase* to, xScene*, F32 dt);

#endif