Walking in the map
Downloads
The timer
Timer {
id: elapsedTimer
interval: 1000/60;
running: true;
repeat: true;
onTriggered: {
main.image1.source = "image://myimageprovider/" + frame;
frame++;
}
}
It calls the image provider roughly 60 times per seconds. It's not very constant, as you can see if you uncomment the following lines in bridge.cpp
/* static clock_t lastClock;
clock_t curClock = clock();
qDebug() << (((float)(curClock - lastClock)) * 1000.0f /CLOCKS_PER_SEC);
lastClock = curClock;
*/
This code displays in QtCreator's console the time between two frames in milliseconds.
Speeding up
QImage floorGfx("gfx/3DView/Floor.png");
QImage ceilingGfx("gfx/3DView/Ceiling.png");
graph2D.drawImage(&image, CVec2(0, 33), ceilingGfx);
graph2D.drawImage(&image, CVec2(0, 72), floorGfx);
Obviously the same goes for the wall graphics too.
class CFileCache
{
public:
CFileCache();
virtual ~CFileCache();
CFileData getFile(std::string fileName);
QImage getImage(std::string fileName);
private:
std::map<std::string, CFileData> mCache;
std::map<std::string, QImage> mImageCache;
};
extern CFileCache fileCache;
Every time we will need to load a file that we will use frequently, we will call the function getFile().
QImage floorGfx = fileCache.getImage("gfx/3DView/Floor.png");
QImage ceilingGfx = fileCache.getImage("gfx/3DView/Ceiling.png");
graph2D.drawImage(&image, CVec2(0, 33), ceilingGfx);
graph2D.drawImage(&image, CVec2(0, 72), floorGfx);
The keyboard
// register the event filter for the keyboard
QApplication::instance()->installEventFilter(&keyboard);
int retValue = app.exec();
// unregister the event filter before exiting
QApplication::instance()->removeEventFilter(&keyboard);
return retValue;
The Event Filter itself is in the file keyboard.cpp in the sources folder:
bool CKeyboard::eventFilter(QObject */*object*/, QEvent *event){
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->isAutoRepeat() == false)
{
quint32 nativeKey = keyEvent->nativeScanCode();
switch (nativeKey)
{
case 16: // 'Q' on QWERTY, 'A' on AZERTY
player.turnLeft();
break;
case 17: // 'W' on QWERTY, 'Z' on AZERTY
player.moveForward();
break;
case 18: // 'E'
player.turnRight();
break;
case 30: // 'A' on QWERTY, 'Q' on AZERTY
player.moveLeft();
break;
case 31: // 'S'
player.moveBackward();
break;
case 32: // 'D'
player.moveRight();
break;
default:
break;
}
}
return true;
}
return false;
}
There is not so much to say about it. As you can see, for every key we call a function in "player.cpp" to move or turn.
Moving
//-----------------------------------------------------------------------------------------
void CPlayer::turnLeft()
{
dir = (dir + 1) % 4;
}
//-----------------------------------------------------------------------------------------
void CPlayer::turnRight()
{
dir = (dir + 4 - 1) % 4;
}
//-----------------------------------------------------------------------------------------
void CPlayer::moveLeft()
{
EWallSide side = getWallSide(eWallSideLeft);
moveIfPossible(side);
}
//-----------------------------------------------------------------------------------------
void CPlayer::moveRight()
{
EWallSide side = getWallSide(eWallSideRight);
moveIfPossible(side);
}
//-----------------------------------------------------------------------------------------
void CPlayer::moveForward()
{
EWallSide side = getWallSide(eWallSideUp);
moveIfPossible(side);
}
//-----------------------------------------------------------------------------------------
void CPlayer::moveBackward()
{
EWallSide side = getWallSide(eWallSideDown);
moveIfPossible(side);
}
//-----------------------------------------------------------------------------------------
void CPlayer::moveIfPossible(EWallSide side)
{
CVec2 newPos = pos;
switch (side)
{
case eWallSideUp:
newPos.y--;
break;
case eWallSideDown:
newPos.y++;
break;
case eWallSideLeft:
newPos.x--;
break;
case eWallSideRight:
newPos.x++;
break;
default:
break;
}
CTile* tile = map.getTile(pos);
if (tile->mWalls[side].mType == 0)
pos = newPos;
}
The "turn" functions only change the direction we are looking at.