vgwidget.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Qt adaptation for our game code.
  3. * Initialises game (game_init, game_prepare) and periodically calls game_update and
  4. * game_render if the application is visible.
  5. * If the application is hidden, game_release is called in order to free resources for other applications
  6. */
  7. #include "vgwidget.h"
  8. #include "game.h"
  9. #include <QDebug>
  10. VGWidget::VGWidget(QWidget *parent)
  11. : QWidget(parent)
  12. {
  13. t0 = QTime::currentTime();
  14. tt = t0;
  15. frames = 0;
  16. game_prepared=0;
  17. game_init();
  18. setAttribute( Qt::WA_AcceptTouchEvents, true );
  19. }
  20. VGWidget::~VGWidget()
  21. {
  22. if(game_prepared)
  23. {
  24. game_prepared=false;
  25. game_release();
  26. }
  27. game_destroy();
  28. }
  29. bool VGWidget::event(QEvent *event)
  30. {
  31. switch(event->type())
  32. {
  33. case QEvent::TouchBegin:
  34. case QEvent::TouchEnd:
  35. case QEvent::TouchUpdate:
  36. {
  37. //qDebug("event->type %d", event->type());
  38. QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
  39. foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints)
  40. {
  41. QPoint p = touchPoint.pos().toPoint();
  42. game_event(touchPoint.state(), p.x(), p.y(), touchPoint.id());
  43. }
  44. }
  45. return true;
  46. break;
  47. case QEvent::WindowActivate:
  48. qDebug("WindowActivate");
  49. setUpdatesEnabled(true);
  50. repaint(); // TODO update() didnt work here, investigate
  51. break;
  52. case QEvent::WindowDeactivate:
  53. qDebug("WindowDeactivate");
  54. if(game_prepared)
  55. {
  56. qDebug("game_release");
  57. game_prepared=false;
  58. game_release();
  59. }
  60. setUpdatesEnabled(false);
  61. break;
  62. }
  63. QWidget::event(event);
  64. }
  65. void VGWidget::paintEvent(QPaintEvent *)
  66. {
  67. //qDebug("paintevent");
  68. //vgFlush();
  69. QTime now = QTime::currentTime();
  70. //#ifndef NDEBUG
  71. int dt=tt.msecsTo(now);
  72. if(frames>100)// || dt>5000)
  73. {
  74. if(dt)
  75. {
  76. message = QString("fps=%1").arg(frames*1000.0f/dt, 0, 'f', 3);
  77. qDebug() << message;
  78. }
  79. tt=now;
  80. frames=0;
  81. }
  82. //#endif
  83. game_update(t0.msecsTo(now)/1000.0f);
  84. frames++;
  85. QPainter painter;
  86. painter.begin(this);
  87. painter.setRenderHints(QPainter::Antialiasing);
  88. if (painter.paintEngine()->type() == QPaintEngine::OpenVG)
  89. {
  90. painter.beginNativePainting();
  91. if(!game_prepared)
  92. {
  93. qDebug("game_prepare");
  94. game_prepared=true;
  95. game_prepare();
  96. }
  97. game_render();
  98. painter.endNativePainting();
  99. }
  100. else
  101. {
  102. qDebug("paintEvent skipped, not using OpenVG");
  103. }
  104. #ifndef NDEBUG
  105. vgFlush();
  106. int frametime = now.msecsTo(QTime::currentTime());
  107. painter.setPen(Qt::white);
  108. painter.setFont(QFont("Arial", 10));
  109. painter.drawText(QPointF(10, 30), message);
  110. int y=(frametime)*360/33;
  111. painter.drawRect(630, 359-y, 1, y);
  112. #endif
  113. painter.end();
  114. update();
  115. }