SDL Game Frame Help

Started by
1 comment, last by rip-off 12 years, 10 months ago
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);
}
}
Advertisement

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?


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.


int thisInt = 5;
-A1P4A 0M3GA
Lead script writer on Scutum [http://www.gamedev.n...-entertainment/]
Team Member of [size=2]Forcas Entertainment
Amateur programmer with C++ and SDL knowledge
Game Enthusiast
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".

This topic is closed to new replies.

Advertisement