• Advertisement
Sign in to follow this  

SDL Game Frame Help

This topic is 2420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am very new to SDL and have been working on a game in SDL recently. In the game, objects will be coming closer, and you must fire use mouse input (aimed by clicking [SDL_MOUSEBUTTONDOWN], dragging [SDL_MOUSEMOTION], and releasing [SDL_MOUSEBUTTONUP]. Below if the loop I am using to aim the firing mechanism, though as of yet it is incomplete). I am concerned, however, that when I begin to play my game, when the objects will come closer, that I will fire, but clicking and dragging to attack will cause all other functions outside the loop (including object movement) to "freeze" in place until the events are done. How can I prevent this from happening so that my game will run smoothly?

Notes: The surface in "apply_surface(i,j,surface,screen);" follows the mouse to help aim. The "down" variable is used so that you have to click down before the cursor will follow the mouse, or else it's just be confusing. The loop works, I'm just wondering if there's a way to restructure it so I won't have a problem in the future when I finish coding this so that everything won't stop when this loop is going on.

Thank you very much for your help in advance.



while ( SDL_PollEvent(&event1) )
{
switch (event1.type)
{
case SDL_MOUSEBUTTONDOWN:
if (event1.button.button == SDL_BUTTON_LEFT)
{
down=0;

i = event1.button.x;
j = event1.button.y;


down=1;

apply_surface(i,j,surface,screen);
SDL_Flip(screen);
}
break;
case SDL_MOUSEMOTION:
if (down==1)
{
i = event1.motion.x;
j = event1.motion.y;
apply_surface(i,j,surface,screen);
SDL_Flip(screen);
}
break;
case SDL_MOUSEBUTTONUP:
if (event1.button.button == SDL_BUTTON_LEFT)
{
if (down==1)
{
i = event1.button.x;
j = event1.button.y;
apply_surface(i,j,surface,screen);
SDL_Flip(screen);
down=0;
}
}
break;
case SDL_QUIT:
exit(0);
}
}

Share this post


Link to post
Share on other sites
Advertisement
[quote name='ars140' timestamp='1307248716' post='4819638']
I am concerned, however, that when I begin to play my game, when the objects will come closer, that I will fire, but clicking and dragging to attack will cause all other functions outside the loop (including object movement) to "freeze" in place until the events are done. How can I prevent this from happening so that my game will run smoothly?
[/quote]

Have you actually seen this happen? The code looks fine to me. Perhaps what you are concerned about is SDL_Flip? If you ever do encounter the issue, just call SDL_Flip once, at the end of your game state's render(); function.

PS: you can put your code inside [c o d e] and [/c o d e] (without the spaces). It makes it easier to read, as it retains formatting.

[code]
int thisInt = 5;
[/code]

Share this post


Link to post
Share on other sites
Don't draw or flip during input handling. What is happening (probably) is that you are taking so long to draw and flip that the next time the loop goes around there is another mouse motion event waiting.

Instead, the event loop should really just set the values "down", "i" and "j" *. Then, in the place you do your other drawing you check if "down == " and draw the sprite.

*[size="1"] These could have better names, such as "mousePressed", "mouseX" and "mouseY".[/size]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement