v. simple SDL question

Started by
7 comments, last by Servant of the Lord 11 years, 8 months ago
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!
Advertisement
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. smile.png

Your draw loop should look like this every frame:

  1. Draw over the entire screen with your background
  2. Draw your objects over the background, with the ones that you want to be shown "over" everything drawn last.
  3. Flip the SDL buffers.
  4. Go back to step 1, for the next frame.


I explain it a bit more in-depth in a different thread.

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. smile.png

Your draw loop should look like this every frame:

  1. Draw over the entire screen with your background
  2. Draw your objects over the background, with the ones that you want to be shown "over" everything drawn last.
  3. Flip the SDL buffers.
  4. Go back to step 1, for the next frame.


I explain it a bit more in-depth in a different thread.


Ah, thank you!

EDIT: It worked!
So it works but there seems to be lag:


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;
}
}
}
}
}
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:

  1. Get and handle events from the user
  2. Update the game based off of the time that past since the last frame.
  3. Draw everything and flip the screen.
  4. Go back to step 1

(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:

  1. Get and handle events from the user
  2. Update the game based off of the time that past since the last frame.
  3. Draw everything:

    1. Draw over the entire screen with your background
    2. Draw your objects over the background, with the ones that you want to be shown "over" everything drawn last.
    3. Flip the SDL buffers.
  4. Go back to step 1


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


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.

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:

  1. Get and handle events from the user
  2. Update the game based off of the time that past since the last frame.
  3. Draw everything and flip the screen.
  4. Go back to step 1

Which means, your loop more detailed should go:

  1. Get and handle events from the user
  2. Update the game based off of the time that past since the last frame.
  3. Draw everything:

    1. Draw over the entire screen with your background
    2. Draw your objects over the background, with the ones that you want to be shown "over" everything drawn last.
    3. Flip the SDL buffers.
  4. Go back to step 1



I feel like such a n00b.
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: Game Loops
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)
(It's & not @)

SDL_PollEvent(&event) 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 discard 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:
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)
{
}
}


The above can be simplified to this:
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)
{
}
}


This is how you're supposed to do it.

This topic is closed to new replies.

Advertisement