Pausing My Program

Started by
6 comments, last by 13moonsago 16 years, 1 month ago
I use Visual Studio C++ Express and SDL. I am making a simple program that makes a sprite walk when the user presses the left or right keys, but when I run the .exe it just flashes up and doesn't wait for me to quit before closing. I have a loop that is not supposed to close the program until the user presses the backspace key, but it isn't working. Also I tried system("pause"); and cin.get(); and neither are working.
A Pirates Life for Me!
Advertisement
Post your code.

system("pause") will probably fail because the process has no standard input stream, as will std::cin.get().
Here is the main file of my program. The timer and squirrel headers don't really matter they are just the class definitions for the sprite and the timer.
#include "SDL/SDL.h"#include "SDL/SDL_image.h"#include "Timer.h"#include "Squirrel.h"#include <string>using namespace std;//The surfacesSDL_Surface *squirrel = NULL;SDL_Surface *screen = NULL;//The event structureSDL_Event event;//The areas of the sprite sheetSDL_Rect clipsNoKnifeRight[CLIPS];SDL_Rect clipsNoKnifeLeft[CLIPS];SDL_Rect clipsKnifeRight[CLIPS];SDL_Rect clipsKnifeLeft[CLIPS];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, 0));        }    }        //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 );}void set_clips(){    //Clip the sprites    for(int count = 0; count < 4; count++)    {	clipsNoKnifeRight[count].x = (SQUIRREL_WIDTH * count);	clipsNoKnifeRight[count].y = 0;	clipsNoKnifeRight[count].w = SQUIRREL_WIDTH;	clipsNoKnifeRight[count].h = SQUIRREL_HEIGHT;    }    for(int count = 4; count < 8; count++)    {	clipsNoKnifeLeft[(count - 4)].x = (SQUIRREL_WIDTH * count);	clipsNoKnifeLeft[(count - 4)].y = 0;	clipsNoKnifeLeft[(count - 4)].w = SQUIRREL_WIDTH;	clipsNoKnifeLeft[(count - 4)].h = SQUIRREL_HEIGHT;    }        for(int count = 0; count < 4; count++)    {  	clipsKnifeRight[count].x = (SQUIRREL_WIDTH * count);	clipsKnifeRight[count].y = SQUIRREL_HEIGHT;	clipsKnifeRight[count].w = SQUIRREL_WIDTH;   	clipsKnifeRight[count].h = SQUIRREL_HEIGHT;    }    for(int count = 4; count < 8; count++)    {  	clipsKnifeLeft[(count - 4)].x = (SQUIRREL_WIDTH * count);	clipsKnifeLeft[(count - 4)].y = SQUIRREL_HEIGHT;	clipsKnifeLeft[(count - 4)].w = SQUIRREL_WIDTH;   	clipsKnifeLeft[(count - 4)].h = SQUIRREL_HEIGHT;    }}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( "Make The Squirrel Walk", NULL );        //If everything initialized fine    return true;}bool load_files(){    //Load the sprite sheet    squirrel = load_image( "squirrelSpriteSheet.png" );        //If there was a problem in loading the sprite    if( squirrel == NULL )    {        return false;        }        //If everything loaded fine    return true;}void clean_up(){    //Free the surface    SDL_FreeSurface( squirrel );        //Quit SDL    SDL_Quit();}int main( int argc, char* args[] ){	bool quit = false;    //Initialize    if( init() == false )    {        return 1;    }        //Load the files    if( load_files() == false )    {        return 1;    }        //Clip the sprite sheet    set_clips();    //The frame rate regulator    Timer fps;        //The squirrel    Squirrel walk;        //Start the frame timer    fps.start();    	while(!quit)	{		if( event.type == SDL_KEYDOWN )		{        //Set quit to true if backspace is pressed        switch( event.key.keysym.sym )        {            case SDLK_BACKSPACE: quit = true;				break;        }		}    //While there's events to handle    while( SDL_PollEvent(&event))    {        //Handle events for the squirrel        walk.handle_events();	}            //Move the squirrel    walk.move();            //Fill the screen white    SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));            //Show the squirrel on the screen    walk.show(clipsNoKnifeRight, clipsNoKnifeLeft, clipsKnifeRight, clipsKnifeLeft);            //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;    }


[Edited by - 13moonsago on March 7, 2008 9:22:46 AM]
A Pirates Life for Me!
Hello,

You're checking the keydown event before the polling.

kind regards
Uncle
Also, can you post Squirrel::handle_events()?

