#include "GameScreenView.h"
#include "Debug.h"
#include <QMouseEvent>
#include <QTimer>
#include <ctime>
GameScreenView::GameScreenView()
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1); // 33
}
GameScreenView::~GameScreenView()
{
}
void GameScreenView::initializeGL()
{
m_gameScreen.initialize();
}
void GameScreenView::paintGL()
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
long seconds = now.tv_sec - m_lastFrameTime.tv_sec;
long nseconds = now.tv_nsec - m_lastFrameTime.tv_nsec;
double elapsed = seconds * 1000.0 + nseconds / 1000000.0;
//printf("elapsed: %lf\n", elapsed);
m_lastFrameTime = now;
setDebugValue("CPU Usage:", m_sysInfo.getCpuUsage());
setDebugValue("Memory Usage:", std::string(std::to_string(m_sysInfo.getMemoryUsage()) + " bytes").c_str());
setDebugValue("Frame time:", std::string(std::to_string(m_sysInfo.getFrameTime() * 1000.0) + " ms").c_str());
setDebugValue("Draw time:", std::string(std::to_string(m_sysInfo.getDrawTime() * 1000.0) + " ms").c_str());
setDebugValue("FPS:", m_sysInfo.getFps());
setDebugValue("Average FPS:", m_sysInfo.getAverageFps());
GameTime::TimePoint preUpdate = GameTime::now();
float aspect = 1.0;
if (height())
aspect = float(width()) / height();
m_gameScreen.update(0.01 * float(elapsed), aspect);
GameTime::Duration updateTime = GameTime::now() - preUpdate;
setDebugValue("Update time:", std::string(std::to_string(updateTime.count() * 0.000001f) + " ms").c_str());
m_sysInfo.preDraw();
glViewport(0,0,width(),height());
m_gameScreen.draw();
m_sysInfo.frameDrawn();
// Adding this here seems to help measure the time inside draw a bit more accurately
glFlush();
glFinish();
}
extern bool toggleDebugCameraView;
void GameScreenView::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Q)
exit(0);
else if (event->key() == Qt::Key_E)
toggleDebugCameraView = !toggleDebugCameraView;
else if (event->key() == Qt::Key_Space || event->key() == Qt::Key_Enter) {
m_gameScreen.touchDown(100, 100);
m_gameScreen.touchUp(100, 100);
}
else if (event->key() == Qt::Key_W || event->key() == Qt::Key_Up) {
m_gameScreen.touchDown(100, 100);
m_gameScreen.touchMove(100, 50);
m_gameScreen.touchUp(100, 50);
}
else if (event->key() == Qt::Key_A || event->key() == Qt::Key_Left) {
m_gameScreen.touchDown(100, 100);
m_gameScreen.touchMove(50, 100);
m_gameScreen.touchUp(50, 100);
}
else if (event->key() == Qt::Key_S || event->key() == Qt::Key_Down) {
m_gameScreen.touchDown(100, 100);
m_gameScreen.touchMove(100, 150);
m_gameScreen.touchUp(100, 150);
}
else if (event->key() == Qt::Key_D || event->key() == Qt::Key_Right) {
m_gameScreen.touchDown(100, 100);
m_gameScreen.touchMove(150, 100);
m_gameScreen.touchUp(150, 100);
}
}
void GameScreenView::mouseMoveEvent(QMouseEvent *event)
{
float xs = 640.0 / width();
float ys = 960.0 / height();
m_gameScreen.touchMove(event->x()*xs, event->y()*ys);
}
void GameScreenView::mousePressEvent(QMouseEvent *event)
{
float xs = 640.0 / width();
float ys = 960.0 / height();
m_gameScreen.touchDown(event->x()*xs, event->y()*ys);
}
void GameScreenView::mouseReleaseEvent(QMouseEvent *event)
{
float xs = 640.0 / width();
float ys = 960.0 / height();
m_gameScreen.touchUp(event->x()*xs, event->y()*ys);
}