Page 1 of 2
[SOLVED] SDL LazyFoo Moving an Image Help
Posted: Wed Feb 02, 2011 9:47 pm
by xx6heartless6xx
I just finished LazyFoo's tutorial 16 on Motion.
In his tutorial each time you move something it continues to past it after each movement. I was wondering how you can move your sprite without it being continually copied. Basically, move the sprite and not showing where it was before the movement.
Re: SDL LazyFoo Moving an Image Help
Posted: Wed Feb 02, 2011 10:21 pm
by krilik
You need to clear the screen of any images that you have drawn by drawing something else over it.
In his tutorial he uses this line of code right before drawing to the screen to produce that effect:
Code: Select all
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
You should run that line of code before you redraw anything to the screen that changes. This will basically whiteout the screen of anything that you had on it previously, giving you a blank slate. You could get the same effect by drawing a background image, or another color.
Re: SDL LazyFoo Moving an Image Help
Posted: Wed Feb 02, 2011 10:40 pm
by xx6heartless6xx
krilik wrote:You need to clear the screen of any images that you have drawn by drawing something else over it.
In his tutorial he uses this line of code right before drawing to the screen to produce that effect:
Code: Select all
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
You should run that line of code before you redraw anything to the screen that changes. This will basically whiteout the screen of anything that you had on it previously, giving you a blank slate. You could get the same effect by drawing a background image, or another color.
I had this before but I removed because if I include this line then it does get rid of the moving the sprite without it leaving a trail BUT I cannot then add this:
Code: Select all
if( check_collision( box, wall) ) {
apply_surface( 200, 300, dot, screen );
}
Once a collision occurs, I want to paste another image on the screen or display anything but I think adding that line you said just clears it out.
How can I get rid of the trailing left behind as I move the image AND display an image after a collision?
Re: SDL LazyFoo Moving an Image Help
Posted: Thu Feb 03, 2011 12:20 am
by XianForce
xx6heartless6xx wrote:krilik wrote:You need to clear the screen of any images that you have drawn by drawing something else over it.
In his tutorial he uses this line of code right before drawing to the screen to produce that effect:
Code: Select all
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
You should run that line of code before you redraw anything to the screen that changes. This will basically whiteout the screen of anything that you had on it previously, giving you a blank slate. You could get the same effect by drawing a background image, or another color.
I had this before but I removed because if I include this line then it does get rid of the moving the sprite without it leaving a trail BUT I cannot then add this:
Code: Select all
if( check_collision( box, wall) ) {
apply_surface( 200, 300, dot, screen );
}
Once a collision occurs, I want to paste another image on the screen or display anything but I think adding that line you said just clears it out.
How can I get rid of the trailing left behind as I move the image AND display an image after a collision?
Well the issue with displaying an image after a collision (the way you're going about it anyways), is that it's only done for a single frame. So the image probably goes up for a fraction of a second, and then disappears.
Re: SDL LazyFoo Moving an Image Help
Posted: Thu Feb 03, 2011 12:33 am
by xx6heartless6xx
XianForce wrote:
Well the issue with displaying an image after a collision (the way you're going about it anyways), is that it's only done for a single frame. So the image probably goes up for a fraction of a second, and then disappears.
How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?
Re: SDL LazyFoo Moving an Image Help
Posted: Thu Feb 03, 2011 8:44 am
by XianForce
xx6heartless6xx wrote:XianForce wrote:
Well the issue with displaying an image after a collision (the way you're going about it anyways), is that it's only done for a single frame. So the image probably goes up for a fraction of a second, and then disappears.
How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?
Just use a boolean flag:
Code: Select all
bool col = false;
col = check_collision( box, wall);
if(col)
{
apply_surface(200, 200, dot, screen);
}
Re: SDL LazyFoo Moving an Image Help
Posted: Thu Feb 03, 2011 3:52 pm
by thejahooli
XianForce wrote:xx6heartless6xx wrote:XianForce wrote:
Well the issue with displaying an image after a collision (the way you're going about it anyways), is that it's only done for a single frame. So the image probably goes up for a fraction of a second, and then disappears.
How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?
Just use a boolean flag:
Code: Select all
bool col = false;
col = check_collision( box, wall);
if(col)
{
apply_surface(200, 200, dot, screen);
}
Wouldn't this have the same effect as his code already. It would work if you moved the 'bool col = false' to outside of the game loop.
Re: SDL LazyFoo Moving an Image Help
Posted: Thu Feb 03, 2011 4:19 pm
by XianForce
thejahooli wrote:XianForce wrote:xx6heartless6xx wrote:XianForce wrote:
Well the issue with displaying an image after a collision (the way you're going about it anyways), is that it's only done for a single frame. So the image probably goes up for a fraction of a second, and then disappears.
How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?
Just use a boolean flag:
Code: Select all
bool col = false;
col = check_collision( box, wall);
if(col)
{
apply_surface(200, 200, dot, screen);
}
Wouldn't this have the same effect as his code already. It would work if you moved the 'bool col = false' to outside of the game loop.
Well the bool col = false; should be outside of the game loop, yes. But since I reassign the value with col = check_collision(box, wall); it should still work
Re: SDL LazyFoo Moving an Image Help
Posted: Thu Feb 03, 2011 4:52 pm
by thejahooli
XianForce wrote:thejahooli wrote:XianForce wrote:xx6heartless6xx wrote:XianForce wrote:
Well the issue with displaying an image after a collision (the way you're going about it anyways), is that it's only done for a single frame. So the image probably goes up for a fraction of a second, and then disappears.
How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?
Just use a boolean flag:
Code: Select all
bool col = false;
col = check_collision( box, wall);
if(col)
{
apply_surface(200, 200, dot, screen);
}
Wouldn't this have the same effect as his code already. It would work if you moved the 'bool col = false' to outside of the game loop.
Well the bool col = false; should be outside of the game loop, yes. But since I reassign the value with col = check_collision(box, wall); it should still work
But that would still be the same as 'if(check_collision(box, wall))', as all you're doing is assigning the value to a variable before checking it.
Re: SDL LazyFoo Moving an Image Help
Posted: Thu Feb 03, 2011 5:17 pm
by XianForce
thejahooli wrote:XianForce wrote:thejahooli wrote:XianForce wrote:xx6heartless6xx wrote:XianForce wrote:
Well the issue with displaying an image after a collision (the way you're going about it anyways), is that it's only done for a single frame. So the image probably goes up for a fraction of a second, and then disappears.
How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?
Just use a boolean flag:
Code: Select all
bool col = false;
col = check_collision( box, wall);
if(col)
{
apply_surface(200, 200, dot, screen);
}
Wouldn't this have the same effect as his code already. It would work if you moved the 'bool col = false' to outside of the game loop.
Well the bool col = false; should be outside of the game loop, yes. But since I reassign the value with col = check_collision(box, wall); it should still work
But that would still be the same as 'if(check_collision(box, wall))', as all you're doing is assigning the value to a variable before checking it.
Oh, wait, does check_collision RESOLVE the collision, then?
Re: SDL LazyFoo Moving an Image Help
Posted: Fri Feb 04, 2011 1:00 am
by xx6heartless6xx
Code: Select all
bool col = false;
col = check_collision( box, wall);
if(col)
{
apply_surface(200, 200, dot, screen);
}
Yes this is the same as what I had earlier and it still does not work. Can anyone out there help me? I still cant get it
Re: SDL LazyFoo Moving an Image Help
Posted: Fri Feb 04, 2011 12:59 pm
by XianForce
xx6heartless6xx wrote:Code: Select all
bool col = false;
col = check_collision( box, wall);
if(col)
{
apply_surface(200, 200, dot, screen);
}
Yes this is the same as what I had earlier and it still does not work. Can anyone out there help me? I still cant get it
I need to see more of your code to help ...
Re: SDL LazyFoo Moving an Image Help
Posted: Fri Feb 04, 2011 11:56 pm
by xx6heartless6xx
Here is my main game loop:
Code: Select all
while( quit == false )
{
//Start the frame timer
fps.start();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the Mover
myMover.handle_input();
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Move the Mover
myMover.move();
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0, 0, 0 ) );
//Show the wall
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 0x77, 0x77, 0x77 ) );
//Show the Mover on the screen
myMover.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
And here is the move() where I am trying to paste an image after a collision but keeps getting wiped out after the screen clears after each move:
Code: Select all
void Mover::move()
{
//Move the Mover left or right
box.x += xVel;
if( check_collision( box, wall) ) {
apply_surface( 200, 300, dot, screen );
}
//If the Mover went too far to the left or right or has collided with the wall
if( ( box.x < 0 ) || ( box.x + MOVER_WIDTH > SCREEN_WIDTH ) || ( check_collision( box, wall ) ) )
{
//Move back
box.x -= xVel;
}
//Move the Mover up or down
box.y += yVel;
//If the Mover went too far up or down or has collided with the wall
if( ( box.y < 0 ) || ( box.y + MOVER_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( box, wall ) ) )
{
//Move back
box.y -= yVel;
}
}
Re: SDL LazyFoo Moving an Image Help
Posted: Sat Feb 05, 2011 1:35 am
by XianForce
Well the first suggestion I have is to decouple your updating and rendering code. You're applying the surface in a method that's meant to perform certain computations (aka update), and that apply_surface call probably shouldn't go there...
Re: SDL LazyFoo Moving an Image Help
Posted: Sat Feb 05, 2011 1:46 pm
by D-e-X
I would give the class a bool collision; in the move function you should do this instead: collision = check_collision(blah...);
then in the show function do this: if(collision) apply_surface(bleh...);