box2dworld.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Box2D QML plugin
  3. * Copyright (C) 2010 Nokia Corporation
  4. *
  5. * This file is part of the Box2D QML plugin.
  6. *
  7. * This library is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  15. * License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef BOX2DWORLD_H
  21. #define BOX2DWORLD_H
  22. #include <QDeclarativeItem>
  23. #include <QList>
  24. #include <QTime>
  25. class Box2DBody;
  26. class Box2DJoint;
  27. class ContactListener;
  28. class b2World;
  29. // TODO: Maybe turn this into a property of the world, though it can't be
  30. // changed dynamically.
  31. static const float scaleRatio = 113.33f; //64 pixel wheel image translated to 19 inch bike wheel
  32. /**
  33. * Wrapper class around a Box2D world.
  34. */
  35. class Box2DWorld : public QDeclarativeItem
  36. {
  37. Q_OBJECT
  38. Q_PROPERTY(float timeStep READ timeStep WRITE setTimeStep)
  39. Q_PROPERTY(int velocityIterations READ velocityIterations WRITE setVelocityIterations)
  40. Q_PROPERTY(int positionIterations READ positionIterations WRITE setPositionIterations)
  41. Q_PROPERTY(int frameTime READ frameTime WRITE setFrameTime)
  42. Q_PROPERTY(QPointF gravity READ gravity WRITE setGravity NOTIFY gravityChanged)
  43. Q_PROPERTY(int fps READ fps NOTIFY fpsChanged)
  44. Q_PROPERTY(bool running READ running)
  45. Q_PROPERTY(bool iap_enabled READ iap_enabled NOTIFY iap_statusChanged)
  46. public:
  47. explicit Box2DWorld(QDeclarativeItem *parent = 0);
  48. ~Box2DWorld();
  49. /**
  50. * The amount of time to step through each frame in seconds.
  51. * By default it is 1 / 60.
  52. */
  53. float timeStep() const { return mTimeStep; }
  54. void setTimeStep(float timeStep) { mTimeStep = timeStep; }
  55. /**
  56. * The number of velocity iterations used to process one step.
  57. * 10 by default.
  58. */
  59. int velocityIterations() const
  60. { return mVelocityIterations; }
  61. void setVelocityIterations(int iterations)
  62. { mVelocityIterations = iterations; }
  63. /**
  64. * The number of position iterations used to process one step.
  65. * 10 by default.
  66. */
  67. int positionIterations() const
  68. { return mPositionIterations; }
  69. void setPositionIterations(int iterations)
  70. { mPositionIterations = iterations; }
  71. int fps() const
  72. { return mFps; }
  73. /**
  74. * The amount of time each frame takes in milliseconds.
  75. * By default it is 1000 / 60.
  76. */
  77. int frameTime() const { return mFrameTime; }
  78. void setFrameTime(int frameTime) { mFrameTime = frameTime; }
  79. QPointF gravity() const { return mGravity; }
  80. void setGravity(const QPointF &gravity);
  81. bool running() const { return mRunning; }
  82. /**
  83. * this property serves as conditional in QML
  84. */
  85. bool iap_enabled() const
  86. {
  87. #ifdef INAPP_BILLING
  88. return true;
  89. #else
  90. return false;
  91. #endif
  92. }
  93. void componentComplete();
  94. void registerBody(Box2DBody *body);
  95. void registerJoint(Box2DJoint *joint);
  96. b2World *world() const { return mWorld; }
  97. Q_INVOKABLE void stop();
  98. Q_INVOKABLE void start();
  99. Q_INVOKABLE void resetVelocities();
  100. private slots:
  101. void unregisterBody();
  102. signals:
  103. void gravityChanged();
  104. void stepped();
  105. void fpsChanged();
  106. void iap_statusChanged();
  107. protected:
  108. void timerEvent(QTimerEvent *);
  109. QVariant itemChange(GraphicsItemChange, const QVariant &);
  110. private:
  111. b2World *mWorld;
  112. ContactListener *mContactListener;
  113. float mTimeStep;
  114. int mVelocityIterations;
  115. int mPositionIterations;
  116. int mFrameTime;
  117. QPointF mGravity;
  118. int mTimerId;
  119. QList<Box2DBody*> mBodies;
  120. QTime time;
  121. int mFps;
  122. float lastTime;
  123. bool mRunning;
  124. };
  125. QML_DECLARE_TYPE(Box2DWorld)
  126. #endif // BOX2DWORLD_H