QUOTE: Be someone’s rainbow today.

feat: Added sound mute button - freezo - A retro platform game

freezo

A retro platform game
git clone git@soophie.de:/srv/git/freezo
log | files | refs | readme

commit 963e99da30498506c2aa767737c83bed0e49c7ef
parent 0619481f3da52146179ce4c102f7011515b6c52c
Author: Sophie <info@soophie.de>
Date:   Tue, 17 Dec 2024 11:43:16 +0100

feat: Added sound mute button

Diffstat:
Minclude/game.h | 2++
Msrc/game.c | 34+++++++++++++++++++++++++++++++++-
Msrc/main.c | 4----
3 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/include/game.h b/include/game.h @@ -12,6 +12,7 @@ typedef struct Game game_t; #include "door.h" #define GAME_SCEEN_TIMER 150 +#define GAME_SOUND_VOLUME 0.5f typedef enum { STATE_MENU, @@ -48,6 +49,7 @@ struct Game { int sceen_timer; door_t *door; RenderTexture2D overlay; + bool is_muted; }; game_t *game_create(void); diff --git a/src/game.c b/src/game.c @@ -42,12 +42,29 @@ game_t *game_create(void) { .sceen_timer = 0, .door = NULL, .overlay = LoadRenderTexture(WINDOW_WIDTH, WINDOW_HEIGHT), + .is_muted = false, }; level_load(game, LEVEL_1); + SetSoundVolume(game->assets.track, GAME_SOUND_VOLUME); + SetSoundVolume(game->assets.locked, GAME_SOUND_VOLUME); return game; } void game_update(game_t *game) { + if (IsKeyPressed(KEY_M)) { + game->is_muted = !game->is_muted; + if (game->is_muted) { + SetSoundVolume(game->assets.track, 0.0f); + SetSoundVolume(game->assets.locked, 0.0f); + } + else { + SetSoundVolume(game->assets.track, GAME_SOUND_VOLUME); + SetSoundVolume(game->assets.locked, GAME_SOUND_VOLUME); + } + } + if (!IsSoundPlaying(game->assets.track)) { + PlaySound(game->assets.track); + } switch (game->state) { case STATE_MENU: { menu_update(game->menu, game); @@ -160,7 +177,16 @@ void game_draw(game_t *game) { tile_draw(game->tiles[i], game); } for (int i = 0; i < game->entities_len; i++) { - entity_draw(&game->entities[i], game); + entity_t *entity = &game->entities[i]; + if (entity->type == ENTITY_GATE || entity->type == ENTITY_DOOR) { + entity_draw(entity, game); + } + } + for (int i = 0; i < game->entities_len; i++) { + entity_t *entity = &game->entities[i]; + if (entity->type != ENTITY_GATE && entity->type != ENTITY_DOOR) { + entity_draw(entity, game); + } } if (game->sceen_timer < 100) { player_draw(game->player, game); @@ -183,6 +209,12 @@ void game_draw(game_t *game) { for (; i < PLAYER_HEALTH / 2; i++) { DrawTextureRec(game->assets.tiles, texture_rect(2, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { TILE_WIDTH / 2.0 + i * (TILE_WIDTH + TILE_WIDTH / 4.0), TILE_WIDTH / 2.0 }, WHITE); } + if (game->is_muted) { + DrawTextureRec(game->assets.tiles, texture_rect(5, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { TILE_WIDTH / 2.0, WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0 }, WHITE); + } + else { + DrawTextureRec(game->assets.tiles, texture_rect(4, 1, TILE_WIDTH, TILE_HEIGHT), (pos_t) { TILE_WIDTH / 2.0, WINDOW_HEIGHT - TILE_HEIGHT - TILE_HEIGHT / 2.0 }, WHITE); + } char xp_text[10]; snprintf(xp_text, 10, "%dXP", game->xp); pos_t xp_pos = (pos_t) { diff --git a/src/main.c b/src/main.c @@ -25,9 +25,6 @@ void handle_segfault(int signal) { game_t *game = NULL; void do_update(void) { - if (!IsSoundPlaying(game->assets.track)) { - PlaySound(game->assets.track); - } game_update(game); game_draw(game); } @@ -39,7 +36,6 @@ int main(void) { InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Freezo"); InitAudioDevice(); game = game_create(); - SetSoundVolume(game->assets.track, 0.5); SetTargetFPS(60); SetExitKey(0); #if defined(PLATFORM_WEB)