Jump to content
  • Advertisement
Sign in to follow this  
Saya

OpenGL [Noob] Going from Glut / OpenGL to SDL / OpenGL

This topic is 3999 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

Hi, i am currently learning OpenGL. I started out using Glut to handle input in which you set up callbacks at the begining for the events and the rendering. But later on i saw and was told than Glut was rather limited and SDL was a better solution for game because it implements at least image loading, input, 2D rendering and sound capabilities. But now i am facing a rather annoying problem. I want to seperate rendering and handling of events and even game logic. In Glut this was easy by setting up callbacks. But in SDL i saw that neither the event handling nor the rendering were thread safe. So my question is, how am i supposed to wait for events (which blocks the thread), render graphics at a decent frame rate and implement game ticks ( like a game function called every 50 ms or so independent from rendering ). Thanks alot for your time!

Share this post


Link to post
Share on other sites
Advertisement
Thanks, that seems fine if i only handle events and render images but what about if i want a game tick mechanism (i dont want the game speed to depend on the cpu).

Share this post


Link to post
Share on other sites
ok maybe i was a bit stupid. I only need to render the frames if some event occurs so i can do:

main loop:
PollEvent
if( enough time went by )
game tick
render

right now im doing

main loop:
PollEvent
render

and a seperate SDL Thread for gameticks containing a loop and
a SDL_Delay mechanism to trigger itself regularly.

But is using PollEvent good? It eats up alot of cpu.

My game thread is something like that:

int gameTicks( void *Game )
{
Uint32 tick_delay = 30; //tick interval in milliseconds
Uint32 start_time, current_time, elapsed_time;

while( !game_end )
{
start_time = SDL_GetTicks();

if( !game_pause )
//do stuff with Game

current_time = SDL_GetTicks();
elapsed_time = current_time - start_time;
if( elapsed_time < tick_delay )
{
SDL_Delay( tick_delay - elapsed_time );
}
}
return 0;
}

Share this post


Link to post
Share on other sites
Well finally i did it all in the main thread using SDL_PollEvent and SDL_GetTicks to control the frames per second and ticks per second.

I know this has really little to do with OpenGL now.

Anyways, heres the main loop code, i think its fine like that but if anyone has general suggestions about how to make a good main loop go on ( im trying to make a small rts btw, i already did a small one in SDL and im trying to do another small one but in 3D now ).


Uint32 fps_desired = 100;
Uint32 fps_tick_delay = 1000/fps_desired;
Uint32 fps_start_time, fps_current_time, fps_elapsed_time;

Uint32 tps_desired = 30;
Uint32 tps_tick_delay = 1000/tps_desired;
Uint32 tps_start_time, tps_current_time, tps_elapsed_time;

tps_start_time = fps_start_time = SDL_GetTicks();

SDL_Event *event = new SDL_Event();
while ( !game_end )
{
/**handle events*/
while( SDL_PollEvent(event) )
{
handleEvent(event, game);
}

/**handle game ticks with tick per seconds contorl*/
tps_current_time = SDL_GetTicks();
tps_elapsed_time = tps_current_time - tps_start_time;
if( tps_elapsed_time >= tps_tick_delay )
{
tps_start_time = SDL_GetTicks();
if( !game_pause )
{
gameTick(game);

}
}

/**handle rendering with frames per second control*/
fps_current_time = SDL_GetTicks();
fps_elapsed_time = fps_current_time - fps_start_time;
if( fps_elapsed_time >= fps_tick_delay )
{
fps_start_time = SDL_GetTicks();
renderScene(game);
}
}

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!