diff options
-rw-r--r-- | 1bitjam.c | 4 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | config.h | 3 | ||||
-rw-r--r-- | plat.c | 98 | ||||
-rw-r--r-- | plat.h | 4 |
5 files changed, 111 insertions, 1 deletions
@@ -23,6 +23,7 @@ int entrypoint(int argc, const char** argv, Arena* m) { app_memory_size ); a = new_app(&h, game_name); + init_audio(); init_fps(&f, default_mpf); while (a->o) { fps_begin(&f); @@ -38,6 +39,8 @@ int entrypoint(int argc, const char** argv, Arena* m) { y--; if (a->btn_states[btn_down] & btn_state_pressed) y++; + if (a->btn_states[btn_shoot] & btn_state_just_pressed) + play_sound(30); ren_text(&r, 30, 60, "Hello"); ren_map(&r, 10, 5, &rect, get_bitmap(asset_id_hello_img)); ren_map(&r, x, y, &rect2, get_bitmap(asset_id_guy_img)); @@ -47,6 +50,7 @@ int entrypoint(int argc, const char** argv, Arena* m) { } fps_end(&f); } + deinit_audio(); deinit_app(a); return error_none; } @@ -3,8 +3,9 @@ tool_compiler = gcc linker = gcc cflags = -I./ -g -DDEBUG -Dplat_x11 -Dplat_x86 \ -Dplat_posix -Dallocation_default_alignment=8 \ +-Dplat_pulse \ -Wall -Wextra -pedantic -std=c90 -lflags = -lX11 -lXi +lflags = -lX11 -lXi -lpulse -lpulse-simple -lpthread -lm target = 1bitjam int_dir = intermediate data_dir = data @@ -14,4 +14,7 @@ #define default_scale 3 #define default_mpf 50 +#define audio_buffer_size 64 +#define audio_sample_rate 8000 + #endif @@ -427,3 +427,101 @@ void app_end(App* a) { } #endif + + +#ifdef plat_pulse +#include "maths.h" +#define inline /* erm sir we are using C90 here... */ +#include <stdlib.h> +#include <math.h> +#include <pulse/simple.h> +#include <pthread.h> + +struct { + short* buf; + pa_simple* dev; + int r, play, time; + pthread_t worker; + pthread_mutex_t mutex; +} audio; + +void* audio_worker(void* arg) { + const int s = audio_buffer_size; + int i, c = 1; + short* buf = audio.buf; + pa_simple* dev = audio.dev; + (void)arg; + while (c) { + pthread_mutex_lock(&audio.mutex); + if (!audio.r) c = 0; + for ( + i = 0; + i < s && audio.play; + i++, audio.play--, audio.time++ + ) buf[i] = (short)(sin((double)audio.time / 0.05) * 16000.0); + pthread_mutex_unlock(&audio.mutex); + for (; i < s; i++) { + buf[i] = 0; + } + pa_simple_write(dev, buf, s * sizeof(short), 0); + } + return 0; +} + +void init_audio(void) { + pa_sample_spec sp = { 0 }; + sp.format = PA_SAMPLE_S16LE; + sp.channels = 1; + sp.rate = audio_sample_rate; + audio.dev = pa_simple_new( + 0, + game_name, + PA_STREAM_PLAYBACK, + 0, + "Game Audio", + &sp, + 0, + 0, + 0 + ); + audio.r = 0; + audio.play = 0; + if (!audio.dev) { + print_err("Failed to create audio device.\n"); + print_war("Running without audio.\n"); + return; + } + audio.r = 1; + audio.buf = malloc( + audio_buffer_size * + sizeof *audio.buf + ); + pthread_mutex_init(&audio.mutex, 0); + pthread_create( + &audio.worker, + 0, + audio_worker, + 0 + ); +} + +void deinit_audio(void) { + if (audio.dev) { + pthread_mutex_lock(&audio.mutex); + audio.r = 0; + pthread_mutex_unlock(&audio.mutex); + pthread_mutex_destroy(&audio.mutex); + pthread_join(audio.worker, 0); + pa_simple_drain(audio.dev, 0); + pa_simple_free(audio.dev); + free(audio.buf); + } +} + +void play_sound(int len) { + pthread_mutex_lock(&audio.mutex); + audio.play += len; + pthread_mutex_unlock(&audio.mutex); +} + +#endif @@ -73,4 +73,8 @@ void deinit_app(App* a); void app_begin(App* a); void app_end(App* a); +void init_audio(void); +void deinit_audio(void); +void play_sound(int len); + #endif |