[SOLVED] SDL LazyFoo Moving an Image Help
Moderator: Coders of Rage
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
[SOLVED] SDL LazyFoo Moving an Image Help
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.
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.
Last edited by xx6heartless6xx on Wed Feb 16, 2011 3:07 am, edited 1 time in total.
-
- Chaos Rift Newbie
- Posts: 25
- Joined: Sat Apr 18, 2009 11:24 pm
- Favorite Gaming Platforms: SNES,PS1
Re: SDL LazyFoo Moving an Image Help
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:
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.
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 ) );
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
Re: SDL LazyFoo Moving an Image Help
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: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: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.Code: Select all
//Fill the screen white SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
Code: Select all
if( check_collision( box, wall) ) {
apply_surface( 200, 300, dot, screen );
}
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
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.xx6heartless6xx wrote: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: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: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.Code: Select all
//Fill the screen white SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
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.Code: Select all
if( check_collision( box, wall) ) { apply_surface( 200, 300, dot, screen ); }
How can I get rid of the trailing left behind as I move the image AND display an image after a collision?
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
Re: SDL LazyFoo Moving an Image Help
How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?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.
Re: SDL LazyFoo Moving an Image Help
Just use a boolean flag:xx6heartless6xx wrote:How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?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.
Code: Select all
bool col = false;
col = check_collision( box, wall);
if(col)
{
apply_surface(200, 200, dot, screen);
}
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: SDL LazyFoo Moving an Image Help
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.XianForce wrote:Just use a boolean flag:xx6heartless6xx wrote:How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?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.
Code: Select all
bool col = false; col = check_collision( box, wall); if(col) { apply_surface(200, 200, dot, screen); }
I'll make your software hardware.
Re: SDL LazyFoo Moving an Image Help
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 workthejahooli wrote: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.XianForce wrote:Just use a boolean flag:xx6heartless6xx wrote:How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?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.
Code: Select all
bool col = false; col = check_collision( box, wall); if(col) { apply_surface(200, 200, dot, screen); }
- thejahooli
- Chaos Rift Junior
- Posts: 265
- Joined: Fri Feb 20, 2009 7:45 pm
- Location: London, England
Re: SDL LazyFoo Moving an Image Help
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.XianForce wrote: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 workthejahooli wrote: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.XianForce wrote:Just use a boolean flag:xx6heartless6xx wrote:How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?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.
Code: Select all
bool col = false; col = check_collision( box, wall); if(col) { apply_surface(200, 200, dot, screen); }
I'll make your software hardware.
Re: SDL LazyFoo Moving an Image Help
Oh, wait, does check_collision RESOLVE the collision, then?thejahooli wrote: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.XianForce wrote: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 workthejahooli wrote: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.XianForce wrote:Just use a boolean flag:xx6heartless6xx wrote:How can I then keep the image up on the screen for the duration of the program while getting rid of the trail?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.
Code: Select all
bool col = false; col = check_collision( box, wall); if(col) { apply_surface(200, 200, dot, screen); }
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
Re: SDL LazyFoo Moving an Image Help
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
I need to see more of your code to help ...xx6heartless6xx wrote: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 itCode: Select all
bool col = false; col = check_collision( box, wall); if(col) { apply_surface(200, 200, dot, screen); }
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
Re: SDL LazyFoo Moving an Image Help
Here is my main game loop:
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
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;
}
}
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
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
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...);
then in the show function do this: if(collision) apply_surface(bleh...);
I remember when I used to be into nostalgia.