From 232af9432581d3eca303ac720187bebb4ed446c6 Mon Sep 17 00:00:00 2001 From: Franck STAUFFER Date: Mon, 3 Aug 2020 18:01:43 +0200 Subject: [PATCH] Add sound, format code --- Makefile | 2 +- resources/buzzer.wav | Bin 0 -> 486 bytes src/main.c | 29 ++++++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 resources/buzzer.wav diff --git a/Makefile b/Makefile index 6379f89..1ad85ea 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_FOTIFY_SOURCE=2 CFLAGS = $(CPPFLAGS) -std=c99 -pedantic -Wall -Wextra -march=native -mtune=native -O2 -LDFLAGS = -Wl,-z,relro,-z,now,-O2 -lSDL2 +LDFLAGS = -Wl,-z,relro,-z,now,-O2 -lSDL2 -lSDL2_mixer default: bin obj bin/chip8 diff --git a/resources/buzzer.wav b/resources/buzzer.wav new file mode 100644 index 0000000000000000000000000000000000000000..bdc7b1bf4b8445d75e30eb50f69a12dfe8a0f8c1 GIT binary patch literal 486 zcmVMNk&HA0RRA3K~_a(ZFC?I000010096*tN;K+tN;K300;nNVRT`+0RRAi zfPjF3dwG0)etv&`fPaB>N-!Z96A%s!5Go`Z7Zel{6%r;)IxHg}8ypxRH%31%DI_8y zA30)mUqd)9EG95TG($)@G%YSFOJGZEWK2IfGCxvVVrOJcLOMBHe2ti)kY-gwQdUJl zIYCuOK|*P1QcFNxbX!nbYG_wUQetgfS$vg^Yg}=Wmw9_>UT#-nW>scyigA33ZHa(! zl9Y;vgMxv9fP!y;o2Ia{xVOBqoQi5(QB6rmMq!JAX&acp_`|Lb7o*$S!#WQjC*TiURz*@rnkJAe{W}C zc64WAc6VrHUTKkrb99)Ccx-8ViiLKBkBWC|c%HVSl(4?6h<=QCe2I~NeTs92o`##X cx|XJ;j+vI6k$s7fm6o2kz|F6mv%$yD0L*~VlmGw# literal 0 HcmV?d00001 diff --git a/src/main.c b/src/main.c index 57c5f64..bf1a560 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include "SDL2/SDL.h" +#include #include #include #include @@ -41,6 +42,8 @@ SDL_Event e; SDL_Surface* s; SDL_Window* w; +Mix_Chunk* buzzer; + Uint32 fg; Uint32 bg; @@ -81,6 +84,8 @@ init_chip8(void) void cleanup(void) { + Mix_FreeChunk(buzzer); + Mix_Quit(); SDL_FreeSurface(s); SDL_DestroyWindow(w); SDL_Quit(); @@ -89,14 +94,19 @@ cleanup(void) uint_fast8_t init_SDL(const char* path) { - if (SDL_Init(SDL_INIT_VIDEO)) { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { fputs("ERROR: Failed to initialize SDL\n", stderr); return 0; } char caption[7 + strlen(path)]; sprintf(caption, "CHIP-8 - %s", path); - w = SDL_CreateWindow(caption, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 64 * SCALE, 32 * SCALE, SDL_WINDOW_SHOWN); + w = SDL_CreateWindow(caption, + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + 64 * SCALE, + 32 * SCALE, + SDL_WINDOW_SHOWN); if (!w) { fputs("ERROR: Failed to create a window\n", stderr); return 0; @@ -108,6 +118,17 @@ init_SDL(const char* path) return 0; } + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 2048)) { + fputs("ERROR: Failed to open audio\n", stderr); + return 0; + } + + buzzer = Mix_LoadWAV("resources/buzzer.wav"); + if (!buzzer) { + fputs("ERROR: Failed to load sound effects\n", stderr); + return 0; + } + if (atexit(cleanup)) { fputs("ERROR: Failed to set exit function\n", stderr); SDL_FreeSurface(s); @@ -531,8 +552,10 @@ timers() if (dt) --dt; - if (st) + if (st) { + Mix_PlayChannel(-1, buzzer, 0); --st; + } } int