00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "gameloop.h"
00021
00022 #include "input/inputdevice.h"
00023 #include "input/keyboard.h"
00024
00025 #include <QtCore/QTime>
00026 #include <QtCore/QDebug>
00027 #include <QtCore/QCoreApplication>
00028
00029 GameLoop::GameLoop( Keyboard* keyb )
00030 {
00031 keyboard = keyb;
00032 connect( this, SIGNAL( startGameLoop() ), SLOT( gameLoop() ), Qt::QueuedConnection );
00033 }
00034
00035 void GameLoop::run()
00036 {
00037 emit startGameLoop();
00038 }
00039
00040 void GameLoop::gameLoop()
00041 {
00042 int nextTick = 0;
00043 int loops = 0;
00044 QTime timer;
00045 int updatesPerSecond = 25;
00046 int maxFrameSkip = 5;
00047 int millisecondsPerUpdate = 1000 / updatesPerSecond;
00048 timer.start();
00049
00050 qDebug() << "starting gameloop";
00051 while( true )
00052 {
00053 QCoreApplication::processEvents();
00054 loops = 0;
00055 while( timer.elapsed() > nextTick && loops < maxFrameSkip )
00056 {
00057 foreach( int button, keyboard->buttonCapabilities() )
00058 {
00059 if( keyboard->buttonPressed( button ) )
00060 qDebug() << keyboard->buttonName( button ) << " is pressed ";
00061
00062 }
00063
00064 nextTick += millisecondsPerUpdate;
00065 ++loops;
00066 }
00067 }
00068 }
00069
00070 #include "gameloop.moc"