Thread: SDL run time error

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    SDL run time error

    Code:
    #include<string>
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "SDL/SDL_mixer.h"
    #define WIDTH  640
    #define HEIGHT  480  
    #define BPP 32
    
    
    SDL_Surface * Load_Image(std::string File)
    {
              SDL_Surface *screen;
              SDL_Surface * Optimal;  
                
                screen = IMG_Load(File.c_str());
                Optimal = SDL_DisplayFormat(screen);
                SDL_FreeSurface(screen);
                return(Optimal);
    }
    
    class player
    {
        Mix_Music *Song;
        
        bool Music_paused;
        bool Musicok;
        SDL_Event event;
        public:
         void  Musicplayer();
               player();
               ~player();
    };
    
    player::player()
    {
                 Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT ,2,4096);
                     
                     Music_paused= false;
                     Musicok= true;
                     Song = NULL;
    }
    
    player::~player()
    {
                     Mix_CloseAudio();
                     Musicok=true;
                     Music_paused=false;
                     Mix_FreeMusic(Song);
                     Song = NULL;
    }
    
    void player::Musicplayer()
    {
         int quit= 0;
         Uint8 *Keystate = SDL_GetKeyState(NULL);
         
         while(quit != 1)
         {
              while(SDL_PollEvent(&event))
              {
                switch(event.type)
                {
                  case SDL_KEYDOWN :
                       if(Keystate[SDLK_1])
                       {  if(Musicok == true)
                             Song = Mix_LoadMUS("TEST.mp3");
                         else 
                             {  Mix_FreeMusic(Song);
                                Song = Mix_LoadMUS("TEST.mp3");
                             }
                       Musicok = false;
                       }
                       if(Keystate[SDLK_2])
                       {  if(Musicok == true)
                             Song = Mix_LoadMUS("TEST2.mp3");
                         else
                             {  Mix_FreeMusic(Song);
                                Song = Mix_LoadMUS("TEST2.mp3");
                             }
                       Musicok = false;
                       }
                       if(Keystate[SDLK_SPACE])
                       {   if(Musicok == true)
                           Mix_PlayMusic(Song, -1);
                           else
                           Mix_RewindMusic();
                       }
                       if(Keystate[SDLK_RETURN])
                       {   if(Music_paused == false)
                          {Mix_PauseMusic();
                           Music_paused = true;}
                           else
                           Mix_ResumeMusic();
                       }
                       break;
                  case SDL_QUIT :
                       quit = 1;
                       break;
               }
         }
    }
    }
     void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
    {
        //Make a temporary rectangle to hold the offsets
        SDL_Rect offset;
        
        //Give the offsets to the rectangle
        offset.x = x;
        offset.y = y;
    //Blit the surface
        SDL_BlitSurface( source, NULL, destination, &offset );
    }
    
     
     
     
     int main( int argc, char* args[] )
     {
       SDL_Surface *screen;
       SDL_Surface *image;
       
       SDL_Init(SDL_INIT_EVERYTHING); 
         
       SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_HWSURFACE);
         
          
       image = Load_Image("sdl_test");
       
       apply_surface(0,0,image,screen);
       
       player Test;
       
       SDL_Event event;
       int i =1;
       
       while( i != 0)
       {
         while(SDL_PollEvent(&event))
         {
           Test.Musicplayer();
           switch(event.type)
           {
             case SDL_QUIT :
                  i = 0;
                  break;
           }
         
         }
       }
       
       SDL_FreeSurface(screen);
       SDL_FreeSurface(image);
       SDL_Quit();
       return 0;
    }
    well my program when run, blinks off and on

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Add SDL_DOUBLEBUF to SDL_SetVideoMode(), move the call to apply_surface() after the switch and then add a call to SDL_Flip(screen).

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    Code:
    #include<string>
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "SDL/SDL_mixer.h"
    #define WIDTH  640
    #define HEIGHT  480  
    #define BPP 32
    
    
    SDL_Surface * Load_Image(std::string File)
    {
              SDL_Surface *screen;
              SDL_Surface * Optimal;  
                
                screen = IMG_Load(File.c_str());
                Optimal = SDL_DisplayFormat(screen);
                SDL_FreeSurface(screen);
                return(Optimal);
    }
    
    class player
    {
        Mix_Music *Song;
        
        bool Music_paused;
        bool Musicok;
        SDL_Event event;
        public:
         void  Musicplayer();
               player();
               ~player();
    };
    
    player::player()
    {
                 Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT ,2,4096);
                     
                     Music_paused= false;
                     Musicok= true;
                     Song = NULL;
    }
    
    player::~player()
    {
                     Mix_CloseAudio();
                     Musicok=true;
                     Music_paused=false;
                     Mix_FreeMusic(Song);
                     Song = NULL;
    }
    
    void player::Musicplayer()
    {
         int quit= 0;
         Uint8 *Keystate = SDL_GetKeyState(NULL);
         
         while(quit != 1)
         {
              while(SDL_PollEvent(&event))
              {
                switch(event.type)
                {
                  case SDL_KEYDOWN :
                       if(Keystate[SDLK_1])
                       {  if(Musicok == true)
                             Song = Mix_LoadMUS("TEST.mp3");
                         else 
                             {  Mix_FreeMusic(Song);
                                Song = Mix_LoadMUS("TEST.mp3");
                             }
                       Musicok = false;
                       }
                       if(Keystate[SDLK_2])
                       {  if(Musicok == true)
                             Song = Mix_LoadMUS("TEST2.mp3");
                         else
                             {  Mix_FreeMusic(Song);
                                Song = Mix_LoadMUS("TEST2.mp3");
                             }
                       Musicok = false;
                       }
                       if(Keystate[SDLK_SPACE])
                       {   if(Musicok == true)
                           Mix_PlayMusic(Song, -1);
                           else
                           Mix_RewindMusic();
                       }
                       if(Keystate[SDLK_RETURN])
                       {   if(Music_paused == false)
                          {Mix_PauseMusic();
                           Music_paused = true;}
                           else
                           Mix_ResumeMusic();
                       }
                       break;
                  case SDL_QUIT :
                       quit = 1;
                       break;
               }
         }
    }
    }
     void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
    {
        //Make a temporary rectangle to hold the offsets
        SDL_Rect offset;
        
        //Give the offsets to the rectangle
        offset.x = x;
        offset.y = y;
    //Blit the surface
        SDL_BlitSurface( source, NULL, destination, &offset );
    }
    
     
     
     
     int main( int argc, char* args[] )
     {
       SDL_Surface *screen;
       SDL_Surface *image;
       
       SDL_Init(SDL_INIT_EVERYTHING); 
         
      screen = SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_DOUBLEBUF);
         
          
       image = Load_Image("sdl_test");
       
      
       
       player Test;
       
       SDL_Event event;
       int i =1;
       
       while( i != 0)
       {
         while(SDL_PollEvent(&event))
         {
           Test.Musicplayer();
           switch(event.type)
           {
             case SDL_QUIT :
                  i = 0;
                  break;
           }
          
         }
       }
       apply_surface(0,0,image,screen);
       
       SDL_Flip(screen);
       SDL_FreeSurface(screen);
       SDL_FreeSurface(image);
       SDL_Quit();
       return 0;
    }
    this is what i alternated soo far

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    You need to put the rendering code (apply_surface() and SDL_Flip() ) in the message loop (i.e. the outer while loop) ..

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    i know in the first loop (outer) but ssame result

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Are you sure your code isnt inside of Test.Musicplayer(), and therefore not rendering? Repost what has changed... (use a debugger to see if it ever passes Musicplayer() when you think it should be rendering...)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    Code:
    #include<string>
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "SDL/SDL_mixer.h"
    #define WIDTH  640
    #define HEIGHT  480  
    #define BPP 32
    
    SDL_Surface *screen;
    
    SDL_Surface *image;
    
    void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
    {
        //Make a temporary rectangle to hold the offsets
        SDL_Rect offset;
        
        //Give the offsets to the rectangle
        offset.x = x;
        offset.y = y;
    //Blit the surface
        SDL_BlitSurface( source, NULL, destination, &offset );
    }
    
    
    SDL_Surface * Load_Image(std::string File)
    {
              SDL_Surface *Picture;
              SDL_Surface * Optimal;  
                
                Picture = IMG_Load(File.c_str());
                Optimal = SDL_DisplayFormat(Picture);
                SDL_FreeSurface(Picture);
                return(Optimal);
    }
    
    class player
    {
        Mix_Music *Song;
        
        bool Music_paused;
        bool Musicok;
        SDL_Event event;
        public:
         void  Musicplayer();
               player();
               ~player();
    };
    
    player::player()
    {
                 Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT ,2,4096);
                     
                     Music_paused= false;
                     Musicok= true;
                     Song = NULL;
    }
    
    player::~player()
    {
                     Mix_CloseAudio();
                     Musicok=true;
                     Music_paused=false;
                     Mix_FreeMusic(Song);
                     Song = NULL;
    }
    
    void player::Musicplayer()
    {
         int quit= 0;
         Uint8 *Keystate = SDL_GetKeyState(NULL);
         
        
        
        apply_surface(0,0,image,screen);
        
        SDL_Flip( screen );
        
              
        
        
         while(quit != 1)
         {
              while(SDL_PollEvent(&event))
              {
                switch(event.type)
                {
                  case SDL_KEYDOWN :
                       if(Keystate[SDLK_1])
                       {  if(Musicok == true)
                             Song = Mix_LoadMUS("TEST.mp3");
                         else 
                             {  Mix_FreeMusic(Song);
                                Song = Mix_LoadMUS("TEST.mp3");
                             }
                       Musicok = false;
                       }
                       if(Keystate[SDLK_2])
                       {  if(Musicok == true)
                             Song = Mix_LoadMUS("TEST2.mp3");
                         else
                             {  Mix_FreeMusic(Song);
                                Song = Mix_LoadMUS("TEST2.mp3");
                             }
                       Musicok = false;
                       }
                       if(Keystate[SDLK_SPACE])
                       {   if(Musicok == true)
                           Mix_PlayMusic(Song, -1);
                           else
                           Mix_RewindMusic();
                       }
                       if(Keystate[SDLK_RETURN])
                       {   if(Music_paused == false)
                          {Mix_PauseMusic();
                           Music_paused = true;}
                           else
                           Mix_ResumeMusic();
                       }
                       break;
                  case SDL_QUIT :
                       quit = 1;
                       break;
               }
         }
    }
    }
     
    
     
     
     
     int main( int argc, char* args[] )
     {
       
       
       
       SDL_Init(SDL_INIT_EVERYTHING); 
         
      screen = SDL_SetVideoMode(WIDTH,HEIGHT,BPP,SDL_DOUBLEBUF);
         
          
       image = Load_Image("sdl_test");
       
      
       
       player Test;
       
       SDL_Event event;
       
       int i =1;
       
       
      Test.Musicplayer();
       
       while( i != 0)
       {
         while(SDL_PollEvent(&event))
         {
           
           
           if(event.type == SDL_QUIT)
               i=0;
          
         }
       
       
       }
       
       
      
    
       
       
       
    
       
       SDL_FreeSurface(screen);
       SDL_FreeSurface(image);
       SDL_Quit();
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    what did i do wrong XD

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    Re-read cactus_hugger's last post. The answer is just so obvious..

  10. #10
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    First off, try to indent your code a bit better. Spending an extra second a line will save you 5 seconds a line when you run into problems like this.

    Code:
    image = Load_Image("sdl_test");
    What is the name of your image file name? Probably that should be changed to "sdl_test.bmp", no?

    Also, attempting
    Code:
    SDL_FreeSurface(screen);
    . . . will crash some systems (Namely mine . . .)

    Code:
      while( i != 0)
       {
         while(SDL_PollEvent(&event))
         {
           
           
           if(event.type == SDL_QUIT)
               i=0;
          
         }
    What is the purpose of this loop?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    thanks and the other loop XD i put it their for main and yeaa stupid me for the image.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM