From: Tobias Gläßer Date: Sun, 25 Jul 2004 19:03:36 +0000 (+0000) Subject: - Major changes in Setup-API. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=c5cbd36c2e01d8c807c8c931ca44fb7c1b48ad18;p=supertux.git - Major changes in Setup-API. - Moved all sound handling into SoundManager, which became a singleton. SVN-Revision: 1620 --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 04d2fe36a..8531ec93a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -4,7 +4,6 @@ lib_LTLIBRARIES = libsupertux.la libsupertux_la_SOURCES =app/globals.cpp \ app/setup.cpp \ audio/musicref.cpp \ - audio/sound.cpp \ audio/sound_manager.cpp \ gui/button.cpp \ gui/menu.cpp \ @@ -37,7 +36,6 @@ libsupertuxapp_HEADERS =app/defines.h \ app/gettext.h \ app/globals.h libsupertuxaudio_HEADERS =audio/musicref.h \ - audio/sound.h \ audio/sound_manager.h libsupertuxgui_HEADERS =gui/button.h \ gui/menu.h \ @@ -61,3 +59,4 @@ libsupertuxvideo_HEADERS =video/drawing_context.h \ video/surface.h libsupertux_la_LDFLAGS = -module + diff --git a/lib/app/setup.cpp b/lib/app/setup.cpp index 50c88b645..83be24430 100644 --- a/lib/app/setup.cpp +++ b/lib/app/setup.cpp @@ -66,7 +66,7 @@ void seticon(void); void usage(char * prog, int ret); /* Does the given file exist and is it accessible? */ -int SuperTux::faccessible(const char *filename) +int FileSystem::faccessible(const char *filename) { struct stat filestat; if (stat(filename, &filestat) == -1) @@ -83,7 +83,7 @@ int SuperTux::faccessible(const char *filename) } /* Can we write to this location? */ -int SuperTux::fwriteable(const char *filename) +int FileSystem::fwriteable(const char *filename) { FILE* fi; fi = fopen(filename, "wa"); @@ -95,7 +95,7 @@ int SuperTux::fwriteable(const char *filename) } /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/ -int SuperTux::fcreatedir(const char* relative_dir) +int FileSystem::fcreatedir(const char* relative_dir) { char path[1024]; snprintf(path, 1024, "%s/%s/", st_dir, relative_dir); @@ -117,40 +117,10 @@ int SuperTux::fcreatedir(const char* relative_dir) } } -FILE * SuperTux::opendata(const char * rel_filename, const char * mode) -{ - char * filename = NULL; - FILE * fi; - - filename = (char *) malloc(sizeof(char) * (strlen(st_dir) + - strlen(rel_filename) + 1)); - - strcpy(filename, st_dir); - /* Open the high score file: */ - - strcat(filename, rel_filename); - - /* Try opening the file: */ - fi = fopen(filename, mode); - - if (fi == NULL) - { - fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename); - - if (strcmp(mode, "r") == 0) - fprintf(stderr, "for read!!!\n"); - else if (strcmp(mode, "w") == 0) - fprintf(stderr, "for write!!!\n"); - } - free( filename ); - - return(fi); -} - /* Get all names of sub-directories in a certain directory. */ /* Returns the number of sub-directories found. */ /* Note: The user has to free the allocated space. */ -string_list_type SuperTux::dsubdirs(const char *rel_path,const char* expected_file) +string_list_type FileSystem::dsubdirs(const char *rel_path,const char* expected_file) { DIR *dirStructP; struct dirent *direntp; @@ -220,7 +190,7 @@ string_list_type SuperTux::dsubdirs(const char *rel_path,const char* expected_f return sdirs; } -string_list_type SuperTux::dfiles(const char *rel_path, const char* glob, const char* exception_str) +string_list_type FileSystem::dfiles(const char *rel_path, const char* glob, const char* exception_str) { DIR *dirStructP; struct dirent *direntp; @@ -285,14 +255,7 @@ string_list_type SuperTux::dfiles(const char *rel_path, const char* glob, const return sdirs; } -void SuperTux::free_strings(char **strings, int num) -{ - int i; - for(i=0; i < num; ++i) - free(strings[i]); -} - -void SuperTux::st_info_setup(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version) +void Setup::info(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version) { package_name = _package_name; package_symbol_name = _package_symbol_name; @@ -301,7 +264,7 @@ package_version = _package_version; /* --- SETUP --- */ /* Set SuperTux configuration and save directories */ -void SuperTux::st_directory_setup(void) +void Setup::directories(void) { char *home; char str[1024]; @@ -319,7 +282,7 @@ void SuperTux::st_directory_setup(void) strcat(st_dir,st_dir_tmp.c_str()); /* Remove .supertux config-file from old SuperTux versions */ - if(faccessible(st_dir)) + if(FileSystem::faccessible(st_dir)) { remove (st_dir); @@ -369,7 +332,7 @@ void SuperTux::st_directory_setup(void) printf("Datadir: %s\n", datadir.c_str()); } -void SuperTux::st_general_setup(void) +void Setup::general(void) { /* Seed random number generator: */ @@ -410,7 +373,7 @@ void SuperTux::st_general_setup(void) } -void SuperTux::st_general_free(void) +void Setup::general_free(void) { /* Free global images: */ @@ -434,7 +397,7 @@ void SuperTux::st_general_free(void) } -void SuperTux::st_video_setup(unsigned int screen_w, unsigned int screen_h) +void Setup::video(unsigned int screen_w, unsigned int screen_h) { /* Init SDL Video: */ if (SDL_Init(SDL_INIT_VIDEO) < 0) @@ -448,9 +411,9 @@ void SuperTux::st_video_setup(unsigned int screen_w, unsigned int screen_h) /* Open display: */ if(use_gl) - st_video_setup_gl(screen_w, screen_h); + video_gl(screen_w, screen_h); else - st_video_setup_sdl(screen_w, screen_h); + video_sdl(screen_w, screen_h); Surface::reload_all(); @@ -458,7 +421,7 @@ void SuperTux::st_video_setup(unsigned int screen_w, unsigned int screen_h) SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str()); } -void SuperTux::st_video_setup_sdl(unsigned int screen_w, unsigned int screen_h) +void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h) { if (use_fullscreen) { @@ -488,7 +451,7 @@ void SuperTux::st_video_setup_sdl(unsigned int screen_w, unsigned int screen_h) } } -void SuperTux::st_video_setup_gl(unsigned int screen_w, unsigned int screen_h) +void Setup::video_gl(unsigned int screen_w, unsigned int screen_h) { #ifndef NOOPENGL @@ -544,7 +507,7 @@ void SuperTux::st_video_setup_gl(unsigned int screen_w, unsigned int screen_h) } -void SuperTux::st_joystick_setup(void) +void Setup::joystick(void) { /* Init Joystick: */ @@ -605,19 +568,19 @@ void SuperTux::st_joystick_setup(void) } } -void SuperTux::st_audio_setup(void) +void Setup::audio(void) { /* Init SDL Audio silently even if --disable-sound : */ - if (audio_device) + if (SoundManager::get()->audio_device_available()) { if (SDL_Init(SDL_INIT_AUDIO) < 0) { /* only print out message if sound or music was not disabled at command-line */ - if (use_sound || use_music) + if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled()) { fprintf(stderr, "\nWarning: I could not initialize audio!\n" @@ -628,23 +591,23 @@ void SuperTux::st_audio_setup(void) because in this case, use_sound & use_music' values are ignored when there's no available audio device */ - use_sound = false; - use_music = false; - audio_device = false; + SoundManager::get()->enable_sound(false); + SoundManager::get()->enable_music(false); + SoundManager::get()->set_audio_device_available(false); } } /* Open sound silently regarless the value of "use_sound": */ - if (audio_device) + if (SoundManager::get()->audio_device_available()) { - if (open_audio(44100, AUDIO_S16, 2, 2048) < 0) + if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0) { /* only print out message if sound or music was not disabled at command-line */ - if (use_sound || use_music) + if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled()) { fprintf(stderr, "\nWarning: I could not set up audio for 44100 Hz " @@ -652,9 +615,9 @@ void SuperTux::st_audio_setup(void) "The Simple DirectMedia error that occured was:\n" "%s\n\n", SDL_GetError()); } - use_sound = false; - use_music = false; - audio_device = false; + SoundManager::get()->enable_sound(false); + SoundManager::get()->enable_music(false); + SoundManager::get()->set_audio_device_available(false); } } @@ -663,20 +626,20 @@ void SuperTux::st_audio_setup(void) /* --- SHUTDOWN --- */ -void SuperTux::st_shutdown(void) +void Termination::shutdown(void) { - close_audio(); + SoundManager::get()->close_audio(); SDL_Quit(); config->save(); } /* --- ABORT! --- */ -void SuperTux::st_abort(const std::string& reason, const std::string& details) +void Termination::abort(const std::string& reason, const std::string& details) { fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str()); - st_shutdown(); - abort(); + shutdown(); + ::abort(); } /* Set Icon (private) */ @@ -722,7 +685,7 @@ void seticon(void) /* Parse command-line arguments: */ -void SuperTux::parseargs(int argc, char * argv[]) +void Setup::parseargs(int argc, char * argv[]) { int i; @@ -819,14 +782,13 @@ void SuperTux::parseargs(int argc, char * argv[]) { /* Disable the compiled in sound feature */ printf("Sounds disabled \n"); - use_sound = false; - audio_device = false; + SoundManager::get()->enable_sound(false); } else if (strcmp(argv[i], "--disable-music") == 0) { /* Disable the compiled in sound feature */ printf("Music disabled \n"); - use_music = false; + SoundManager::get()->enable_music(false); } else if (strcmp(argv[i], "--debug") == 0) { @@ -906,7 +868,7 @@ void usage(char * prog, int ret) exit(ret); } -std::vector SuperTux::read_directory(const std::string& pathname) +std::vector FileSystem::read_directory(const std::string& pathname) { std::vector dirnames; diff --git a/lib/app/setup.h b/lib/app/setup.h index 86fcfbe18..60a0272b0 100644 --- a/lib/app/setup.h +++ b/lib/app/setup.h @@ -23,35 +23,44 @@ #include #include #include "../gui/menu.h" -#include "../audio/sound.h" #include "../special/base.h" namespace SuperTux { -int faccessible(const char *filename); -int fcreatedir(const char* relative_dir); -int fwriteable(const char *filename); -std::vector read_directory(const std::string& pathname); - -FILE * opendata(const char * filename, const char * mode); -string_list_type dsubdirs(const char *rel_path, const char* expected_file); -string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str); -void free_strings(char **strings, int num); -void st_info_setup(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version); -void st_directory_setup(void); -void st_general_setup(void); -void st_general_free(); -void st_video_setup_sdl(unsigned int screen_w, unsigned int screen_h); -void st_video_setup_gl(unsigned int screen_w, unsigned int screen_h); -void st_video_setup(unsigned int screen_w, unsigned int screen_h); -void st_audio_setup(void); -void st_joystick_setup(void); -void st_shutdown(void); -void st_abort(const std::string& reason, const std::string& details); - -void parseargs(int argc, char * argv[]); - -} +/// File system utility functions +struct FileSystem + { + static int faccessible(const char *filename); + static int fcreatedir(const char* relative_dir); + static int fwriteable(const char *filename); + static std::vector read_directory(const std::string& pathname); + static string_list_type dsubdirs(const char *rel_path, const char* expected_file); + static string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str); + }; + +/// All you need to get an application up and running +struct Setup + { + static void info(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version); + static void directories(void); + static void general(void); + static void general_free(); + static void video_sdl(unsigned int screen_w, unsigned int screen_h); + static void video_gl(unsigned int screen_w, unsigned int screen_h); + static void video(unsigned int screen_w, unsigned int screen_h); + static void audio(void); + static void joystick(void); + static void parseargs(int argc, char * argv[]); + }; + +/// Termination handling +struct Termination + { + static void shutdown(void); + static void abort(const std::string& reason, const std::string& details); + }; + +} //namespace SuperTux #endif /*SUPERTUX_SETUP_H*/ diff --git a/lib/audio/sound.cpp b/lib/audio/sound.cpp deleted file mode 100644 index c35e1926f..000000000 --- a/lib/audio/sound.cpp +++ /dev/null @@ -1,77 +0,0 @@ -// $Id$ -// -// SuperTux - A Jump'n Run -// Copyright (C) 2000 Bill Kendrick -// Copyright (C) 2004 Duong-Khang NGUYEN -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -#include -#include "../audio/sound.h" - -/*global variable*/ -bool use_sound = true; /* handle sound on/off menu and command-line option */ -bool use_music = true; /* handle music on/off menu and command-line option */ -bool audio_device = true; /* != 0: available and initialized */ - -#include - -std::vector sounds; - -/* --- OPEN THE AUDIO DEVICE --- */ - -int open_audio (int frequency, Uint16 format, int channels, int chunksize) -{ - if (Mix_OpenAudio( frequency, format, channels, chunksize ) < 0) - return -1; - - // allocate 16 channels for mixing - if (Mix_AllocateChannels(8) != 8) - return -2; - - return 0; -} - - -/* --- CLOSE THE AUDIO DEVICE --- */ - -void close_audio( void ) -{ - if (audio_device) { - Mix_CloseAudio(); - } -} - - -/* --- LOAD A SOUND --- */ - -Mix_Chunk* load_sound(const std::string& file) -{ - if(!audio_device) - return 0; - - Mix_Chunk* snd = Mix_LoadWAV(file.c_str()); - - /*if (snd == 0) - st_abort("Can't load", file);*/ - - return(snd); -} - -void free_chunk(Mix_Chunk *chunk) -{ - Mix_FreeChunk( chunk ); -} - diff --git a/lib/audio/sound.h b/lib/audio/sound.h deleted file mode 100644 index 975021fbf..000000000 --- a/lib/audio/sound.h +++ /dev/null @@ -1,53 +0,0 @@ -// $Id$ -// -// SuperTux - A Jump'n Run -// Copyright (C) 2000 Bill Kendrick -// Copyright (C) 2004 Duong-Khang NGUYEN -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -#ifndef SUPERTUX_SOUND_H -#define SUPERTUX_SOUND_H - -//#include "defines.h" /* get YES/NO defines */ -#include - -/*global variable*/ -extern bool use_sound; /* handle sound on/off menu and command-line option */ -extern bool use_music; /* handle music on/off menu and command-line */ -extern bool audio_device; /* != 0: available and initialized */ - -/* enum of different internal music types */ -enum Music_Type { - NO_MUSIC, - LEVEL_MUSIC, - HURRYUP_MUSIC, - HERRING_MUSIC -}; - -#include -#include - -/* variables for stocking the sound and music */ -extern std::vector sounds; - -/* functions handling the sound and music */ -int open_audio(int frequency, Uint16 format, int channels, int chunksize); -void close_audio( void ); - -Mix_Chunk * load_sound(const std::string& file); -void free_chunk(Mix_Chunk*chunk); - -#endif /*SUPERTUX_SOUND_H*/ diff --git a/lib/audio/sound_manager.cpp b/lib/audio/sound_manager.cpp index fd4b231fd..344035b5e 100644 --- a/lib/audio/sound_manager.cpp +++ b/lib/audio/sound_manager.cpp @@ -22,15 +22,16 @@ #include "../audio/sound_manager.h" #include "../audio/musicref.h" -#include "../audio/sound.h" #include "../app/globals.h" #include "../app/setup.h" #include "../special/moving_object.h" using namespace SuperTux; +SoundManager* SoundManager::instance_ = 0; + SoundManager::SoundManager() - : current_music(0), music_enabled(true) + : current_music(0), m_music_enabled(true) , m_sound_enabled(true) , audio_device(true) { } @@ -38,12 +39,15 @@ SoundManager::~SoundManager() { if(audio_device) Mix_HaltMusic(); + +sounds.clear(); +destroy_instance(); } void SoundManager::play_sound(Mix_Chunk* sound) { - if(!audio_device || !use_sound) + if(!audio_device || !m_sound_enabled) return; Mix_PlayChannel(-1, sound, 0); @@ -60,7 +64,7 @@ SoundManager::play_sound(Mix_Chunk* sound, const MovingObject* object, const Vec void SoundManager::play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2) { - if(!audio_device || !use_sound) + if(!audio_device || !m_sound_enabled) return; // TODO make sure this formula is good @@ -89,7 +93,7 @@ SoundManager::load_music(const std::string& file) return MusicRef(0); if(!exists_music(file)) - st_abort("Couldn't load musicfile ", file.c_str()); + Termination::abort("Couldn't load musicfile ", file.c_str()); std::map::iterator i = musics.find(file); assert(i != musics.end()); @@ -145,7 +149,7 @@ SoundManager::play_music(const MusicRef& musicref, int loops) current_music = musicref.music; current_music->refcount++; - if(music_enabled) + if(m_music_enabled) Mix_PlayMusic(current_music->music, loops); } @@ -171,20 +175,79 @@ SoundManager::enable_music(bool enable) if(!audio_device) return; - if(enable == music_enabled) + if(enable == m_music_enabled) return; - music_enabled = enable; - if(music_enabled == false) { + m_music_enabled = enable; + if(m_music_enabled == false) { Mix_HaltMusic(); } else { Mix_PlayMusic(current_music->music, -1); } } +void +SoundManager::enable_sound(bool enable) +{ + if(!audio_device) + return; + + m_sound_enabled = enable; +} + SoundManager::MusicResource::~MusicResource() { // don't free music buggy SDL_Mixer crashs for some mod files // Mix_FreeMusic(music); } +/* --- LOAD A SOUND --- */ + +Mix_Chunk* SoundManager::load_sound(const std::string& file) +{ + if(!audio_device) + return 0; + + Mix_Chunk* snd = Mix_LoadWAV(file.c_str()); + + /*if (snd == 0) + Termination::abort("Can't load", file);*/ + + return(snd); +} + +void SoundManager::free_chunk(Mix_Chunk *chunk) +{ + Mix_FreeChunk( chunk ); +} + + +/* --- OPEN THE AUDIO DEVICE --- */ + +int SoundManager::open_audio (int frequency, Uint16 format, int channels, int chunksize) +{ + if (Mix_OpenAudio( frequency, format, channels, chunksize ) < 0) + return -1; + + // allocate 16 channels for mixing + if (Mix_AllocateChannels(8) != 8) + return -2; + + return 0; +} + + +/* --- CLOSE THE AUDIO DEVICE --- */ + +void SoundManager::close_audio( void ) +{ + if (audio_device) { + Mix_CloseAudio(); + } +} + +Mix_Chunk* SuperTux::IDToSound(int id) +{ + return SoundManager::get()->sounds[id]; +} + diff --git a/lib/audio/sound_manager.h b/lib/audio/sound_manager.h index 48a144063..f5cfb3096 100644 --- a/lib/audio/sound_manager.h +++ b/lib/audio/sound_manager.h @@ -21,6 +21,7 @@ #define SUPERTUX_SOUND_MANAGER_H #include +#include #include #include "SDL_mixer.h" @@ -32,14 +33,20 @@ namespace SuperTux class MusicRef; class MovingObject; + /// enum of different internal music types + enum Music_Type { + NO_MUSIC, + LEVEL_MUSIC, + HURRYUP_MUSIC, + HERRING_MUSIC + }; + /// Sound manager /** This class handles all sounds that are played */ class SoundManager { public: - SoundManager(); - ~SoundManager(); /// Play sound. void play_sound(Mix_Chunk* sound); @@ -47,10 +54,14 @@ namespace SuperTux void play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2); /// Play sound relative to a MovingObject and a Vector. void play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos); - + /// Load music. /** Is used to load the music for a MusicRef. */ MusicRef load_music(const std::string& file); + + /// Load sound. + Mix_Chunk * load_sound(const std::string& file); + /// Test if a certain music file exists. bool exists_music(const std::string& filename); @@ -64,9 +75,68 @@ namespace SuperTux /// Enable/Disable music. void enable_music(bool enable); + /// Is music enabled? + bool music_enabled() + { + return m_music_enabled; + } + + /// Enable/Disable sound. + void enable_sound(bool enable); + + /// Is sound enabled? + bool sound_enabled() + { + return m_sound_enabled; + } + + /* functions handling the sound and music */ + int open_audio(int frequency, Uint16 format, int channels, int chunksize); + void close_audio( void ); + + /// Is audio available? + bool audio_device_available() + { + return audio_device; + } + + void set_audio_device_available(bool available) + { + audio_device = available; + } + + static SoundManager* get() + { + return instance_ ? instance_ : instance_ = new SoundManager(); + } + static void destroy_instance() + { + delete instance_; + instance_ = 0; + } + + void add_sound(Mix_Chunk* sound, int id) + { + sounds[id] = sound; + } + + Mix_Chunk* get_sound(int id) + { + return sounds[id]; + } + private: + SoundManager(); + ~SoundManager(); + // music part friend class MusicRef; + friend class Setup; + friend Mix_Chunk* IDToSound(int id); + + static SoundManager* instance_ ; + + void free_chunk(Mix_Chunk* chunk); /// Resource for music. /** Contains the raw music data and @@ -84,12 +154,18 @@ namespace SuperTux void free_music(MusicResource* music); + /*variables for stocking the sound and music*/ + std::map sounds; std::map musics; MusicResource* current_music; - bool music_enabled; + bool m_music_enabled; + bool m_sound_enabled; + bool audio_device; /* != 0: available and initialized */ + }; + Mix_Chunk* IDToSound(int id); + } // namespace SuperTux - #endif /*SUPERTUX_SOUND_MANAGER_H*/ diff --git a/lib/gui/button.cpp b/lib/gui/button.cpp index 8c82519e9..a004cce30 100644 --- a/lib/gui/button.cpp +++ b/lib/gui/button.cpp @@ -79,7 +79,7 @@ void Button::add_icon(const std::string& icon_file, int mw, int mh) if(!icon_file.empty()) { snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str()); - if(!faccessible(filename)) + if(!FileSystem::faccessible(filename)) snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str()); } else diff --git a/lib/gui/menu.cpp b/lib/gui/menu.cpp index 30c0b9c50..57adf1002 100644 --- a/lib/gui/menu.cpp +++ b/lib/gui/menu.cpp @@ -34,7 +34,6 @@ #include "../video/screen.h" #include "../video/drawing_context.h" #include "../app/setup.h" -#include "../audio/sound.h" #include "../special/timer.h" #include "../app/gettext.h" #include "../math/vector.h" diff --git a/lib/special/sprite.cpp b/lib/special/sprite.cpp index 75e6e5459..cf35687c4 100644 --- a/lib/special/sprite.cpp +++ b/lib/special/sprite.cpp @@ -34,14 +34,14 @@ Sprite::Sprite(lisp_object_t* cur) LispReader reader(cur); if(!reader.read_string("name", name)) - st_abort("Sprite wihtout name", ""); + Termination::abort("Sprite wihtout name", ""); reader.read_int("x-hotspot", x_hotspot); reader.read_int("y-hotspot", y_hotspot); reader.read_float("fps", fps); std::vector images; if(!reader.read_string_vector("images", images)) - st_abort("Sprite contains no images: ", name.c_str()); + Termination::abort("Sprite contains no images: ", name.c_str()); for(std::vector::size_type i = 0; i < images.size(); ++i) { diff --git a/lib/special/sprite.h b/lib/special/sprite.h index 8de7d5cd4..f397a4bd1 100644 --- a/lib/special/sprite.h +++ b/lib/special/sprite.h @@ -32,24 +32,6 @@ namespace SuperTux class Sprite { - private: - std::string name; - - int x_hotspot; - int y_hotspot; - - /** Frames per second */ - float fps; - - /** Number of seconds that a frame is displayed until it is switched - to the next frame */ - float frame_delay; - - float time; - - std::vector surfaces; - - void init_defaults(); public: /** cur has to be a pointer to data in the form of ((x-hotspot 5) (y-hotspot 10) ...) */ @@ -86,7 +68,25 @@ namespace SuperTux return surfaces[frame]; else return surfaces[0]; - } + } + private: + std::string name; + + int x_hotspot; + int y_hotspot; + + /** Frames per second */ + float fps; + + /** Number of seconds that a frame is displayed until it is switched + to the next frame */ + float frame_delay; + + float time; + + std::vector surfaces; + + void init_defaults(); }; } //namespace SuperTux diff --git a/lib/utils/configfile.cpp b/lib/utils/configfile.cpp index 0ef0374e2..46344222d 100644 --- a/lib/utils/configfile.cpp +++ b/lib/utils/configfile.cpp @@ -23,6 +23,7 @@ #include "../utils/configfile.h" #include "../app/setup.h" #include "../app/globals.h" +#include "../audio/sound_manager.h" using namespace SuperTux; @@ -38,14 +39,44 @@ static void defaults () { /* Set defaults: */ debug_mode = false; - audio_device = true; + SoundManager::get()->set_audio_device_available(true); use_fullscreen = false; show_fps = false; use_gl = false; - use_sound = true; - use_music = true; + SoundManager::get()->enable_sound(true); + SoundManager::get()->enable_music(true); +} + +FILE * SuperTux::opendata(const char * rel_filename, const char * mode) +{ + char * filename = NULL; + FILE * fi; + + filename = (char *) malloc(sizeof(char) * (strlen(st_dir) + + strlen(rel_filename) + 1)); + + strcpy(filename, st_dir); + /* Open the high score file: */ + + strcat(filename, rel_filename); + + /* Try opening the file: */ + fi = fopen(filename, mode); + + if (fi == NULL) + { + fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename); + + if (strcmp(mode, "r") == 0) + fprintf(stderr, "for read!!!\n"); + else if (strcmp(mode, "w") == 0) + fprintf(stderr, "for write!!!\n"); + } + free( filename ); + + return(fi); } void Config::load() @@ -78,8 +109,11 @@ void Config::load() LispReader reader(lisp_cdr(root_obj)); reader.read_bool("fullscreen", use_fullscreen); - reader.read_bool("sound", use_sound); - reader.read_bool("music", use_music); + bool temp; + reader.read_bool("sound", temp); + SoundManager::get()->enable_sound(temp); + reader.read_bool("music", temp); + SoundManager::get()->enable_music(temp); reader.read_bool("show_fps", show_fps); std::string video; @@ -116,8 +150,8 @@ void Config::save () fprintf(config, "(supertux-config\n"); fprintf(config, "\t;; the following options can be set to #t or #f:\n"); fprintf(config, "\t(fullscreen %s)\n", use_fullscreen ? "#t" : "#f"); - fprintf(config, "\t(sound %s)\n", use_sound ? "#t" : "#f"); - fprintf(config, "\t(music %s)\n", use_music ? "#t" : "#f"); + fprintf(config, "\t(sound %s)\n", SoundManager::get()->sound_enabled() ? "#t" : "#f"); + fprintf(config, "\t(music %s)\n", SoundManager::get()->music_enabled() ? "#t" : "#f"); fprintf(config, "\t(show_fps %s)\n", show_fps ? "#t" : "#f"); fprintf(config, "\n\t;; either \"opengl\" or \"sdl\"\n"); diff --git a/lib/utils/configfile.h b/lib/utils/configfile.h index 897982c31..a6471cc42 100644 --- a/lib/utils/configfile.h +++ b/lib/utils/configfile.h @@ -24,6 +24,8 @@ namespace SuperTux { +FILE * opendata(const char * filename, const char * mode); + class Config { public: void load (); diff --git a/lib/utils/lispreader.cpp b/lib/utils/lispreader.cpp index d7d141d80..50fe768b2 100644 --- a/lib/utils/lispreader.cpp +++ b/lib/utils/lispreader.cpp @@ -1132,7 +1132,7 @@ LispReader::read_float (const char* name, float& f) return false; if (!lisp_real_p(lisp_car(obj)) && !lisp_integer_p(lisp_car(obj))) - st_abort("LispReader expected type real at token: ", name); + Termination::abort("LispReader expected type real at token: ", name); f = lisp_real(lisp_car(obj)); return true; @@ -1149,7 +1149,7 @@ LispReader::read_string_vector (const char* name, std::vector& vec) while(!lisp_nil_p(obj)) { if (!lisp_string_p(lisp_car(obj))) - st_abort("LispReader expected type string at token: ", name); + Termination::abort("LispReader expected type string at token: ", name); vec.push_back(lisp_string(lisp_car(obj))); obj = lisp_cdr(obj); } @@ -1167,7 +1167,7 @@ LispReader::read_int_vector (const char* name, std::vector& vec) while(!lisp_nil_p(obj)) { if (!lisp_integer_p(lisp_car(obj))) - st_abort("LispReader expected type integer at token: ", name); + Termination::abort("LispReader expected type integer at token: ", name); vec.push_back(lisp_integer(lisp_car(obj))); obj = lisp_cdr(obj); } @@ -1185,7 +1185,7 @@ LispReader::read_int_vector (const char* name, std::vector& vec) while(!lisp_nil_p(obj)) { if (!lisp_integer_p(lisp_car(obj))) - st_abort("LispReader expected type integer at token: ", name); + Termination::abort("LispReader expected type integer at token: ", name); vec.push_back(lisp_integer(lisp_car(obj))); obj = lisp_cdr(obj); } @@ -1253,7 +1253,7 @@ LispReader::read_string (const char* name, std::string& str, bool translatable) return false; if (!lisp_string_p(lisp_car(obj))) - st_abort("LispReader expected type string at token: ", name); + Termination::abort("LispReader expected type string at token: ", name); str = lisp_string(lisp_car(obj)); return true; } @@ -1266,7 +1266,7 @@ LispReader::read_bool (const char* name, bool& b) return false; if (!lisp_boolean_p(lisp_car(obj))) - st_abort("LispReader expected type bool at token: ", name); + Termination::abort("LispReader expected type bool at token: ", name); b = lisp_boolean(lisp_car(obj)); return true; } diff --git a/lib/video/drawing_context.h b/lib/video/drawing_context.h index e0b2df5fd..46742344b 100644 --- a/lib/video/drawing_context.h +++ b/lib/video/drawing_context.h @@ -47,6 +47,7 @@ namespace SuperTux LAYER_GUI = 500 }; + /// Handles drawing of things. /** * This class provides functions for drawing things on screen. It also * maintains a stack of transforms that are applied to graphics. @@ -57,26 +58,26 @@ namespace SuperTux DrawingContext(); ~DrawingContext(); - /** Adds a drawing request for a surface into the request list */ + /// Adds a drawing request for a surface into the request list. void draw_surface(const Surface* surface, const Vector& position, int layer, Uint32 drawing_effect = NONE_EFFECT); - /** Adds a drawing request for part of a surface */ + /// Adds a drawing request for part of a surface. void draw_surface_part(const Surface* surface, const Vector& source, const Vector& size, const Vector& dest, int layer, Uint32 drawing_effect = NONE_EFFECT); - /** draws a text */ + /// Draws a text. void draw_text(Font* font, const std::string& text, const Vector& position, int layer, Uint32 drawing_effect = NONE_EFFECT); - /** draws aligned text */ + /// Draws aligned text. void draw_text_center(Font* font, const std::string& text, const Vector& position, int layer, Uint32 drawing_effect = NONE_EFFECT); - /** draws a color gradient onto the whole screen */ + /// Draws a color gradient onto the whole screen */ void draw_gradient(Color from, Color to, int layer); - /** fills a rectangle */ + /// Fills a rectangle. void draw_filled_rect(const Vector& topleft, const Vector& size, Color color, int layer); - /** Processes all pending drawing requests and flushes the list */ + /// Processes all pending drawing requests and flushes the list. void do_drawing(); const Vector& get_translation() const @@ -91,7 +92,7 @@ namespace SuperTux void push_transform(); void pop_transform(); - /** apply that effect in the next draws (effects are listed on surface.h) */ + /// Apply that effect in the next draws (effects are listed on surface.h). void set_drawing_effect(int effect); private: diff --git a/lib/video/font.h b/lib/video/font.h index d0b328b31..b32cd9050 100644 --- a/lib/video/font.h +++ b/lib/video/font.h @@ -33,11 +33,11 @@ namespace SuperTux and displays it in a StarTrek fashion */ void display_text_file(const std::string& file, float scroll_speed); - /* Text type */ + /// Font class Font { public: - /* Kinds of texts. */ + /// Kinds of texts. enum FontType { TEXT, // images for all characters NUM // only images for numbers @@ -46,7 +46,7 @@ namespace SuperTux Font(const std::string& file, FontType type, int w, int h, int shadowsize=2); ~Font(); - /** returns the height of the font */ + /// returns the height of the font. float get_height() const; /** returns the width of a given text. (Note that I won't add a normal * get_width function here, as we might switch to variable width fonts in the diff --git a/lib/video/surface.cpp b/lib/video/surface.cpp index 714227822..57cc4d270 100644 --- a/lib/video/surface.cpp +++ b/lib/video/surface.cpp @@ -46,7 +46,7 @@ SurfaceData::SurfaceData(SDL_Surface* temp, bool use_alpha_) temp->format->Bmask, temp->format->Amask); if(!surface) - st_abort("No memory left.", ""); + Termination::abort("No memory left.", ""); SDL_SetAlpha(temp,0,0); SDL_BlitSurface(temp, NULL, surface, NULL); } @@ -260,7 +260,7 @@ sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, temp = IMG_Load(file.c_str()); if (temp == NULL) - st_abort("Can't load", file); + Termination::abort("Can't load", file); /* Set source rectangle for conv: */ @@ -291,7 +291,7 @@ sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, sdl_surface = SDL_DisplayFormatAlpha(conv); if (sdl_surface == NULL) - st_abort("Can't covert to display format", file); + Termination::abort("Can't covert to display format", file); if (use_alpha == false && !use_gl) SDL_SetAlpha(sdl_surface, 0, 0); @@ -311,7 +311,7 @@ sdl_surface_from_file(const std::string& file, bool use_alpha) temp = IMG_Load(file.c_str()); if (temp == NULL) - st_abort("Can't load", file); + Termination::abort("Can't load", file); if(use_alpha == false && !use_gl) sdl_surface = SDL_DisplayFormat(temp); @@ -319,7 +319,7 @@ sdl_surface_from_file(const std::string& file, bool use_alpha) sdl_surface = SDL_DisplayFormatAlpha(temp); if (sdl_surface == NULL) - st_abort("Can't covert to display format", file); + Termination::abort("Can't covert to display format", file); if (use_alpha == false && !use_gl) SDL_SetAlpha(sdl_surface, 0, 0); @@ -358,7 +358,7 @@ SuperTux::sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha) } if (sdl_surface == NULL) - st_abort("Can't covert to display format", "SURFACE"); + Termination::abort("Can't covert to display format", "SURFACE"); if (use_alpha == false && !use_gl) SDL_SetAlpha(sdl_surface, 0, 0); @@ -376,7 +376,7 @@ sdl_surface_from_gradient(Color top, Color bottom, int w, int h) screen->format->Gmask, screen->format->Bmask, screen->format->Amask); if(sdl_surface == NULL) - st_abort("Cannot create surface for the gradient", "SURFACE"); + Termination::abort("Cannot create surface for the gradient", "SURFACE"); if(top == bottom) { diff --git a/src/badguy.cpp b/src/badguy.cpp index 807a8d86c..b3ef65d1f 100644 --- a/src/badguy.cpp +++ b/src/badguy.cpp @@ -357,7 +357,7 @@ BadGuy::action_mriceblock(double elapsed_time) tux.kick_timer.start(KICKING_TIME); set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right); physic.set_velocity_x((dir == LEFT) ? -3.5 : 3.5); - sound_manager->play_sound(sounds[SND_KICK], this, Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos()); } } @@ -367,7 +367,7 @@ BadGuy::action_mriceblock(double elapsed_time) check_horizontal_bump(); if(mode == KICK && changed != dir) { - sound_manager->play_sound(sounds[SND_RICOCHET], get_pos(), Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_RICOCHET), get_pos(), Sector::current()->player->get_pos()); } } @@ -561,7 +561,7 @@ BadGuy::action_bomb(double elapsed_time) dying = DYING_NOT; // now the bomb hurts timer.start(EXPLODETIME); - sound_manager->play_sound(sounds[SND_EXPLODE], this, Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_EXPLODE), this, Sector::current()->player->get_pos()); } else if(mode == BOMB_EXPLODE) { remove_me(); return; @@ -1034,7 +1034,7 @@ BadGuy::squish_me(Player* player) Sector::current()->add_score(Vector(base.x, base.y), 50 * player_status.score_multiplier); - sound_manager->play_sound(sounds[SND_SQUISH], get_pos(), Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos()); player_status.score_multiplier++; dying = DYING_SQUISHED; @@ -1054,7 +1054,7 @@ BadGuy::squish(Player* player) player->bounce(this); Sector::current()->add_score(Vector(base.x, base.y), 50 * player_status.score_multiplier); - sound_manager->play_sound(sounds[SND_SQUISH], get_pos(), Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(), Sector::current()->player->get_pos()); player_status.score_multiplier++; return; @@ -1062,7 +1062,7 @@ BadGuy::squish(Player* player) if (mode == NORMAL || mode == KICK) { /* Flatten! */ - sound_manager->play_sound(sounds[SND_STOMP], get_pos(), Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_STOMP), get_pos(), Sector::current()->player->get_pos()); mode = FLAT; set_sprite(img_mriceblock_flat_left, img_mriceblock_flat_right); physic.set_velocity_x(0); @@ -1070,7 +1070,7 @@ BadGuy::squish(Player* player) timer.start(4000); } else if (mode == FLAT) { /* Kick! */ - sound_manager->play_sound(sounds[SND_KICK], this, Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos()); if (player->base.x < base.x + (base.width/2)) { physic.set_velocity_x(5); @@ -1171,7 +1171,7 @@ BadGuy::kill_me(int score) score * player_status.score_multiplier); /* Play death sound: */ - sound_manager->play_sound(sounds[SND_FALL], this, Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_FALL), this, Sector::current()->player->get_pos()); } void @@ -1334,7 +1334,7 @@ BadGuy::collision(void *p_c_object, int c_object, CollisionType type) /* Get kicked if were flat */ if (mode == FLAT && !dying) { - sound_manager->play_sound(sounds[SND_KICK], this, Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_KICK), this, Sector::current()->player->get_pos()); // Hit from left side if (player->base.x < base.x) { diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 87c7bdc80..a1ab9466d 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -110,7 +110,7 @@ GameSession::restart_level() level->do_vertical_flip(); currentsector = level->get_sector("main"); if(!currentsector) - st_abort("Level has no main sector.", ""); + Termination::abort("Level has no main sector.", ""); currentsector->activate("main"); #if 0 // TODO @@ -153,7 +153,7 @@ GameSession::~GameSession() void GameSession::levelintro(void) { - sound_manager->halt_music(); + SoundManager::get()->halt_music(); char str[60]; @@ -256,7 +256,7 @@ GameSession::process_events() switch(event.type) { case SDL_QUIT: /* Quit event - quit: */ - st_abort("Received window close", ""); + Termination::abort("Received window close", ""); break; case SDL_KEYDOWN: /* A keypress! */ @@ -302,7 +302,7 @@ GameSession::process_events() switch(event.type) { case SDL_QUIT: /* Quit event - quit: */ - st_abort("Received window close", ""); + Termination::abort("Received window close", ""); break; case SDL_KEYDOWN: /* A keypress! */ @@ -474,7 +474,7 @@ GameSession::check_end_conditions() { end_sequence = ENDSEQUENCE_RUNNING; last_x_pos = -1; - sound_manager->play_music(level_end_song, 0); + SoundManager::get()->play_music(level_end_song, 0); endsequence_timer.start(7000); // 5 seconds until we finish the map tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.) } @@ -716,7 +716,7 @@ void bumpbrick(float x, float y) Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32)); - sound_manager->play_sound(sounds[SND_BRICK], Vector(x, y), Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos()); } /* (Status): */ diff --git a/src/gameloop.h b/src/gameloop.h index 172c9fa05..d82c8a3b7 100644 --- a/src/gameloop.h +++ b/src/gameloop.h @@ -23,7 +23,6 @@ #define SUPERTUX_GAMELOOP_H #include "special/timer.h" -#include "audio/sound.h" #include "special/base.h" using namespace SuperTux; diff --git a/src/high_scores.cpp b/src/high_scores.cpp index 4e1dceb3f..87940041f 100644 --- a/src/high_scores.cpp +++ b/src/high_scores.cpp @@ -141,8 +141,8 @@ void save_hs(int score) /* Save data file: */ filename = highscore_filename; - fcreatedir(filename.c_str()); - if(fwriteable(filename.c_str())) + FileSystem::fcreatedir(filename.c_str()); + if(FileSystem::fwriteable(filename.c_str())) { fi = fopen(filename.c_str(), "w"); if (fi == NULL) diff --git a/src/level_subset.cpp b/src/level_subset.cpp index fb530498c..ba5d803e1 100644 --- a/src/level_subset.cpp +++ b/src/level_subset.cpp @@ -106,12 +106,12 @@ void LevelSubset::load(const char* subset) snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset); if(access(filename, R_OK) == 0) { - files = read_directory(filename); + files = FileSystem::read_directory(filename); } else { snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset); - files = read_directory(filename); + files = FileSystem::read_directory(filename); } for(std::vector::iterator i = files.begin(); i != files.end(); ++i) @@ -131,11 +131,11 @@ LevelSubset::save() /* Save data file: */ filename = "/levels/" + name + "/"; - fcreatedir(filename.c_str()); + FileSystem::fcreatedir(filename.c_str()); filename = std::string(st_dir) + "/levels/" + name + "/info"; - if(!fwriteable(filename.c_str())) + if(!FileSystem::fwriteable(filename.c_str())) filename = datadir + "/levels/" + name + "/info"; - if(fwriteable(filename.c_str())) + if(FileSystem::fwriteable(filename.c_str())) { fi = fopen(filename.c_str(), "w"); if (fi == NULL) diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index 998451a78..49cfa354d 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -77,7 +77,7 @@ enum { TM_IA, TM_BG, TM_FG }; LevelEditor::LevelEditor() { - level_subsets = dsubdirs("/levels", "level1.stl"); + level_subsets = FileSystem::dsubdirs("/levels", "level1.stl"); le_level_subset = new LevelSubset; le_level = NULL; @@ -201,7 +201,7 @@ int LevelEditor::run(char* filename) le_level = NULL; le_levelnb = 1; - sound_manager->halt_music(); + SoundManager::get()->halt_music(); while (SDL_PollEvent(&event)) {} @@ -544,8 +544,8 @@ void LevelEditor::update_level_settings_menu() level_settings_menu->get_item_by_id(MNID_NAME).change_input(le_level->name.c_str()); level_settings_menu->get_item_by_id(MNID_AUTHOR).change_input(le_level->author.c_str()); - string_list_copy(level_settings_menu->get_item_by_id(MNID_SONG).list, dfiles("music/",NULL, "-fast")); - string_list_copy(level_settings_menu->get_item_by_id(MNID_BGIMG).list, dfiles("images/background",NULL, NULL)); + string_list_copy(level_settings_menu->get_item_by_id(MNID_SONG).list, FileSystem::dfiles("music/",NULL, "-fast")); + string_list_copy(level_settings_menu->get_item_by_id(MNID_BGIMG).list, FileSystem::dfiles("images/background",NULL, NULL)); string_list_add_item(level_settings_menu->get_item_by_id(MNID_BGIMG).list,""); string_list_add_item(level_settings_menu->get_item_by_id(MNID_PARTICLE).list,""); string_list_add_item(level_settings_menu->get_item_by_id(MNID_PARTICLE).list,"snow"); @@ -1687,7 +1687,7 @@ void LevelEditor::testlevel() session.run(); player_status.reset(); - sound_manager->halt_music(); + SoundManager::get()->halt_music(); Menu::set_current(NULL); } diff --git a/src/misc.cpp b/src/misc.cpp index 6ff02567f..7873929f2 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -46,7 +46,7 @@ void process_options_menu(void) if(use_gl != options_menu->isToggled(MNID_OPENGL)) { use_gl = !use_gl; - st_video_setup(screen->w,screen->h); + Setup::video(screen->w,screen->h); } #else options_menu->get_item_by_id(MNID_OPENGL).toggled = false; @@ -56,18 +56,17 @@ void process_options_menu(void) if(use_fullscreen != options_menu->isToggled(MNID_FULLSCREEN)) { use_fullscreen = !use_fullscreen; - st_video_setup(screen->w,screen->h); + Setup::video(screen->w,screen->h); } break; case MNID_SOUND: - if(use_sound != options_menu->isToggled(MNID_SOUND)) - use_sound = !use_sound; + if(SoundManager::get()->sound_enabled() != options_menu->isToggled(MNID_SOUND)) + SoundManager::get()->enable_sound(!SoundManager::get()->sound_enabled()); break; case MNID_MUSIC: - if(use_music != options_menu->isToggled(MNID_MUSIC)) + if(SoundManager::get()->music_enabled() != options_menu->isToggled(MNID_MUSIC)) { - use_music = !use_music; - sound_manager->enable_music(use_music); + SoundManager::get()->enable_music(!SoundManager::get()->music_enabled()); } break; case MNID_SHOWFPS: @@ -107,10 +106,10 @@ void st_menu(void) options_menu->additem(MN_DEACTIVE,_("OpenGL (not supported)"),use_gl, 0, MNID_OPENGL); #endif options_menu->additem(MN_TOGGLE,_("Fullscreen"),use_fullscreen,0, MNID_FULLSCREEN); - if(audio_device) + if(SoundManager::get()->audio_device_available()) { - options_menu->additem(MN_TOGGLE,_("Sound "), use_sound,0, MNID_SOUND); - options_menu->additem(MN_TOGGLE,_("Music "), use_music,0, MNID_MUSIC); + options_menu->additem(MN_TOGGLE,_("Sound "), SoundManager::get()->sound_enabled(),0, MNID_SOUND); + options_menu->additem(MN_TOGGLE,_("Music "), SoundManager::get()->music_enabled(),0, MNID_MUSIC); } else { diff --git a/src/player.cpp b/src/player.cpp index 624f18186..d52d28ab9 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -419,7 +419,7 @@ Player::handle_horizontal_input() if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) { if(fabs(vx)>SKID_XM && !skidding_timer.check()) { skidding_timer.start(SKID_TIME); - sound_manager->play_sound(sounds[SND_SKID]); + SoundManager::get()->play_sound(IDToSound(SND_SKID)); ax *= 2.5; } else { ax *= 2; @@ -486,9 +486,9 @@ Player::handle_vertical_input() jumping = true; can_jump = false; if (size == SMALL) - sound_manager->play_sound(sounds[SND_JUMP]); + SoundManager::get()->play_sound(IDToSound(SND_JUMP)); else - sound_manager->play_sound(sounds[SND_BIGJUMP]); + SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP)); } // Let go of jump key else if(input.up == UP && jumping && physic.get_velocity_y() > 0) @@ -681,7 +681,7 @@ Player::grabdistros() if(player_status.lives < MAX_LIVES) ++player_status.lives; /*We want to hear the sound even, if MAX_LIVES is reached*/ - sound_manager->play_sound(sounds[SND_LIFEUP]); + SoundManager::get()->play_sound(IDToSound(SND_LIFEUP)); } } @@ -923,7 +923,7 @@ Player::kill(HurtMode mode) if(dying) return; - sound_manager->play_sound(sounds[SND_HURT]); + SoundManager::get()->play_sound(IDToSound(SND_HURT)); physic.set_velocity_x(0); diff --git a/src/player.h b/src/player.h index b2a40c1d1..ca8aa538b 100644 --- a/src/player.h +++ b/src/player.h @@ -27,7 +27,6 @@ #include "special/base.h" #include "video/surface.h" #include "collision.h" -#include "audio/sound.h" #include "special/moving_object.h" #include "math/physic.h" #include "app/defines.h" diff --git a/src/resources.cpp b/src/resources.cpp index 48eb42890..74bd667d3 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -82,8 +82,6 @@ void loadshared() int i; sprite_manager = new SpriteManager(datadir + "/supertux.strf"); - sound_manager = new SoundManager(); - sound_manager->enable_music(use_music); /* Tuxes: */ smalltux_star = sprite_manager->load("smalltux-star"); @@ -265,11 +263,11 @@ void loadshared() Send a mail to me: neoneurone@users.sf.net, if you have another opinion. :) */ for (i = 0; i < NUM_SOUNDS; i++) - sounds.push_back(load_sound(datadir + soundfilenames[i])); + SoundManager::get()->add_sound(SoundManager::get()->load_sound(datadir + soundfilenames[i]),i); /* Herring song */ - herring_song = sound_manager->load_music(datadir + "/music/SALCON.MOD"); - level_end_song = sound_manager->load_music(datadir + "/music/leveldone.mod"); + herring_song = SoundManager::get()->load_music(datadir + "/music/SALCON.MOD"); + level_end_song = SoundManager::get()->load_music(datadir + "/music/leveldone.mod"); } @@ -311,12 +309,8 @@ void unloadshared(void) for (int i = 0; i < DOOR_OPENING_FRAMES; i++) delete door_opening[i]; - for (i = 0; i < NUM_SOUNDS; i++) - free_chunk(sounds[i]); - delete sprite_manager; sprite_manager = 0; - delete sound_manager; sound_manager = 0; } diff --git a/src/sector.cpp b/src/sector.cpp index 3bb3ee60c..a108e3065 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -669,7 +669,7 @@ Sector::add_bullet(const Vector& pos, float xm, Direction dir) throw std::runtime_error("wrong bullet type."); add_object(new_bullet); - sound_manager->play_sound(sounds[SND_SHOOT]); + SoundManager::get()->play_sound(IDToSound(SND_SHOOT)); return true; } @@ -711,7 +711,7 @@ Sector::trybreakbrick(const Vector& pos, bool small) solids->change_at(pos, tile->next_tile); } - sound_manager->play_sound(sounds[SND_DISTRO]); + SoundManager::get()->play_sound(IDToSound(SND_DISTRO)); player_status.score = player_status.score + SCORE_DISTRO; player_status.distros++; return true; @@ -727,7 +727,7 @@ Sector::trybreakbrick(const Vector& pos, bool small) (int)(pos.y / 32) * 32), tile); /* Get some score: */ - sound_manager->play_sound(sounds[SND_BRICK]); + SoundManager::get()->play_sound(IDToSound(SND_BRICK)); player_status.score = player_status.score + SCORE_BRICK; return true; @@ -765,7 +765,7 @@ Sector::tryemptybox(const Vector& pos, Direction col_side) { case 1: // Box with a distro! add_bouncy_distro(Vector(posx, posy)); - sound_manager->play_sound(sounds[SND_DISTRO]); + SoundManager::get()->play_sound(IDToSound(SND_DISTRO)); player_status.score = player_status.score + SCORE_DISTRO; player_status.distros++; break; @@ -775,7 +775,7 @@ Sector::tryemptybox(const Vector& pos, Direction col_side) add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP); else /* Tux is big, add a fireflower: */ add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER); - sound_manager->play_sound(sounds[SND_UPGRADE]); + SoundManager::get()->play_sound(IDToSound(SND_UPGRADE)); break; case 5: // Add an ice flower upgrade! @@ -783,7 +783,7 @@ Sector::tryemptybox(const Vector& pos, Direction col_side) add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP); else /* Tux is big, add an iceflower: */ add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER); - sound_manager->play_sound(sounds[SND_UPGRADE]); + SoundManager::get()->play_sound(IDToSound(SND_UPGRADE)); break; case 3: // Add a golden herring @@ -823,7 +823,7 @@ Sector::trygrabdistro(const Vector& pos, int bounciness) return; solids->change_at(pos, tile->next_tile); - sound_manager->play_sound(sounds[SND_DISTRO]); + SoundManager::get()->play_sound(IDToSound(SND_DISTRO)); if (bounciness == BOUNCE) { @@ -868,7 +868,7 @@ Sector::load_music() char* song_path; char* song_subtitle; - level_song = sound_manager->load_music(datadir + "/music/" + song_title); + level_song = SoundManager::get()->load_music(datadir + "/music/" + song_title); song_path = (char *) malloc(sizeof(char) * datadir.length() + strlen(song_title.c_str()) + 8 + 5); @@ -876,10 +876,10 @@ Sector::load_music() strcpy(strstr(song_subtitle, "."), "\0"); sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), song_subtitle, strstr(song_title.c_str(), ".")); - if(!sound_manager->exists_music(song_path)) { + if(!SoundManager::get()->exists_music(song_path)) { level_song_fast = level_song; } else { - level_song_fast = sound_manager->load_music(song_path); + level_song_fast = SoundManager::get()->load_music(song_path); } free(song_subtitle); free(song_path); @@ -891,16 +891,16 @@ Sector::play_music(int type) currentmusic = type; switch(currentmusic) { case HURRYUP_MUSIC: - sound_manager->play_music(level_song_fast); + SoundManager::get()->play_music(level_song_fast); break; case LEVEL_MUSIC: - sound_manager->play_music(level_song); + SoundManager::get()->play_music(level_song); break; case HERRING_MUSIC: - sound_manager->play_music(herring_song); + SoundManager::get()->play_music(herring_song); break; default: - sound_manager->halt_music(); + SoundManager::get()->halt_music(); break; } } diff --git a/src/special.cpp b/src/special.cpp index 09f287ca4..7762d2752 100644 --- a/src/special.cpp +++ b/src/special.cpp @@ -27,7 +27,6 @@ #include "camera.h" #include "gameloop.h" #include "video/screen.h" -#include "audio/sound.h" #include "scene.h" #include "app/globals.h" #include "player.h" @@ -280,7 +279,7 @@ Upgrade::bump(Player* player) if(kind != UPGRADE_GROWUP) return; - sound_manager->play_sound(sounds[SND_BUMP_UPGRADE], Vector(base.x, base.y), Sector::current()->player->get_pos()); + SoundManager::get()->play_sound(IDToSound(SND_BUMP_UPGRADE), Vector(base.x, base.y), Sector::current()->player->get_pos()); // determine new direction Direction old_dir = dir; @@ -328,30 +327,30 @@ Upgrade::collision(void* p_c_object, int c_object, CollisionType type) if (kind == UPGRADE_GROWUP) { - sound_manager->play_sound(sounds[SND_EXCELLENT]); + SoundManager::get()->play_sound(IDToSound(SND_EXCELLENT)); pplayer->grow(true); } else if (kind == UPGRADE_FIREFLOWER) { - sound_manager->play_sound(sounds[SND_COFFEE]); + SoundManager::get()->play_sound(IDToSound(SND_COFFEE)); pplayer->grow(true); pplayer->got_power = pplayer->FIRE_POWER; } else if (kind == UPGRADE_ICEFLOWER) { - sound_manager->play_sound(sounds[SND_COFFEE]); + SoundManager::get()->play_sound(IDToSound(SND_COFFEE)); pplayer->grow(true); pplayer->got_power = pplayer->ICE_POWER; } else if (kind == UPGRADE_FIREFLOWER) { - sound_manager->play_sound(sounds[SND_COFFEE]); + SoundManager::get()->play_sound(IDToSound(SND_COFFEE)); pplayer->grow(true); pplayer->got_power = pplayer->FIRE_POWER; } else if (kind == UPGRADE_HERRING) { - sound_manager->play_sound(sounds[SND_HERRING]); + SoundManager::get()->play_sound(IDToSound(SND_HERRING)); pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME); Sector::current()->play_music(HERRING_MUSIC); } @@ -359,7 +358,7 @@ Upgrade::collision(void* p_c_object, int c_object, CollisionType type) { if(player_status.lives < MAX_LIVES) { player_status.lives++; - sound_manager->play_sound(sounds[SND_LIFEUP]); + SoundManager::get()->play_sound(IDToSound(SND_LIFEUP)); } } diff --git a/src/supertux.cpp b/src/supertux.cpp index 01f15f820..d089f0131 100644 --- a/src/supertux.cpp +++ b/src/supertux.cpp @@ -52,15 +52,15 @@ int main(int argc, char * argv[]) textdomain(PACKAGE); bind_textdomain_codeset(PACKAGE, "ISO-8859-1"); - st_info_setup(PACKAGE_NAME,PACKAGE,PACKAGE_VERSION); + Setup::info(PACKAGE_NAME,PACKAGE,PACKAGE_VERSION); - st_directory_setup(); - parseargs(argc, argv); + Setup::directories(); + Setup::parseargs(argc, argv); - st_audio_setup(); - st_video_setup(SCREEN_W,SCREEN_H); - st_joystick_setup(); - st_general_setup(); + Setup::audio(); + Setup::video(SCREEN_W,SCREEN_H); + Setup::joystick(); + Setup::general(); st_menu(); loadshared(); @@ -99,13 +99,13 @@ int main(int argc, char * argv[]) //SDL_Flip(screen); unloadshared(); - st_general_free(); + Setup::general_free(); st_menu_free(); TileManager::destroy_instance(); #ifdef DEBUG Surface::debug_check(); #endif - st_shutdown(); + Termination::shutdown(); #ifndef DEBUG // we want to see the backtrace in gdb when in debug mode } catch (SuperTuxException &e) diff --git a/src/tile_manager.cpp b/src/tile_manager.cpp index c963a721d..7d6841a10 100644 --- a/src/tile_manager.cpp +++ b/src/tile_manager.cpp @@ -59,7 +59,7 @@ void TileManager::load_tileset(std::string filename) lisp_object_t* root_obj = lisp_read_from_file(filename); if (!root_obj) - st_abort("Couldn't load file", filename); + Termination::abort("Couldn't load file", filename); if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0) { diff --git a/src/title.cpp b/src/title.cpp index 6a4c5f041..802576310 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -103,7 +103,7 @@ void free_contrib_menu() void generate_contrib_menu() { /** Generating contrib levels list by making use of Level Subset */ - string_list_type level_subsets = dsubdirs("/levels", "info"); + string_list_type level_subsets = FileSystem::dsubdirs("/levels", "info"); free_contrib_menu(); @@ -272,7 +272,7 @@ void title(void) img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true); /* Generating contrib maps by only using a string_list */ - worldmap_list = dfiles("levels/worldmap", NULL, "icyisland.stwm"); + worldmap_list = FileSystem::dfiles("levels/worldmap", NULL, "icyisland.stwm"); titlesession->get_current_sector()->activate(); titlesession->set_current(); diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 8f3282c72..8c11af927 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -98,7 +98,7 @@ TileManager::TileManager() lisp_object_t* root_obj = lisp_read_from_file(stwt_filename); if (!root_obj) - st_abort("Couldn't load file", stwt_filename); + Termination::abort("Couldn't load file", stwt_filename); if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0) { @@ -386,7 +386,7 @@ WorldMap::load_map() { lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename); if (!root_obj) - st_abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename); + Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename); if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0) { @@ -531,7 +531,7 @@ WorldMap::get_input() switch(event.type) { case SDL_QUIT: - st_abort("Received window close", ""); + Termination::abort("Received window close", ""); break; case SDL_KEYDOWN: @@ -771,7 +771,7 @@ WorldMap::update(float delta) break; } - sound_manager->play_music(song); + SoundManager::get()->play_music(song); Menu::set_current(0); if (!savegame_file.empty()) savegame(savegame_file); @@ -954,8 +954,8 @@ WorldMap::display() quit = false; - song = sound_manager->load_music(datadir + "/music/" + music); - sound_manager->play_music(song); + song = SoundManager::get()->load_music(datadir + "/music/" + music); + SoundManager::get()->play_music(song); unsigned int last_update_time; unsigned int update_time;