SDL mixer wont play

Started by
0 comments, last by Replicon 17 years, 10 months ago
I have done everything I can to figure this problem out but unfortunately no luck. SDL mixer wont play and I dont understand it. My IDE (DEVC++) is set up perfectly. Irecieve no errors in when compiling the code. so here is the source hopefully you fellow game programmers can help.

#include "SDL.h"
#include "SDL_mixer.h"
#include <string>
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "game_time.h"

//window settings and functions
const int SCREEN_WIDTH= 640;
const int SCREEN_HEIGHT= 480;
const int SCREEN_BPP= 32;    
bool init();
bool clean_up();


//music variables
Mix_Music *music = NULL;

//frame rate time, variables
//The frames per second 
const int FRAMES_PER_SECOND = 20;
Timer fps;
bool cap = true;
int frame = 0;

//font colors and text type variables and functions
TTF_Font *font;
SDL_Color textColor = { 255, 55, 20 };// color of text

//surfaces that will be used
SDL_Surface *player1= NULL;
SDL_Surface *background= NULL;
SDL_Surface *screen= NULL;
SDL_Surface *message= NULL;
//event handler variables
SDL_Event event;

//Image Loading functions
SDL_Surface *load_image(std::string filename);

//Image optimization fuctions
void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface* destination);


int main( int argc, char* args[] )
{
 //app startup check
 if(init()==false)
 {
     return 1;
 }           
 

 
   //loop control variable
   bool go_on=true;
   //screen images
   player1= load_image("adv2as2.bmp");
   background= load_image("bg1.bmp");
   
  
   
   //apply images to screen
   apply_surface(0,0,background,screen);
   apply_surface(180,140,player1,screen);
   
  
  
   
   //update sdl screen
   if(SDL_Flip(screen)==-1)
   {
                           return 1;
   }                        
   
 
   //loop
   while(go_on)
   {
         while(SDL_PollEvent(&event))
         {
            //If a key was pressed
            if( event.type == SDL_KEYDOWN )
            {
                //If enter was pressed
                if( event.key.keysym.sym == SDLK_RETURN )
                {
                    //if enter was pressed cap frame rate
                    !cap;
                }
                if( event.key.keysym.sym == SDLK_9 )
                {
                    if((Mix_PlayMusic( music, -1)==-1)
                       {return 1;} 
                }
            }    
            else if(event.type==SDL_QUIT)
            {
                go_on=false;
            }
         } 
         apply_surface(0,0,background,screen);
        apply_surface(180,140,player1,screen); 
   }                                                                   
   
   clean_up();
   
    
    
    return 0;
}


bool init()
{
   if( SDL_Init( SDL_INIT_EVERYTHING )==-1)
   {
       return false;
   } 
   
   screen= SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
    
   if(TTF_Init()==-1)
   {
      return false;
   }  
   else
   {
       //if ttf check is good initialize font format
       font=TTF_OpenFont( "abberancy.TTF", 50 );
       if(font==NULL)
          return false;  
   }
   //Initialize SDL_mixer 
   if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ) 
   { 
       return false; 
   } 
   else // if everything is fine load music
   {
       music=Mix_LoadMUS( "dev.wav" );  
   }        
   if(screen == NULL)
   {
             return false;
   } 
  
   //window caption
   SDL_WM_SetCaption("BattleField",NULL);

return true;

}


SDL_Surface *load_image(std::string filename)
{
     //temporary image loader
     SDL_Surface *loadedImage= NULL;
     
     //the optimized image tgat will be used
     SDL_Surface *optimizedImage= NULL;
     
     //Load Image 
     loadedImage= IMG_Load(filename.c_str());
     
     //if nothng is wrong with the file
     if(loadedImage!=NULL)
     {      
       //Create an optimized image
       optimizedImage= SDL_DisplayFormat(loadedImage);
       
       //Free the old Image
       SDL_FreeSurface(loadedImage);
     }
     
     //color key new optimized image
     if(optimizedImage!=NULL)
     {
        //clear black from behind image
        Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0, 0);
        SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );
     }
     //return new image
     return optimizedImage;
}    

void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface* destination)
{
     //Make a temporary rectangle to hold the offsets
     SDL_Rect offset;
     
     //Dive the offsets of the triangle
     offset.x=x;
     offset.y=y;
     
     //Blit surface
     SDL_BlitSurface(source, NULL, destination, &offset);
} 

bool clean_up()
{
     //Free all used surfaces memory
   
   SDL_FreeSurface(player1);
   SDL_FreeSurface(background);
   SDL_FreeSurface(message);
   //Free the sound effects 
   Mix_FreeMusic( music ); 
   TTF_CloseFont( font );
   SDL_Quit(); 
   TTF_Quit();
   
}


Furthermore this is from mostly from lazyfoo's tutorial.
"A pointerA and pointerB fell in love with the same adress"
Advertisement
I'm not going to dig through that right now, but some things you should try/consider:

- Instead of just returning on errors, use SDL_GetError for all relevant places where an SDL call is made. You may be able to track down your problem that way.

- Does your game just not play the music, or does it actually crash?

- Have you tried loading it as you would a normal sound file (Mix_LoadWAV / Mix_PlayChannel) instead of music?

- Are you sure the file is in the correct location? (hey, never know)

- Try initializing your sound device using AUDIO_S16 instead of MIX_DEFAULT_FORMAT, and see what happens (though the SDL_GetError should catch this for you).

This topic is closed to new replies.

Advertisement