diff options
| author | Menase Teshale <osome3717@gmail.com> | 2026-08-22 11:23:54 +0300 |
|---|---|---|
| committer | Menase Teshale <osome3717@gmail.com> | 2026-08-22 11:23:54 +0300 |
| commit | 15b194df64db5850757d195344e682bb8ec4c138 (patch) | |
| tree | 70b387ae167f8fe2636fbfcaaf8b9c7869c0c435 /src/game.c | |
| parent | 05d3e3628f8d4ffe0e9dde7ecfdcb0a15d03cf23 (diff) | |
Audiooo
Diffstat (limited to 'src/game.c')
| -rw-r--r-- | src/game.c | 41 |
1 files changed, 38 insertions, 3 deletions
@@ -1,6 +1,7 @@ #include <SDL3/SDL.h> #include <SDL3_image/SDL_image.h> #include <stdlib.h> +#include <SDL3_mixer/SDL_mixer.h> #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); |
