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,6 +22,8 @@
22 22
 
23 23
 #include "banks.h"
24 24
 #include "score.h"
25
+#include "sample.h"
26
+#include "sound.h"
25 27
 #include "config.h"
26 28
 
27 29
 struct config_mem {
@@ -62,6 +64,9 @@ void conf_init(void) BANKED {
62 64
         mem.config.music_vol = 0x07;
63 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 72
 void conf_write_crc(void) BANKED {

+ 72
- 2
src/main.c View File

@@ -43,6 +43,11 @@ uint8_t debug_special_value = 0;
43 43
 
44 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 51
 const struct debug_entry debug_entries[DEBUG_ENTRY_COUNT] = {
47 52
     { .name = "marker",   .flag = DBG_MARKER,      .max = 1 }, // 0
48 53
     { .name = "invuln",   .flag = DBG_GOD_MODE,    .max = 1 }, // 1
@@ -98,6 +103,71 @@ static void about_screen(void) NONBANKED {
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 171
 static void splash_win(void) NONBANKED {
102 172
     HIDE_WIN;
103 173
 
@@ -223,7 +293,7 @@ static void splash(void) NONBANKED {
223 293
             highscore(0);
224 294
             splash_win();
225 295
         } else if (key_pressed(J_SELECT)) {
226
-            about_screen();
296
+            conf_screen();
227 297
             splash_win();
228 298
         } else if (key_pressed(J_START)) {
229 299
             if ((key_debug() == 0) && (!(conf_get()->debug_flags & DBG_MENU))) {
@@ -332,7 +402,7 @@ static void splash(void) NONBANKED {
332 402
                         snd_music(debug_special_value - 1);
333 403
                     }
334 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 406
                     if (debug_special_value > 0) {
337 407
                         sample_play(debug_special_value - 1);
338 408
                     }

+ 11
- 2
src/main.h View File

@@ -26,16 +26,25 @@
26 26
 #include <gbdk/platform.h>
27 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 37
 struct debug_entry {
32
-    char name[DEBUG_ENTRY_NAME_LEN + 1];
38
+    char name[ENTRY_NAME_LEN + 1];
33 39
     enum debug_flag flag;
34 40
     uint8_t max;
35 41
 };
36 42
 
37 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 48
 extern uint8_t debug_menu_index;
40 49
 extern uint8_t debug_special_value;
41 50
 

+ 32
- 2
src/maps.c View File

@@ -277,10 +277,10 @@ void win_debug(void) NONBANKED {
277 277
     str_center("Debug Menu", 0, 0);
278 278
 
279 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 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 285
             uint8_t n_len = strlen(name_buff);
286 286
             name_buff[n_len] = ' ';
@@ -301,6 +301,36 @@ void win_debug(void) NONBANKED {
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 334
 void win_name(int32_t score) NONBANKED {
305 335
     set_win_based(0, 0,
306 336
                   title_map_WIDTH / title_map_TILE_W, title_map_HEIGHT / title_map_TILE_H,

+ 1
- 0
src/maps.h View File

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

+ 16
- 2
src/sample.c View File

@@ -33,6 +33,9 @@ BANKREF(sample)
33 33
 static volatile uint8_t play_bank = 1;
34 34
 static volatile const uint8_t *play_sample = 0;
35 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 40
 struct sfxs {
38 41
     uint8_t bank;
@@ -55,15 +58,20 @@ void sample_play(enum SFXS sfx) BANKED {
55 58
         play_bank = sfxs[sfx].bank;
56 59
         play_sample = sfxs[sfx].smp;
57 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 69
 void sample_isr(void) NONBANKED NAKED {
62 70
     __asm
63 71
     ld hl, #_play_length    ; something left to play?
64 72
     ld a, (hl+)
65 73
     or (hl)
66
-    ret z
74
+    jp z, done
67 75
 
68 76
     ld hl, #_play_sample
69 77
     ld a, (hl+)
@@ -93,7 +101,8 @@ void sample_isr(void) NONBANKED NAKED {
93 101
     ldh (_NR30_REG), a
94 102
     ld a, #0xFE             ; length of wave
95 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 106
     ldh (_NR32_REG), a
98 107
     xor a                   ; low freq bits are zero
99 108
     ldh (_NR33_REG), a
@@ -119,5 +128,10 @@ void sample_isr(void) NONBANKED NAKED {
119 128
     sbc #0
120 129
     ld (hl), a
121 130
     ret
131
+
132
+done:
133
+    ld a, #0
134
+    ld (_playing), a
135
+    ret z
122 136
     __endasm;
123 137
 }

+ 3
- 0
src/sample.h View File

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

+ 6
- 4
src/sound.c View File

@@ -47,6 +47,8 @@ static volatile uint8_t bank;
47 47
 static volatile uint16_t off = 0;
48 48
 static volatile uint16_t last_t = 0;
49 49
 
50
+uint8_t snd_vol_music = 0x00;
51
+
50 52
 struct snds {
51 53
     uint8_t bank;
52 54
     struct music const * snd;
@@ -65,12 +67,12 @@ static void play_note(enum notes note) NONBANKED {
65 67
         END_ROM_BANK();
66 68
 
67 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 71
         NR13_REG = freq & 0xFF; // given frequency
70 72
         NR14_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
71 73
     } else {
72 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 76
         NR13_REG = 0x00; // lowest frequency
75 77
         NR14_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
76 78
     }
@@ -83,12 +85,12 @@ static void play_note2(enum notes note) NONBANKED {
83 85
         END_ROM_BANK();
84 86
 
85 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 89
         NR23_REG = freq & 0xFF; // given frequency
88 90
         NR24_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
89 91
     } else {
90 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 94
         NR23_REG = 0x00; // lowest frequency
93 95
         NR24_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
94 96
     }

+ 2
- 0
src/sound.h View File

@@ -86,4 +86,6 @@ void snd_play(void);
86 86
 
87 87
 BANKREF_EXTERN(sound)
88 88
 
89
+extern uint8_t snd_vol_music;
90
+
89 91
 #endif // __SOUND_H__

Loading…
Cancel
Save