feat: sound effects & working death

This commit is contained in:
Martin Prokoph
2025-03-01 00:20:45 +01:00
parent 6e82495bb5
commit 16a11bb9e1
7 changed files with 48 additions and 3 deletions

View File

@@ -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()

View File

@@ -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/
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/

BIN
src/assets/failure.ogg Normal file

Binary file not shown.

BIN
src/assets/success.ogg Normal file

Binary file not shown.

18
src/events.hpp Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include <SDL3/SDL.h>
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);
}

View File

@@ -9,6 +9,7 @@
#include <filesystem>
#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);
}

View File

@@ -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();
}
/**