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 <QtCore/QTime>
00023 #include <QtCore/QDebug>
00024 #include <QtCore/QCoreApplication>
00025
00026 GameLoop::GameLoop( QList<InputDevice*> inputList )
00027 : m_inputList( inputList )
00028 {
00029 connect( this, SIGNAL( startGameLoop() ), SLOT( gameLoop() ), Qt::QueuedConnection );
00030 }
00031
00032 void GameLoop::run()
00033 {
00034 emit startGameLoop();
00035 }
00036
00037 void GameLoop::gameLoop()
00038 {
00039 int nextTick = 0;
00040 int loops = 0;
00041 QTime timer;
00042 int updatesPerSecond = 25;
00043 int maxFrameSkip = 5;
00044 int millisecondsPerUpdate = 1000 / updatesPerSecond;
00045 timer.start();
00046
00047 qDebug() << "starting gameloop";
00048 while( true )
00049 {
00050 QCoreApplication::processEvents();
00051 loops = 0;
00052 while( timer.elapsed() > nextTick && loops < maxFrameSkip )
00053 {
00054 foreach( InputDevice * id, m_inputList )
00055 {
00056 foreach( int button, id->buttonCapabilities() )
00057 {
00058 if( id->buttonPressed( button ) )
00059 qDebug() << id->buttonName( button ) << " is pressed ";
00060
00061 }
00062 }
00063
00064 nextTick += millisecondsPerUpdate;
00065 ++loops;
00066 }
00067 }
00068 }
00069
00070 #include "gameloop.moc"