diff --git a/CMakeLists.txt b/CMakeLists.txt index a53249a..ccc7b3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -154,6 +154,8 @@ else() copy_helper("assets/Inter-VariableFont.ttf") copy_helper("assets/MartianMono-VariableFont.ttf") copy_helper("assets/sunset_on_the_beach.ogg") + copy_helper("assets/success.ogg") + copy_helper("assets/failure.ogg") copy_helper("assets/gs_tiger.svg") endif() diff --git a/src/assets/attributions.txt b/src/assets/attributions.txt index 46ba3af..20cc0f2 100644 --- a/src/assets/attributions.txt +++ b/src/assets/attributions.txt @@ -1,3 +1,6 @@ "Sunset On The Beach" Kumiku Licensed under CC0 1.0 -https://freemusicarchive.org/music/Komiku/Poupis_incredible_adventures_/Komiku_-_Poupis_incredible_adventures__-_55_Sunset_on_the_beach/ \ No newline at end of file +https://freemusicarchive.org/music/Komiku/Poupis_incredible_adventures_/Komiku_-_Poupis_incredible_adventures__-_55_Sunset_on_the_beach/ + +Success sound (CC0): https://freesound.org/people/jolup123/sounds/668791/ +Failure sound (CC0): https://freesound.org/people/_def/sounds/751863/ \ No newline at end of file diff --git a/src/assets/failure.ogg b/src/assets/failure.ogg new file mode 100644 index 0000000..fa25db6 Binary files /dev/null and b/src/assets/failure.ogg differ diff --git a/src/assets/success.ogg b/src/assets/success.ogg new file mode 100644 index 0000000..ef58bcd Binary files /dev/null and b/src/assets/success.ogg differ diff --git a/src/events.hpp b/src/events.hpp new file mode 100644 index 0000000..ae1cab5 --- /dev/null +++ b/src/events.hpp @@ -0,0 +1,18 @@ +#pragma once +#include + +const Uint32 playerDeathEvent = SDL_RegisterEvents(1); +static SDL_Event ADVENTURA_DEATH_EVENT; + +static void registerEvents() { + if (playerDeathEvent != 0) { + SDL_Event event; + SDL_zero(ADVENTURA_DEATH_EVENT); + ADVENTURA_DEATH_EVENT.type = playerDeathEvent; + ADVENTURA_DEATH_EVENT.user.code = 1; + SDL_PushEvent(&event); + } +} +static void emitDeathEvent() { + SDL_PushEvent(&ADVENTURA_DEATH_EVENT); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 6aa2ced..bca89a9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include #include "adventura.cpp" +#include "events.hpp" constexpr uint32_t windowStartWidth = 1200; constexpr uint32_t windowStartHeight = 800; @@ -16,6 +17,7 @@ constexpr uint32_t windowStartHeight = 800; void loadWorld(void* appstate, string worldFile); void resetWorld(void* appstate); void loadNextWorld(void* appstate); +void playSound(void* appstate, string soundFile); struct AppContext { SDL_Window* window; @@ -28,6 +30,7 @@ struct AppContext { Player* player; World* world; TTF_Font* font; + std::filesystem::path basePath; }; SDL_AppResult SDL_Fail(){ @@ -40,6 +43,7 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) { if (not SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)){ return SDL_Fail(); } + registerEvents(); // init TTF if (not TTF_Init()) { @@ -148,7 +152,8 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) { .music = music, .player = player, .world = world, - .font = font + .font = font, + .basePath = basePath }; loadNextWorld(*appstate); @@ -177,8 +182,13 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event* event) { } if ((*app->player).hasReachedGoal()) { + playSound(appstate, "assets/success.ogg"); loadNextWorld(appstate); } + if (event->type == playerDeathEvent) { + playSound(appstate, "assets/failure.ogg"); + resetWorld(appstate); + } if (event->type == SDL_EVENT_QUIT) { app->app_quit = SDL_APP_SUCCESS; @@ -281,4 +291,14 @@ void loadNextWorld(void* appstate) { resetWorld(appstate); // You won } +} +void playSound(void* appstate, string soundFile) { + auto* app = (AppContext*)appstate; + // load the sound + auto soundPath = app->basePath / soundFile; + auto sound = Mix_LoadWAV(soundPath.string().c_str()); + if (not sound) { + return; + } + Mix_PlayChannel(-1, sound, 0); } \ No newline at end of file diff --git a/src/player.hpp b/src/player.hpp index f3e1d0c..5b549b4 100644 --- a/src/player.hpp +++ b/src/player.hpp @@ -5,6 +5,7 @@ #include "blockPos.hpp" #include "output.hpp" +#include "events.hpp" class Player { public: @@ -55,7 +56,7 @@ public: */ void setPos(BlockPos newPos) { if (!world.containsPos(newPos)) { - alive = false; + unalive(); return; } this->pos = newPos; @@ -90,6 +91,7 @@ public: playerTexture = DEAD_PLAYER_TEXTURE; //redraw(world, this->mapToWorldspace()); alive = false; + emitDeathEvent(); } /**