From 88afb540821c4457d2d256f9b25058109f4a60f2 Mon Sep 17 00:00:00 2001 From: huaqo-games Date: Wed, 15 Oct 2025 21:58:42 +0200 Subject: [PATCH] feat: soundOn flag --- external/engine/soundtrack.h | 22 +++++++++++++++++++--- include/app.h | 7 ++++--- include/flags.h | 1 + include/game.h | 4 ++-- include/logo.h | 4 ++-- include/menu.h | 12 ++++++------ 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/external/engine/soundtrack.h b/external/engine/soundtrack.h index 07d010e..b790d18 100644 --- a/external/engine/soundtrack.h +++ b/external/engine/soundtrack.h @@ -4,19 +4,35 @@ typedef struct { Music music; bool playing; + bool paused; } Soundtrack; Soundtrack LoadSoundtrack(const char* musicPath){ Soundtrack soundtrack = { .music = LoadMusicStream(musicPath), - .playing = 0 + .playing = false, + .paused = false }; PlayMusicStream(soundtrack.music); return soundtrack; } -void UpdateSoundtrack(Soundtrack *soundtrack){ - UpdateMusicStream(soundtrack->music); +void UpdateSoundtrack(Soundtrack *soundtrack, bool playing){ + + if (playing && !soundtrack->playing){ + ResumeMusicStream(soundtrack->music); + soundtrack->paused = false; + } else if (!playing && soundtrack->playing){ + PauseMusicStream(soundtrack->music); + soundtrack->paused = true; + } + + soundtrack->playing = playing; + + if (soundtrack->playing) { + UpdateMusicStream(soundtrack->music); + } + } #endif // SOUNDTRACK_H diff --git a/include/app.h b/include/app.h index 4e999c6..95f45dc 100644 --- a/include/app.h +++ b/include/app.h @@ -27,6 +27,7 @@ void ConfigApp(App* app){ InitConfig(&app->config, "config/config.ini"); app->state.currentView = GetConfigInt(&app->config, "startView"); app->flags.showFPS = GetConfigInt(&app->config, "showFPS"); + app->flags.soundOn = GetConfigInt(&app->config, "soundOn"); app->window.title = GetConfigString(&app->config, "window_title"); app->window.width = GetConfigInt(&app->config, "window_width"); app->window.height = GetConfigInt(&app->config, "window_height"); @@ -51,16 +52,16 @@ void UpdateApp(App *app){ case LOGO: { UpdateLogo(&app->logo, &app->state.currentView, &app->state.running); - RenderLogo(&app->logo, &app->flags.showFPS); + RenderLogo(&app->logo, &app->flags); }break; case MENU: { - UpdateMenu(&app->menu, &app->state.currentView, &app->state.running); + UpdateMenu(&app->menu, &app->state, &app->flags); RenderMenu(&app->menu, &app->font, app->window.title, &app->flags); }break; case GAMEPLAY: { - UpdateGame(&app->game, &app->state.currentView, &app->state.running); + UpdateGame(&app->game, &app->state.currentView, &app->state.running, &app->flags); RenderGame(&app->game, &app->state, &app->flags); }break; default: break; diff --git a/include/flags.h b/include/flags.h index de09d9e..ef8faf4 100644 --- a/include/flags.h +++ b/include/flags.h @@ -3,6 +3,7 @@ typedef struct { bool showFPS; + bool soundOn; } Flags; #endif // FLAGS_H diff --git a/include/game.h b/include/game.h index 05bbb73..d0a0628 100644 --- a/include/game.h +++ b/include/game.h @@ -75,7 +75,7 @@ void InitGame(GameState *g, Window *window) g->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &g->textures[CURSOR]); } -void UpdateGame(GameState *g, View *currentView, bool *running) +void UpdateGame(GameState *g, View *currentView, bool *running, Flags *flags) { if (GetFPS() <= 60) { @@ -95,7 +95,7 @@ void UpdateGame(GameState *g, View *currentView, bool *running) } for (int i = 0; i < SOUNDTRACK_COUNT; i++) { - UpdateSoundtrack(&g->soundtracks[i]); + UpdateSoundtrack(&g->soundtracks[i], flags->soundOn); } UpdateMouse(&g->mouse, &g->camera); diff --git a/include/logo.h b/include/logo.h index ec5534d..6b59944 100644 --- a/include/logo.h +++ b/include/logo.h @@ -78,7 +78,7 @@ void UpdateLogo(LogoState *l, View *currentView, bool *running) { } } -void RenderLogo(const LogoState *l, bool *showFPS) { +void RenderLogo(const LogoState *l, Flags *flags) { BeginDrawing(); ClearBackground(RAYWHITE); @@ -107,7 +107,7 @@ void RenderLogo(const LogoState *l, bool *showFPS) { DrawText(TextSubtext("raylib", 0, l->lettersCount), GetScreenWidth() / 2 - 44, GetScreenHeight() / 2 + 48, 50, Fade(BLACK, l->alpha)); } - if (*showFPS){ + if (flags->showFPS){ DrawFPS(10, 10); } diff --git a/include/menu.h b/include/menu.h index 2c91722..2cd5c27 100644 --- a/include/menu.h +++ b/include/menu.h @@ -102,13 +102,13 @@ void InitMenu(MenuState *m) m->quitButton = CreateImageButton(NOT_CLICKED, &m->textures[MENU_BIG_BUTTONS_TEX], &m->assets.textureAssets[MENU_BIG_BUTTONS_TEX], &quitButtonNotPressed, &quitButtonPressed, &quitButtonHover, &quitButtonDestRec); } -void UpdateMenu(MenuState *m, View *currentView, bool *running) +void UpdateMenu(MenuState *m, State *state, Flags *flags) { for (int i = 0; i < MENU_SOUNDTRACK_COUNT; i++) { - UpdateSoundtrack(&m->soundtracks[i]); - } + UpdateSoundtrack(&m->soundtracks[i], flags->soundOn); + } if (GetFPS() >= 60) { @@ -117,13 +117,13 @@ void UpdateMenu(MenuState *m, View *currentView, bool *running) if (isImageButtonClicked(&m->playButton, m->sounds[HOVER_SOUND], m->sounds[CLICK_SOUND])) { - *running = true; - *currentView = GAMEPLAY; + state->running = true; + state->currentView = GAMEPLAY; } if (isImageButtonClicked(&m->quitButton, m->sounds[HOVER_SOUND], m->sounds[CLICK_SOUND]) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) { - *running = false; + state->running = false; } UpdateBackground(&m->background);