diff options
author | Bad Diode <bd@badd10de.dev> | 2023-05-28 19:17:48 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2023-05-28 19:17:48 +0200 |
commit | 52bdd0d79c77df0a7752fd15ebcc730f9e9796b1 (patch) | |
tree | 1efa9654ecdc5a933976bbbb37d9ec18562c1320 | |
parent | 70b7b9f719ab673ff776cfc0f9b7bc67dc88bac1 (diff) | |
download | stepper-52bdd0d79c77df0a7752fd15ebcc730f9e9796b1.tar.gz stepper-52bdd0d79c77df0a7752fd15ebcc730f9e9796b1.zip |
Refactor settings into own files
-rw-r--r-- | src/globals.c | 9 | ||||
-rw-r--r-- | src/main.c | 114 | ||||
-rw-r--r-- | src/save.c | 17 | ||||
-rw-r--r-- | src/save.h | 21 | ||||
-rw-r--r-- | src/sequencer.c | 7 | ||||
-rw-r--r-- | src/settings.c | 63 | ||||
-rw-r--r-- | src/settings.h | 44 |
7 files changed, 152 insertions, 123 deletions
diff --git a/src/globals.c b/src/globals.c index 8ce79d5..60e64e7 100644 --- a/src/globals.c +++ b/src/globals.c | |||
@@ -17,6 +17,7 @@ u32 last_channel_loc = 0; | |||
17 | u32 last_pattern_loc = 0; | 17 | u32 last_pattern_loc = 0; |
18 | u32 last_right_col_loc = 0; | 18 | u32 last_right_col_loc = 0; |
19 | u32 last_step_counter = 0; | 19 | u32 last_step_counter = 0; |
20 | bool clear_screen = false; | ||
20 | 21 | ||
21 | // | 22 | // |
22 | // Color indexes. | 23 | // Color indexes. |
@@ -113,3 +114,11 @@ void handle_param_selection_ch1(void); | |||
113 | void handle_param_selection_ch2(void); | 114 | void handle_param_selection_ch2(void); |
114 | void handle_param_selection_ch3(void); | 115 | void handle_param_selection_ch3(void); |
115 | void handle_param_selection_ch4(void); | 116 | void handle_param_selection_ch4(void); |
117 | |||
118 | typedef enum Scene { | ||
119 | SCENE_SEQUENCER = 0, | ||
120 | SCENE_SETTINGS, | ||
121 | } Scene; | ||
122 | |||
123 | static Scene scene = SCENE_SETTINGS; | ||
124 | static Scene next_scene = SCENE_SETTINGS; | ||
@@ -55,6 +55,8 @@ WITH REGARD TO THIS SOFTWARE. | |||
55 | #include "gba/gba.h" | 55 | #include "gba/gba.h" |
56 | 56 | ||
57 | #include "renderer_m0.c" | 57 | #include "renderer_m0.c" |
58 | #include "globals.c" | ||
59 | #include "settings.c" | ||
58 | #include "sequencer.c" | 60 | #include "sequencer.c" |
59 | 61 | ||
60 | #define PROF_ENABLE 0 | 62 | #define PROF_ENABLE 0 |
@@ -99,65 +101,6 @@ render_sequencer(void) { | |||
99 | PROF(draw_cursors(), draw_cursor_cycles); | 101 | PROF(draw_cursors(), draw_cursor_cycles); |
100 | } | 102 | } |
101 | 103 | ||
102 | bool clear_screen = false; | ||
103 | |||
104 | typedef enum Scene { | ||
105 | SCENE_SEQUENCER = 0, | ||
106 | SCENE_SETTINGS, | ||
107 | } Scene; | ||
108 | |||
109 | static scene = SCENE_SETTINGS; | ||
110 | static next_scene = SCENE_SETTINGS; | ||
111 | |||
112 | typedef enum SyncSetting { | ||
113 | SYNC_NONE = 0, | ||
114 | SYNC_OUT_LINK_16, | ||
115 | SYNC_OUT_LINK_8, | ||
116 | SYNC_OUT_LINK_4, | ||
117 | SYNC_NUM, | ||
118 | } SyncSetting; | ||
119 | |||
120 | char * sync_setting_str[] = { | ||
121 | "NONE", | ||
122 | "LINK OUT (16)", | ||
123 | "LINK OUT (8)", | ||
124 | "LINK OUT (4)", | ||
125 | }; | ||
126 | |||
127 | typedef enum ThemeSetting { | ||
128 | THEME_DEFAULT = 0, | ||
129 | } ThemeSetting; | ||
130 | |||
131 | char * theme_setting_str[] = { | ||
132 | "DEFAULT", | ||
133 | }; | ||
134 | |||
135 | typedef enum CursorSetting { | ||
136 | CURSOR_DEFAULT = 0, | ||
137 | } CursorSetting; | ||
138 | |||
139 | char * cursor_setting_str[] = { | ||
140 | "LINE", | ||
141 | }; | ||
142 | |||
143 | typedef struct Settings { | ||
144 | SyncSetting sync; | ||
145 | ThemeSetting theme; | ||
146 | CursorSetting cursor; | ||
147 | } Settings; | ||
148 | |||
149 | static Settings settings = {0}; | ||
150 | static settings_cursor_loc = 0; | ||
151 | |||
152 | void | ||
153 | draw_settings_cursor(void) { | ||
154 | int x = 6; | ||
155 | int y = 17 + settings_cursor_loc * 12; | ||
156 | draw_line(x + 1, y + 3, x + 1, y + 7, COL_CYAN); | ||
157 | draw_line(x + 2, y + 4, x + 2, y + 6, COL_CYAN); | ||
158 | draw_line(x + 3, y + 5, x + 3, y + 5, COL_CYAN); | ||
159 | } | ||
160 | |||
161 | void | 104 | void |
162 | render_settings(void) { | 105 | render_settings(void) { |
163 | PROF(draw_rect(0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, 1), clear_cycles); | 106 | PROF(draw_rect(0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, 1), clear_cycles); |
@@ -168,62 +111,13 @@ render_settings(void) { | |||
168 | txt_printf(" CURSOR: %s\n\n", cursor_setting_str[settings.cursor]); | 111 | txt_printf(" CURSOR: %s\n\n", cursor_setting_str[settings.cursor]); |
169 | txt_render(); | 112 | txt_render(); |
170 | txt_clear(); | 113 | txt_clear(); |
171 | draw_settings_cursor(); | 114 | PROF(draw_settings_cursor(), draw_cursor_cycles); |
172 | } | ||
173 | |||
174 | #define N_SETTINGS 3 | ||
175 | |||
176 | void | ||
177 | handle_settings_input(void) { | ||
178 | if (key_tap(KEY_DOWN)) { | ||
179 | if (settings_cursor_loc == (N_SETTINGS - 1)) { | ||
180 | settings_cursor_loc = 0; | ||
181 | } else { | ||
182 | settings_cursor_loc++; | ||
183 | } | ||
184 | clear_screen = true; | ||
185 | } | ||
186 | if (key_tap(KEY_UP)) { | ||
187 | if (settings_cursor_loc == 0) { | ||
188 | settings_cursor_loc = N_SETTINGS - 1; | ||
189 | } else { | ||
190 | settings_cursor_loc--; | ||
191 | } | ||
192 | clear_screen = true; | ||
193 | } | ||
194 | if (key_tap(KEY_R)) { | ||
195 | switch (settings_cursor_loc) { | ||
196 | case 0: { | ||
197 | if ((settings.sync + 1) >= SYNC_NUM) { | ||
198 | settings.sync = 0; | ||
199 | } else { | ||
200 | settings.sync++; | ||
201 | } | ||
202 | } break; | ||
203 | } | ||
204 | clear_screen = true; | ||
205 | } | ||
206 | if (key_tap(KEY_L)) { | ||
207 | switch (settings_cursor_loc) { | ||
208 | case 0: { | ||
209 | if (settings.sync == 0) { | ||
210 | settings.sync = SYNC_NUM - 1; | ||
211 | } else { | ||
212 | settings.sync--; | ||
213 | } | ||
214 | } break; | ||
215 | } | ||
216 | clear_screen = true; | ||
217 | } | ||
218 | if (key_tap(KEY_B)) { | ||
219 | next_scene = SCENE_SEQUENCER; | ||
220 | } | ||
221 | } | 115 | } |
222 | 116 | ||
223 | void | 117 | void |
224 | render(void) { | 118 | render(void) { |
225 | if (clear_screen) { | 119 | if (clear_screen) { |
226 | screen_fill(COL_BG); | 120 | PROF(screen_fill(COL_BG), clear_cycles); |
227 | clear_screen = false; | 121 | clear_screen = false; |
228 | } | 122 | } |
229 | switch (scene) { | 123 | switch (scene) { |
@@ -1,17 +1,9 @@ | |||
1 | #include "save.h" | ||
2 | |||
1 | // | 3 | // |
2 | // Save data. | 4 | // Save data. |
3 | // | 5 | // |
4 | 6 | ||
5 | #define SRAM ((vu8*)(MEM_CART)) | ||
6 | #define SAVE_INIT_MAGIC 0xBADD10DE | ||
7 | |||
8 | typedef struct Metadata { | ||
9 | u8 blank; | ||
10 | u32 initialized; | ||
11 | int current_bank; | ||
12 | int current_pattern; | ||
13 | } Metadata; | ||
14 | |||
15 | static Metadata metadata = {0}; | 7 | static Metadata metadata = {0}; |
16 | 8 | ||
17 | void | 9 | void |
@@ -38,3 +30,8 @@ load_bank(int i) { | |||
38 | sram_read(&patterns, sizeof(Metadata) + i * sizeof(patterns), sizeof(patterns)); | 30 | sram_read(&patterns, sizeof(Metadata) + i * sizeof(patterns), sizeof(patterns)); |
39 | } | 31 | } |
40 | 32 | ||
33 | void | ||
34 | save_metadata(void) { | ||
35 | metadata.settings = settings; | ||
36 | sram_write(&metadata, 0, sizeof(Metadata)); | ||
37 | } | ||
diff --git a/src/save.h b/src/save.h new file mode 100644 index 0000000..ccc190b --- /dev/null +++ b/src/save.h | |||
@@ -0,0 +1,21 @@ | |||
1 | #ifndef SAVE_H | ||
2 | #define SAVE_H | ||
3 | |||
4 | #include "settings.h" | ||
5 | |||
6 | #define SRAM ((vu8*)(MEM_CART)) | ||
7 | #define SAVE_INIT_MAGIC 0xBADD10DE | ||
8 | |||
9 | typedef struct Metadata { | ||
10 | u8 blank; | ||
11 | u32 initialized; | ||
12 | int current_bank; | ||
13 | int current_pattern; | ||
14 | Settings settings; | ||
15 | } Metadata; | ||
16 | |||
17 | void save_bank(int i); | ||
18 | void load_bank(int i); | ||
19 | void save_metadata(void); | ||
20 | |||
21 | #endif // SAVE_H | ||
diff --git a/src/sequencer.c b/src/sequencer.c index 7c989f2..44ec7bb 100644 --- a/src/sequencer.c +++ b/src/sequencer.c | |||
@@ -1,4 +1,3 @@ | |||
1 | #include "globals.c" | ||
2 | #include "rng.c" | 1 | #include "rng.c" |
3 | #include "text.h" | 2 | #include "text.h" |
4 | #include "assets.c" | 3 | #include "assets.c" |
@@ -346,7 +345,7 @@ select_bank(int i) { | |||
346 | save_bank(current_bank); | 345 | save_bank(current_bank); |
347 | metadata.current_pattern = current_pattern; | 346 | metadata.current_pattern = current_pattern; |
348 | metadata.current_bank = i; | 347 | metadata.current_bank = i; |
349 | sram_write(&metadata, 0, sizeof(Metadata)); | 348 | save_metadata(); |
350 | if (current_bank != i) { | 349 | if (current_bank != i) { |
351 | load_bank(i); | 350 | load_bank(i); |
352 | } | 351 | } |
@@ -965,6 +964,7 @@ handle_sequencer_input(void) { | |||
965 | if (key_tap(KEY_START)) { | 964 | if (key_tap(KEY_START)) { |
966 | if (key_hold(KEY_SELECT)) { | 965 | if (key_hold(KEY_SELECT)) { |
967 | save_bank(current_bank); | 966 | save_bank(current_bank); |
967 | save_metadata(); | ||
968 | return; | 968 | return; |
969 | } | 969 | } |
970 | // Stop the sequencer or start playing from the beginning. | 970 | // Stop the sequencer or start playing from the beginning. |
@@ -992,7 +992,7 @@ sequencer_init(void) { | |||
992 | } | 992 | } |
993 | dma_fill(&metadata, 0, sizeof(Metadata), 3); | 993 | dma_fill(&metadata, 0, sizeof(Metadata), 3); |
994 | metadata.initialized = SAVE_INIT_MAGIC; | 994 | metadata.initialized = SAVE_INIT_MAGIC; |
995 | sram_write(&metadata, 0, sizeof(Metadata)); | 995 | save_metadata(); |
996 | save_bank(0); | 996 | save_bank(0); |
997 | save_bank(1); | 997 | save_bank(1); |
998 | save_bank(2); | 998 | save_bank(2); |
@@ -1002,6 +1002,7 @@ sequencer_init(void) { | |||
1002 | current_pattern = metadata.current_pattern; | 1002 | current_pattern = metadata.current_pattern; |
1003 | next_pattern = metadata.current_pattern; | 1003 | next_pattern = metadata.current_pattern; |
1004 | pattern_selection_loc = current_pattern; | 1004 | pattern_selection_loc = current_pattern; |
1005 | settings = metadata.settings; | ||
1005 | load_bank(metadata.current_bank); | 1006 | load_bank(metadata.current_bank); |
1006 | } | 1007 | } |
1007 | 1008 | ||
diff --git a/src/settings.c b/src/settings.c new file mode 100644 index 0000000..fd5b090 --- /dev/null +++ b/src/settings.c | |||
@@ -0,0 +1,63 @@ | |||
1 | #include "settings.h" | ||
2 | #include "save.h" | ||
3 | |||
4 | static Settings settings = {0}; | ||
5 | static int settings_cursor_loc = 0; | ||
6 | |||
7 | void | ||
8 | draw_settings_cursor(void) { | ||
9 | int x = 6; | ||
10 | int y = 17 + settings_cursor_loc * 12; | ||
11 | draw_line(x + 1, y + 3, x + 1, y + 7, COL_CYAN); | ||
12 | draw_line(x + 2, y + 4, x + 2, y + 6, COL_CYAN); | ||
13 | draw_line(x + 3, y + 5, x + 3, y + 5, COL_CYAN); | ||
14 | } | ||
15 | |||
16 | void | ||
17 | handle_settings_input(void) { | ||
18 | if (key_tap(KEY_DOWN)) { | ||
19 | if (settings_cursor_loc == (N_SETTINGS - 1)) { | ||
20 | settings_cursor_loc = 0; | ||
21 | } else { | ||
22 | settings_cursor_loc++; | ||
23 | } | ||
24 | clear_screen = true; | ||
25 | } | ||
26 | if (key_tap(KEY_UP)) { | ||
27 | if (settings_cursor_loc == 0) { | ||
28 | settings_cursor_loc = N_SETTINGS - 1; | ||
29 | } else { | ||
30 | settings_cursor_loc--; | ||
31 | } | ||
32 | clear_screen = true; | ||
33 | } | ||
34 | if (key_tap(KEY_R)) { | ||
35 | switch (settings_cursor_loc) { | ||
36 | case 0: { | ||
37 | if ((settings.sync + 1) >= SYNC_NUM) { | ||
38 | settings.sync = 0; | ||
39 | } else { | ||
40 | settings.sync++; | ||
41 | } | ||
42 | } break; | ||
43 | } | ||
44 | save_metadata(); | ||
45 | clear_screen = true; | ||
46 | } | ||
47 | if (key_tap(KEY_L)) { | ||
48 | switch (settings_cursor_loc) { | ||
49 | case 0: { | ||
50 | if (settings.sync == 0) { | ||
51 | settings.sync = SYNC_NUM - 1; | ||
52 | } else { | ||
53 | settings.sync--; | ||
54 | } | ||
55 | } break; | ||
56 | } | ||
57 | save_metadata(); | ||
58 | clear_screen = true; | ||
59 | } | ||
60 | if (key_tap(KEY_B)) { | ||
61 | next_scene = SCENE_SEQUENCER; | ||
62 | } | ||
63 | } | ||
diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..db46144 --- /dev/null +++ b/src/settings.h | |||
@@ -0,0 +1,44 @@ | |||
1 | #ifndef SETTINGS_H | ||
2 | #define SETTINGS_H | ||
3 | |||
4 | #define N_SETTINGS 3 | ||
5 | |||
6 | typedef enum SyncSetting { | ||
7 | SYNC_NONE = 0, | ||
8 | SYNC_OUT_LINK_16, | ||
9 | SYNC_OUT_LINK_8, | ||
10 | SYNC_OUT_LINK_4, | ||
11 | SYNC_NUM, | ||
12 | } SyncSetting; | ||
13 | |||
14 | char * sync_setting_str[] = { | ||
15 | "NONE", | ||
16 | "LINK OUT (16)", | ||
17 | "LINK OUT (8)", | ||
18 | "LINK OUT (4)", | ||
19 | }; | ||
20 | |||
21 | typedef enum ThemeSetting { | ||
22 | THEME_DEFAULT = 0, | ||
23 | } ThemeSetting; | ||
24 | |||
25 | char * theme_setting_str[] = { | ||
26 | "DEFAULT", | ||
27 | }; | ||
28 | |||
29 | typedef enum CursorSetting { | ||
30 | CURSOR_DEFAULT = 0, | ||
31 | } CursorSetting; | ||
32 | |||
33 | char * cursor_setting_str[] = { | ||
34 | "LINE", | ||
35 | }; | ||
36 | |||
37 | typedef struct Settings { | ||
38 | SyncSetting sync; | ||
39 | ThemeSetting theme; | ||
40 | CursorSetting cursor; | ||
41 | } Settings; | ||
42 | |||
43 | |||
44 | #endif // SETTINGS_H | ||