diff options
-rw-r--r-- | Makefile | 20 | ||||
-rw-r--r-- | src/all.c | 21 | ||||
-rw-r--r-- | src/index.html.in | 17 |
3 files changed, 52 insertions, 6 deletions
@@ -1,8 +1,8 @@ -build/main: src/all.c src/all.h build/images.c src/types.c src/levels.c src/tick.c +build/main: src/all.c build/images.c build/music.c src/types.c src/levels.c src/tick.c mkdir -p build gcc -Wall -Wextra -Wpedantic -DSDL $$(pkg-config --libs sdl3) -o build/main src/all.c -build/main.wasm: src/all.c src/all.h +build/main.wasm: src/all.c src/types.c src/levels.c src/tick.c mkdir -p build clang --target=wasm32 -nostdlib -DWASM -Wall -Wextra -Wpedantic \ -Wl,--no-entry -fno-builtin -o build/main.wasm src/all.c @@ -11,11 +11,15 @@ build/music.mp3.b64: res/music.mp3 mkdir -p build (printf '"data:audio/mpeg;base64,'; base64 < res/music.mp3 | tr -d '\n'; printf '"') > build/music.mp3.b64 +build/%.png.b64: res/%.png + mkdir -p build + (printf '"data:image/png;base64,'; base64 < $< | tr -d '\n'; printf '"') > $@ + build/main.wasm.b64: build/main.wasm mkdir -p build (printf '"'; base64 < build/main.wasm | tr -d '\n'; printf '"') > build/main.wasm.b64 -build/index.html: src/index.html.in build/main.wasm.b64 build/music.mp3.b64 +build/index.html: src/index.html.in build/main.wasm.b64 build/music.mp3.b64 build/continue.png.b64 mkdir -p build clang -E -P -undef -nostdinc -x c -o build/index.html src/index.html.in @@ -27,6 +31,16 @@ build/%.qoi: res/%.png mkdir -p build magick $< $@ +build/music.pcm: res/music.mp3 + ffmpeg -i res/music.mp3 -acodec pcm_s16le -f s16le build/music.pcm + +build/music.c: build/music.pcm + ( \ + echo 'unsigned char musicBytes[] = {' && \ + xxd -i < build/music.pcm && \ + echo '};' \ + ) > build/music.c + build/images.c: build/continue.qoi build/exit.qoi build/pause.qoi build/play.qoi build/restart.qoi build/img mkdir -p build (\ @@ -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); } diff --git a/src/index.html.in b/src/index.html.in index aca8b6d..c239f6e 100644 --- a/src/index.html.in +++ b/src/index.html.in @@ -39,6 +39,11 @@ const WASM = const MUSIC = #include "../build/music.mp3.b64" +const IMAGES = [ +#include "../build/continue.png.b64" +, +]; + async function main() { let bytes = Uint8Array.from(atob(WASM), function(c) { return c.charCodeAt(0); @@ -57,6 +62,13 @@ async function main() { audio.loop = true; let musicPlaying = false; + let images = [null]; + for (let i = 0; i < IMAGES.length; i++) { + const image = new Image(); + image.src = IMAGES[i]; + images.push(image); + } + const start = Date.now(); function now() { return Date.now() - start; @@ -79,7 +91,7 @@ async function main() { let ops = dl.subarray(1); for (let i = 0; i < len; i++) { - let op = ops.subarray(6*i, 6*i+6); + let op = ops.subarray(7*i, 7*i+7); const color = new Uint8Array(new Uint32Array(op.subarray(4, 6)).buffer); ctx.fillStyle = `#${color[0].toString(16).padStart(2, "0")}${color[1].toString(16).padStart(2, "0")}${color[2].toString(16).padStart(2, "0")}`; ctx.globalAlpha = color[3] / 255; @@ -87,6 +99,9 @@ async function main() { ctx.strokeStyle = `#${color[4].toString(16).padStart(2, "0")}${color[5].toString(16).padStart(2, "0")}${color[6].toString(16).padStart(2, "0")}`; ctx.globalAlpha = color[7] / 255; ctx.strokeRect(op[0], op[1], op[2], op[3]); + if (op[6] !== 0) { + ctx.drawImage(images[op[6]], op[0], op[1], op[2], op[3]); + } } } |