generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighScore.cpp
144 lines (111 loc) · 2.91 KB
/
HighScore.cpp
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
/*
* HighScore.cpp - part of 32Blox (revised edition!)
*
* Copyright (C) 2020 Pete Favelle <[email protected]>
*
* This file is released under the MIT License; see LICENSE for details
*
* The HighScore class manages the high score table, which will be saved onto
* the SD card (if we can)
*/
/* System headers. */
#include <string.h>
/* Local headers. */
#include "32blit.hpp"
#include "32blox.hpp"
#include "HighScore.hpp"
/* Functions. */
/*
* constructor - create the contents of this State container.
*/
HighScore::HighScore( void )
{
/* File stuff now handled by the API, we just ask it nicely to load. */
load();
/* All done. */
return;
}
/*
* rank_score - determines where in the table the provided score should go
*
* uint16_t - the score being checked
*
* Returns the table position, or MAX_SCORES if it's below all scores.
*/
uint8_t HighScore::rank( uint16_t p_score )
{
/* Scan through the high score table, looking to see where this score goes. */
for( uint8_t i = 0; i < MAX_SCORES; i++ )
{
/* If the score equals or exceeds the current entry, this is it! */
if ( p_score >= scores[i].score )
{
return i;
}
}
/* If we drop out here, we found nothing matching. */
return MAX_SCORES;
}
/*
* load - loads the table from the file, if we can.
*/
void HighScore::load( void )
{
/* We can just let the API handle this now. */
if ( !blit::read_save( scores, SAVE_SLOT_HISCORE ) )
{
/* Failure means the file was empty. */
for( uint8_t i = 0; i < MAX_SCORES; i++ )
{
strcpy( scores[i].name, "ahnlak" );
scores[i].score = 0;
}
}
/* All done. */
return;
}
/*
* save - adds the score to the table, and saves it to persistent storage
*
* uint16_t - the score being saved,
* const char * - the name being saved with the score
*/
void HighScore::save( uint16_t p_score, const char *p_name )
{
/* Work out where we want to add ourselves. */
uint8_t l_position = rank( p_score );
/* Sanity check; this had better be a valid position! */
if ( l_position >= MAX_SCORES )
{
return;
}
/* Shuffle entries below down, to make room. */
for( uint8_t i = MAX_SCORES-1; i > l_position; i-- )
{
strcpy( scores[i].name, scores[i-1].name );
scores[i].score = scores[i-1].score;
}
/* Fill in the new data. */
strcpy( scores[l_position].name, p_name );
scores[l_position].score = p_score;
/* And lastly, ask the API to save all this. */
blit::write_save( scores, SAVE_SLOT_HISCORE );
/* All done. */
return;
}
/*
* get_entry - fetches the high score details for the given position.
*
* uint8_t - the position in the table
*/
const hiscore_t *HighScore::get_entry( uint8_t p_position )
{
/* Sanity check the position requested. */
if ( p_position >= MAX_SCORES )
{
return nullptr;
}
/* Then simply return the appropriate entry. */
return &scores[p_position];
}
/* End of HighScore.cpp */