   Motion and image blitting! [SDL and C++]
  I set up SDL yesterday so I could start working with graphics and sound etc. I have been reading the lazyfoo tutorials (http://lazyfoo.net/SDL_tutorials/index.php) and I was on tutorial 16 which is about motion (http://lazyfoo.net/SDL_tutorials/lesson16/index.php).Basically I copied all the code from that tutorial into a new VC++ project. In the code it has a black dot moving on a white screen. I wanted two differences:1. The white screen drawn would be replaced by a background bitmap I made.2. The dot would be replaced by my character, which has four faces: front, left, right and back. The faces were to change depending on which direction the character was moving.So I attempted to alter the code so that my changes would be implemented. After fixing all the compiling errors, of which there were several, the program ran but nothing appeared on the screen, not even the window which was to hold the game.I think the error lies somewhere in my attempt to change the face. I made a global int called state which holds the state, 1 being front, 2 being left, 3 being right and 4 being back. In the handle_input() function - line 202 - I made it so that as well as moving the character in it's designated direction, it also changes the state to the correct face. Then in my altered show() function - line 261 - I take the state as a parameter and show the correct face. The show function is called in main, clearly using state as it's argument.So, I am at a loss as to why my game is not working. Here is the entire code so you can see what I have done. All the code is meant to do is move the character and change it's face according to the arrow keys. It is meant to be over a bitmap background.Also, I didn't touch the timer class or anything to do with the timer, so assuming that works, don't bother reading over that./*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may notbe redestributed without written permission.*///The headers#include "SDL.h"#include "SDL_image.h"#include <string>//The screen attributesconst int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;//The frame rateconst int FRAMES_PER_SECOND = 20;//The dimensions of the characterconst int CHARACTER_WIDTH = 40;const int CHARACTER_HEIGHT = 40;int state;//The surfacesSDL_Surface *character_front = NULL;SDL_Surface *character_left = NULL;SDL_Surface *character_right = NULL;SDL_Surface *character_back = NULL;SDL_Surface *background = NULL;SDL_Surface *screen = NULL;//The event structureSDL_Event event;//The character that will move around on the screenclass Character{    private:    //The X and Y offsets of the dot    int x, y;    //The velocity of the dot    int xVel, yVel;    public:    //Initializes the variables    Character();    //Takes key presses and adjusts the dot's velocity    void handle_input();    //Moves the dot    void move();    //Shows the dot on the screen    void show(int s);};//The timerclass Timer{    private:    //The clock time when the timer started    int startTicks;    //The ticks stored when the timer was paused    int pausedTicks;    //The timer status    bool paused;    bool started;    public:    //Initializes variables    Timer();    //The various clock actions    void start();    void stop();    void pause();    void unpause();    //Gets the timer's time    int get_ticks();    //Checks the status of the timer    bool is_started();    bool is_paused();};SDL_Surface *load_image( std::string filename ){    //The image that's loaded    SDL_Surface* loadedImage = NULL;    //The optimized surface that will be used    SDL_Surface* optimizedImage = NULL;    //Load the image    loadedImage = IMG_Load( filename.c_str() );    //If the image loaded    if( loadedImage != NULL )    {   //Create an optimized surface   optimizedImage = SDL_DisplayFormat( loadedImage );   //Free the old surface   SDL_FreeSurface( loadedImage );   //If the surface was optimized   if( optimizedImage != NULL )   {  //Color key surface  SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );   }    }    //Return the optimized surface    return optimizedImage;}void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ){    //Holds offsets    SDL_Rect offset;    //Get offsets    offset.x = x;    offset.y = y;    //Blit    SDL_BlitSurface( source, clip, destination, &offset );}bool init(){    //Initialize all SDL subsystems    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )    {   return false;    }    //Set up the screen    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );    //If there was an error in setting up the screen    if( screen == NULL )    {   return false;    }    //Set the window caption    SDL_WM_SetCaption( "Game", NULL );    //If everything initialized fine    return true;}bool load_files(){    //Load the dot image    character_front = load_image( "character_front.bmp" );character_left = load_image( "character_left.bmp" );character_right = load_image( "character_right.bmp" );character_back = load_image( "character_back.bmp" );background = load_image( "background.bmp" );    //If there was a problem in loading the dot    if( character_front == NULL || character_left == NULL || character_right == NULL || character_back == NULL || background == NULL )    {   return false;    }    //If everything loaded fine    return true;}void clean_up(){    //Free the surface    SDL_FreeSurface( character_front );SDL_FreeSurface( character_left );SDL_FreeSurface( character_right );SDL_FreeSurface( character_back );SDL_FreeSurface( background );    //Quit SDL    SDL_Quit();}Character::Character(){    //Initialize the offsets    x = 0;    y = 0;    //Initialize the velocity    xVel = 0;    yVel = 0;}void Character::handle_input(){    //If a key was pressed    if( event.type == SDL_KEYDOWN )    {   //Adjust the velocity   switch( event.key.keysym.sym )   {  case SDLK_UP: yVel -= CHARACTER_HEIGHT / 2; state = 4; break;  case SDLK_DOWN: yVel += CHARACTER_HEIGHT / 2;state = 1; break;  case SDLK_LEFT: xVel -= CHARACTER_WIDTH / 2; state = 2; break;  case SDLK_RIGHT: xVel += CHARACTER_WIDTH / 2;state = 3; break;   }    }    //If a key was released    else if( event.type == SDL_KEYUP )    {   //Adjust the velocity   switch( event.key.keysym.sym )   {  case SDLK_UP: yVel += CHARACTER_HEIGHT / 2;state = 4; break;  case SDLK_DOWN: yVel -= CHARACTER_HEIGHT / 2;state = 1; break;  case SDLK_LEFT: xVel += CHARACTER_WIDTH / 2;state = 2; break;  case SDLK_RIGHT: xVel -= CHARACTER_WIDTH / 2;state = 3; break;   }    }}void Character::move(){    //Move the character left or right    x += xVel;    //If the character went too far to the left or right    if( ( x < 0 ) || ( x + CHARACTER_WIDTH > SCREEN_WIDTH ) )    {   //move back   x -= xVel;    }    //Move the character up or down    y += yVel;    //If the character went too far up or down    if( ( y < 0 ) || ( y + CHARACTER_HEIGHT > SCREEN_HEIGHT ) )    {   //move back   y -= yVel;    }}void Character::show(int s){switch(s) {case 1://Show the character    apply_surface( x, y, character_front, screen ); break;case 2://Show the character    apply_surface( x, y, character_left, screen ); break;case 3://Show the character    apply_surface( x, y, character_right, screen ); break;case 4://Show the character    apply_surface( x, y, character_back, screen ); break;}}Timer::Timer(){    //Initialize the variables    startTicks = 0;    pausedTicks = 0;    paused = false;    started = false;}void Timer::start(){    //Start the timer    started = true;    //Unpause the timer    paused = false;    //Get the current clock time    startTicks = SDL_GetTicks();}void Timer::stop(){    //Stop the timer    started = false;    //Unpause the timer    paused = false;}void Timer::pause(){    //If the timer is running and isn't already paused    if( ( started == true ) && ( paused == false ) )    {   //Pause the timer   paused = true;   //Calculate the paused ticks   pausedTicks = SDL_GetTicks() - startTicks;    }}void Timer::unpause(){    //If the timer is paused    if( paused == true )    {   //Unpause the timer   paused = false;   //Reset the starting ticks   startTicks = SDL_GetTicks() - pausedTicks;   //Reset the paused ticks   pausedTicks = 0;    }}int Timer::get_ticks(){    //If the timer is running    if( started == true )    {   //If the timer is paused   if( paused == true )   {  //Return the number of ticks when the timer was paused  return pausedTicks;   }   else   {  //Return the current time minus the start time  return SDL_GetTicks() - startTicks;   }    }    //If the timer isn't running    return 0;}bool Timer::is_started(){    return started;}bool Timer::is_paused(){    return paused;}int main( int argc, char* args[] ){    //Quit flag    bool quit = false;    //The dot that will be used    Character myCharacter;    //The frame rate regulator    Timer fps;    //Initialize    if( init() == false )    {   return 1;    }    //Load the files    if( load_files() == false )    {   return 1;    }    //While the user hasn't quit    while( quit == false )    {   //Start the frame timer   fps.start();   //While there's events to handle   while( SDL_PollEvent( &event ) )   {  //Handle events for the dot  myCharacter.handle_input();  //If the user has Xed out the window  if( event.type == SDL_QUIT )  { //Quit the program quit = true;  }   }   //Move the character   myCharacter.move();   //Apply the message to the screen   apply_surface( 0, 0, background, screen );   //Show the dot on the screen   myCharacter.show(state);   //Update the screen   if( SDL_Flip( screen ) == -1 )   {  return 1;   }   //Cap the frame rate   if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )   {  SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );   }    }    //Clean up    clean_up();    return 0;}Thank you for your time!   
  First - in the future I would break up the code using [ source ] [ /source ] tags instead of  tags when posting a large amount of code as it makes it a bit easier to read the post.<br><br>Second - have you tried placing a break point in an area and stepping through your code to narrow down where the error is coming from?<br><br>The Visual Studio debugger is &#111;ne of your best friends for these types of problems. Just left click in the margin space to the left of a line of code to insert or remove a break point and click the start debugging arrow to run the code. The code will attempt to run until it reaches a break point. &#79;nce it reaches the break point code execution will pause waiting for you to "step in", "step over" or "step out" of a code block. Each time you perform &#111;ne of these actions you will see the values of the variables in your current scope in the watch window (usually at the bottom left of the code window depending &#111;n how your IDE is configured).<br><br>Stepping through the code this way usually results in a palm to the forehead and a good old fashioned "Doh!" as you realize the simple logic error. If not it will always help to narrow down where you think the error is happening before posting to these forums. A single function posting will almost always get more results &#111;n the forums than dumping all of your code for others to debug for you.<br><br>;  I appreciate your post very much. Thank you! I will be sure to try to use the debugger in the future, and I didn't know about the source tags either, thanks!   