Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sound capability to the game. #7216

Merged
merged 1 commit into from Apr 13, 2014
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -23,6 +23,7 @@ PROJECT(CataclysmDDA)

OPTION(LOCALIZE "Toggle gettext localization/translation." ON)
OPTION(SDL "Use SDL curses emulation instead of terminal support. Also enables tiles support." OFF)
OPTION(SOUND "Use SDL mixer to play music and sounds." ON)
OPTION(LUA "Enable lua scripting support." ON)

# Note: The CMake documentation says it's better to list the actual source
@@ -37,7 +38,7 @@ ADD_EXECUTABLE(cataclysm ${CataclysmDDA_SRCS} ${CataclysmDDA_HDRS})
# Custom command that will be executed whenever the "cataclysm" target is built.
# Copy all the relevant game data into the build directory.
ADD_CUSTOM_COMMAND(
TARGET cataclysm PRE_BUILD
TARGET cataclysm PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/data $<TARGET_FILE_DIR:cataclysm>/data
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/LICENSE.txt $<TARGET_FILE_DIR:cataclysm> # Don't forget the license :)
)
@@ -63,7 +64,7 @@ Include(FindPkgConfig)
IF(LUA)
ADD_DEFINITIONS(-DLUA)
PKG_SEARCH_MODULE(LUA51 REQUIRED lua5.1)

if(LUA51_FOUND)
ADD_DEFINITIONS(-I${LUA51_INCLUDE_DIRS})
target_link_libraries(cataclysm ${LUA51_LIBRARIES})
@@ -73,24 +74,30 @@ ENDIF()
IF(SDL)
# Look for SDL2 using pkgconfig
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)

# Harcode these for now since they don't seem to be included in pkg-config
# CMake is bound to get SDL2 finders eventually.
#PKG_SEARCH_MODULE(SDL2_TTF REQUIRED sdl2_ttf)
#PKG_SEARCH_MODULE(SDL2_IMAGE REQUIRED sdl2_image)
SET(SDL2_TTF_LIBRARIES -lSDL2_ttf)
SET(SDL2_IMAGE_LIBRARIES -lSDL2_image)

TARGET_LINK_LIBRARIES(cataclysm ${SDL2_LIBRARIES} ${SDL2_TTF_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})
include_directories(${SDL2_INCLUDE_DIRS})

# Install GFX directory
ADD_CUSTOM_COMMAND(
TARGET cataclysm PRE_BUILD
TARGET cataclysm PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/gfx $<TARGET_FILE_DIR:cataclysm>/gfx
)

ADD_DEFINITIONS(-DSDLTILES -DTILES)

IF(SOUND)
SET(SDL2_MIXER_LIBRARIES -lSDL2_mixer)
TARGET_LINK_LIBRARIES(cataclysm ${SDL2_MIXER_LIBRARIES})
ADD_DEFINITIONS(-DSDL_SOUND)
ENDIF()
ELSEIF(MSVC OR MINGW)
# On windows our default isn't curses, but rather GDI
TARGET_LINK_LIBRARIES(cataclysm gdi32 intl iconv winmm)
@@ -22,6 +22,8 @@
# make RELEASE=1
# Tiles (uses SDL rather than ncurses)
# make TILES=1
# Sound (requires SDL, so TILES must be enabled)
# make TILES=1 SOUND=1
# Disable gettext, on some platforms the dependencies are hard to wrangle.
# make LOCALIZE=0
# Compile localization files for specified languages
@@ -65,6 +67,13 @@ DEBUG = -g
#DEFINES += -DDEBUG_ENABLE_MAP_GEN
#DEFINES += -DDEBUG_ENABLE_GAME

ifdef SOUND
ifndef TILES
$(error "SOUND=1 only works with TILES=1")
endif
CXXFLAGS += "-DSDL_SOUND"
endif

VERSION = 0.A


