Sign in to follow this  
stannisbaratheon

v. simple SDL question

Recommended Posts

Hi everyone,

I started learning SDL a few days ago. I'm currently at the stage of loading pictures and doing basic event handling.

At the moment I have a program which allows you to move an object(surface) by releasing left mouse button. Unfortunately the original remains. For this I'm using SDL_blitsurface.

How can I either remove the original object or is there a better function to use?

Thanks!

Share this post


Link to post
Share on other sites
In SDL, and most other APIs, you can't "erase" something that has already been drawn. You just draw over what you no longer want.
So, at the beginning of each frame, draw over the entire screen with a solid color or your background to "erase" everything, then re-draw everything. This is how 99% of games works. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Your draw loop should look like this [i]every frame[/i]:[list=1]
[*]Draw over the entire screen with your background
[*]Draw your objects over the background, with the ones that you want to be shown "over" everything drawn last.
[*]Flip the SDL buffers.
[*]Go back to step 1, for the next frame.
[/list]

I explain it a bit more in-depth in a [url="http://www.gamedev.net/topic/619314-how-to-remove-the-left-overs-of-sdl-fillrect/page__p__4907146#entry4907146"]different thread[/url]. Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1343493858' post='4964011']
In SDL, and most other APIs, you can't "erase" something that has already been drawn. You just draw over what you no longer want.
So, at the beginning of each frame, draw over the entire screen with a solid color or your background to "erase" everything, then re-draw everything. This is how 99% of games works. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Your draw loop should look like this [i]every frame[/i]:[list=1]
[*]Draw over the entire screen with your background
[*]Draw your objects over the background, with the ones that you want to be shown "over" everything drawn last.
[*]Flip the SDL buffers.
[*]Go back to step 1, for the next frame.
[/list]

I explain it a bit more in-depth in a [url="http://www.gamedev.net/topic/619314-how-to-remove-the-left-overs-of-sdl-fillrect/page__p__4907146#entry4907146"]different thread[/url].
[/quote]

Ah, thank you!

EDIT: It worked! Edited by stannisbaratheon

Share this post


Link to post
Share on other sites
So it works but there seems to be lag:

[CODE]
while (quit==false)
{
if (SDL_PollEvent(&event))
{
if (event.button.x>dim.x &&event.button.x<dim.x+ring5->w && event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT && event.button.y >dim.y && event.button.y<dim.y+ring5->h )
{
while(dropped ==false)
{
SDL_PollEvent(&event);
if (event.type == SDL_MOUSEBUTTONUP)
{
dim.x = event.button.x;
dim.y = event.button.y;
SDL_BlitSurface (image,NULL,screen,NULL);
SDL_BlitSurface (ring5,NULL,screen,&dim);

SDL_Flip(screen);
SDL_Delay (2000);
break;
}
}
}
}
}
[/CODE]

Share this post


Link to post
Share on other sites
This is because you are drawing inside your event loop. Your event loop should only handle events. Your update loop should only update. And your drawing loop should only draw.

Your entire main loop should go:[list=1]
[*]Get and handle events from the user
[*]Update the game based off of the time that past since the last frame.
[*]Draw everything and flip the screen.
[*]Go back to step 1
[/list]
(Separate them into different functions or subsystems - don't just have a huge giant function that contains everything)

Which means, your loop more detailed should go:[list=1]
[*]Get and handle events from the user
[*]Update the game based off of the time that past since the last frame.
[*]Draw everything:
[list=1]
[*]Draw over the entire screen with your background
[*]Draw your objects over the background, with the ones that you want to be shown "over" everything drawn last.
[*]Flip the SDL buffers.
[/list] [*]Go back to step 1
[/list]

Also, you are calling SDL_PollEvent() twice. Only call it once, and put it in a while() loop, not an if() statement, like this:
[CODE]while(SDL_PollEvent(&event))
{
//Handle every event.
}
[/CODE]

Otherwise you only handle one event per frame, which means events can build up and build up if you get two or more events a frame, and then you get a traffic-jam of events, and when the user clicks the mouse, it may be a dozen or more frames before the event is handled because their might be other events in the line waiting.
Handle every event that shows up, whenever they are triggered, then move on to the updating and drawing. Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1343495544' post='4964018']
This is because you are drawing inside your event loop. Your event loop should only handle events. Your update loop should only update. And your drawing loop should only draw.

Your entire main loop should go:[list=1]
[*]Get and handle events from the user
[*]Update the game based off of the time that past since the last frame.
[*]Draw everything and flip the screen.
[*]Go back to step 1
[/list]
Which means, your loop more detailed should go:[list=1]
[*]Get and handle events from the user
[*]Update the game based off of the time that past since the last frame.
[*]Draw everything:
[list=1]
[*]Draw over the entire screen with your background
[*]Draw your objects over the background, with the ones that you want to be shown "over" everything drawn last.
[*]Flip the SDL buffers.
[/list] [*]Go back to step 1
[/list]
[/quote]

I feel like such a n00b.

Share this post


Link to post
Share on other sites
Anything we attempt to learn, we start off as a noob in. Better to feel like a noob, and grow into a master, then to feel like a master but never grow into one.

There is nothing wrong with not knowing something, and nothing wrong with having difficulty in learning - the only thing wrong is giving up before you understand. Ask questions until you get it, and if you continue to make mistakes, it's part of the learning experience. Your goal is not to avoid mistakes or avoid failure, but to learn from mistakes and failures and to use that experiential knowledge to reach your real goal.

You might want to read this also: [url="http://lazyfoo.net/articles/article04/index.php"]Game Loops[/url] Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
Final question (I promise).

Why does this code work:

if (SDL_PollEvent (@event) )
if (right click)
if (left click)

when this does not:

if (SDL_PollEvent(@event) && right click)
if (SDL_PollEvent(@event) && left click)

Share this post


Link to post
Share on other sites
(It's [b]&[/b] not [b]@[/b])

[b]SDL_PollEvent(&event)[/b] does two things:

First, it assigns the next event to 'event', second it discards that event from the event queue. It's a stack of events, with each new mouse and keyboard added to the top of the stack, and SDL_PollEvent() pops the old event from the bottom of the stack.

Each time you call SDL_PollEvent() it will [b]discard[/b] the event. If you call it twice, you are discarding two events.
SDL_PollEvent() also returns false if there are no more events.

Think of it like this:
[code]while(true) //Loop forever until we break.
{
SDL_Event event;

//Discard the previous event, if there was one, and set 'event' to the new event.
bool weStillHaveEvents = SDL_PollEvent(event);

//If we don't have any events left to deal with, break.
if(weStillHaveEvents == false)
break;

if(event == left click)
{

}
else if(event == right click)
{

}
else if(event == keyboard key was pressed)
{
}
}
[/code]

The above can be simplified to this:
[code]SDL_Event event;
while(SDL_PollEvent(event)) //Loop until SDL_PollEvent() returns false, saying there is no more events to handle.
{
if(event == left click)
{
}
else if(event == right click)
{
}
else if(event == keyboard key was pressed)
{
}
}[/code]

This is how you're supposed to do it. Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

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