diff options
author | quou <quou@disroot.org> | 2023-05-07 17:09:58 +1000 |
---|---|---|
committer | quou <quou@disroot.org> | 2023-05-07 17:09:58 +1000 |
commit | 955f6ae56cdd3b20eabb44e7c9b931bdb17c2b3e (patch) | |
tree | 62f5dc92a675a5fdc48a0474d11d229cb5f2780d | |
parent | 1867e71ac2870f904e0856fd3b093abed6e8a58b (diff) |
Basic music and sound.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | config.h | 7 | ||||
-rw-r--r-- | error.h | 3 | ||||
-rw-r--r-- | game.c | 15 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | platform.c | 13 | ||||
-rw-r--r-- | player.c | 3 | ||||
-rw-r--r-- | sound.c | 105 | ||||
-rw-r--r-- | sound.h | 16 |
9 files changed, 151 insertions, 14 deletions
@@ -103,6 +103,7 @@ sources = \ rect.c \ render.c \ solid.c \ + sound.c \ spawner.c \ sprite.c \ sprite_system.c \ @@ -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 @@ -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 @@ -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( @@ -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(); @@ -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; @@ -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; } @@ -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++; + } +} @@ -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 |