Browse Source

add conf screen for sfx and music volume

Thomas B 1 month ago
parent
commit
e3c534203e
9 changed files with 148 additions and 12 deletions
  1. 5
    0
      src/config.ba0.c
  2. 72
    2
      src/main.c
  3. 11
    2
      src/main.h
  4. 32
    2
      src/maps.c
  5. 1
    0
      src/maps.h
  6. 16
    2
      src/sample.c
  7. 3
    0
      src/sample.h
  8. 6
    4
      src/sound.c
  9. 2
    0
      src/sound.h

+ 5
- 0
src/config.ba0.c View File

22
 
22
 
23
 #include "banks.h"
23
 #include "banks.h"
24
 #include "score.h"
24
 #include "score.h"
25
+#include "sample.h"
26
+#include "sound.h"
25
 #include "config.h"
27
 #include "config.h"
26
 
28
 
27
 struct config_mem {
29
 struct config_mem {
62
         mem.config.music_vol = 0x07;
64
         mem.config.music_vol = 0x07;
63
         score_reset();
65
         score_reset();
64
     }
66
     }
67
+
68
+    snd_vol_sfx = mem.config.sfx_vol;
69
+    snd_vol_music = mem.config.music_vol;
65
 }
70
 }
66
 
71
 
67
 void conf_write_crc(void) BANKED {
72
 void conf_write_crc(void) BANKED {

+ 72
- 2
src/main.c View File

43
 
43
 
44
 BANKREF(main)
44
 BANKREF(main)
45
 
45
 
46
+const struct conf_entry conf_entries[CONF_ENTRY_COUNT] = {
47
+    { .name = "sfx-vol",  .var = &snd_vol_sfx,   .max = 0x0F }, // 0
48
+    { .name = "musi-vol", .var = &snd_vol_music, .max = 0x0F }, // 1
49
+};
50
+
46
 const struct debug_entry debug_entries[DEBUG_ENTRY_COUNT] = {
51
 const struct debug_entry debug_entries[DEBUG_ENTRY_COUNT] = {
47
     { .name = "marker",   .flag = DBG_MARKER,      .max = 1 }, // 0
52
     { .name = "marker",   .flag = DBG_MARKER,      .max = 1 }, // 0
48
     { .name = "invuln",   .flag = DBG_GOD_MODE,    .max = 1 }, // 1
53
     { .name = "invuln",   .flag = DBG_GOD_MODE,    .max = 1 }, // 1
98
     }
103
     }
99
 }
104
 }
100
 
105
 
