Digimon World 3 decomp
The European version, SLES_039.36
Loading...
Searching...
No Matches
registry.c
Go to the documentation of this file.
1#include "engine/game.h"
2
3/* Forgets every registered task */
4void clearTaskRegistry(void) {
5 s32 i;
6
7 for (i = TASK_REGISTRY_SIZE - 1; i >= 0; i--) {
9 }
10}
11
12/* Adds a task to the first free entry of the registry (ignored when it is full) */
13void registerTask(Task *task) {
14 s32 i;
15 Task **p;
16
17 for (i = 0, p = TASK_REGISTRY.tasks; i < TASK_REGISTRY_SIZE; i++, p++) {
18 if (*p == NULL) {
19 *p = task;
20 return;
21 }
22 }
23}
24
25/* Removes a task from the registry */
26void unregisterTask(Task *task) {
27 s32 i;
28 Task **p;
29
30 for (i = 0, p = TASK_REGISTRY.tasks; i < TASK_REGISTRY_SIZE; i++, p++) {
31 if (*p == task) {
32 *p = NULL;
33 return;
34 }
35 }
36}
37
38/* The next registered task that matches findTask's id and keys (-1 matches anything), or NULL */
39void *findNextTask(void) {
40 s32 i;
41 Task *e;
42
43 for (i = TASK_REGISTRY.findNext; i < TASK_REGISTRY_SIZE; i++) {
44 e = TASK_REGISTRY.tasks[i];
45 if (e != NULL && (TASK_REGISTRY.findId == -1 || e->id == TASK_REGISTRY.findId) &&
48 TASK_REGISTRY.findNext = i + 1;
49 return TASK_REGISTRY.tasks[i];
50 }
51 }
52 return NULL;
53}
54
55/* The first registered task that matches (-1 matches anything), or NULL */
56void *findTask(s32 id, s32 key1, s32 key2) {
61 return findNextTask();
62}
63
64/* The update runs with its stack in the scratchpad */
65#define SetSpadStack(addr) \
66 __asm__ volatile("move $8,%0\n\tsw $29,0($8)\n\taddiu $8,$8,-16\n\tmove $29,$8" : : "r"(addr) : "$8", "memory")
67#define ResetSpadStack() __asm__ volatile("addiu $29,$29,16\n\tlw $29,0($29)" : : : "memory")
68
69/* One frame of a task and its children; returns NULL once the task is gone */
71 s32 dying = task->state == TASK_KILL;
72
73 SetSpadStack(0x1F8003FC);
74 if (task->state == TASK_RUN && task->paused != 0) {
75 if (task->paused > 0) {
76 task->paused = -1;
77 }
78 } else {
79 task->update(task, task->children);
80 }
82 if (!dying) {
83 if (task->state != TASK_RUN || task->paused == 0) {
85 }
86 } else {
87 task->destroy(task);
88 task = NULL;
89 }
90 return task;
91}
92
93/* Runs a frame of each child of a task, dropping the ones that end */
94void runChildTasks(Task *task) {
95 s32 count = task->childCount;
96 Task **children = task->children;
97 s32 i;
98
99 for (i = 0; i < count; i++) {
100 if (children[i] != NULL) {
101 children[i] = executeTask(children[i]);
102 }
103 }
104}
105
106/* Runs a frame of a task and its children; returns it, or NULL once it is gone */
108 if (task != NULL) {
109 return executeTask(task);
110 }
111 return NULL;
112}
113
114/* Ends a task now: its last update and destroy run at once */
115void killTask(Task *task) {
116 if (task != NULL) {
117 task->setState(task, TASK_KILL);
118 TASK_REGISTRY.funcs.run(task);
119 }
120}
121
#define NULL
Definition common.h:17
signed int s32
Definition common.h:11
#define TASK_REGISTRY_SIZE
The tasks the registry holds at most.
Definition task.h:67
#define TASK_RUN
Definition task.h:35
#define TASK_KILL
Definition task.h:37
void clearTaskRegistry(void)
Forgets every registered task.
Definition registry.c:4
Task * runTask(Task *task)
Runs a frame of a task and its children; returns it, or NULL once it is gone.
Definition registry.c:107
#define SetSpadStack(addr)
The update runs with its stack in the scratchpad.
Definition registry.c:65
void unregisterTask(Task *task)
Removes a task from the registry.
Definition registry.c:26
void registerTask(Task *task)
Adds a task to the first free entry of the registry (ignored when it is full)
Definition registry.c:13
void * findNextTask(void)
The next registered task that matches findTask's id and keys (-1 matches anything),...
Definition registry.c:39
#define ResetSpadStack()
Definition registry.c:67
TaskRegistry TASK_REGISTRY
Definition registry.c:122
Task * executeTask(Task *task)
One frame of a task and its children; returns NULL once the task is gone.
Definition registry.c:70
void runChildTasks(Task *task)
Runs a frame of each child of a task, dropping the ones that end.
Definition registry.c:94
void killTask(Task *task)
Ends a task now: its last update and destroy run at once.
Definition registry.c:115
void * findTask(s32 id, s32 key1, s32 key2)
The first registered task that matches (-1 matches anything), or NULL.
Definition registry.c:56
struct Task *(* run)(struct Task *task)
Definition task.h:21
void(* runChildren)(struct Task *task)
Definition task.h:20
Tasks created with an id, so that other code can find them.
Definition task.h:70
s32 findKey2
Definition task.h:74
Task * tasks[100]
NULL for a free entry.
Definition task.h:71
TaskFuncs funcs
Definition task.h:76
s32 findId
-1 matches anything
Definition task.h:72
s32 findKey1
Definition task.h:73
s32 findNext
Definition task.h:75
Any task, seen through its header.
Definition task.h:62
s32 key1
Definition task.h:63
s32 key2
Definition task.h:63
void(* destroy)(struct Task *task)
Definition task.h:63
void(* setState)(struct Task *task, s32 state)
Definition task.h:63
s32 id
Definition task.h:63
s32 paused
Definition task.h:63
s32 childCount
Definition task.h:63
void(* update)()
Definition task.h:63
s32 state
Definition task.h:63
void * children
Definition task.h:63