diff --git a/cat_api.h b/cat_api.h index 25271cc..1b9e768 100644 --- a/cat_api.h +++ b/cat_api.h @@ -30,6 +30,12 @@ static SDL_Renderer* renderer = NULL; static SDL_Texture* texture = NULL; static int screen_width = 0; static int screen_height = 0; +/* audio */ +static unsigned int wav_length; +static unsigned char* wav_buffer; +static SDL_AudioSpec wav_spec; +static SDL_AudioDeviceID device_id = 0; +static int cat_audio_playing = 0; void cat_pixel(int layer, int visible, int x, int y, unsigned char r, unsigned char g, unsigned char b) { @@ -118,6 +124,29 @@ void cat_fill(int layer, int visible, int x, int y, int w, int h, unsigned char /* TODO: cat bresenham */ +/* audio... */ + +void cat_stop_audio(void) { + SDL_PauseAudioDevice(device_id, 1); + SDL_CloseAudioDevice(device_id); + SDL_FreeWAV(wav_buffer); + cat_audio_playing = 0; + /*SDL_Quit();*/ +} + +void cat_play_wav(char* file) +{ + if (cat_audio_playing) cat_stop_audio(); + cat_audio_playing = 1; + if (SDL_LoadWAV(file, &wav_spec, &wav_buffer, &wav_length) == NULL) { + printf("file is null!!\n"); + return; + } + device_id = SDL_OpenAudioDevice(NULL, 0, &wav_spec, NULL, 0); + SDL_QueueAudio(device_id, wav_buffer, wav_length); + SDL_PauseAudioDevice(device_id, 0); +} + struct cat_event* cat_get_event(void) { struct cat_event* out = malloc(sizeof(struct cat_event)); diff --git a/etc/audio.c b/etc/audio.c deleted file mode 100644 index 38b594d..0000000 --- a/etc/audio.c +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include - -static unsigned int wav_length; /* length of our sample */ -static unsigned char* wav_buffer; /* buffer containing our audio file */ -static SDL_AudioSpec wav_spec; -static SDL_AudioDeviceID device_id = 0; -static int cat_audio_playing = 0; - -void cat_stop_audio(void) { - SDL_PauseAudioDevice(device_id, 1); - SDL_CloseAudioDevice(device_id); - SDL_FreeWAV(wav_buffer); - cat_audio_playing = 0; - /*SDL_Quit();*/ -} - -void cat_play_wav(char* file) -{ - if (cat_audio_playing) cat_stop_audio(); - cat_audio_playing = 1; - if (SDL_LoadWAV(file, &wav_spec, &wav_buffer, &wav_length) == NULL) { - printf("file is null!!\n"); - return; - } - device_id = SDL_OpenAudioDevice(NULL, 0, &wav_spec, NULL, 0); - SDL_QueueAudio(device_id, wav_buffer, wav_length); - SDL_PauseAudioDevice(device_id, 0); -} - -int main(int argc, char** argv) { - SDL_Init(SDL_INIT_EVERYTHING); - cat_play_wav("etc/example.wav"); - SDL_Delay(300); - cat_stop_audio(); - fgetc(stdin); - return 0; -} - diff --git a/main.c b/main.c index e39316d..0778a11 100644 --- a/main.c +++ b/main.c @@ -133,6 +133,7 @@ int main(int argc, char** argv) { cat_init(); cat_create_window(1920, 1080); + cat_play_wav("etc/example.wav"); cat_fill(0, 1, 0, 0, screen_width - 1, screen_height - 1, 132, 155, 132); while (1) game_loop(); return 0;