106
+static void conf_screen(void) NONBANKED {
107
+    HIDE_WIN;
108
+
109
+    move_win(MINWNDPOSX, MINWNDPOSY);
110
+    hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
111
+    win_conf();
112
+
113
+    SHOW_WIN;
114
+
115
+    debug_menu_index = 0;
116
+
117
+    while (1) {
118
+        key_read();
119
+
120
+        if (key_pressed(J_SELECT)) {
121
+            about_screen();
122
+            break;
123
+        } else if (key_pressed(J_UP)) {
124
+            if (debug_menu_index > 0) {
125
+                debug_menu_index--;
126
+            } else {
127
+                debug_menu_index = CONF_ENTRY_COUNT - 1;
128
+            }
129
+            win_conf();
130
+        } else if (key_pressed(J_DOWN)) {
131
+            if (debug_menu_index < (CONF_ENTRY_COUNT - 1)) {
132
+                debug_menu_index++;
133
+            } else {
134
+                debug_menu_index = 0;
135
+            }
136
+            win_conf();
137
+        } else if (key_pressed(J_LEFT)) {
138
+            START_ROM_BANK(BANK(main));
139
+                if (*conf_entries[debug_menu_index].var > 0) {
140
+                    (*conf_entries[debug_menu_index].var)--;
141
+                } else {
142
+                    *conf_entries[debug_menu_index].var = conf_entries[debug_menu_index].max;
143
+                }
144
+                conf_get()->music_vol = snd_vol_music;
145
+                conf_get()->sfx_vol = snd_vol_sfx;
146
+                conf_write_crc();
147
+            END_ROM_BANK();
148
+            win_conf();
149
+        } else if (key_pressed(J_RIGHT)) {
150
+            START_ROM_BANK(BANK(main));
151
+                if (*conf_entries[debug_menu_index].var < conf_entries[debug_menu_index].max) {
152
+                    (*conf_entries[debug_menu_index].var)++;
153
+                } else {
154
+                    *conf_entries[debug_menu_index].var = 0;
155
+                }
156
+                conf_get()->music_vol = snd_vol_music;
157
+                conf_get()->sfx_vol = snd_vol_sfx;
158
+                conf_write_crc();
159
+            END_ROM_BANK();
160
+            win_conf();
161
+        } else if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_START)) {
162
+            break;
163
+        }
164
+
165
+        vsync();
166
+    }
167
+
168
+    debug_menu_index = 0;
169
+}
170
+
101
 static void splash_win(void) NONBANKED {
171
 static void splash_win(void) NONBANKED {
102
     HIDE_WIN;
172
     HIDE_WIN;
103
 
173
 
223
             highscore(0);
293
             highscore(0);
224
             splash_win();
294
             splash_win();
225
         } else if (key_pressed(J_SELECT)) {
295
         } else if (key_pressed(J_SELECT)) {
226
-            about_screen();
296
+            conf_screen();
227
             splash_win();
297
             splash_win();
228
         } else if (key_pressed(J_START)) {
298
         } else if (key_pressed(J_START)) {
229
             if ((key_debug() == 0) && (!(conf_get()->debug_flags & DBG_MENU))) {
299
             if ((key_debug() == 0) && (!(conf_get()->debug_flags & DBG_MENU))) {
332
                         snd_music(debug_special_value - 1);
402
                         snd_music(debug_special_value - 1);
333
                     }
403
                     }
334
                     snd_note_off();
404
                     snd_note_off();
335
-                } else if (switch_special && (debug_menu_index == 3)) {
405
+                } else if ((switch_special || (!sample_running())) && (debug_menu_index == 3)) {
336
                     if (debug_special_value > 0) {
406
                     if (debug_special_value > 0) {
337
                         sample_play(debug_special_value - 1);
407
                         sample_play(debug_special_value - 1);
338
                     }
408
                     }

+ 11
- 2
src/main.h View File

26
 #include <gbdk/platform.h>
26
 #include <gbdk/platform.h>
27
 #include <stdint.h>
27
 #include <stdint.h>
28
 
28
 
29
-#define DEBUG_ENTRY_NAME_LEN 8
29
+#define ENTRY_NAME_LEN 8
30
+
31
+struct conf_entry {
32
+    char name[ENTRY_NAME_LEN + 1];
33
+    uint8_t *var;
34
+    uint8_t max;
35
+};
30
 
36
 
31
 struct debug_entry {
37
 struct debug_entry {
32
-    char name[DEBUG_ENTRY_NAME_LEN + 1];
38
+    char name[ENTRY_NAME_LEN + 1];
33
     enum debug_flag flag;
39
     enum debug_flag flag;
34
     uint8_t max;
40
     uint8_t max;
35
 };
41
 };
36
 
42
 
37
 BANKREF_EXTERN(main)
43
 BANKREF_EXTERN(main)
38
 
44
 
45
+#define CONF_ENTRY_COUNT 2
46
+extern const struct conf_entry conf_entries[CONF_ENTRY_COUNT];
47
+
39
 extern uint8_t debug_menu_index;
48
 extern uint8_t debug_menu_index;
40
 extern uint8_t debug_special_value;
49
 extern uint8_t debug_special_value;
41
 
50
 

+ 32
- 2
src/maps.c View File

277
     str_center("Debug Menu", 0, 0);
277
     str_center("Debug Menu", 0, 0);
278
 
278
 
279
     for (uint8_t i = 0; (i < DEBUG_ENTRY_COUNT) && (i < 7); i++) {
279
     for (uint8_t i = 0; (i < DEBUG_ENTRY_COUNT) && (i < 7); i++) {
280
-        char name_buff[DEBUG_ENTRY_NAME_LEN + 2 + 1] = {0};
280
+        char name_buff[ENTRY_NAME_LEN + 2 + 1] = {0};
281
 
281
 
282
         START_ROM_BANK(BANK(main));
282
         START_ROM_BANK(BANK(main));
283
-            strncpy(name_buff, debug_entries[i].name, DEBUG_ENTRY_NAME_LEN + 1);
283
+            strncpy(name_buff, debug_entries[i].name, ENTRY_NAME_LEN + 1);
284
 
284
 
285
             uint8_t n_len = strlen(name_buff);
285
             uint8_t n_len = strlen(name_buff);
286
             name_buff[n_len] = ' ';
286
             name_buff[n_len] = ' ';
301
     }
301
     }
302
 }
302
 }
