diff --git a/.appveyor.yml b/.appveyor.yml index cd1f634..7b4df8a 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -45,6 +45,15 @@ before_build: %DEPENDS% copy SDL2_ttf-2.0.14\%ARCH%\lib\*.a %PREFIX%\lib > nul %DEPENDS% copy SDL2_ttf-2.0.14\%ARCH%\bin\*.dll %PREFIX%\lib > nul + # SDL2_mixer + - |- + %DEPENDS% appveyor DownloadFile https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-devel-2.0.2-mingw.tar.gz + %DEPENDS% 7z x SDL2_mixer-devel-2.0.2-mingw.tar.gz > nul + %DEPENDS% 7z x SDL2_mixer-devel-2.0.2-mingw.tar > nul + %DEPENDS% copy SDL2_mixer-2.0.2\%ARCH%\include\SDL2\* %PREFIX%\include\SDL2 > nul + %DEPENDS% copy SDL2_mixer-2.0.2\%ARCH%\lib\*.a %PREFIX%\lib > nul + %DEPENDS% copy SDL2_mixer-2.0.2\%ARCH%\bin\*.dll %PREFIX%\lib > nul + # Lua - ps: (New-Object Net.WebClient).DownloadFile("https://sourceforge.net/projects/luabinaries/files/5.3.4/Windows%20Libraries/Static/lua-5.3.4_Win32_mingw4_lib.zip/download", "c:\lua\lua-5.3.4_Win32_mingw4_lib.zip") - |- diff --git a/CMakeLists.txt b/CMakeLists.txt index 5417fdd..b70368d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ include(FindLua) include(cmake/FindSDL2.cmake) include(cmake/FindSDL2_image.cmake) include(cmake/FindSDL2_ttf.cmake) +include(cmake/FindSDL2_mixer.cmake) if (NOT WIN32) include(FindX11) @@ -25,6 +26,7 @@ include_directories( ${SDL2_INCLUDE_DIR} ${SDL2_IMAGE_INCLUDE_DIR} ${SDL2_TTF_INCLUDE_DIR} + ${SDL2_MIXER_INCLUDE_DIR} ) if (NOT WIN32) @@ -74,6 +76,7 @@ add_executable(breakhack src/menu src/collisions src/keyboard + src/mixer ) target_link_libraries(breakhack @@ -81,6 +84,7 @@ target_link_libraries(breakhack ${SDL2_LIBRARY} ${SDL2_IMAGE_LIBRARY} ${SDL2_TTF_LIBRARY} + ${SDL2_MIXER_LIBRARY} ) if (NOT WIN32) diff --git a/assets/Sounds/FX/click.wav b/assets/Sounds/FX/click.wav new file mode 100644 index 0000000..d8b6d9d Binary files /dev/null and b/assets/Sounds/FX/click.wav differ diff --git a/src/main.c b/src/main.c index 132f1c0..d4611b5 100644 --- a/src/main.c +++ b/src/main.c @@ -21,6 +21,7 @@ #include "particle_engine.h" #include "menu.h" #include "keyboard.h" +#include "mixer.h" static SDL_Window *gWindow = NULL; static SDL_Renderer *gRenderer = NULL; @@ -70,7 +71,7 @@ bool initSDL(void) info("Scaling by %f", renderScale); } - if (SDL_Init(SDL_INIT_VIDEO) < 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { error("Could not initiate SDL2: %s", SDL_GetError()); return false; @@ -88,6 +89,8 @@ bool initSDL(void) return false; } + mixer_init(); + gWindow = SDL_CreateWindow("Breakhack", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, @@ -541,6 +544,7 @@ void close(void) particle_engine_close(); timer_destroy(menuTimer); + mixer_close(); SDL_DestroyRenderer(gRenderer); SDL_DestroyWindow(gWindow); gWindow = NULL; diff --git a/src/menu.c b/src/menu.c index 38cdd3d..4c1a7fb 100644 --- a/src/menu.c +++ b/src/menu.c @@ -6,6 +6,7 @@ #include "defines.h" #include "gui_button.h" #include "keyboard.h" +#include "mixer.h" typedef struct MenuItems_t { Sprite *sprite; @@ -47,6 +48,9 @@ menu_handle_event(Menu *m, SDL_Event *event) return; } + if (reset_buttons) + mixer_play_effect(CLICK); + int current_select = 0; bool mouse_selection = false; items = m->items; diff --git a/src/mixer.c b/src/mixer.c new file mode 100644 index 0000000..cf773d3 --- /dev/null +++ b/src/mixer.c @@ -0,0 +1,48 @@ +#include +#include "mixer.h" +#include "util.h" + +static Mix_Music *music = NULL; +static Mix_Chunk *effects[LAST_EFFECT]; + +static Mix_Chunk* +load_effect(char *path) +{ + Mix_Chunk *effect = Mix_LoadWAV(path); + if (effect == NULL) + fatal("Failed to load effect: %s", path); + return effect; +} + +static void +load_effects(void) +{ + effects[CLICK] = load_effect("assets/Sounds/FX/click.wav"); +} + +void +mixer_init(void) +{ + if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) == -1) { + fatal("Failed to load SDL2_mixer"); + } + load_effects(); +} + +void +mixer_play_effect(Fx fx) +{ + if (Mix_PlayChannel( -1, effects[fx], 0) == -1) + error("Unable to play sound: %u", (unsigned int) fx); +} + +void +mixer_close(void) +{ + for (size_t i = 0; i < LAST_EFFECT; ++i) { + Mix_FreeChunk(effects[i]); + } + if (music) + Mix_FreeMusic(music); + Mix_CloseAudio(); +} diff --git a/src/mixer.h b/src/mixer.h new file mode 100644 index 0000000..0b40e1f --- /dev/null +++ b/src/mixer.h @@ -0,0 +1,20 @@ +#ifndef MIXER_H_ +#define MIXER_H_ + +#include + +typedef enum Fx_t { + CLICK, + LAST_EFFECT +} Fx; + +void +mixer_init(void); + +void +mixer_play_effect(Fx fx); + +void +mixer_close(void); + +#endif // MIXER_H_