Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #302 from stefanbeller/errorhandling

Introduce Utils::loadSfx function, which includes errorhandling.
  • Loading branch information...
commit bce917589a04a87714b7a1e7755cdd7ebe883cef 2 parents 92fcc6d + 5d5b084
@clintbellanger authored
View
14 src/Avatar.cpp
@@ -207,15 +207,11 @@ void Avatar::loadSounds() {
Mix_FreeChunk(sound_block);
Mix_FreeChunk(level_up);
- sound_melee = Mix_LoadWAV(mods->locate("soundfx/melee_attack.ogg").c_str());
- sound_hit = Mix_LoadWAV(mods->locate("soundfx/" + stats.base + "_hit.ogg").c_str());
- sound_die = Mix_LoadWAV(mods->locate("soundfx/" + stats.base + "_die.ogg").c_str());
- sound_block = Mix_LoadWAV(mods->locate("soundfx/powers/block.ogg").c_str());
- level_up = Mix_LoadWAV(mods->locate("soundfx/level_up.ogg").c_str());
-
- if (!sound_melee || !sound_hit || !sound_die || !level_up) {
- printf("Mix_LoadWAV: %s\n", Mix_GetError());
- }
+ sound_melee = loadSfx(mods->locate("soundfx/melee_attack.ogg"), "Avatar melee attack");
+ sound_hit = loadSfx(mods->locate("soundfx/" + stats.base + "_hit.ogg"), "Avatar was hit");
+ sound_die = loadSfx(mods->locate("soundfx/" + stats.base + "_die.ogg"), "Avatar death");
+ sound_block = loadSfx(mods->locate("soundfx/powers/block.ogg"), "Avatar blocking");
+ level_up = loadSfx(mods->locate("soundfx/level_up.ogg"), "Avatar leveling up");
}
}
View
15 src/EnemyManager.cpp
@@ -50,24 +50,19 @@ void EnemyManager::loadSounds(const string& type_id) {
if (audio && SOUND_VOLUME && type_id != "none") {
string path;
path = mods->locate("soundfx/enemies/" + type_id + "_phys.ogg");
- sound_phys.push_back(Mix_LoadWAV(path.c_str()));
- if (!sound_phys.back()) fprintf(stderr, "Could not load %s\n", path.c_str());
+ sound_phys.push_back(loadSfx(path, "EnemyManager physical attack sound"));
path = mods->locate("soundfx/enemies/" + type_id + "_ment.ogg");
- sound_ment.push_back(Mix_LoadWAV(path.c_str()));
- if (!sound_ment.back()) fprintf(stderr, "Could not load %s\n", path.c_str());
+ sound_ment.push_back(loadSfx(path, "EnemyManager mental attack sound"));
path = mods->locate("soundfx/enemies/" + type_id + "_hit.ogg");
- sound_hit.push_back(Mix_LoadWAV(path.c_str()));
- if (!sound_hit.back()) fprintf(stderr, "Could not load %s\n", path.c_str());
+ sound_hit.push_back(loadSfx(path, "EnemyManager physical hit sound"));
path = mods->locate("soundfx/enemies/" + type_id + "_die.ogg");
- sound_die.push_back(Mix_LoadWAV(path.c_str()));
- if (!sound_die.back()) fprintf(stderr, "Could not load %s\n", path.c_str());
+ sound_die.push_back(loadSfx(path, "EnemyManager die sound"));
path = mods->locate("soundfx/enemies/" + type_id + "_critdie.ogg");
- sound_critdie.push_back(Mix_LoadWAV(path.c_str()));
- if (!sound_critdie.back()) fprintf(stderr, "Could not load %s\n", path.c_str());
+ sound_critdie.push_back(loadSfx(path, "EnemyManager critdeath sound"));
} else {
sound_phys.push_back(NULL);
View
26 src/ItemManager.cpp
@@ -349,18 +349,18 @@ void ItemManager::loadSounds() {
memset(sfx, 0, sizeof(sfx));
if (audio && SOUND_VOLUME) {
- sfx[SFX_BOOK] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_book.ogg").c_str());
- sfx[SFX_CLOTH] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_cloth.ogg").c_str());
- sfx[SFX_COINS] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_coins.ogg").c_str());
- sfx[SFX_GEM] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_gem.ogg").c_str());
- sfx[SFX_LEATHER] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_leather.ogg").c_str());
- sfx[SFX_METAL] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_metal.ogg").c_str());
- sfx[SFX_PAGE] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_page.ogg").c_str());
- sfx[SFX_MAILLE] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_maille.ogg").c_str());
- sfx[SFX_OBJECT] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_object.ogg").c_str());
- sfx[SFX_HEAVY] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_heavy.ogg").c_str());
- sfx[SFX_WOOD] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_wood.ogg").c_str());
- sfx[SFX_POTION] = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_potion.ogg").c_str());
+ sfx[SFX_BOOK] = loadSfx(mods->locate("soundfx/inventory/inventory_book.ogg"),"ItemManager books");
+ sfx[SFX_CLOTH] = loadSfx(mods->locate("soundfx/inventory/inventory_cloth.ogg"), "ItemManager cloths");
+ sfx[SFX_COINS] = loadSfx(mods->locate("soundfx/inventory/inventory_coins.ogg"), "ItemManager coins");
+ sfx[SFX_GEM] = loadSfx(mods->locate("soundfx/inventory/inventory_gem.ogg"), "ItemManager gems");
+ sfx[SFX_LEATHER] = loadSfx(mods->locate("soundfx/inventory/inventory_leather.ogg"), "ItemManager leather");
+ sfx[SFX_METAL] = loadSfx(mods->locate("soundfx/inventory/inventory_metal.ogg"), "ItemManager metal");
+ sfx[SFX_PAGE] = loadSfx(mods->locate("soundfx/inventory/inventory_page.ogg"), "ItemManager page");
+ sfx[SFX_MAILLE] = loadSfx(mods->locate("soundfx/inventory/inventory_maille.ogg"), "ItemManager maille");
+ sfx[SFX_OBJECT] = loadSfx(mods->locate("soundfx/inventory/inventory_object.ogg"), "ItemManager objects");
+ sfx[SFX_HEAVY] = loadSfx(mods->locate("soundfx/inventory/inventory_heavy.ogg"), "ItemManager heavy");
+ sfx[SFX_WOOD] = loadSfx(mods->locate("soundfx/inventory/inventory_wood.ogg"), "ItemManager wood");
+ sfx[SFX_POTION] = loadSfx(mods->locate("soundfx/inventory/inventory_potion.ogg"), "ItemManager potions");
}
}
@@ -604,7 +604,7 @@ TooltipData ItemManager::getTooltip(int item, StatBlock *stats, int context) {
if (items[item].flavor != "") {
tip.addText(items[item].flavor, color_flavor);
}
-
+
// buy or sell price
if (items[item].price > 0) {
View
3  src/LootManager.cpp
@@ -97,7 +97,8 @@ LootManager::LootManager(ItemManager *_items, MapRenderer *_map, StatBlock *_her
loadGraphics();
if (audio && SOUND_VOLUME)
- loot_flip = Mix_LoadWAV(mods->locate("soundfx/flying_loot.ogg").c_str());
+ loot_flip = loadSfx(mods->locate("soundfx/flying_loot.ogg").c_str(), "LootManager dropping loot");
+
full_msg = false;
if (!lootManager)
View
2  src/MapRenderer.cpp
@@ -81,7 +81,7 @@ void MapRenderer::playSFX(string filename) {
Mix_FreeChunk(sfx);
sfx = NULL;
if (audio) {
- sfx = Mix_LoadWAV(mods->locate(filename).c_str());
+ sfx = loadSfx(mods->locate(filename), "MapRenderer background music");
sfx_filename = filename;
}
}
View
7 src/MenuManager.cpp
@@ -207,11 +207,8 @@ void MenuManager::loadIcons() {
void MenuManager::loadSounds() {
if (audio && SOUND_VOLUME) {
- sfx_open = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_page.ogg").c_str());
- sfx_close = Mix_LoadWAV(mods->locate("soundfx/inventory/inventory_book.ogg").c_str());
-
- if (!sfx_open || !sfx_close)
- fprintf(stderr, "Mix_LoadWAV: %s\n", Mix_GetError());
+ sfx_open = loadSfx(mods->locate("soundfx/inventory/inventory_page.ogg"), "MenuManager open tab");
+ sfx_close = loadSfx(mods->locate("soundfx/inventory/inventory_book.ogg"), "MenuManager close tab");
} else {
sfx_open = NULL;
sfx_close = NULL;
View
6 src/NPC.cpp
@@ -195,18 +195,18 @@ int NPC::loadSound(const string& filename, int type) {
if (!SOUND_VOLUME || !audio)
return -1;
- Mix_Chunk *a = Mix_LoadWAV(mods->locate("soundfx/npcs/" + filename).c_str());
+ Mix_Chunk *a = loadSfx(mods->locate("soundfx/npcs/" + filename), "NPC voice");
if (!a)
return -1;
if (type == NPC_VOX_INTRO) {
vox_intro.push_back(a);
- return vox_intro.size()-1;
+ return vox_intro.size() - 1;
}
if (type == NPC_VOX_QUEST) {
vox_quests.push_back(a);
- return vox_quests.size()-1;
+ return vox_quests.size() - 1;
}
return -1;
}
View
6 src/PowerManager.cpp
@@ -305,11 +305,9 @@ int PowerManager::loadSFX(const string& filename) {
// we don't already have this sound loaded, so load it
Mix_Chunk* sound;
if (audio && SOUND_VOLUME) {
- sound = Mix_LoadWAV(mods->locate("soundfx/powers/" + filename).c_str());
- if(!sound) {
- cerr << "Couldn't load power soundfx: " << filename << endl;
+ sound = loadSfx(mods->locate("soundfx/powers/" + filename), "PowerManager sfx");
+ if (!sound)
return -1;
- }
} else {
sound = NULL;
}
View
8 src/Utils.cpp
@@ -361,3 +361,11 @@ bool checkPixel(Point px, SDL_Surface *surface) {
return true;
}
+
+Mix_Chunk *loadSfx(const string &filename, const string &errormessage)
+{
+ Mix_Chunk * sound = Mix_LoadWAV(filename.c_str());
+ if (!sound)
+ fprintf(stderr, "%s: Loading sound %s failed: %s \n", errormessage.c_str(), filename.c_str(), Mix_GetError());
+ return sound;
+}
View
4 src/Utils.h
@@ -28,6 +28,8 @@ FLARE. If not, see http://www.gnu.org/licenses/
#include <SDL.h>
#include <SDL_image.h>
+#include <SDL_mixer.h>
+
#include <string>
#include <stdint.h>
@@ -109,4 +111,6 @@ SDL_Surface* createAlphaSurface(int width, int height);
* The bright pink (rgb 0xff00ff) is set as transparent color.
*/
SDL_Surface* createSurface(int width, int height);
+
+Mix_Chunk *loadSfx(const std::string &filename, const std::string &errormessage);
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.