Irrlicht code snippet: slow down FPS


A simple code snippet to slow down frame rate and slow down CPU usage.

#include <irrlicht.h>
#include <iostream>
using namespace irr;

struct IrrlichtDelayFPS {
  int maxFPS;
  int lastFPS;
  int yieldCounter;
  IrrlichtDevice *device;
  IrrlichtDelayFPS() { maxFPS = 50;  yieldCounter = 0;  device = 0; }
  void delay() {
    video::IVideoDriver* driver = device->getVideoDriver();
    int lastFPS = driver->getFPS();
    for( int i = 0 ; i < yieldCounter ; i++ ) {
      device->yield();
    }
    if ( lastFPS >maxFPS ) { yieldCounter++ ; }
    else {
      if (yieldCounter > 0 ) { --yieldCounter; }
      else { yieldCounter = 0 ; }
    }
  }
};

/* 

  IrrlichtDelayFPS delayFPS;
  delayFPS.maxFPS = 50; //set max FPS
  delayFPS.device = device; //set device

while(device->run()) {
   //your code here
  //delay loop
  delayFPS.delay();
}

*/
IrrlichtFPS

Comments are closed.