Sign in to follow this  

Pausing My Program

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 surfaces
SDL_Surface *squirrel = NULL;
SDL_Surface *screen = NULL;

//The event structure
SDL_Event event;

//The areas of the sprite sheet
SDL_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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Here is Squirrel.h


#ifndef SQUIRREL_H
#define SQUIRREL_H

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"


//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The frames per second
const int FRAMES_PER_SECOND = 10;

//The dimenstions of the squirrel
const int SQUIRREL_WIDTH = 50;
const int SQUIRREL_HEIGHT = 50;

//The direction status of the squirrel
const int SQUIRREL_RIGHT = 0;
const int SQUIRREL_LEFT = 1;

//The number of squirrel frames
const int CLIPS = 4;

//The squirrel
class 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;
}
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 surfaces
SDL_Surface *squirrel = NULL;
SDL_Surface *screen = NULL;

//The event structure
SDL_Event event;

//The areas of the sprite sheet
SDL_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;
}
}

Share this post


Link to post
Share on other sites

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this