• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
stannisbaratheon

v. simple SDL question

8 posts in this topic

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!
0

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
2

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
0

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]
0

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
2

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

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
0

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)
0

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
0

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  
Followers 0