303
 
303
 
304
+void win_conf(void) NONBANKED {
305
+    set_win_based(0, 0,
306
+                  title_map_WIDTH / title_map_TILE_W, title_map_HEIGHT / title_map_TILE_H,
307
+                  title_map_map, 0, BANK(title_map), title_map_MAP_ATTRIBUTES, BANK(title_map));
308
+
309
+    // TODO paging when more options added
310
+
311
+    str_center("Conf Menu", 0, 0);
312
+
313
+    for (uint8_t i = 0; (i < CONF_ENTRY_COUNT) && (i < 7); i++) {
314
+        char name_buff[ENTRY_NAME_LEN + 2 + 1] = {0};
315
+
316
+        START_ROM_BANK(BANK(main));
317
+            strncpy(name_buff, conf_entries[i].name, ENTRY_NAME_LEN + 1);
318
+
319
+            uint8_t n_len = strlen(name_buff);
320
+            name_buff[n_len] = ' ';
321
+            if (*conf_entries[i].var < 10) {
322
+                name_buff[n_len + 1] = *conf_entries[i].var + '0';
323
+            } else {
324
+                name_buff[n_len + 1] = *conf_entries[i].var - 10 + 'A';
325
+            }
326
+            name_buff[n_len + 2] = '\0';
327
+            n_len += 2;
328
+        END_ROM_BANK();
329
+
330
+        str(name_buff, (LINE_WIDTH - n_len) * 2, (i * 2) + 3, (debug_menu_index == i) ? 1 : 0);
331
+    }
332
+}
333
+
304
 void win_name(int32_t score) NONBANKED {
334
 void win_name(int32_t score) NONBANKED {
305
     set_win_based(0, 0,
335
     set_win_based(0, 0,
306
                   title_map_WIDTH / title_map_TILE_W, title_map_HEIGHT / title_map_TILE_H,
336
                   title_map_WIDTH / title_map_TILE_W, title_map_HEIGHT / title_map_TILE_H,

+ 1
- 0
src/maps.h View File

31
 void win_score_clear(uint8_t is_black);
31
 void win_score_clear(uint8_t is_black);
32
 void win_score_draw(struct scores score, uint8_t off, uint8_t is_black);
32
 void win_score_draw(struct scores score, uint8_t off, uint8_t is_black);
33
 void win_about(void);
33
 void win_about(void);
34
+void win_conf(void);
34
 void win_debug(void);
35
 void win_debug(void);
35
 void win_name(int32_t score);
36
 void win_name(int32_t score);
36
 void win_name_draw(uint16_t name, uint8_t is_black, uint8_t pos);
37
 void win_name_draw(uint16_t name, uint8_t is_black, uint8_t pos);

+ 16
- 2
src/sample.c View File

33
 static volatile uint8_t play_bank = 1;
33
 static volatile uint8_t play_bank = 1;
34
 static volatile const uint8_t *play_sample = 0;
34
 static volatile const uint8_t *play_sample = 0;
35
 static volatile uint16_t play_length = 0;
35
 static volatile uint16_t play_length = 0;
36
+static volatile uint8_t playing = 0;
37
+
38
+uint8_t snd_vol_sfx = 0x00;
36
 
39
 
37
 struct sfxs {
40
 struct sfxs {
38
     uint8_t bank;
41
     uint8_t bank;
55
         play_bank = sfxs[sfx].bank;
58
         play_bank = sfxs[sfx].bank;
56
         play_sample = sfxs[sfx].smp;
59
         play_sample = sfxs[sfx].smp;
57
         play_length = sfxs[sfx].len;
60
         play_length = sfxs[sfx].len;
61
+        playing = 1;
58
     }
62
     }
59
 }
63
 }
60
 
64
 
65
+uint8_t sample_running(void) BANKED {
66
+    return playing;
67
+}
68
+
61
 void sample_isr(void) NONBANKED NAKED {
69
 void sample_isr(void) NONBANKED NAKED {
62
     __asm
70
     __asm
63
     ld hl, #_play_length    ; something left to play?
71
     ld hl, #_play_length    ; something left to play?
64
     ld a, (hl+)
72
     ld a, (hl+)
65
     or (hl)
73
     or (hl)
66
-    ret z
74
+    jp z, done
67
 
75
 
68
     ld hl, #_play_sample
76
     ld hl, #_play_sample
69
     ld a, (hl+)
77
     ld a, (hl+)
93
     ldh (_NR30_REG), a
101
     ldh (_NR30_REG), a
94
     ld a, #0xFE             ; length of wave
102
     ld a, #0xFE             ; length of wave
95
     ldh (_NR31_REG), a
103
     ldh (_NR31_REG), a
96
-    ld a, #0x20             ; volume
104
+    ld a, (_snd_vol_sfx)    ; volume
105
+    swap a                  ; shift vol to upper bits
97
     ldh (_NR32_REG), a
106
     ldh (_NR32_REG), a
98
     xor a                   ; low freq bits are zero
107
     xor a                   ; low freq bits are zero
99
     ldh (_NR33_REG), a
108
     ldh (_NR33_REG), a
119
     sbc #0
128
     sbc #0
120
     ld (hl), a
129
     ld (hl), a
121
     ret
130
     ret
131
+
132
+done:
133
+    ld a, #0
134
+    ld (_playing), a
135
+    ret z
122
     __endasm;
136
     __endasm;
123
 }
137
 }

+ 3
- 0
src/sample.h View File

31
 };
31
 };
