You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ugameengine.cpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "ugameengine.h"
  2. #include <QTime>
  3. UGameEngine::UGameEngine(AbstractRenderDevice* device)
  4. : _device(device)
  5. {
  6. for (int i = 0; i < 8; ++i) {
  7. _entitiesUpdateThreads.append(new EntitiesUpdateThread(this));
  8. }
  9. }
  10. UGameEngine::~UGameEngine()
  11. {
  12. }
  13. void UGameEngine::update()
  14. {
  15. int entitiesCount = _entitites.size();
  16. if (entitiesCount > 1000) {
  17. int threads = _entitiesUpdateThreads.size();
  18. for (int t = 0; t < threads; ++t) {
  19. EntitiesUpdateThread* thread = _entitiesUpdateThreads[t];
  20. int begin = (entitiesCount / threads) * t;
  21. int end = (entitiesCount / threads) * (t + 1);
  22. if (t == threads - 1) {
  23. end = entitiesCount;
  24. }
  25. thread->update(begin, end);
  26. }
  27. for (int t = 0; t < threads; ++t) {
  28. EntitiesUpdateThread* thread = _entitiesUpdateThreads[t];
  29. thread->wait();
  30. }
  31. }
  32. else {
  33. for(int i = 0; i < _entitites.size(); ++i) {
  34. UGEEntity* entity = _entitites[i];
  35. if (entity->isVisible()) {
  36. entity->update();
  37. }
  38. }
  39. }
  40. }
  41. void UGameEngine::draw()
  42. {
  43. _device->preDraw();
  44. QTime time;
  45. time.start();
  46. update();
  47. int update = time.elapsed();
  48. time.restart();
  49. for(int i = 0; i < _entitites.size(); ++i) {
  50. UGEEntity* entity = _entitites[i];
  51. if (entity->isVisible()) {
  52. entity->draw(_device);
  53. }
  54. }
  55. int draw = time.elapsed();
  56. // qDebug() << update << draw << (update + draw);
  57. _device->postDraw();
  58. }
  59. const QList<UGEEntity *> &UGameEngine::getEntities() const
  60. {
  61. return _entitites;
  62. }
  63. UGEEntity *UGameEngine::getEntity(int i) const
  64. {
  65. return _entitites[i];
  66. }
  67. Vector3D UGameEngine::get2DFrom3D(const Vector3D &pos)
  68. {
  69. return _device->get2DFrom3D(pos);
  70. }
  71. Vector3D UGameEngine::get3DFrom2D(const Vector2D &pos)
  72. {
  73. return _device->get3DFrom2D(pos);
  74. }
  75. UGEEntity *UGameEngine::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos)
  76. {
  77. Vector3D nearestPoint;
  78. UGEEntity* entity = 0;
  79. }
  80. void UGameEngine::addEntity(UGEEntity *entity)
  81. {
  82. _entitites.append(entity);
  83. }
  84. void UGameEngine::lookAt(const Vector3D &eye, const Vector3D &center, const Vector3D &up)
  85. {
  86. _device->lookAt(eye, center, up);
  87. }
  88. void UGameEngine::setClearColor(const QColor &clearColor)
  89. {
  90. _device->setClearColor(clearColor);
  91. }
  92. void UGameEngine::initialize(int fov, int width, int height)
  93. {
  94. _device->initialize(fov, width, height);
  95. }
  96. void UGameEngine::resize(int width, int height)
  97. {
  98. _device->resize(width, height);
  99. }
  100. void UGameEngine::loadTextureFromFile(const QVariant &id, const QString &filename)
  101. {
  102. _device->loadTextureFromFile(id, filename);
  103. }