-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressBar.c
93 lines (77 loc) · 2.74 KB
/
progressBar.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
#include "core.h"
#include "types.h"
#include "screen.h"
#include "text.h"
#include "resources.h"
#include <stdio.h>
#include <libdragon.h>
static bool _isLoading[MAX_PLAYERS] = {0};
static timer_link_t* _timer;
static const int UPDATE_TIME = 500000;
/**
* Fired periodically as we load a cartridge. It updates the display so we know
* how much longer to wait (and that we haven't crashed.)
* @param controllerNumber controller we are loading from.
*/
static void updateProgressIndicator(const byte controllerNumber) {
byte loadPercent = getLoadProgress(controllerNumber);
string text;
getText(TextLoadingCartridge, text);
sprintf(text, "%s - %d%%", text, loadPercent);
Rectangle screen = {};
getScreenPosition(controllerNumber, &screen);
natural textTop = screen.Top - TEXT_HEIGHT + (screen.Width / 2);
while(!(rootState.Frame = display_lock()));
// Hide the previous text.
prepareRdpForSprite(rootState.Frame);
loadSprite(getSpriteSheet(), GB_BG_TEXTURE, MIRROR_DISABLED);
rdp_draw_textured_rectangle(0, screen.Left - 1, screen.Top, screen.Left + screen.Width, screen.Top + screen.Height, MIRROR_XY);
drawTextParagraph(rootState.Frame, text, screen.Left + TEXT_WIDTH, textTop, 0.8, screen.Width - TEXT_WIDTH);
display_show(rootState.Frame);
}
/**
* Timer callback, it updates the progress bar of any player that is needs one.
* @param ovfl
*/
static void onLoadProgressTimer(int ovfl) {
for (byte i = 0; i < MAX_PLAYERS; i++) {
if (_isLoading[i]) {
updateProgressIndicator(i);
}
}
}
/**
* Sets up handlers to display a progress bar for the cartridge load.
* @param playerNumber identifies player loading a cartridge.
*/
void startLoadProgressTimer(const byte playerNumber) {
// The main loop is locked up with loading the rom, so we need to have an internal
// display lock/show loop.
// This needs to be balanced with the main loop or things get out of whack.
display_show(rootState.Frame);
_isLoading[playerNumber] = true;
if (!_timer) {
_timer = new_timer(TIMER_TICKS(UPDATE_TIME), TF_CONTINUOUS, onLoadProgressTimer);
}
}
/**
* Cleans up timer once a cartridge is loaded
* @param playerNumber player no longer using the progress bar
*/
void closeLoadProgressTimer(const byte playerNumber) {
_isLoading[playerNumber] = false;
// Check if there are any other players still using the timer.
bool isDone = true;
for (byte i = 0; i < MAX_PLAYERS; i++) {
if (_isLoading[i]) {
isDone = false;
}
}
// If this was the last one, clean up.
if (isDone) {
delete_timer(_timer);
_timer = null;
}
// Balance with the main loop.
while(!(rootState.Frame = display_lock()));
}