-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfetti_util.c
181 lines (149 loc) · 4.57 KB
/
confetti_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "global.h"
#include "confetti_util.h"
#include "malloc.h"
#include "main.h"
#include "digit_obj_util.h"
static EWRAM_DATA struct
{
u8 count;
struct ConfettiUtil *array;
} *sWork = NULL;
bool32 ConfettiUtil_Init(u8 count)
{
u8 i = 0;
if (count == 0)
return FALSE;
if (count > 64)
count = 64;
sWork = AllocZeroed(sizeof(*sWork));
if (sWork == NULL)
return FALSE;
sWork->array = AllocZeroed(count * sizeof(struct ConfettiUtil));
if (sWork->array == NULL)
{
FREE_AND_SET_NULL(sWork);
return FALSE;
}
sWork->count = count;
for (i = 0; i < count; i++)
{
memcpy(&sWork->array[i].oam, &gDummyOamData, sizeof(struct OamData));
sWork->array[i].dummied = TRUE;
}
return TRUE;
}
bool32 ConfettiUtil_Free(void)
{
u8 i = 0;
if (sWork == NULL)
return FALSE;
for (i = 0; i < sWork->count; i++)
memcpy(&gMain.oamBuffer[i + 64], &gDummyOamData, sizeof(struct OamData));
memset(sWork->array, 0, sWork->count * sizeof(struct ConfettiUtil));
FREE_AND_SET_NULL(sWork->array);
memset(sWork, 0, sizeof(*sWork));
FREE_AND_SET_NULL(sWork);
return TRUE;
}
bool32 ConfettiUtil_Update(void)
{
u8 i = 0;
if (sWork == NULL || sWork->array == NULL)
return FALSE;
for (i = 0; i < sWork->count; i++)
{
if (sWork->array[i].active && sWork->array[i].allowUpdates)
{
if (sWork->array[i].callback != NULL)
sWork->array[i].callback(&sWork->array[i]);
if (sWork->array[i].dummied)
{
memcpy(&gMain.oamBuffer[i + 64], &gDummyOamData, sizeof(struct OamData));
}
else
{
sWork->array[i].oam.y = sWork->array[i].y + sWork->array[i].yDelta;
sWork->array[i].oam.x = sWork->array[i].x + sWork->array[i].xDelta;
sWork->array[i].oam.priority = sWork->array[i].priority;
sWork->array[i].oam.tileNum = sWork->array[i].tileNum;
memcpy(&gMain.oamBuffer[i + 64], &sWork->array[i], sizeof(struct OamData));
}
}
}
return TRUE;
}
static bool32 SetAnimAndTileNum(struct ConfettiUtil *structPtr, u8 animNum)
{
u16 tileStart;
if (structPtr == NULL)
return FALSE;
tileStart = GetSpriteTileStartByTag(structPtr->tileTag);
if (tileStart == 0xFFFF)
return FALSE;
structPtr->animNum = animNum;
structPtr->tileNum = (GetTilesPerImage(structPtr->oam.shape, structPtr->oam.size) * animNum) + tileStart;
return TRUE;
}
u8 ConfettiUtil_SetCallback(u8 id, void (*func)(struct ConfettiUtil *))
{
if (sWork == NULL || id >= sWork->count)
return 0xFF;
else if (!sWork->array[id].active)
return 0xFF;
sWork->array[id].callback = func;
return id;
}
u8 ConfettiUtil_SetData(u8 id, u8 dataArrayId, s16 dataValue)
{
if (sWork == NULL || id >= sWork->count)
return 0xFF;
else if (!sWork->array[id].active || dataArrayId > ARRAY_COUNT(sWork->array[id].data) - 1) // - 1 b/c last slot is reserved for taskId
return 0xFF;
sWork->array[id].data[dataArrayId] = dataValue;
return id;
}
u8 ConfettiUtil_AddNew(const struct OamData *oam, u16 tileTag, u16 palTag, s16 x, s16 y, u8 animNum, u8 priority)
{
struct ConfettiUtil *structPtr = NULL;
u8 i;
if (sWork == NULL || oam == NULL)
return 0xFF;
for (i = 0; i < sWork->count; i++)
{
if (!sWork->array[i].active)
{
structPtr = &sWork->array[i];
memset(structPtr, 0, sizeof(*structPtr));
structPtr->id = i;
structPtr->active = TRUE;
structPtr->allowUpdates = TRUE;
break;
}
}
if (structPtr == NULL)
return 0xFF;
memcpy(&structPtr->oam, oam, sizeof(*oam));
structPtr->tileTag = tileTag;
structPtr->palTag = palTag;
structPtr->x = x;
structPtr->y = y;
structPtr->oam.paletteNum = IndexOfSpritePaletteTag(palTag);
if (priority < 4)
{
structPtr->priority = priority;
structPtr->oam.priority = priority;
}
SetAnimAndTileNum(structPtr, animNum);
return structPtr->id;
}
u8 ConfettiUtil_Remove(u8 id)
{
if (sWork == NULL || !sWork->array[id].active)
return 0xFF;
memset(&sWork->array[id], 0, sizeof(struct ConfettiUtil));
sWork->array[id].oam.y = DISPLAY_HEIGHT;
sWork->array[id].oam.x = DISPLAY_WIDTH;
sWork->array[id].dummied = TRUE;
memcpy(&gMain.oamBuffer[id + 64], &gDummyOamData, sizeof(struct OamData));
return id;
}