aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2023-05-07 17:09:58 +1000
committerquou <quou@disroot.org>2023-05-07 17:09:58 +1000
commit955f6ae56cdd3b20eabb44e7c9b931bdb17c2b3e (patch)
tree62f5dc92a675a5fdc48a0474d11d229cb5f2780d
parent1867e71ac2870f904e0856fd3b093abed6e8a58b (diff)
Basic music and sound.
-rw-r--r--Makefile1
-rw-r--r--config.h7
-rw-r--r--error.h3
-rw-r--r--game.c15
-rw-r--r--main.c2
-rw-r--r--platform.c13
-rw-r--r--player.c3
-rw-r--r--sound.c105
-rw-r--r--sound.h16
9 files changed, 151 insertions, 14 deletions
diff --git a/Makefile b/Makefile
index 2bc251d..743db78 100644
--- a/Makefile
+++ b/Makefile
@@ -103,6 +103,7 @@ sources = \
rect.c \
render.c \
solid.c \
+ sound.c \
spawner.c \
sprite.c \
sprite_system.c \
diff --git a/config.h b/config.h
index 4465d68..2df5ce8 100644
--- a/config.h
+++ b/config.h
@@ -10,10 +10,9 @@
#define renderer_h 240
#define renderer_scale 2
-#define no_sound 1
+#define no_sound 0
-/* 16 KB should be enough. */
-#define asset_memory (1024 * 1024 * 16)
+#define asset_memory (1024 * 120)
#define animation_max_frames 8
@@ -24,4 +23,6 @@
#define wave_max_subwaves 8
#define wave_max_spawns 16
+#define max_beeps 32
+
#endif
diff --git a/error.h b/error.h
index bd2dbd4..1065dcc 100644
--- a/error.h
+++ b/error.h
@@ -6,7 +6,8 @@ typedef enum {
error_file_not_found,
error_out_of_memory,
error_invalid_asset,
- error_gameplay_error
+ error_gameplay_error,
+ error_sound_error
} Error;
#endif
diff --git a/game.c b/game.c
index 064fc22..bb96e43 100644
--- a/game.c
+++ b/game.c
@@ -3,6 +3,7 @@
#include "game.h"
#include "input.h"
#include "platform.h"
+#include "sound.h"
#include "sprite.h"
#include "standard.h"
#include "systems.h"
@@ -18,6 +19,8 @@ void gameplay_new(Game* game) {
init_player(&world->player, world);
init_waver(&world->waver);
world->gmemory = gmemory_max;
+
+ set_song(song_main);
}
void gameplay_next_wave(Game* game) {
@@ -68,6 +71,8 @@ static void menu_init(Game* game) {
font = get_default_font();
+ set_song(song_menu);
+
init_menu(&game->menu, game);
menu_add(&game->menu, "Play", menu_start, font);
menu_add(&game->menu, "Credits", menu_credits, font);
@@ -266,7 +271,6 @@ static void credits_init(Game* game) {
}
static void credits_update(Game* game) {
- Colour colour;
const BM_Font* font;
font = get_default_font();
@@ -282,7 +286,7 @@ static void credits_update(Game* game) {
rfont_text(font, 0, 10, "Design: quou");
rfont_text(font, 0, 20, "Sound: quou");
rfont_text(font, 0, 30, "Graphics: quou");
- rfont_text(font, 0, 40, "Music: drummyfish");
+ rfont_text(font, 0, 20, "Music: tejeez");
rfont_text(font, 0, 50, "Source Code: git.quou.xyz");
}
@@ -293,6 +297,8 @@ static void dead_init(Game* game) {
const BM_Font* font;
Menu* menu;
+ set_song(song_dead);
+
menu = &game->menu;
font = get_default_font();
init_menu(menu, game);
@@ -324,11 +330,12 @@ static void dead_update(Game* game) {
"Out of Memory"
);
} else if (world->oom == -1) {
- rfont_text(
+ rfont_text_col(
font,
renderer_w / 2 - 35,
game->menu.y - 15,
- "You win"
+ "YOU WIN",
+ make_colour(0xc00000, 255)
);
} else {
rfont_text(
diff --git a/main.c b/main.c
index 4f1c979..f4306d6 100644
--- a/main.c
+++ b/main.c
@@ -1,10 +1,12 @@
#include "game.h"
#include "standard.h"
+#include "sound.h"
Game game;
void on_init(int argc, char** argv) {
init_renderer();
+ init_sound();
load_assets();
diff --git a/platform.c b/platform.c
index 9bd5300..f3b45cb 100644
--- a/platform.c
+++ b/platform.c
@@ -1,6 +1,7 @@
#include "config.h"
-#include "render.h"
#include "input.h"
+#include "render.h"
+#include "sound.h"
#if defined(plat_emscripten)
#include <emscripten.h>
@@ -145,17 +146,17 @@ int audio_len, audio_pos;
int ticker;
void fill_audio(void *udata, Uint8 *stream, int len) {
- /*if (audio_len == 0) {
+ if (audio_len == 0) {
audio_len = 1024;
audio_pos = 0;
}
len = (len > audio_len ? audio_len : len);
- sound_sys.mix(((unsigned short*)stream), len);
+ sound_mix(((unsigned char*)stream), len);
audio_pos += len;
- audio_len -= len;*/
+ audio_len -= len;
}
void reset_input() {
@@ -363,8 +364,8 @@ int main(int argc, char** argv) {
SDL_SetWindowTitle(window, game_name);
#if !no_sound
- aud_spec.freq = 22050;
- aud_spec.format = AUDIO_U16;
+ aud_spec.freq = 8000;
+ aud_spec.format = AUDIO_U8;
aud_spec.channels = 1;
aud_spec.samples = 1024;
aud_spec.callback = fill_audio;
diff --git a/player.c b/player.c
index 9075048..dbd318d 100644
--- a/player.c
+++ b/player.c
@@ -6,6 +6,7 @@
#include "game_config.h"
#include "input.h"
#include "player.h"
+#include "sound.h"
#include "standard.h"
#include "world.h"
@@ -132,6 +133,8 @@ void update_player(Player* player, World* world) {
100
);
+ play_beep(30, 1000);
+
player->shoot_countdown = player->shoot_cooldown;
}
diff --git a/sound.c b/sound.c
new file mode 100644
index 0000000..611bca9
--- /dev/null
+++ b/sound.c
@@ -0,0 +1,105 @@
+#include "config.h"
+#include "sound.h"
+
+#if DEBUG
+#include "error.h"
+#include "platform.h"
+#endif
+
+typedef struct {
+ int pitch;
+ int length;
+} Beep;
+
+static struct {
+ unsigned t;
+ Song song;
+ Beep beeps[max_beeps];
+ int beep_count;
+} sys;
+
+typedef unsigned char (*Song_Func)(unsigned t);
+
+static unsigned char menu_song(unsigned t) {
+ return 0;
+}
+
+static unsigned char main_song(unsigned t) {
+ /* From tejeez 2011-10-05
+ * I didn't have time to make my own music PepeHands */
+ return (~t>>2)*((127&t*(7&t>>10))<(245&t*(2+(5&t>>14))));
+}
+
+static unsigned char dead_song(unsigned t) {
+ return 0;
+}
+
+static unsigned char win_song(unsigned t) {
+ return 0;
+}
+
+void init_sound() {
+ sys.t = 0;
+ sys.song = song_menu;
+}
+
+void set_song(Song song) {
+ sys.song = song;
+ sys.t = 0;
+}
+
+Song_Func get_current_song_f() {
+ switch (sys.song) {
+ case song_menu:
+ return menu_song;
+ case song_main:
+ return main_song;
+ case song_win:
+ return win_song;
+ case song_dead:
+ return dead_song;
+ }
+ return 0;
+}
+
+void play_beep(int pitch, int length) {
+ Beep* beep;
+
+#if DEBUG
+ if (sys.beep_count >= max_beeps) {
+ platform_err("Too many beeps.\n");
+ platform_abort(error_sound_error);
+ }
+#endif
+
+ beep = &sys.beeps[sys.beep_count++];
+ beep->pitch = pitch;
+ beep->length = length;
+}
+
+void sound_mix(unsigned char* stream, int len) {
+ int i, j;
+ Song_Func f;
+ Beep* beep;
+
+ f = get_current_song_f();
+
+ for (i = 0; i < len; i++) {
+/* stream[i] = sys.t % 50;*/
+ stream[i] = 0;
+ stream[i] = f(sys.t) / 2;
+ for (j = sys.beep_count - 1; j >= 0; j--) {
+ beep = &sys.beeps[j];
+ stream[i] |= (sys.t % beep->pitch) * 5;
+ beep->length--;
+
+ if (beep->length <= 0) {
+ if (sys.beep_count > 1) {
+ *beep = sys.beeps[sys.beep_count - 1];
+ }
+ sys.beep_count--;
+ }
+ }
+ sys.t++;
+ }
+}
diff --git a/sound.h b/sound.h
new file mode 100644
index 0000000..bde891b
--- /dev/null
+++ b/sound.h
@@ -0,0 +1,16 @@
+#ifndef sound_h
+#define sound_h
+
+typedef enum {
+ song_menu,
+ song_main,
+ song_win,
+ song_dead
+} Song;
+
+void init_sound();
+void sound_mix(unsigned char* stream, int len);
+void set_song(Song song);
+void play_beep(int pitch, int length);
+
+#endif