I'm trying to create a small 2D RPG-type engine using C++, SDL/OpenGL and Tiled. My little demo looks very similar to a Pokemon or Zelda game.
I was curious how to go about regulating FPS and how many times a certain event action is processed. For example, each iteration of my main loop handles events (such as if an arrow key is pressed, move the character up one tile, et cetera), then draws, and then updates then screen.
If I wanted the game to draw 24 frames every second, but only run the keyboard actions 6 times per second, how would I go about doing that? Is this even reasonable? Should I regulate everything to process 6 frames per second? Or would it make sense to create two threads, one for drawing and one for event handling?
This is my main game loop:
while (!theGame->getQuit()) {
fps.start();
handleEvents();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
theGame->setQuit(true);
break;
case SDL_KEYDOWN:
handleKeyDown(event.key.keysym.sym);
case SDL_KEYUP:
handleKeyUp(event.key.keysym.sym);
break;
default:
break;
}
}
update();
render();
// FRAMES_PER_SECOND = 6 OR 24
if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND) {
SDL_Delay((1000 / FRAMES_PER_SECOND) - fps.get_ticks());
}
}