Browse Source

add simple sound output, noise when shooting. traveltime of objects is now independent of speed.

Thomas B 2 months ago
parent
commit
065bece792
4 changed files with 66 additions and 7 deletions
  1. 11
    4
      src/main.c
  2. 3
    3
      src/obj.c
  3. 33
    0
      src/sound.c
  4. 19
    0
      src/sound.h

+ 11
- 4
src/main.c View File

18
 #include "maps.h"
18
 #include "maps.h"
19
 #include "obj.h"
19
 #include "obj.h"
20
 #include "sprites.h"
20
 #include "sprites.h"
21
+#include "sound.h"
21
 
22
 
22
 // The metasprite will be built starting with hardware sprite zero (the first)
23
 // The metasprite will be built starting with hardware sprite zero (the first)
23
 #define SPR_NUM_START 0
24
 #define SPR_NUM_START 0
70
 
71
 
71
     spr_init();
72
     spr_init();
72
     obj_init();
73
     obj_init();
74
+    snd_init();
73
 
75
 
74
     SHOW_SPRITES;
76
     SHOW_SPRITES;
75
     SPRITES_8x8;
77
     SPRITES_8x8;
133
         }
135
         }
134
 
136
 
135
         if (KEY_PRESSED(J_B)) {
137
         if (KEY_PRESSED(J_B)) {
138
+            int8_t ret = -1;
136
             switch (rot) {
139
             switch (rot) {
137
                 case ROT_0:
140
                 case ROT_0:
138
-                    obj_add(SPR_SHOT, 0, -SHIP_OFF, 0, -SHOT_SPEED);
141
+                    ret = obj_add(SPR_SHOT, 0, -SHIP_OFF, SpdX, SpdY - SHOT_SPEED);
139
                 break;
142
                 break;
140
 
143
 
141
                 case ROT_90:
144
                 case ROT_90:
142
-                    obj_add(SPR_SHOT, SHIP_OFF, 0, SHOT_SPEED, 0);
145
+                    ret = obj_add(SPR_SHOT, SHIP_OFF, 0, SpdX + SHOT_SPEED, SpdY);
143
                 break;
146
                 break;
144
 
147
 
145
                 case ROT_180:
148
                 case ROT_180:
146
-                    obj_add(SPR_SHOT, 0, SHIP_OFF, 0, SHOT_SPEED);
149
+                    ret = obj_add(SPR_SHOT, 0, SHIP_OFF, SpdX, SpdY + SHOT_SPEED);
147
                 break;
150
                 break;
148
 
151
 
149
                 case ROT_270:
152
                 case ROT_270:
150
-                    obj_add(SPR_SHOT, -SHIP_OFF, 0, -SHOT_SPEED, 0);
153
+                    ret = obj_add(SPR_SHOT, -SHIP_OFF, 0, SpdX - SHOT_SPEED, SpdY);
151
                 break;
154
                 break;
152
 
155
 
153
                 default:
156
                 default:
154
                     break;
157
                     break;
155
             }
158
             }
159
+
160
+            if (ret == OBJ_ADDED) {
161
+                snd_noise();
162
+            }
156
         }
163
         }
157
 
164
 
158
         PosX += SpdX;
165
         PosX += SpdX;

+ 3
- 3
src/obj.c View File

18
 #include "sprites.h"
18
 #include "sprites.h"
19
 
19
 
20
 #define MAX_OBJ 10
20
 #define MAX_OBJ 10
21
-#define MAX_TRAVEL 2000
21
+#define MAX_TRAVEL 250
22
 
22
 
23
 struct obj {
23
 struct obj {
24
     uint8_t active;
24
     uint8_t active;
27
     int16_t off_y;
27
     int16_t off_y;
28
     int16_t spd_x;
28
     int16_t spd_x;
29
     int16_t spd_y;
29
     int16_t spd_y;
30
-    uint16_t travel;
30
+    uint8_t travel;
31
 };
31
 };
32
 
32
 
33
 static struct obj objs[MAX_OBJ];
33
 static struct obj objs[MAX_OBJ];
71
         objs[i].off_x += objs[i].spd_x - spd_x;
71
         objs[i].off_x += objs[i].spd_x - spd_x;
72
         objs[i].off_y += objs[i].spd_y - spd_y;
72
         objs[i].off_y += objs[i].spd_y - spd_y;
73
 
73
 
74
-        objs[i].travel += abs(objs[i].spd_x) + abs(objs[i].spd_y);
74
+        objs[i].travel += 1;
75
         if (objs[i].travel >= MAX_TRAVEL) {
75
         if (objs[i].travel >= MAX_TRAVEL) {
76
             objs[i].active = 0;
76
             objs[i].active = 0;
77
         }
77
         }

+ 33
- 0
src/sound.c View File

1
+/*
2
+ * sound.c
3
+ * Duality
4
+ *
5
+ * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
6
+ *
7
+ * Based on examples from gbdk-2020:
8
+ * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/cross-platform/metasprites/src/metasprites.c
9
+ * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/gb/galaxy/galaxy.c
10
+ * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/gb/rand/rand.c
11
+ */
12
+
13
+#include <gbdk/platform.h>
14
+
15
+#include "sound.h"
16
+
17
+void snd_init(void) {
18
+    NR52_REG = 0x80; // sound on
19
+    NR51_REG = 0xFF; // all channels on left and right
20
+
21
+#ifdef DEBUG
22
+    NR50_REG = 0x33; // left and right on half volume
23
+#else
24
+    NR50_REG = 0x77; // left and right on full volume
25
+#endif
26
+}
27
+
28
+void snd_noise(void) {
29
+    NR41_REG = 0x2F; // length timer, higher value is shorter time (up to 0x3F)
30
+    NR42_REG = 0xF0; // initially full volume, no volume changes over time
31
+    NR43_REG = 0x11; // frequency distribution
32
+    NR44_REG = 0xC0; // trigger and enable length
33
+}

+ 19
- 0
src/sound.h View File

1
+/*
2
+ * sound.h
3
+ * Duality
4
+ *
5
+ * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
6
+ *
7
+ * Based on examples from gbdk-2020:
8
+ * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/cross-platform/metasprites/src/metasprites.c
9
+ * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/gb/galaxy/galaxy.c
10
+ * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/gb/rand/rand.c
11
+ */
12
+
13
+#ifndef __SOUND_H__
14
+#define __SOUND_H__
15
+
16
+void snd_init(void);
17
+void snd_noise(void);
18
+
19
+#endif // __SOUND_H__

Loading…
Cancel
Save