You probably should have code like this:
class Squirrel {public:    void handle_event( const SDL_Event &event )    {        // do whatever    }};int main(){    Squirrel squirrel;    SDL_Event event;    while(running)    {        while(SDL_PollEvent(&event))        {            if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_BACKSPACE )            {                running = false;            }            else            {                squirrel.handle_event(event);            }        }        // ...    }}


Otherwise, how does the squirrel know which event to handle?
Here is Squirrel.h

#ifndef SQUIRREL_H#define SQUIRREL_H#include "SDL/SDL.h"#include "SDL/SDL_image.h"//Screen attributesconst int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;//The frames per secondconst int FRAMES_PER_SECOND = 10;//The dimenstions of the squirrelconst int SQUIRREL_WIDTH = 50;const int SQUIRREL_HEIGHT = 50;//The direction status of the squirrelconst int SQUIRREL_RIGHT = 0;const int SQUIRREL_LEFT = 1;//The number of squirrel framesconst int CLIPS = 4;//The squirrelclass Squirrel{    private:    //The offset    int offSet;        //Its rate of movement    int velocity;        //Its current frame    int frame;        //Its animation status    int status;    public:    //Construstor    Squirrel();        //Handles input    void handle_events();        //Moves the squirrel    void move();            //Shows the squirrel    void show(SDL_Rect [CLIPS], SDL_Rect [CLIPS], SDL_Rect [CLIPS], SDL_Rect [CLIPS]);    };#endif


and here is Squirrel.cpp

#include "Squirrel.h"#include "SDL/SDL.h"#include "SDL/SDL_image.h"#include <string>using namespace std;Squirrel::Squirrel(){    //Initialize movement variables    offSet = 0;    velocity = 0;        //Initialize animation variables    frame = 0;    status = SQUIRREL_RIGHT;}void Squirrel::move(){    //Move    offSet += velocity;        //Keep the squirrel in bounds    if( ( offSet < 0 ) || ( offSet + SQUIRREL_WIDTH > SCREEN_WIDTH ) )    {        offSet -= velocity;        }}void Squirrel::show(SDL_Rect (clipsNoKnifeRight)[CLIPS], SDL_Rect (clipsNoKnifeLeft)[CLIPS], SDL_Rect (clipsKnifeRight)[CLIPS], SDL_Rect (clipsKnifeLeft)[CLIPS]){    //If Squirrel is moving left    if( velocity < 0 )    {        //Set the animation to left        status = SQUIRREL_LEFT;                //Move to the next frame in the animation	frame++;     }    //If Squirrel is moving right    else if( velocity > 0 )    {        //Set the animation to right        status = SQUIRREL_RIGHT;                //Move to the next frame in the animation        frame++;    }    //If Squirrel standing    else    {        //Restart the animation        frame = 0;        }        //Loop the animation    if( frame >= CLIPS )    {        frame = 0;    }        //Show the squirrel    if( status == SQUIRREL_RIGHT )    {        apply_surface( offSet, SCREEN_HEIGHT - SQUIRREL_HEIGHT, squirrel, screen, &clipsNoKnifeRight[frame] );    }    else if( status == SQUIRREL_LEFT )    {        apply_surface( offSet, SCREEN_HEIGHT - SQUIRREL_HEIGHT, squirrel, screen, &clipsNoKnifeLeft[frame] );    }}void Squirrel::handle_events(){    //If a key was pressed    if( event.type == SDL_KEYDOWN )    {        //Set the velocity        switch( event.key.keysym.sym )        {            case SDLK_RIGHT: velocity += SQUIRREL_WIDTH / 4;				break;            case SDLK_LEFT: velocity -= SQUIRREL_WIDTH / 4;				break;        }    }    //If a key was released    else if( event.type == SDL_KEYUP )    {        //Set the velocity        switch( event.key.keysym.sym )        {            case SDLK_RIGHT: velocity -= SQUIRREL_WIDTH / 4; break;            case SDLK_LEFT: velocity += SQUIRREL_WIDTH / 4; break;        }    }}
A Pirates Life for Me!
I can't see how that compiles. There appears to be no declaration of the "event" variable in Squirrel::handle_events.

My advice would be to pass the event as an argument to the handle_event, like I showed in the above post.
I used the old version of the source code before I moved the definition of Squirrel::handle_events() and Squirrel::show() into main.cpp.
this is main.cpp
#include "SDL/SDL.h"#include "SDL/SDL_image.h"#include "Timer.h"#include "Squirrel.h"#include <string>using namespace std;//The surfacesSDL_Surface *squirrel = NULL;SDL_Surface *screen = NULL;//The event structureSDL_Event event;//The areas of the sprite sheetSDL_Rect clipsNoKnifeRight[CLIPS];SDL_Rect clipsNoKnifeLeft[CLIPS];SDL_Rect clipsKnifeRight[CLIPS];SDL_Rect clipsKnifeLeft[CLIPS];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, 0));        }    }        //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 );}void set_clips(){    //Clip the sprites    for(int count = 0; count < 4; count++)    {	clipsNoKnifeRight[count].x = (SQUIRREL_WIDTH * count);	clipsNoKnifeRight[count].y = 0;	clipsNoKnifeRight[count].w = SQUIRREL_WIDTH;	clipsNoKnifeRight[count].h = SQUIRREL_HEIGHT;    }    for(int count = 4; count < 8; count++)    {	clipsNoKnifeLeft[(count - 4)].x = (SQUIRREL_WIDTH * count);	clipsNoKnifeLeft[(count - 4)].y = 0;	clipsNoKnifeLeft[(count - 4)].w = SQUIRREL_WIDTH;	clipsNoKnifeLeft[(count - 4)].h = SQUIRREL_HEIGHT;    }        for(int count = 0; count < 4; count++)    {  	clipsKnifeRight[count].x = (SQUIRREL_WIDTH * count);	clipsKnifeRight[count].y = SQUIRREL_HEIGHT;	clipsKnifeRight[count].w = SQUIRREL_WIDTH;   	clipsKnifeRight[count].h = SQUIRREL_HEIGHT;    }    for(int count = 4; count < 8; count++)    {  	clipsKnifeLeft[(count - 4)].x = (SQUIRREL_WIDTH * count);	clipsKnifeLeft[(count - 4)].y = SQUIRREL_HEIGHT;	clipsKnifeLeft[(count - 4)].w = SQUIRREL_WIDTH;   	clipsKnifeLeft[(count - 4)].h = SQUIRREL_HEIGHT;    }}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( "Make The Squirrel Walk", NULL );        //If everything initialized fine    return true;}bool load_files(){    //Load the sprite sheet    squirrel = load_image( "squirrelSpriteSheet.png" );        //If there was a problem in loading the sprite    if( squirrel == NULL )    {        return false;        }        //If everything loaded fine    return true;}void clean_up(){    //Free the surface    SDL_FreeSurface( squirrel );        //Quit SDL    SDL_Quit();}int main( int argc, char* args[] ){	bool quit = false;    //Initialize    if( init() == false )    {        return 1;    }        //Load the files    if( load_files() == false )    {        return 1;    }        //Clip the sprite sheet    set_clips();    //The frame rate regulator    Timer fps;        //The squirrel    Squirrel walk;        //Start the frame timer    fps.start();    	while(!quit)	{		if( event.type == SDL_KEYDOWN )		{        //Set the velocity        switch( event.key.keysym.sym )        {            case SDLK_BACKSPACE: quit = true;				break;        }		}    //While there's events to handle    while( SDL_PollEvent(&event))    {        //Handle events for the squirrel        walk.handle_events();	}            //Move the squirrel    walk.move();            //Fill the screen white    SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));            //Show the squirrel on the screen    walk.show(clipsNoKnifeRight, clipsNoKnifeLeft, clipsKnifeRight, clipsKnifeLeft);            //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;    }void Squirrel::show(SDL_Rect (clipsNoKnifeRight)[CLIPS], SDL_Rect (clipsNoKnifeLeft)[CLIPS], SDL_Rect (clipsKnifeRight)[CLIPS], SDL_Rect (clipsKnifeLeft)[CLIPS]){    //If Squirrel is moving left    if( velocity < 0 )    {        //Set the animation to left        status = SQUIRREL_LEFT;                //Move to the next frame in the animation	frame++;     }    //If Squirrel is moving right    else if( velocity > 0 )    {        //Set the animation to right        status = SQUIRREL_RIGHT;                //Move to the next frame in the animation        frame++;    }    //If Squirrel standing    else    {        //Restart the animation        frame = 0;        }        //Loop the animation    if( frame >= CLIPS )    {        frame = 0;    }        //Show the squirrel    if( status == SQUIRREL_RIGHT )    {        apply_surface( offSet, SCREEN_HEIGHT - SQUIRREL_HEIGHT, squirrel, screen, &clipsNoKnifeRight[frame] );    }    else if( status == SQUIRREL_LEFT )    {        apply_surface( offSet, SCREEN_HEIGHT - SQUIRREL_HEIGHT, squirrel, screen, &clipsNoKnifeLeft[frame] );    }}void Squirrel::handle_events(){    //If a key was pressed    if( event.type == SDL_KEYDOWN )    {        //Set the velocity        switch( event.key.keysym.sym )        {            case SDLK_RIGHT: velocity += SQUIRREL_WIDTH / 4;				break;            case SDLK_LEFT: velocity -= SQUIRREL_WIDTH / 4;				break;        }    }    //If a key was released    else if( event.type == SDL_KEYUP )    {        //Set the velocity        switch( event.key.keysym.sym )        {            case SDLK_RIGHT: velocity -= SQUIRREL_WIDTH / 4; break;            case SDLK_LEFT: velocity += SQUIRREL_WIDTH / 4; break;        }    }}

and here the the new Squirrel.cpp
#include "Squirrel.h"#include "SDL/SDL.h"#include "SDL/SDL_image.h"#include <string>using namespace std;Squirrel::Squirrel(){    //Initialize movement variables    offSet = 0;    velocity = 0;        //Initialize animation variables    frame = 0;    status = SQUIRREL_RIGHT;}void Squirrel::move(){    //Move    offSet += velocity;        //Keep the squirrel in bounds    if( ( offSet < 0 ) || ( offSet + SQUIRREL_WIDTH > SCREEN_WIDTH ) )    {        offSet -= velocity;        }}
A Pirates Life for Me!

This topic is closed to new replies.

Advertisement