Browse Source

add nicer game-over music. make more room in rom0 by banking obj functions. dont spawn stuff on top of player.

Thomas B 1 month ago
parent
commit
7b91d3b049
6 changed files with 159 additions and 39 deletions
  1. 4
    0
      src/main.c
  2. 13
    14
      src/obj.c
  3. 6
    4
      src/obj.h
  4. 9
    12
      src/sound.c
  5. 49
    1
      src/sound.h
  6. 78
    8
      src/sound_over.c

+ 4
- 0
src/main.c View File

68
     }
68
     }
69
 
69
 
70
     while (1) {
70
     while (1) {
71
+        snd_play();
71
         key_read();
72
         key_read();
72
 
73
 
73
         if (key_pressed(J_A) || key_pressed(J_B)) {
74
         if (key_pressed(J_A) || key_pressed(J_B)) {
88
     SHOW_WIN;
89
     SHOW_WIN;
89
 
90
 
90
     while (1) {
91
     while (1) {
92
+        snd_play();
91
         key_read();
93
         key_read();
92
 
94
 
93
         if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_SELECT)) {
95
         if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_SELECT)) {
312
     DISPLAY_ON;
314
     DISPLAY_ON;
313
     enable_interrupts();
315
     enable_interrupts();
314
 
316
 
317
+    snd_music_off();
315
     snd_gameover_music();
318
     snd_gameover_music();
316
 
319
 
317
     char name[3] = { 'a', 'a', 'a' };
320
     char name[3] = { 'a', 'a', 'a' };
319
     win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
322
     win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
320
 
323
 
321
     while (1) {
324
     while (1) {
325
+        snd_play();
322
         key_read();
326
         key_read();
323
 
327
 
324
         if (key_pressed(J_LEFT)) {
328
         if (key_pressed(J_LEFT)) {

+ 13
- 14
src/obj.c View File

17
  * See <http://www.gnu.org/licenses/>.
17
  * See <http://www.gnu.org/licenses/>.
18
  */
18
  */
19
 
19
 
20
-#include <gbdk/platform.h>
21
 #include <stdint.h>
20
 #include <stdint.h>
22
 #include <string.h>
21
 #include <string.h>
23
 #include <stdlib.h>
22
 #include <stdlib.h>
63
 #define GRAVITY_SHIFT (POS_SCALE_OBJS + 4)
62
 #define GRAVITY_SHIFT (POS_SCALE_OBJS + 4)
64
 
63
 
65
 #define DAMAGE_RANGE (14 << POS_SCALE_OBJS)
64
 #define DAMAGE_RANGE (14 << POS_SCALE_OBJS)
66
-#define DAMAGE_INC 5
65
+#define DAMAGE_INC 4
67
 
66
 
68
 #define HEALTH_RANGE (12 << POS_SCALE_OBJS)
67
 #define HEALTH_RANGE (12 << POS_SCALE_OBJS)
69
 #define HEALTH_INC HEALTH_MAX
68
 #define HEALTH_INC HEALTH_MAX
91
 static struct obj objs[MAX_OBJ];
90
 static struct obj objs[MAX_OBJ];
92
 static uint8_t obj_cnt[SPRITE_COUNT];
91
 static uint8_t obj_cnt[SPRITE_COUNT];
93
 
92
 
94
-void obj_init(void) NONBANKED {
93
+void obj_init(void) BANKED {
95
     memset(objs, 0, sizeof(objs));
94
     memset(objs, 0, sizeof(objs));
96
     memset(obj_cnt, 0, sizeof(obj_cnt));
95
     memset(obj_cnt, 0, sizeof(obj_cnt));
97
 }
96
 }
98
 
97
 
99
-static uint8_t is_too_close(int8_t x, int8_t y, uint8_t n, int8_t *x_c, int8_t *y_c) NONBANKED {
98
+static uint8_t is_too_close(int8_t x, int8_t y, uint8_t n, int8_t *x_c, int8_t *y_c) {
100
     for (uint8_t i = 0; i < n; i++) {
99
     for (uint8_t i = 0; i < n; i++) {
101
         int dst_x = abs(x_c[i] - x);
100
         int dst_x = abs(x_c[i] - x);
102
         int dst_y = abs(y_c[i] - y);
101
         int dst_y = abs(y_c[i] - y);
107
     return 0;
106
     return 0;
108
 }
107
 }
109
 
108
 
110
-static void generate_coords(uint8_t n, int8_t *x_c, int8_t *y_c) NONBANKED {
109
+static void generate_coords(uint8_t n, int8_t *x_c, int8_t *y_c) {
111
     int8_t x = 0;
110
     int8_t x = 0;
112
     int8_t y = 0;
111
     int8_t y = 0;
113
 
112
 
120
     y_c[n] = y;
119
     y_c[n] = y;
121
 }
120
 }
122
 
121
 
123
-void obj_spawn(void) NONBANKED {
124
-    int8_t x_coords[MAX_DARK + MAX_LIGHT + MAX_SHOT_DARK + MAX_SHOT_LIGHT];
125
-    int8_t y_coords[MAX_DARK + MAX_LIGHT + MAX_SHOT_DARK + MAX_SHOT_LIGHT];
122
+void obj_spawn(void) BANKED {
123
+    int8_t x_coords[MAX_DARK + MAX_LIGHT + MAX_SHOT_DARK + MAX_SHOT_LIGHT + 1];
124
+    int8_t y_coords[MAX_DARK + MAX_LIGHT + MAX_SHOT_DARK + MAX_SHOT_LIGHT + 1];
126
     memset(x_coords, 0, sizeof(x_coords));
125
     memset(x_coords, 0, sizeof(x_coords));
127
     memset(y_coords, 0, sizeof(y_coords));
126
     memset(y_coords, 0, sizeof(y_coords));
128
 
127
 
129
     for (uint8_t i = 0; i < MAX_DARK; i++) {
128
     for (uint8_t i = 0; i < MAX_DARK; i++) {
130
-        uint8_t n = i;
129
+        uint8_t n = i + 1;
131
         generate_coords(n, x_coords, y_coords);
130
         generate_coords(n, x_coords, y_coords);
132
         obj_add(SPR_DARK, x_coords[n], y_coords[n], 0, 0);
131
         obj_add(SPR_DARK, x_coords[n], y_coords[n], 0, 0);
133
     }
132
     }
134
     for (uint8_t i = 0; i < MAX_LIGHT; i++) {
133
     for (uint8_t i = 0; i < MAX_LIGHT; i++) {
135
-        uint8_t n = MAX_DARK + i;
134
+        uint8_t n = MAX_DARK + i + 1;
136
         generate_coords(n, x_coords, y_coords);
135
         generate_coords(n, x_coords, y_coords);
137
         obj_add(SPR_LIGHT, x_coords[n], y_coords[n], 0, 0);
136
         obj_add(SPR_LIGHT, x_coords[n], y_coords[n], 0, 0);
138
     }
137
     }
139
     for (uint8_t i = 0; i < MAX_SHOT_DARK; i++) {
138
     for (uint8_t i = 0; i < MAX_SHOT_DARK; i++) {
140
-        uint8_t n = MAX_DARK + MAX_LIGHT + i;
139
+        uint8_t n = MAX_DARK + MAX_LIGHT + i + 1;
141
         generate_coords(n, x_coords, y_coords);
140
         generate_coords(n, x_coords, y_coords);
142
         obj_add(SPR_SHOT_DARK, x_coords[n], y_coords[n], 0, 0);
141
         obj_add(SPR_SHOT_DARK, x_coords[n], y_coords[n], 0, 0);
143
     }
142
     }
144
     for (uint8_t i = 0; i < MAX_SHOT_LIGHT; i++) {
143
     for (uint8_t i = 0; i < MAX_SHOT_LIGHT; i++) {
145
-        uint8_t n = MAX_DARK + MAX_LIGHT + MAX_SHOT_LIGHT + i;
144
+        uint8_t n = MAX_DARK + MAX_LIGHT + MAX_SHOT_LIGHT + i + 1;
146
         generate_coords(n, x_coords, y_coords);
145
         generate_coords(n, x_coords, y_coords);
147
         obj_add(SPR_SHOT_LIGHT, x_coords[n], y_coords[n], 0, 0);
146
         obj_add(SPR_SHOT_LIGHT, x_coords[n], y_coords[n], 0, 0);
148
     }
147
     }
149
 }
148
 }
150
 
149
 
151
-enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_t spd_x, int16_t spd_y) NONBANKED {
150
+enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_t spd_x, int16_t spd_y) BANKED {
152
     uint8_t obj_cnt = 0xFF;
151
     uint8_t obj_cnt = 0xFF;
153
     for (uint8_t i = 0; i < MAX_OBJ; i++) {
152
     for (uint8_t i = 0; i < MAX_OBJ; i++) {
154
         if (!objs[i].active) {
153
         if (!objs[i].active) {
172
     return OBJ_ADDED;
171
     return OBJ_ADDED;
173
 }
172
 }
174
 
173
 
175
-int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *hiwater) NONBANKED {
174
+int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *hiwater) BANKED {
176
     int16_t damage = 0;
175
     int16_t damage = 0;
177
 
176
 
178
     // initial speed
177
     // initial speed

+ 6
- 4
src/obj.h View File

20
 #ifndef __OBJ_H__
20
 #ifndef __OBJ_H__
21
 #define __OBJ_H__
21
 #define __OBJ_H__
22
 
22
 
23
+#include <gbdk/platform.h>
23
 #include <stdint.h>
24
 #include <stdint.h>
25
+
24
 #include "sprites.h"
26
 #include "sprites.h"
25
 
27
 
26
 enum OBJ_STATE {
28
 enum OBJ_STATE {
28
     OBJ_LIST_FULL,
30
     OBJ_LIST_FULL,
29
 };
31
 };
30
 
32
 
31
-void obj_init(void);
32
-void obj_spawn(void);
33
-enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_t spd_x, int16_t spd_y);
34
-int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *hiwater);
33
+void obj_init(void) BANKED;
34
+void obj_spawn(void) BANKED;
35
+enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_t spd_x, int16_t spd_y) BANKED;
36
+int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *hiwater) BANKED;
35
 
37
 
36
 #endif // __OBJ_H__
38
 #endif // __OBJ_H__

+ 9
- 12
src/sound.c View File

79
     play_note(SILENCE);
79
     play_note(SILENCE);
80
 }
80
 }
81
 
81
 
82
+static void play_current_note(void) NONBANKED {
83
+    START_ROM_BANK(bank);
84
+        play_note(music->notes[off]);
85
+    END_ROM_BANK();
86
+}
87
+
82
 void snd_menu_music(void) BANKED {
88
 void snd_menu_music(void) BANKED {
83
     music = &music_menu;
89
     music = &music_menu;
84
     bank = BANK(sound_menu);
90
     bank = BANK(sound_menu);
85
     off = 0;
91
     off = 0;
86
     last_t = timer_get();
92
     last_t = timer_get();
87
-
88
-    START_ROM_BANK(bank);
89
-        play_note(music->notes[off]);
90
-    END_ROM_BANK();
93
+    play_current_note();
91
 }
94
 }
92
 
95
 
93
 void snd_game_music(void) BANKED {
96
 void snd_game_music(void) BANKED {
95
     bank = BANK(sound_game);
98
     bank = BANK(sound_game);
96
     off = 0;
99
     off = 0;
97
     last_t = timer_get();
100
     last_t = timer_get();
98
-
99
-    START_ROM_BANK(bank);
100
-    play_note(music->notes[off]);
101
-    END_ROM_BANK();
101
+    play_current_note();
102
 }
102
 }
103
 
103
 
104
 void snd_gameover_music(void) BANKED {
104
 void snd_gameover_music(void) BANKED {
106
     bank = BANK(sound_over);
106
     bank = BANK(sound_over);
107
     off = 0;
107
     off = 0;
108
     last_t = timer_get();
108
     last_t = timer_get();
109
-
110
-    START_ROM_BANK(bank);
111
-    play_note(music->notes[off]);
112
-    END_ROM_BANK();
109
+    play_current_note();
113
 }
110
 }
114
 
111
 
115
 void snd_play(void) NONBANKED {
112
 void snd_play(void) NONBANKED {

+ 49
- 1
src/sound.h View File

29
     C3, Cd3, D3, Dd3, E3, F3, Fd3, G3, Gd3, A3, Ad3, B3, // 36 .. 47
29
     C3, Cd3, D3, Dd3, E3, F3, Fd3, G3, Gd3, A3, Ad3, B3, // 36 .. 47
30
     C4, Cd4, D4, Dd4, E4, F4, Fd4, G4, Gd4, A4, Ad4, B4, // 48 .. 59
30
     C4, Cd4, D4, Dd4, E4, F4, Fd4, G4, Gd4, A4, Ad4, B4, // 48 .. 59
31
     C5, Cd5, D5, Dd5, E5, F5, Fd5, G5, Gd5, A5, Ad5, B5, // 60 .. 71
31
     C5, Cd5, D5, Dd5, E5, F5, Fd5, G5, Gd5, A5, Ad5, B5, // 60 .. 71
32
-    SILENCE, END                                         // 72 .. 73
32
+    SILENCE, END,                                        // 72 .. 73
33
+    SIL = SILENCE,
34
+
35
+    Db0 = Cd0,
36
+    Eb0 = Dd0, Ed0 = F0,
37
+    Fb0 = E0,
38
+    Gb0 = Fd0,
39
+    Ab0 = Gd0,
40
+    Bb0 = Ad0,
41
+
42
+    Cb1 = B0,
43
+    Db1 = Cd1,
44
+    Eb1 = Dd1, Ed1 = F1,
45
+    Fb1 = E1,
46
+    Gb1 = Fd1,
47
+    Ab1 = Gd1,
48
+    Bb1 = Ad1,
49
+
50
+    Cb2 = B1,
51
+    Db2 = Cd2,
52
+    Eb2 = Dd2, Ed2 = F2,
53
+    Fb2 = E2,
54
+    Gb2 = Fd2,
55
+    Ab2 = Gd2,
56
+    Bb2 = Ad2,
57
+
58
+    Cb3 = B2,
59
+    Db3 = Cd3,
60
+    Eb3 = Dd3, Ed3 = F3,
61
+    Fb3 = E3,
62
+    Gb3 = Fd3,
63
+    Ab3 = Gd3,
64
+    Bb3 = Ad3,
65
+
66
+    Cb4 = B3,
67
+    Db4 = Cd4,
68
+    Eb4 = Dd4, Ed4 = F4,
69
+    Fb4 = E4,
70
+    Gb4 = Fd4,
71
+    Ab4 = Gd4,
72
+    Bb4 = Ad4,
73
+
74
+    Cb5 = B4,
75
+    Db5 = Cd5,
76
+    Eb5 = Dd5, Ed5 = F5,
77
+    Fb5 = E5,
78
+    Gb5 = Fd5,
79
+    Ab5 = Gd5,
80
+    Bb5 = Ad5,
33
 };
81
 };
34
 
82
 
35
 struct music {
83
 struct music {

+ 78
- 8
src/sound_over.c View File

29
 BANKREF(sound_over)
29
 BANKREF(sound_over)
30
 
30
 
31
 const enum notes over_music[] = {
31
 const enum notes over_music[] = {
32
-    C3, C3, G3, G3, A3, A3, G3, SILENCE,
33
-    F3, F3, E3, E3, D3, D3, C3, SILENCE,
34
-    G3, G3, F3, F3, E3, E3, D3, D3,
35
-    G3, G3, F3, F3, E3, E3, D3, D3,
36
-    C3, C3, G3, G3, A3, A3, G3, SILENCE,
37
-    F3, F3, E3, E3, D3, D3, C3, SILENCE,
38
-    SILENCE, SILENCE, END
32
+    // fanfare
33
+    /*
34
+     C3,  C3,  C3,  C3, Ab2, Ab2, Bb2, Bb2,
35
+     C3,  C3, SIL, Bb2,  C3,  C3, SIL, SIL,
36
+    */
37
+
38
+    // repeat
39
+
40
+    Ab4,  C5, Ab4,  F4, Ab4, Ab4, Ab4,  C5,
41
+    Ab4,  F4, Ab4, Bb4,  C5, Bb4, Ab4,  F4,
42
+
43
+     A4,  C5,  A4,  F4,  A4,  A4,  A4,  C5,
44
+     A4,  F4,  A4, Bb4,  C5, Bb4,  A4,  F4,
45
+
46
+    Ab4,  C5, Ab4,  F4, Ab4, Ab4, Ab4,  C5,
47
+    Ab4,  F4, Ab4, Bb4,  C5, Bb4, Ab4,  F4,
48
+
49
+    Ab4,  C5, Ab4,  E4, Ab4, Ab4, Ab4,  C5,
50
+    Ab4,  E4, Ab4, Bb4,  C5, Bb4, Ab4,  E4,
51
+
52
+    Ab4,  C5, Ab4,  F4, Ab4, Ab4, Ab4,  C5,
53
+    Ab4,  F4, Ab4, Bb4,  C5, Bb4, Ab4,  F4,
54
+
55
+     A4,  C5,  A4,  F4,  A4,  A4,  A4,  C5,
56
+     A4,  F4,  A4, Bb4,  C5, Bb4,  A4,  F4,
57
+
58
+    Bb4, Db5, Bb4, Gb4, Bb4, Bb4, Bb4, Db5,
59
+    Bb4, Gb4, Bb4,  C5, Db5,  C5, Bb4, Gb4,
60
+
61
+    Bb4, Db5, Bb4, Gb4, Bb4, Bb4, Bb4, Db5,
62
+    Bb4, Gb4, Bb4,  C5, Db5,  C5, Bb4, Gb4,
63
+
64
+    Bb4,  D5, Bb4,  G4, Bb4, Bb4, Bb4, D5,
65
+    Bb4,  G4, Bb4,  C5,  D5,  C5, Bb4, G4,
66
+
67
+    Bb4,  D5, Bb4,  G4, Bb4, Bb4, Bb4, D5,
68
+    Bb4,  G4, Bb4,  C5,  D5,  C5, Bb4, G4,
69
+
70
+    // repeat
71
+
72
+    Ab4,  C5, Ab4,  F4, Ab4, Ab4, Ab4,  C5,
73
+    Ab4,  F4, Ab4, Bb4,  C5, Bb4, Ab4,  F4,
74
+
75
+    A4,  C5,  A4,  F4,  A4,  A4,  A4,  C5,
76
+    A4,  F4,  A4, Bb4,  C5, Bb4,  A4,  F4,
77
+
78
+    Ab4,  C5, Ab4,  F4, Ab4, Ab4, Ab4,  C5,
79
+    Ab4,  F4, Ab4, Bb4,  C5, Bb4, Ab4,  F4,
80
+
81
+    Ab4,  C5, Ab4,  E4, Ab4, Ab4, Ab4,  C5,
82
+    Ab4,  E4, Ab4, Bb4,  C5, Bb4, Ab4,  E4,
83
+
84
+    Ab4,  C5, Ab4,  F4, Ab4, Ab4, Ab4,  C5,
85
+    Ab4,  F4, Ab4, Bb4,  C5, Bb4, Ab4,  F4,
86
+
87
+    A4,  C5,  A4,  F4,  A4,  A4,  A4,  C5,
88
+    A4,  F4,  A4, Bb4,  C5, Bb4,  A4,  F4,
89
+
90
+    Bb4, Db5, Bb4, Gb4, Bb4, Bb4, Bb4, Db5,
91
+    Bb4, Gb4, Bb4,  C5, Db5,  C5, Bb4, Gb4,
92
+
93
+    Bb4, Db5, Bb4, Gb4, Bb4, Bb4, Bb4, Db5,
94
+    Bb4, Gb4, Bb4,  C5, Db5,  C5, Bb4, Gb4,
95
+
96
+    Bb4,  D5, Bb4,  G4, Bb4, Bb4, Bb4, D5,
97
+    Bb4,  G4, Bb4,  C5,  D5,  C5, Bb4, G4,
98
+
99
+    Bb4, Dd5, Bb4,  G4, Bb4, Bb4, Bb4, D5,
100
+    Bb4,  G4, Bb4,  C5,  D5,  C5, Bb4, G4,
101
+
102
+    // end
103
+
104
+    SILENCE, SILENCE, SILENCE, SILENCE,
105
+    SILENCE, SILENCE, SILENCE, SILENCE,
106
+    SILENCE, SILENCE, SILENCE, SILENCE,
107
+    SILENCE, SILENCE, SILENCE, SILENCE,
108
+    END
39
 };
109
 };
40
 
110
 
41
 const struct music music_over = {
111
 const struct music music_over = {
42
     .notes = over_music,
112
     .notes = over_music,
43
-    .duration = 200,
113
+    .duration = 120,
44
 };
114
 };

Loading…
Cancel
Save