Sound effects

This commit is contained in:
2025-06-01 18:22:48 +02:00
parent f9336dcbd8
commit a69fc7d2b0
7 changed files with 49 additions and 6 deletions

Binary file not shown.

BIN
assets/sounds/hit.wav Normal file

Binary file not shown.

BIN
assets/sounds/pickup.wav Normal file

Binary file not shown.

View File

@@ -25,6 +25,14 @@ static constexpr uint8_t BACKGROUND_MUSIC_DATA[] = {
#embed "../assets/sounds/JamaicanSunrise.wav"
};
static constexpr uint8_t PICKUP_SOUND_DATA[] = {
#embed "../assets/sounds/pickup.wav"
};
static constexpr uint8_t HIT_SOUND_DATA[] = {
#embed "../assets/sounds/hit.wav"
};
static constexpr uint8_t DEFAULT_FONT_DATA[] = {
#embed "../assets/fonts/OpenTTD-Sans.ttf"
};
@@ -96,7 +104,11 @@ AssetModule::AssetModule(flecs::world& world)
.basket = Texture{.sdl_texture = basket, .texture_atlas_layout = basket_layout}});
auto background_music = load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA));
world.set<AudioAssets>(AudioAssets{.background_music = background_music});
auto pickup_sound = load_audio(PICKUP_SOUND_DATA, sizeof(PICKUP_SOUND_DATA));
auto hit_sound = load_audio(HIT_SOUND_DATA, sizeof(HIT_SOUND_DATA));
world.set<AudioAssets>(AudioAssets{.background_music = background_music,
.pickup_sound = pickup_sound,
.hit_sound = hit_sound});
auto font = load_font(DEFAULT_FONT_DATA, sizeof(DEFAULT_FONT_DATA));
world.set<FontAssets>(FontAssets{.default_font = font});

View File

@@ -31,6 +31,8 @@ struct TextureAssets
struct AudioAssets
{
AudioAsset background_music;
AudioAsset pickup_sound;
AudioAsset hit_sound;
};
struct FontAsset

View File

@@ -7,9 +7,10 @@ AudioModule::AudioModule(flecs::world& world)
auto* music_stream = SDL_OpenAudioDeviceStream(
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets->background_music.spec, NULL, NULL);
auto* sound_stream = SDL_OpenAudioDeviceStream(
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets->background_music.spec, NULL, NULL);
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets->pickup_sound.spec, NULL, NULL);
SDL_ResumeAudioStreamDevice(music_stream);
SDL_ResumeAudioStreamDevice(sound_stream);
world.set<AudioStreams>(
AudioStreams{.music_stream = music_stream, .sound_stream = sound_stream});

View File

@@ -122,26 +122,54 @@ struct LevelModule
// e.destruct();
// });
world.system<Game, Position, Fruit, Collided>("CollectFruit")
world.system<Game, AudioStreams, AudioAssets, Position, Fruit, Collided>("CollectFruit")
.term_at(0)
.singleton()
.term_at(1)
.singleton()
.term_at(2)
.singleton()
.each(
[](flecs::entity e, Game& game, Position& pos, Fruit, Collided)
[](flecs::entity e,
Game& game,
AudioStreams& audio_streams,
AudioAssets& audio_assets,
Position& pos,
Fruit,
Collided)
{
game.score += 10;
pos.x += 1000;
// e.destruct();
SDL_PutAudioStreamData(audio_streams.sound_stream,
audio_assets.pickup_sound.buffer,
audio_assets.pickup_sound.buffer_length);
});
world.system<Game, Position, Spider, Collided>("CollectSpider")
world.system<Game, AudioStreams, AudioAssets, Position, Spider, Collided>("CollectSpider")
.term_at(0)
.singleton()
.term_at(1)
.singleton()
.term_at(2)
.singleton()
.each(
[](flecs::entity e, Game& game, Position& pos, Spider, Collided)
[](flecs::entity e,
Game& game,
AudioStreams& audio_streams,
AudioAssets& audio_assets,
Position& pos,
Spider,
Collided)
{
game.score -= 50;
pos.x += 1000;
// e.destruct();
SDL_PutAudioStreamData(audio_streams.sound_stream,
audio_assets.hit_sound.buffer,
audio_assets.hit_sound.buffer_length);
});
world.system<ButtonInput const, Position, Size const, Sprite const, Basket>("MoveBasket")