Browse Source

refactor music and sfx functions

Thomas B 1 month ago
parent
commit
d7828e5545
8 changed files with 87 additions and 99 deletions
  1. 4
    4
      src/game.c
  2. 13
    45
      src/main.c
  3. 1
    1
      src/obj.c
  4. 20
    18
      src/sample.c
  5. 14
    3
      src/sample.h
  6. 21
    24
      src/sound.c
  7. 10
    4
      src/sound.h
  8. 4
    0
      src/sprites.c

+ 4
- 4
src/game.c View File

@@ -125,7 +125,7 @@ static void status(uint8_t health, uint8_t power, uint8_t *hiwater) NONBANKED {
125 125
 static void show_explosion(uint16_t power) NONBANKED {
126 126
     snd_music_off();
127 127
     snd_note_off();
128
-    sample_play_explosion_ship();
128
+    sample_play(SFX_EXPL_SHIP);
129 129
 
130 130
     for (uint8_t n = 0; n < (4 * 4 * 4); n++) {
131 131
         uint8_t hiwater = SPR_NUM_START;
@@ -172,7 +172,7 @@ int32_t game(void) NONBANKED {
172 172
     DISPLAY_ON;
173 173
     enable_interrupts();
174 174
 
175
-    snd_game_music();
175
+    snd_music(SND_GAME);
176 176
 
177 177
     while(1) {
178 178
         key_read();
@@ -321,7 +321,7 @@ int32_t game(void) NONBANKED {
321 321
             }
322 322
 
323 323
             if (ret == OBJ_ADDED) {
324
-                sample_play_shoot();
324
+                sample_play(SFX_SHOT);
325 325
 
326 326
                 if (score > 0) {
327 327
                     score--;
@@ -338,7 +338,7 @@ int32_t game(void) NONBANKED {
338 338
             }
339 339
 
340 340
             // restart bg music
341
-            snd_game_music();
341
+            snd_music(SND_GAME);
342 342
 
343 343
             // re-draw ship sprite
344 344
             redraw = 1;

+ 13
- 45
src/main.c View File

@@ -46,8 +46,8 @@ BANKREF(main)
46 46
 const struct debug_entry debug_entries[DEBUG_ENTRY_COUNT] = {
47 47
     { .name = "marker",   .flag = DBG_MARKER,      .max = 1 }, // 0
48 48
     { .name = "invuln",   .flag = DBG_GOD_MODE,    .max = 1 }, // 1
49
-    { .name = "music",    .flag = DBG_NONE,        .max = 3 }, // 2
50
-    { .name = "sfx-test", .flag = DBG_NONE,        .max = 3 }, // 3
49
+    { .name = "music",    .flag = DBG_NONE,        .max = SND_COUNT }, // 2
50
+    { .name = "sfx-test", .flag = DBG_NONE,        .max = SFX_COUNT }, // 3
51 51
     { .name = "cl score", .flag = DBG_CLEAR_SCORE, .max = 1 }, // 4
52 52
     { .name = "0 scores", .flag = DBG_ZERO_SCORE,  .max = 1 }, // 5
53 53
 };
@@ -148,7 +148,7 @@ static void splash_anim(uint8_t *hiwater) NONBANKED {
148 148
             spr_draw(SPR_SHIP, FLIP_NONE, -4, -42 - 1, 4, hiwater);
149 149
             if (frame == 0) {
150 150
                 obj_add(SPR_SHOT, SHIP_OFF, -42, SHOT_SPEED, 0);
151
-                sample_play_shoot();
151
+                sample_play(SFX_SHOT);
152 152
             }
153 153
             break;
154 154
 
@@ -180,7 +180,7 @@ static void splash_anim(uint8_t *hiwater) NONBANKED {
180 180
             spr_draw(SPR_SHIP, FLIP_X, 4, -42, 4, hiwater);
181 181
             if (frame == 0) {
182 182
                 obj_add(SPR_SHOT, -SHIP_OFF, -42, -SHOT_SPEED, 0);
183
-                sample_play_shoot();
183
+                sample_play(SFX_SHOT);
184 184
             }
185 185
             break;
186 186
     }
@@ -210,7 +210,7 @@ static void splash(void) NONBANKED {
210 210
     enable_interrupts();
211 211
 
212 212
     if (!(conf_get()->debug_flags & DBG_MENU)) {
213
-        snd_menu_music();
213
+        snd_music(SND_MENU);
214 214
     }
215 215
 
216 216
     while (1) {
@@ -323,50 +323,18 @@ static void splash(void) NONBANKED {
323 323
                     debug_special_value = 0;
324 324
                     conf_write_crc();
325 325
                     splash_win();
326
-                    snd_menu_music();
326
+                    snd_music(SND_MENU);
327 327
                 }
328 328
 
329 329
                 if (switch_special && (debug_menu_index == 2)) {
330
-                    switch (debug_special_value) {
331
-                        case 0:
332
-                            snd_music_off();
333
-                            snd_note_off();
334
-                            break;
335
-
336
-                        case 1:
337
-                            snd_menu_music();
338
-                            snd_note_off();
339
-                            break;
340
-
341
-                        case 2:
342
-                            snd_game_music();
343
-                            snd_note_off();
344
-                            break;
345
-
346
-                        case 3:
347
-                            snd_gameover_music();
348
-                            snd_note_off();
349
-                            break;
350
-
351
-                        default:
352
-                            break;
330
+                    snd_music_off();
331
+                    if (debug_special_value > 0) {
332
+                        snd_music(debug_special_value - 1);
353 333
                     }
334
+                    snd_note_off();
354 335
                 } else if (switch_special && (debug_menu_index == 3)) {
355
-                    switch (debug_special_value) {
356
-                        case 1:
357
-                            sample_play_shoot();
358
-                            break;
359
-
360
-                        case 2:
361
-                            sample_play_explosion_orbs();
362
-                            break;
363
-
364
-                        case 3:
365
-                            sample_play_explosion_ship();
366
-                            break;
367
-
368
-                        default:
369
-                            break;
336
+                    if (debug_special_value > 0) {
337
+                        sample_play(debug_special_value - 1);
370 338
                     }
371 339
                 }
372 340
             }
@@ -423,7 +391,7 @@ static uint16_t ask_name(int32_t score) NONBANKED {
423 391
     DISPLAY_ON;
424 392
     enable_interrupts();
425 393
 
426
-    snd_gameover_music();
394
+    snd_music(SND_GAMEOVER);
427 395
 
428 396
     char name[3] = { 'a', 'a', 'a' };
429 397
     uint8_t pos = 0;

+ 1
- 1
src/obj.c View File

@@ -342,7 +342,7 @@ int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *
342 342
 
343 343
                     if ((abs(objs[i].off_x - objs[j].off_x) <= SHOT_RANGE)
344 344
                             && (abs(objs[i].off_y - objs[j].off_y) <= SHOT_RANGE)) {
345
-                        sample_play_explosion_orbs();
345
+                        sample_play(SFX_EXPL_ORB);
346 346
 
347 347
                         objs[j].active = 0;
348 348
 

+ 20
- 18
src/sample.c View File

@@ -28,31 +28,33 @@
28 28
 #include "sfx_expl_ship.h"
29 29
 #include "sample.h"
30 30
 
31
+BANKREF(sample)
32
+
31 33
 static volatile uint8_t play_bank = 1;
32 34
 static volatile const uint8_t *play_sample = 0;
33 35
 static volatile uint16_t play_length = 0;
34 36
 
35
-void sample_play_shoot(void) NONBANKED {
36
-    CRITICAL {
37
-        play_bank = BANK(sfx_shoot);
38
-        play_sample = sfx_shoot;
39
-        play_length = sfx_shoot_SIZE >> 4;
37
+struct sfxs {
38
+    uint8_t bank;
39
+    uint8_t * const smp;
40
+    uint16_t len;
41
+};
42
+
43
+static const struct sfxs sfxs[SFX_COUNT] = {
44
+    { .bank = BANK(sfx_shoot),     .smp = sfx_shoot,     .len = sfx_shoot_SIZE >> 4 },     // SFX_SHOT
45
+    { .bank = BANK(sfx_expl_orb),  .smp = sfx_expl_orb,  .len = sfx_expl_orb_SIZE >> 4 },  // SFX_EXPL_ORB
46
+    { .bank = BANK(sfx_expl_ship), .smp = sfx_expl_ship, .len = sfx_expl_ship_SIZE >> 4 }, // SFX_EXPL_SHIP
47
+};
48
+
49
+void sample_play(enum SFXS sfx) BANKED {
50
+    if (sfx >= SFX_COUNT) {
51
+        return;
40 52
     }
41
-}
42
-
43
-void sample_play_explosion_orbs(void) NONBANKED {
44
-    CRITICAL {
45
-        play_bank = BANK(sfx_expl_orb);
46
-        play_sample = sfx_expl_orb;
47
-        play_length = sfx_expl_orb_SIZE >> 4;
48
-    }
49
-}
50 53
 
51
-void sample_play_explosion_ship(void) NONBANKED {
52 54
     CRITICAL {
53
-        play_bank = BANK(sfx_expl_ship);
54
-        play_sample = sfx_expl_ship;
55
-        play_length = sfx_expl_ship_SIZE >> 4;
55
+        play_bank = sfxs[sfx].bank;
56
+        play_sample = sfxs[sfx].smp;
57
+        play_length = sfxs[sfx].len;
56 58
     }
57 59
 }
58 60
 

+ 14
- 3
src/sample.h View File

@@ -20,9 +20,20 @@
20 20
 #ifndef __SAMPLE_H__
21 21
 #define __SAMPLE_H__
22 22
 
23
-void sample_play_shoot(void);
24
-void sample_play_explosion_orbs(void);
25
-void sample_play_explosion_ship(void);
23
+#include <gbdk/platform.h>
24
+
25
+enum SFXS {
26
+    SFX_SHOT = 0,
27
+    SFX_EXPL_ORB,
28
+    SFX_EXPL_SHIP,
29
+
30
+    SFX_COUNT
31
+};
32
+
33
+void sample_play(enum SFXS sfx) BANKED;
34
+
26 35
 void sample_isr(void);
27 36
 
37
+BANKREF_EXTERN(sample)
38
+
28 39
 #endif // __SAMPLE_H__

+ 21
- 24
src/sound.c View File

@@ -42,10 +42,21 @@ const uint16_t frequencies[SILENCE] = {
42 42
     1985, 1988, 1992, 1995, 1998, 2001, 2004, 2006, 2009, 2011, 2013, 2015  // 60 .. 71
43 43
 };
44 44
 
45
-static struct music const * music = NULL;
46
-static uint8_t bank;
47
-static uint16_t off = 0;
48
-static uint16_t last_t = 0;
45
+static volatile struct music const * music = NULL;
46
+static volatile uint8_t bank;
47
+static volatile uint16_t off = 0;
48
+static volatile uint16_t last_t = 0;
49
+
50
+struct snds {
51
+    uint8_t bank;
52
+    struct music const * snd;
53
+};
54
+
55
+static const struct snds snds[SND_COUNT] = {
56
+    { .bank = BANK(sound_menu), .snd = &music_menu }, // SND_MENU
57
+    { .bank = BANK(sound_game), .snd = &music_game }, // SND_GAME
58
+    { .bank = BANK(sound_over), .snd = &music_over }, // SND_GAMEOVER
59
+};
49 60
 
50 61
 static void play_note(enum notes note) NONBANKED {
51 62
     if (note < SILENCE) {
@@ -68,7 +79,7 @@ static void play_note(enum notes note) NONBANKED {
68 79
 static void play_note2(enum notes note) NONBANKED {
69 80
     if (note < SILENCE) {
70 81
         START_ROM_BANK(BANK(sound));
71
-        uint16_t freq = frequencies[note];
82
+            uint16_t freq = frequencies[note];
72 83
         END_ROM_BANK();
73 84
 
74 85
         NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
@@ -126,28 +137,14 @@ void snd_note_off(void) BANKED {
126 137
     play_note2(SILENCE);
127 138
 }
128 139
 
129
-void snd_menu_music(void) BANKED {
130
-    CRITICAL {
131
-        music = &music_menu;
132
-        bank = BANK(sound_menu);
133
-        off = 0;
134
-        last_t = timer_get();
135
-    }
136
-}
137
-
138
-void snd_game_music(void) BANKED {
139
-    CRITICAL {
140
-        music = &music_game;
141
-        bank = BANK(sound_game);
142
-        off = 0;
143
-        last_t = timer_get();
140
+void snd_music(enum SOUNDS snd) BANKED {
141
+    if (snd >= SND_COUNT) {
142
+        return;
144 143
     }
145
-}
146 144
 
147
-void snd_gameover_music(void) BANKED {
148 145
     CRITICAL {
149
-        music = &music_over;
150
-        bank = BANK(sound_over);
146
+        music = snds[snd].snd;
147
+        bank = snds[snd].bank;
151 148
         off = 0;
152 149
         last_t = timer_get();
153 150
     }

+ 10
- 4
src/sound.h View File

@@ -69,13 +69,19 @@ struct music {
69 69
     uint16_t repeat;
70 70
 };
71 71
 
72
-void snd_init(void) BANKED;
72
+enum SOUNDS {
73
+    SND_MENU = 0,
74
+    SND_GAME,
75
+    SND_GAMEOVER,
76
+
77
+    SND_COUNT
78
+};
73 79
 
80
+void snd_init(void) BANKED;
74 81
 void snd_music_off(void) BANKED;
75 82
 void snd_note_off(void) BANKED;
76
-void snd_menu_music(void) BANKED;
77
-void snd_game_music(void) BANKED;
78
-void snd_gameover_music(void) BANKED;
83
+void snd_music(enum SOUNDS snd) BANKED;
84
+
79 85
 void snd_play(void);
80 86
 
81 87
 BANKREF_EXTERN(sound)

+ 4
- 0
src/sprites.c View File

@@ -56,6 +56,10 @@ void spr_init_pal(void) NONBANKED {
56 56
 void spr_draw(enum SPRITES sprite, enum SPRITE_FLIP flip,
57 57
               int8_t x_off, int8_t y_off, uint8_t frame,
58 58
               uint8_t *hiwater) NONBANKED {
59
+    if (sprite >= SPRITE_COUNT) {
60
+        return;
61
+    }
62
+
59 63
     START_ROM_BANK(metasprites[sprite].bank);
60 64
 
61 65
     if (frame >= metasprites[sprite].ms_n) {

Loading…
Cancel
Save