add missing tools/pngdump to the repo
[gbajam22] / src / scoredb.c
1 /*
2 Score-keeping for GBA with SRAM cartridge (originally part of gbatris)
3 Copyright (C) 2019-2022  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "scoredb.h"
22 #include "debug.h"
23 #include "gbaregs.h"
24
25 #define MAGIC "VOXBURG1"
26 #define SCORE_OFFS      8
27
28
29 #define read_sram(offs, buf, sz) \
30         sramcpy(buf, (unsigned char*)SRAM_ADDR + (offs), sz)
31 #define write_sram(offs, buf, sz) \
32         sramcpy((unsigned char*)SRAM_ADDR + (offs), buf, sz)
33
34 static void sramcpy(void *dst, void *src, int size);
35
36 int last_score_rank = -1;
37
38 int load_scores(void)
39 {
40         int i;
41         char magic[8];
42
43         read_sram(0, magic, 8);
44         if(memcmp(magic, MAGIC, sizeof magic) != 0) {
45                 for(i=0; i<10; i++) {
46                         memcpy(scores[i].name, "----", NAME_SIZE + 1);
47                 }
48                 /* scores[10] keeps the last entered name. initialize it empty */
49                 scores[10].name[0] = 0;
50                 return -1;
51         }
52
53         read_sram(SCORE_OFFS, scores, sizeof scores);
54         return 0;
55 }
56
57 void save_scores(void)
58 {
59         write_sram(0, MAGIC, 8);
60         write_sram(SCORE_OFFS, scores, sizeof scores);
61 }
62
63 void save_score(char *name, int score, int time, int level)
64 {
65         int i, rank = -1;
66
67         if(!score) {
68                 last_score_rank = -1;
69                 return;
70         }
71
72         for(i=0; i<10; i++) {
73                 if(scores[i].score <= score) {
74                         rank = i;
75                         break;
76                 }
77         }
78         last_score_rank = rank;
79
80         if(rank == -1) return;
81
82         memmove(scores + rank + 1, scores + rank, (9 - rank) * sizeof(struct score_entry));
83
84         if(strlen(name) > NAME_SIZE) {
85                 name[NAME_SIZE] = 0;
86         }
87         sprintf(scores[rank].name, "%s", name);
88         scores[rank].score = score;
89         scores[rank].time = time;
90         scores[rank].level = level;
91
92         /* also copy to the last entered name slot */
93         scores[10] = scores[rank];
94
95         save_scores();
96 }
97
98 int is_highscore(int score)
99 {
100         return scores[9].score <= score;
101 }
102
103 static void sramcpy(void *dst, void *src, int size)
104 {
105         unsigned char *sptr = src;
106         unsigned char *dptr = dst;
107         while(size-- > 0) {
108                 *dptr++ = *sptr++;
109         }
110 }