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 0000000..bdc7b1b Binary files /dev/null and b/resources/buzzer.wav differ 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