-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighscore.h
66 lines (54 loc) · 1.14 KB
/
Highscore.h
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
#ifndef __HIGHSCORE_H__
#define __HIGHSCORE_H__
#include "Arduboy.h"
#include "EEPROM.h"
class Highscore {
public:
static const int16_t MEMORY_MAX = 5;
private:
int16_t score[MEMORY_MAX];
public:
Highscore();
~Highscore();
int16_t readScore(int16_t rank);
void writeScore(int16_t _score);
};
Highscore::Highscore()
{
for (int i = 0; i < MEMORY_MAX; i++) {
uint8_t hi = EEPROM.read(2 * i);
uint8_t lo = EEPROM.read(2 * i + 1);
if ((hi == 0xFF) && (lo == 0xFF))
{
score[i] = 0;
}
else
{
score[i] = (hi << 8) | lo;
}
}
}
Highscore::~Highscore()
{
}
int16_t Highscore::readScore(int16_t rank)
{
return score[rank];
}
void Highscore::writeScore(int16_t _score)
{
for (int i = 0; i < MEMORY_MAX; i++) {
if (score[i] < _score) {
for (int j = MEMORY_MAX - 1; j > i; j--) {
score[j] = score[j - 1];
EEPROM.write(2 * j, (score[j] >> 8) & 0xFF);
EEPROM.write(2 * j + 1, score[j] & 0xFF);
}
score[i] = _score;
EEPROM.write(2 * i, (score[i] >> 8) & 0xFF);
EEPROM.write(2 * i + 1, score[i] & 0xFF);
break;
}
}
}
#endif __HIGHSCORE_H__