I'm learning C++ and openGL and have this program as a result from tutorials and playing around. The problem is that the main loop is running at "full speed", making the program unnecessarily cpu intensive. I have managed to make it only perform rendering every 16.7 ms or so, but the outside loop that is waiting to render still is iterating as fast as my computer can handle.
This is the full main.cpp: http://pastebin.com/KaCW7wZw
This is the main loop at line 95:
while (!terminate) {
if (SDL_GetTicks() >= time_start + frame_rate)
{
time_start = SDL_GetTicks();
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) {
terminate = true;
break;
}
}
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderLoop(entities, &counter);
SDL_GL_SwapWindow(m_window);;
counter += 1.0f;
}
}
I've tried googling and haven't found anything concrete (obviously), except that using some kind of sleep is bad design for these loops.
So how can I alter this to slow the whole thing down, and avoid having 100% cpu usage for a program that's not doing much at the moment?