I've come across an issue with developing magic tools using the Mix_LoadWAV() function when  --nosound is enabled or when the users hardware does not support sound.

First off I'd like to say that the whole problem is a result of checking to make sure that Mix_LoadWAV() returns a valid value. If (as all the previoustools do) no error checking is performed then this issue does not occur. But to me that seem's like an inelegant solution. I'd also like to point out that there is already code to make sure that no sound is played under any condition if --nosound is enabled or tuxpaint is muted.

Mix_LoadWAV() is used by magic tools to load any sounds they wish to use. It is a direct call to the SDL API and therin lies the issue. If sound is disabled or unavailble then tuxpaint.c does not intialise the SDL Sound API and therefor all calls to Mix_LoadWAV() fail and return an error condition. If (as my tools do) the return condition of Mix_LoadWAV() is checked to make sure that the sound file is loaded then it appears to the magic tool that the sound file does not exist. 

My suggested solution (below) is to make all magic tools call a tuxpaint api function to load sounds rather than having them making direct calls to SDL (it seems unusual that they dont do this already, considering the playsound function is already used in this way). This function would act as a wrapper for the SDL Mix_LoadWAV() function but would only return an error when sound is enabled and the file doesnt exist. Since the tuxpaint playsound function checks to see if sound is enabled before attempting to play a sound this should cause no issues in the main tuxpaint code.

The only issue I can see with this solution is that it would probably be best to update all current tools to use the new function (the old function will still work, it would just be bad practice to use) so that new developers dont inadvertandly copy old code.

I believe that is probably a better solution than focing the plugin developer to ignore error checking and I wanted to check if anyone has any comments or ideas on a better way of implementing a solution before I started looking into adding this feature.



//My suggested wrapper function

//Returns a pointer to a loaded sound file if sound is enabled, returns NULL on error or -1 if sound is disabled

//magic_playsound can still be called if this fucntion returns -1 as magic_playsound has inbuilt checking for disabled sound
Mix_Chunk * magic_loadsound(char* file){
  if (!use_sound){
  //If sound is diabled return a dummy value 
  return (Mix_Chunk*)-1;
  //Mix_LoadWAV returns NULL if the file doesnt exist so we can just pass on the return value
  Mix_Chunk * temp = Mix_LoadWAV(file);
  return temp;

//Below is the already existing playsound function

void magic_playsound(Mix_Chunk * snd, int left_right, int up_down)
#ifndef NOSOUND

  int left, dist;

  // Don't play if sound is disabled (nosound), or sound is temporarily
  // muted (Alt+S), or sound ptr is NULL
  if (mute || !use_sound || snd == NULL)