Binary file not shown.
Binary file not shown.
@@ -0,0 +1,18 @@
{
"playlists" : [
{
"id" : "title",
"shuffle" : false,
"files" : [
{
"file": "Dark_Days_Ahead_demo_2.ogg",
"volume": 128
},
{
"file": "cataclysmthemeREV6.ogg",
"volume": 90
}
]
}
]
}
@@ -129,6 +129,9 @@ void game::print_menu_items(WINDOW *w_in, std::vector<std::string> vItems, int i

bool game::opening_screen()
{
// Play title music, whoo!
play_music("title");

world_generator->set_active_world(NULL);
// This actually _loads_ what worlds exist.
world_generator->get_all_worlds();
@@ -697,6 +697,11 @@ void initOptions() {
false
); // populate the options dynamically

OPTIONS["MUSIC_VOLUME"] = cOpt("graphics", _("Music Volume"),
_("SDL ONLY: Adjust the volume of the music being played in the background."),
0, 200, 100
); // populate the options dynamically

OPTIONS["AUTO_NOTES"] = cOpt("general", _("Auto notes"),
_("If true automatically sets notes on places that have stairs that go up or down"),
true
@@ -1461,4 +1461,10 @@ bool is_draw_tiles_mode() {
return false;
}

void play_music(std::string playlist) {
}

void play_sound(std::string identifier) {
}

#endif
@@ -176,4 +176,7 @@ int get_terminal_height();
*/
bool is_draw_tiles_mode();

void play_music(std::string playlist);
void play_sound(std::string identifier);

#endif
@@ -42,6 +42,10 @@
#include "SDL2/SDL_image.h"
#endif

#ifdef SDL_SOUND
#include "SDL_mixer.h"
#endif

//***********************************
//Globals *
//***********************************
@@ -53,6 +57,13 @@ static unsigned long interval = 25;
static bool needupdate = false;
#endif

#ifdef SDL_SOUND
/** The music we're currently playing. */
Mix_Music *current_music = NULL;
std::string current_playlist = "";
int current_playlist_at = 0;
#endif

/**
* A class that draws a single character on screen.
*/
@@ -272,11 +283,28 @@ bool WinCreate()

SDL_JoystickEventState(SDL_ENABLE);

// Set up audio mixer.
#ifdef SDL_SOUND
int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16;
int audio_channels = 2;
int audio_buffers = 4096;

SDL_Init(SDL_INIT_AUDIO);

if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
DebugLog() << "Failed to open audio mixer.\n";
}
#endif

return true;
};

void WinDestroy()
{
#ifdef SDL_SOUND
Mix_CloseAudio();
#endif
if(joystick) {
SDL_JoystickClose(joystick);
joystick = 0;
@@ -1078,6 +1106,9 @@ int projected_window_height(int)
return OPTIONS["TERMINAL_Y"] * fontheight;
}

// forward declaration
void load_soundset();

//Basic Init, create the font, backbuffer, etc
WINDOW *curses_init(void)
{
@@ -1202,6 +1233,9 @@ WINDOW *curses_init(void)

init_colors();

// initialize sound set
load_soundset();

// Reset the font pointer
font = Font::load_font(typeface, fontsize, fontwidth, fontheight);
if (font == NULL) {
@@ -1714,4 +1748,96 @@ bool is_draw_tiles_mode() {
return use_tiles;
}

struct music_playlist {
// list of filenames relative to the soundpack location
std::vector<std::string> files;
std::vector<int> volumes;
bool shuffle;

music_playlist() : shuffle(false) {
}
};

std::map<std::string, music_playlist> playlists;

#ifdef SDL_SOUND

void musicFinished();

void play_music_file(std::string filename, int volume) {
current_music = Mix_LoadMUS((FILENAMES["datadir"] + "/sound/" + filename).c_str());
Mix_VolumeMusic(volume * OPTIONS["MUSIC_VOLUME"] / 100);
Mix_PlayMusic(current_music, 0);
Mix_HookMusicFinished(musicFinished);
}

/** Callback called when we finish playing music. */
void musicFinished() {
Mix_HaltMusic();
Mix_FreeMusic(current_music);
current_music = NULL;

// Load the next file to play.
current_playlist_at++;

// Wrap around if we reached the end of the playlist.
if(current_playlist_at >= playlists[current_playlist].files.size()) {
current_playlist_at = 0;
}

std::string filename = playlists[current_playlist].files[current_playlist_at];
play_music_file(filename, playlists[current_playlist].volumes[current_playlist_at]);
}
#endif

void play_music(std::string playlist) {
#ifdef SDL_SOUND

if(playlists[playlist].files.size() == 0) {
return;
}

// Don't interrupt playlist that's already playing.
if(playlist == current_playlist) {
return;
}

std::string filename = playlists[playlist].files[0];
int volume = playlists[playlist].volumes[0];

current_playlist = playlist;
current_playlist_at = 0;

play_music_file(filename, volume);

#endif
}

void load_soundset() {
std::string location = FILENAMES["datadir"] + "/sound/soundset.json";
std::ifstream jsonstream(location.c_str(), std::ifstream::binary);
if (jsonstream.good()) {
JsonIn json(jsonstream);
JsonObject config = json.get_object();
JsonArray playlists_ = config.get_array("playlists");

for(int i=0; i < playlists_.size(); i++) {
JsonObject playlist = playlists_.get_object(i);

std::string playlist_id = playlist.get_string("id");
music_playlist playlist_to_load;
playlist_to_load.shuffle = playlist.get_bool("shuffle", false);

JsonArray playlist_files = playlist.get_array("files");
for(int j=0; j < playlist_files.size(); j++) {
JsonObject entry = playlist_files.get_object(j);
playlist_to_load.files.push_back(entry.get_string("file"));
playlist_to_load.volumes.push_back(entry.get_int("volume"));
}

playlists[playlist_id] = playlist_to_load;
}
}
}

#endif // TILES
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.