Introduced a mixer and one click sound.

This commit is contained in:
Linus_Probert 2018-02-14 16:04:40 +01:00
parent d6d578c3aa
commit b502e78dd9
7 changed files with 90 additions and 1 deletions

View File

@ -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")
- |-

View File

@ -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)

BIN
assets/Sounds/FX/click.wav Normal file

Binary file not shown.

View File

@ -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;

View File

@ -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;

48
src/mixer.c Normal file
View File

@ -0,0 +1,48 @@
#include <SDL2/SDL_mixer.h>
#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();
}

20
src/mixer.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef MIXER_H_
#define MIXER_H_
#include <stdbool.h>
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_