From 15b194df64db5850757d195344e682bb8ec4c138 Mon Sep 17 00:00:00 2001 From: Menase Teshale Date: Sat, 22 Aug 2026 11:23:54 +0300 Subject: Audiooo --- .gitmodules | 3 +++ CMakeLists.txt | 2 ++ assets/Music/background.mp3 | Bin 0 -> 42285312 bytes assets/pop-sound.mp3 | Bin 0 -> 23040 bytes src/app.h | 7 ++++--- src/game.c | 41 ++++++++++++++++++++++++++++++++++++++--- src/main.c | 16 +++++++++++----- vendored/SDL_mixer | 1 + 8 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 assets/Music/background.mp3 create mode 100644 assets/pop-sound.mp3 create mode 160000 vendored/SDL_mixer diff --git a/.gitmodules b/.gitmodules index ce7f8c5..ffeb0e5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "vendored/SDL_image"] path = vendored/SDL_image url = https://github.com/libsdl-org/SDL_image.git +[submodule "vendored/SDL_mixer"] + path = vendored/SDL_mixer + url = https://github.com/libsdl-org/SDL_mixer.git diff --git a/CMakeLists.txt b/CMakeLists.txt index e16158b..1c5d12d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) add_subdirectory(vendored/SDL EXCLUDE_FROM_ALL) add_subdirectory(vendored/SDL_image EXCLUDE_FROM_ALL) +add_subdirectory(vendored/SDL_mixer EXCLUDE_FROM_ALLL) add_executable(game "src/app.h" @@ -17,6 +18,7 @@ target_link_libraries(game PRIVATE SDL3::SDL3 SDL3::Headers SDL3_image::SDL3_image + SDL3_mixer::SDL3_mixer ) if(EMSCRIPTEN) set(CMAKE_EXECUTABLE_SUFFIX ".js" CACHE INTERNAL "") diff --git a/assets/Music/background.mp3 b/assets/Music/background.mp3 new file mode 100644 index 0000000..3301e0f Binary files /dev/null and b/assets/Music/background.mp3 differ diff --git a/assets/pop-sound.mp3 b/assets/pop-sound.mp3 new file mode 100644 index 0000000..28b742f Binary files /dev/null and b/assets/pop-sound.mp3 differ diff --git a/src/app.h b/src/app.h index acbd2dd..75ff2e6 100644 --- a/src/app.h +++ b/src/app.h @@ -2,18 +2,19 @@ #define APP_H_ #include +#include typedef enum { menu_state, game_state } application_state; -void init_app(SDL_Renderer* renderer); +void init_app(SDL_Renderer* renderer, MIX_Mixer* mixer); void free_app(); -void init_game(SDL_Renderer* renderer); +void init_game(SDL_Renderer* renderer, MIX_Mixer* mixer); void free_game(); -void update_game(float dt); +void update_game(float dt, MIX_Mixer* mixer); void draw_game(SDL_Renderer* renderer); void game_event(SDL_Event* e); diff --git a/src/game.c b/src/game.c index c75200e..61323a3 100644 --- a/src/game.c +++ b/src/game.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "app.h" #define MIN_TILES_TO_CLEAR 3 //More than this number of tiles per row or column will trigger clear @@ -98,6 +99,11 @@ typedef struct { float background_scaled_width; float background_scaled_height; + + //Sounds + MIX_Track* background_music_track; + MIX_Track* effect_track; + MIX_Audio* pop_audio; } s_game; extern SDL_Window* window; @@ -208,7 +214,7 @@ void clear_board() { game.num_pieces = 0; } -void init_game(SDL_Renderer* renderer) { +void init_game(SDL_Renderer* renderer, MIX_Mixer* mixer) { SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); if (game.spritesheet == NULL) { @@ -218,7 +224,6 @@ void init_game(SDL_Renderer* renderer) { if (game.background_texture == NULL) { game.background_texture = IMG_LoadTexture(renderer, "assets/Full-background.png"); - /* SDL_SetTextureScaleMode(game.background_texture, SDL_SCALEMODE_NEAREST); */ int window_height; float texture_width, texture_height; @@ -230,6 +235,27 @@ void init_game(SDL_Renderer* renderer) { game.background_scaled_height = scale * texture_height; } + if (game.background_music_track == NULL) { + MIX_Audio* music_audio = MIX_LoadAudio(mixer, "assets/Music/background.mp3", false); + game.background_music_track = MIX_CreateTrack(mixer); + + MIX_SetTrackAudio(game.background_music_track, music_audio); + MIX_DestroyAudio(music_audio); + } + + if (game.effect_track == NULL) { + game.effect_track = MIX_CreateTrack(mixer); + } + + if (game.pop_audio == NULL) { + game.pop_audio = MIX_LoadAudio(mixer, "assets/pop-sound.mp3", false); + } + + SDL_PropertiesID props = SDL_CreateProperties(); + SDL_SetNumberProperty(props, MIX_PROP_PLAY_LOOPS_NUMBER, -1); + MIX_PlayTrack(game.background_music_track, props); + SDL_DestroyProperties(props); + game.board_width = BOARD_WIDTH; game.board_height = BOARD_HEIGHT; @@ -242,6 +268,9 @@ void init_game(SDL_Renderer* renderer) { void free_game() { SDL_DestroyTexture(game.spritesheet); SDL_DestroyTexture(game.background_texture); + MIX_DestroyTrack(game.background_music_track); + MIX_DestroyTrack(game.effect_track); + MIX_DestroyAudio(game.pop_audio); } SDL_FPoint lerp_points(SDL_FPoint a, SDL_FPoint b, float fraction) { @@ -428,6 +457,11 @@ void cascade_tiles() { } } +static inline void play_sound_effect(MIX_Audio* audio) { + MIX_SetTrackAudio(game.effect_track, audio); + MIX_PlayTrack(game.effect_track, 0); +} + void update_piece_swap_animation_state() { for (int i = 0; i < game.num_pieces; i++) { if (game.pieces[i].state != static_state && game.pieces[i].state != fall_state) @@ -448,6 +482,7 @@ void update_piece_swap_animation_state() { tiles_cleared = try_clear_at(swap_y, swap_x) || tiles_cleared; if (tiles_cleared) { + play_sound_effect(game.pop_audio); cascade_tiles(); game.state = intro_state; return; @@ -486,7 +521,7 @@ void scroll_background(float dt) { } } -void update_game(float dt) { +void update_game(float dt, MIX_Mixer* mixer) { int window_width, window_height; SDL_GetWindowSizeInPixels(window, &window_width, &window_height); diff --git a/src/main.c b/src/main.c index 4c138f8..b126e92 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #define SDL_MAIN_USE_CALLBACKS #include +#include #include "app.h" @@ -15,10 +16,12 @@ SDL_Window* window; SDL_Renderer* renderer; +MIX_Mixer* mixer; + application_state app_state; SDL_AppResult SDL_AppInit(void** userdata, int nargs, char** argc) { - SDL_Init(SDL_INIT_VIDEO); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); srand(time(NULL)); window = SDL_CreateWindow(WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, 0); @@ -27,7 +30,10 @@ SDL_AppResult SDL_AppInit(void** userdata, int nargs, char** argc) { SDL_SetRenderVSync(renderer, true); #endif - init_app(renderer); + MIX_Init(); + mixer = MIX_CreateMixerDevice( SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL); + + init_app(renderer, mixer); return SDL_APP_CONTINUE; } @@ -60,7 +66,7 @@ SDL_AppResult SDL_AppIterate(void* userdata) { switch (app_state) { case game_state: - update_game(SDL_min(dt, MAX_DELTATIME)); + update_game(SDL_min(dt, MAX_DELTATIME), mixer); draw_game(renderer); break; @@ -81,8 +87,8 @@ void SDL_AppQuit(void* userdata, SDL_AppResult result) { SDL_Quit(); } -void init_app(SDL_Renderer* renderer) { - init_game(renderer); +void init_app(SDL_Renderer* renderer, MIX_Mixer* mixer) { + init_game(renderer, mixer); app_state = game_state; } diff --git a/vendored/SDL_mixer b/vendored/SDL_mixer new file mode 160000 index 0000000..f9bf1c9 --- /dev/null +++ b/vendored/SDL_mixer @@ -0,0 +1 @@ +Subproject commit f9bf1c9d67d7e7cf0c5f257be3a30b2908cc1ccc -- cgit v1.2.3