Add sound, format code

This commit is contained in:
Franck STAUFFER 2020-08-03 18:01:43 +02:00
parent e6b5a4dd1b
commit 232af94325
Signed by: franck.stauffer
GPG Key ID: AAF5A94045CEC261
3 changed files with 27 additions and 4 deletions

View File

@ -1,6 +1,6 @@
CPPFLAGS = -D_DEFAULT_SOURCE -D_FOTIFY_SOURCE=2 CPPFLAGS = -D_DEFAULT_SOURCE -D_FOTIFY_SOURCE=2
CFLAGS = $(CPPFLAGS) -std=c99 -pedantic -Wall -Wextra -march=native -mtune=native -O2 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 default: bin obj bin/chip8

BIN
resources/buzzer.wav Normal file

Binary file not shown.

View File

@ -1,4 +1,5 @@
#include "SDL2/SDL.h" #include "SDL2/SDL.h"
#include <SDL2/SDL_mixer.h>
#include <signal.h> #include <signal.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -41,6 +42,8 @@ SDL_Event e;
SDL_Surface* s; SDL_Surface* s;
SDL_Window* w; SDL_Window* w;
Mix_Chunk* buzzer;
Uint32 fg; Uint32 fg;
Uint32 bg; Uint32 bg;
@ -81,6 +84,8 @@ init_chip8(void)
void void
cleanup(void) cleanup(void)
{ {
Mix_FreeChunk(buzzer);
Mix_Quit();
SDL_FreeSurface(s); SDL_FreeSurface(s);
SDL_DestroyWindow(w); SDL_DestroyWindow(w);
SDL_Quit(); SDL_Quit();
@ -89,14 +94,19 @@ cleanup(void)
uint_fast8_t uint_fast8_t
init_SDL(const char* path) 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); fputs("ERROR: Failed to initialize SDL\n", stderr);
return 0; return 0;
} }
char caption[7 + strlen(path)]; char caption[7 + strlen(path)];
sprintf(caption, "CHIP-8 - %s", 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) { if (!w) {
fputs("ERROR: Failed to create a window\n", stderr); fputs("ERROR: Failed to create a window\n", stderr);
return 0; return 0;
@ -108,6 +118,17 @@ init_SDL(const char* path)
return 0; 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)) { if (atexit(cleanup)) {
fputs("ERROR: Failed to set exit function\n", stderr); fputs("ERROR: Failed to set exit function\n", stderr);
SDL_FreeSurface(s); SDL_FreeSurface(s);
@ -531,8 +552,10 @@ timers()
if (dt) if (dt)
--dt; --dt;
if (st) if (st) {
Mix_PlayChannel(-1, buzzer, 0);
--st; --st;
}
} }
int int