[SOLVED] SDL LazyFoo Moving an Image Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

[SOLVED] SDL LazyFoo Moving an Image Help

Post 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.
Last edited by xx6heartless6xx on Wed Feb 16, 2011 3:07 am, edited 1 time in total.
krilik
Chaos Rift Newbie
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

Post 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.
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: SDL LazyFoo Moving an Image Help

Post 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?
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SDL LazyFoo Moving an Image Help

Post 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.
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: SDL LazyFoo Moving an Image Help

Post 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?
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SDL LazyFoo Moving an Image Help

Post 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);
}
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: SDL LazyFoo Moving an Image Help

Post 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.
I'll make your software hardware.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SDL LazyFoo Moving an Image Help

Post 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 ;)
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: SDL LazyFoo Moving an Image Help

Post 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.
I'll make your software hardware.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SDL LazyFoo Moving an Image Help

Post 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?
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: SDL LazyFoo Moving an Image Help

Post 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
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SDL LazyFoo Moving an Image Help

Post 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 ...
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: SDL LazyFoo Moving an Image Help

Post 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;
    }
}
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SDL LazyFoo Moving an Image Help

Post 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...
D-e-X
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 28, 2010 6:49 pm

Re: SDL LazyFoo Moving an Image Help

Post 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...);
I remember when I used to be into nostalgia.
Post Reply