Add background music

This commit is contained in:
2025-05-25 10:12:34 +02:00
parent dd46e0a5c1
commit 3a59f10682
12 changed files with 55 additions and 5 deletions

5
NOTICE
View File

@@ -1,7 +1,8 @@
Assets used by this project:
- "Fruit+" by SciGho https://ninjikin.itch.io/fruit
- "Fruit+" by SciGho - https://ninjikin.itch.io/fruit
License: https://creativecommons.org/licenses/by/4.0/
- "Jungle Background (Parallax)" by TrashBoat93
URL: https://trashboat93.itch.io/jungle-background-parallax
- "Jamaican Sunrise - Reggae" by DavidKBD
URL: https://davidkbd.itch.io/tropical-dreams-spring-and-summer-music-pack

View File

@@ -1,4 +1,5 @@
#include "assets.hpp"
#include "audio.hpp"
#include "sdl_types.hpp"
#include <SDL3_image/SDL_image.h>
@@ -6,11 +7,15 @@
#include <spdlog/spdlog.h>
static constexpr uint8_t BACKGROUND_DATA[] = {
#embed "jungle.jpg"
#embed "assets/images/jungle.jpg"
};
static constexpr uint8_t FRUITS_DATA[] = {
#embed "fruits.jpg"
#embed "assets/images/fruits.jpg"
};
static constexpr uint8_t BACKGROUND_MUSIC_DATA[] = {
#embed "assets/sounds/JamaicanSunrise.wav"
};
SDL_Texture *load_texture(uint8_t const *data, size_t size,
@@ -24,6 +29,18 @@ SDL_Texture *load_texture(uint8_t const *data, size_t size,
return texture;
}
AudioAsset load_audio(uint8_t const *data, size_t size) {
AudioAsset audio_asset;
auto *iostream = SDL_IOFromConstMem(data, size);
bool res = SDL_LoadWAV_IO(iostream, false, &audio_asset.spec,
&audio_asset.buffer, &audio_asset.buffer_length);
if (!res) {
spdlog::error("Failed to load audio file!\nCause: {}", SDL_GetError());
}
return audio_asset;
}
void init_assets(flecs::world &world) {
auto *renderer = world.get<SdlHandles>()->renderer;
@@ -41,4 +58,8 @@ void init_assets(flecs::world &world) {
.texture_atlas_layout = background_layout},
.fruits = Texture{.sdl_texture = fruits,
.texture_atlas_layout = fruits_layout}});
auto background_music =
load_audio(BACKGROUND_MUSIC_DATA, sizeof(BACKGROUND_MUSIC_DATA));
world.set<AudioAssets>(AudioAssets{.background_music = background_music});
}

View File

@@ -1,5 +1,7 @@
#pragma once
#include "audio.hpp"
#include <SDL3/SDL.h>
#include <flecs.h>
@@ -20,4 +22,8 @@ struct TextureAssets {
Texture fruits;
};
struct AudioAssets {
AudioAsset background_music;
};
void init_assets(flecs::world &world);

View File

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 74 KiB

View File

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 41 KiB

View File

Before

Width:  |  Height:  |  Size: 165 KiB

After

Width:  |  Height:  |  Size: 165 KiB

View File

Before

Width:  |  Height:  |  Size: 91 KiB

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

Binary file not shown.

BIN
assets/sounds/blip.wav Normal file

Binary file not shown.

10
audio.hpp Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
struct AudioAsset {
SDL_AudioSpec spec;
uint8_t *buffer;
uint32_t buffer_length;
};

View File

@@ -34,7 +34,7 @@ int main() {
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
if (!SDL_Init(SDL_INIT_VIDEO)) {
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
spdlog::critical("Failed to initialize SDL!\nCause: {}", SDL_GetError());
std::terminate();
}
@@ -152,6 +152,15 @@ int main() {
pos.y += vel.y;
});
auto *audio_assets = world.get<AudioAssets>();
auto *stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_assets->background_music.spec, NULL, NULL);
if (!stream) {
SDL_Log("Couldn't create audio stream: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_ResumeAudioStreamDevice(stream);
bool exit_gameloop = false;
while (!exit_gameloop) {
auto *input = world.get_mut<ButtonInput>();
@@ -184,6 +193,9 @@ int main() {
}
// Game Logic
if (SDL_GetAudioStreamQueued(stream) < (int)audio_assets->background_music.buffer_length) {
SDL_PutAudioStreamData(stream, audio_assets->background_music.buffer, audio_assets->background_music.buffer_length);
}
// Render
SDL_RenderClear(renderer);