Jump to content

  • Log In with Google      Sign In   
  • Create Account


v. simple SDL question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 stannisbaratheon   Members   -  Reputation: 109

Like
0Likes
Like

Posted 28 July 2012 - 10:30 AM

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!

Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 18575

Like
2Likes
Like

Posted 28 July 2012 - 10:44 AM

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. Posted Image

Your draw loop should look like this every frame:
  • 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.

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

Edited by Servant of the Lord, 28 July 2012 - 10:44 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 stannisbaratheon   Members   -  Reputation: 109

Like
0Likes
Like

Posted 28 July 2012 - 10:48 AM

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. Posted Image

Your draw loop should look like this every frame:

  • 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.

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


Ah, thank you!

EDIT: It worked!

Edited by stannisbaratheon, 28 July 2012 - 10:50 AM.


#4 stannisbaratheon   Members   -  Reputation: 109

Like
0Likes
Like

Posted 28 July 2012 - 11:07 AM

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


#5 Servant of the Lord   Crossbones+   -  Reputation: 18575

Like
2Likes
Like

Posted 28 July 2012 - 11:12 AM

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:
  • 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
(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:
  • Get and handle events from the user
  • Update the game based off of the time that past since the last frame.
  • Draw everything:
    • 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

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.

Edited by Servant of the Lord, 28 July 2012 - 11:16 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#6 stannisbaratheon   Members   -  Reputation: 109

Like
0Likes
Like

Posted 28 July 2012 - 11:13 AM

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:

  • 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
Which means, your loop more detailed should go:
  • Get and handle events from the user
  • Update the game based off of the time that past since the last frame.
  • Draw everything:
    • 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


I feel like such a n00b.

#7 Servant of the Lord   Crossbones+   -  Reputation: 18575

Like
0Likes
Like

Posted 28 July 2012 - 11:21 AM

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

Edited by Servant of the Lord, 28 July 2012 - 11:25 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#8 stannisbaratheon   Members   -  Reputation: 109

Like
0Likes
Like

Posted 28 July 2012 - 12:38 PM

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)

#9 Servant of the Lord   Crossbones+   -  Reputation: 18575

Like
0Likes
Like

Posted 28 July 2012 - 12:51 PM

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

Edited by Servant of the Lord, 28 July 2012 - 12:51 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS