From fa8d5f6a922f9159f8c050ea8b7fd6070475e00d Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Sat, 5 Apr 2025 23:31:34 +0100 Subject: Add music to SDL port --- src/all.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/all.c') diff --git a/src/all.c b/src/all.c index 7955b90..15f2558 100644 --- a/src/all.c +++ b/src/all.c @@ -139,7 +139,7 @@ static DrawList *render(State *state, UI *ui, Arena *a) { .h = cellHeight, .fill = {255, 0, 0, 63}, .border = {255, 0, 0, 255}, - .image = 5, + .image = 1, }; drawList->els[drawList->len++] = (DrawElement) { @@ -229,6 +229,10 @@ typedef struct { SDL_Texture *textures[sizeof(images) / sizeof(images[0])]; +#include "../build/music.c" + +SDL_AudioStream *stream; + int main(int argc, char **argv) { (void) argc; (void) argv; @@ -252,7 +256,7 @@ int main(int argc, char **argv) { game->state.buttonStates[i] = BUTTON_STATE_IDLE; } - SDL_Init(SDL_INIT_VIDEO); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); SDL_Window *w = SDL_CreateWindow( "LDJam 57", game->ui.width, @@ -282,6 +286,15 @@ int main(int argc, char **argv) { } SDL_UpdateTexture(textures[j], NULL, pixels, images[j].width * 4); } + + SDL_AudioSpec audioSpec = { + .format = SDL_AUDIO_S16LE, + .channels = 2, + .freq = 48000, + }; + stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audioSpec, NULL, NULL); + SDL_PutAudioStreamData(stream, musicBytes, sizeof(musicBytes)); + SDL_ResumeAudioStreamDevice(stream); for (;;) { uint64_t now = SDL_GetTicks(); @@ -351,6 +364,10 @@ int main(int argc, char **argv) { update(game, now, a); + if (SDL_GetAudioStreamQueued(stream) < (int) sizeof(musicBytes) / 8) { + SDL_PutAudioStreamData(stream, musicBytes, sizeof(musicBytes)); + } + SDL_RenderPresent(r); } -- cgit v1.2.3