32
 
32
 
33
 void sample_play(enum SFXS sfx) BANKED;
33
 void sample_play(enum SFXS sfx) BANKED;
34
+uint8_t sample_running(void) BANKED;
34
 
35
 
35
 void sample_isr(void);
36
 void sample_isr(void);
36
 
37
 
37
 BANKREF_EXTERN(sample)
38
 BANKREF_EXTERN(sample)
38
 
39
 
40
+extern uint8_t snd_vol_sfx;
41
+
39
 #endif // __SAMPLE_H__
42
 #endif // __SAMPLE_H__

+ 6
- 4
src/sound.c View File

47
 static volatile uint16_t off = 0;
47
 static volatile uint16_t off = 0;
48
 static volatile uint16_t last_t = 0;
48
 static volatile uint16_t last_t = 0;
49
 
49
 
50
+uint8_t snd_vol_music = 0x00;
51
+
50
 struct snds {
52
 struct snds {
51
     uint8_t bank;
53
     uint8_t bank;
52
     struct music const * snd;
54
     struct music const * snd;
65
         END_ROM_BANK();
67
         END_ROM_BANK();
66
 
68
 
67
         NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
69
         NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
68
-        NR12_REG = 0x70; // half volume, no change
70
+        NR12_REG = (snd_vol_music << 4) | 0x00; // given volume, no change
69
         NR13_REG = freq & 0xFF; // given frequency
71
         NR13_REG = freq & 0xFF; // given frequency
70
         NR14_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
72
         NR14_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
71
     } else {
73
     } else {
72
         NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
74
         NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
73
-        NR12_REG = 0x10; // 'lowest' volume without pop, no change
75
+        NR12_REG = 0x00; // silence
74
         NR13_REG = 0x00; // lowest frequency
76
         NR13_REG = 0x00; // lowest frequency
75
         NR14_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
77
         NR14_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
76
     }
78
     }
83
         END_ROM_BANK();
85
         END_ROM_BANK();
84
 
86
 
85
         NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
87
         NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
86
-        NR22_REG = 0x70; // half volume, no change
88
+        NR22_REG = (snd_vol_music << 4) | 0x00; // given volume, no change
87
         NR23_REG = freq & 0xFF; // given frequency
89
         NR23_REG = freq & 0xFF; // given frequency
88
         NR24_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
90
         NR24_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
89
     } else {
91
     } else {
90
         NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
92
         NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
91
-        NR22_REG = 0x10; // 'lowest' volume without pop, no change
93
+        NR22_REG = 0x00; // silence
92
         NR23_REG = 0x00; // lowest frequency
94
         NR23_REG = 0x00; // lowest frequency
93
         NR24_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
95
         NR24_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
94
     }
96
     }

+ 2
- 0
src/sound.h View File

86
 
86
 
87
 BANKREF_EXTERN(sound)
87
 BANKREF_EXTERN(sound)
88
 
88
 
89
+extern uint8_t snd_vol_music;
90
+
89
 #endif // __SOUND_H__
91
 #endif // __SOUND_H__

Loading…
Cancel
Save