-
Notifications
You must be signed in to change notification settings - Fork 45
/
core.c
380 lines (296 loc) · 9.62 KB
/
core.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/***************************************************************
core.c
The file contains Core minigame code, which handles stuff like
player, AI, and game loop information.
***************************************************************/
#include <libdragon.h>
#include "core.h"
#include "minigame.h"
#include "config.h"
#include "setup.h"
#include "menu.h"
#include "results.h"
#include "logo.h"
#include "savestate.h"
#include "title.h"
/*********************************
Structures
*********************************/
typedef struct {
joypad_port_t port;
} Player;
/*********************************
Globals
*********************************/
// Player info
static Player global_core_players[JOYPAD_PORT_COUNT];
static uint32_t global_core_playercount;
static AiDiff global_core_aidifficulty = AI_DIFFICULTY;
static PlyNum global_core_chooser;
// Minigame info
static bool global_core_playeriswinner[MAXPLAYERS];
// Core info
static double global_core_subtick = 0;
// Level info
static Level* global_core_curlevel;
static Level* global_core_nextlevel = NULL;
static Level global_core_alllevels[LEVELCOUNT];
// Game info
static NextRound global_nextroundtype = NR_LEAST;
/*==============================
core_get_subtick
Gets the current subtick. Use this to help smooth
movements in your draw loop
@return The current subtick
==============================*/
void core_set_subtick(double subtick)
{
global_core_subtick = subtick;
}
/*==============================
core_set_playercount
Sets the number of human players
@param The list of which controllers are enabled
==============================*/
void core_set_playercount(bool* enabledconts)
{
int plynum = 0;
// Attempt to find the first N left-most available controllers
for (int i=0; i<MAXPLAYERS; i++)
{
if (enabledconts[i])
{
global_core_players[plynum].port = i;
plynum++;
}
}
global_core_playercount = plynum;
}
/*==============================
core_get_playerconts
TODO
==============================*/
void core_get_playerconts(bool* enabledconts)
{
for (int i=0; i<MAXPLAYERS; i++)
enabledconts[i] = false;
for (int i=0; i<global_core_playercount; i++)
enabledconts[global_core_players[i].port] = true;
}
/*==============================
core_set_aidifficulty
Sets the AI difficulty
@param The AI difficulty
==============================*/
void core_set_aidifficulty(AiDiff difficulty)
{
global_core_aidifficulty = difficulty;
}
/*==============================
core_get_winner
Returns whether a player has won the last minigame.
@param The player to query
@return True if the player has won, false otherwise.
==============================*/
bool core_get_winner(PlyNum ply)
{
return global_core_playeriswinner[ply];
}
/*==============================
core_set_winner
Set the winner of the minigame. You can call this
multiple times to set multiple winners.
@param The winning player
==============================*/
void core_set_winner(PlyNum ply)
{
global_core_playeriswinner[ply] = true;
}
/*==============================
core_get_aidifficulty
Gets the current AI difficulty
@return The AI difficulty
==============================*/
AiDiff core_get_aidifficulty()
{
return global_core_aidifficulty;
}
/*==============================
core_get_subtick
Gets the subtick of the current frame
@return The frame's subtick
==============================*/
double core_get_subtick()
{
return global_core_subtick;
}
/*==============================
core_get_playercount
Get the number of human players
@return The number of players
==============================*/
uint32_t core_get_playercount()
{
return global_core_playercount;
}
/*==============================
core_get_playercontroller
Get the controller port of this player.
Because player 1's controller might not be plugged
into port number 1.
@param The player we want
@return The controller port
==============================*/
joypad_port_t core_get_playercontroller(PlyNum ply)
{
return global_core_players[ply].port;
}
/*==============================
core_reset_winners
Resets the winners
==============================*/
void core_reset_winners()
{
for (int i=0; i<MAXPLAYERS; i++)
global_core_playeriswinner[i] = false;
}
/*==============================
core_initlevels
Initializes the levels struct
==============================*/
void core_initlevels()
{
global_core_nextlevel = NULL;
global_core_curlevel = NULL;
global_core_alllevels[LEVEL_LOADSAVE].funcPointer_init = loadsave_init;
global_core_alllevels[LEVEL_LOADSAVE].funcPointer_loop = loadsave_loop;
global_core_alllevels[LEVEL_LOADSAVE].funcPointer_fixedloop = NULL;
global_core_alllevels[LEVEL_LOADSAVE].funcPointer_cleanup = loadsave_cleanup;
global_core_alllevels[LEVEL_MAINMENU].funcPointer_init = titlescreen_init;
global_core_alllevels[LEVEL_MAINMENU].funcPointer_loop = titlescreen_loop;
global_core_alllevels[LEVEL_MAINMENU].funcPointer_fixedloop = NULL;
global_core_alllevels[LEVEL_MAINMENU].funcPointer_cleanup = titlescreen_cleanup;
global_core_alllevels[LEVEL_GAMESETUP].funcPointer_init = setup_init;
global_core_alllevels[LEVEL_GAMESETUP].funcPointer_loop = setup_loop;
global_core_alllevels[LEVEL_GAMESETUP].funcPointer_fixedloop = NULL;
global_core_alllevels[LEVEL_GAMESETUP].funcPointer_cleanup = setup_cleanup;
global_core_alllevels[LEVEL_MINIGAMESELECT].funcPointer_init = menu_init;
global_core_alllevels[LEVEL_MINIGAMESELECT].funcPointer_loop = menu_loop;
global_core_alllevels[LEVEL_MINIGAMESELECT].funcPointer_fixedloop = NULL;
global_core_alllevels[LEVEL_MINIGAMESELECT].funcPointer_cleanup = menu_cleanup;
global_core_alllevels[LEVEL_RESULTS].funcPointer_init = results_init;
global_core_alllevels[LEVEL_RESULTS].funcPointer_loop = results_loop;
global_core_alllevels[LEVEL_RESULTS].funcPointer_fixedloop = NULL;
global_core_alllevels[LEVEL_RESULTS].funcPointer_cleanup = results_cleanup;
}
/*==============================
core_level_changeto
Changes the level
@param The level to change to
==============================*/
void core_level_changeto(LevelDef level)
{
if (level == LEVEL_MINIGAME)
{
global_core_alllevels[LEVEL_MINIGAME].funcPointer_init = minigame_get_game()->funcPointer_init;
global_core_alllevels[LEVEL_MINIGAME].funcPointer_loop = minigame_get_game()->funcPointer_loop;
global_core_alllevels[LEVEL_MINIGAME].funcPointer_fixedloop = minigame_get_game()->funcPointer_fixedloop;
global_core_alllevels[LEVEL_MINIGAME].funcPointer_cleanup = minigame_get_game()->funcPointer_cleanup;
}
global_core_nextlevel = &global_core_alllevels[level];
}
/*==============================
core_level_doinit
Calls the level's init function
==============================*/
void core_level_doinit()
{
if (global_core_nextlevel != NULL)
{
global_core_curlevel = global_core_nextlevel;
global_core_nextlevel = NULL;
}
if (global_core_curlevel == &global_core_alllevels[LEVEL_MINIGAME])
core_reset_winners();
if (global_core_curlevel->funcPointer_init)
global_core_curlevel->funcPointer_init();
}
/*==============================
core_level_doloop
Calls the level's loop function
@param The deltatime
==============================*/
void core_level_doloop(float deltatime)
{
if (global_core_curlevel->funcPointer_loop)
global_core_curlevel->funcPointer_loop(deltatime);
}
/*==============================
core_level_dofixedloop
Calls the level's fixed loop function
@param The deltatime
==============================*/
void core_level_dofixedloop(float deltatime)
{
if (global_core_curlevel->funcPointer_fixedloop)
global_core_curlevel->funcPointer_fixedloop(deltatime);
}
/*==============================
core_level_docleanup
Calls the level's cleanup function
==============================*/
void core_level_docleanup()
{
rspq_wait();
for (int i=0; i<32; i++)
mixer_ch_stop(i);
//menu_copy_minigame_frame();
if (global_core_curlevel->funcPointer_cleanup)
global_core_curlevel->funcPointer_cleanup();
if (global_core_curlevel == &global_core_alllevels[LEVEL_MINIGAME])
minigame_cleanup();
mixer_close();
mixer_init(32);
}
/*==============================
core_level_waschanged
Checks if the level was recently changed
@return If the level was recently changed
==============================*/
bool core_level_waschanged()
{
return global_core_nextlevel != NULL;
}
/*==============================
core_set_nextround
Sets how the next round is decided
==============================*/
void core_set_nextround(NextRound type)
{
global_nextroundtype = type;
}
/*==============================
core_get_nextround
Gets how the next round is decided
@return The NextRound type
==============================*/
NextRound core_get_nextround()
{
return global_nextroundtype;
}
/*==============================
core_set_curchooser
TODO
==============================*/
void core_set_curchooser(PlyNum ply)
{
global_core_chooser = ply;
}
/*==============================
core_get_curchooser
TODO
==============================*/
PlyNum core_get_curchooser()
{
return global_core_chooser;
}