test.h
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef TEST_H
24 #define TEST_H
25 
26 #include "box2d/box2d.h"
27 #include "draw.h"
28 
29 #include <stdlib.h>
30 
31 struct Settings;
32 class Test;
33 
34 #define RAND_LIMIT 32767
35 
37 inline float RandomFloat()
38 {
39  float r = (float)(rand() & (RAND_LIMIT));
40  r /= RAND_LIMIT;
41  r = 2.0f * r - 1.0f;
42  return r;
43 }
44 
46 inline float RandomFloat(float lo, float hi)
47 {
48  float r = (float)(rand() & (RAND_LIMIT));
49  r /= RAND_LIMIT;
50  r = (hi - lo) * r + lo;
51  return r;
52 }
53 
54 // This is called when a joint in the world is implicitly destroyed
55 // because an attached body is destroyed. This gives us a chance to
56 // nullify the mouse joint.
58 {
59 public:
60  void SayGoodbye(b2Fixture* fixture) override { B2_NOT_USED(fixture); }
61  void SayGoodbye(b2Joint* joint) override;
62 
64 };
65 
67 
69 {
77  float separation;
78 };
79 
80 class Test : public b2ContactListener
81 {
82 public:
83 
84  Test();
85  virtual ~Test();
86 
87  void DrawTitle(const char* string);
88  virtual void Step(Settings& settings);
89  virtual void UpdateUI() {}
90  virtual void Keyboard(int key) { B2_NOT_USED(key); }
91  virtual void KeyboardUp(int key) { B2_NOT_USED(key); }
92  void ShiftMouseDown(const b2Vec2& p);
93  virtual void MouseDown(const b2Vec2& p);
94  virtual void MouseUp(const b2Vec2& p);
95  virtual void MouseMove(const b2Vec2& p);
96  void LaunchBomb();
97  void LaunchBomb(const b2Vec2& position, const b2Vec2& velocity);
98 
99  void SpawnBomb(const b2Vec2& worldPt);
100  void CompleteBombSpawn(const b2Vec2& p);
101 
102  // Let derived tests know that a joint was destroyed.
103  virtual void JointDestroyed(b2Joint* joint) { B2_NOT_USED(joint); }
104 
105  // Callbacks for derived classes.
106  virtual void BeginContact(b2Contact* contact) override { B2_NOT_USED(contact); }
107  virtual void EndContact(b2Contact* contact) override { B2_NOT_USED(contact); }
108  virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) override;
109  virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) override
110  {
111  B2_NOT_USED(contact);
112  B2_NOT_USED(impulse);
113  }
114 
115  void ShiftOrigin(const b2Vec2& newOrigin);
116 
117 protected:
118  friend class DestructionListener;
119  friend class BoundaryListener;
120  friend class ContactListener;
121 
138 };
139 
140 typedef Test* TestCreateFcn();
141 
142 int RegisterTest(const char* category, const char* name, TestCreateFcn* fcn);
143 
144 //
145 struct TestEntry
146 {
147  const char* category;
148  const char* name;
150 };
151 
152 #define MAX_TESTS 256
154 extern int g_testCount;
155 
156 #endif
TestCreateFcn * createFcn
Definition: test.h:149
DestructionListener m_destructionListener
Definition: test.h:126
virtual void UpdateUI()
Definition: test.h:89
b2Vec2 position
Definition: test.h:73
b2PointState state
Definition: test.h:74
b2Vec2 normal
Definition: test.h:72
float normalImpulse
Definition: test.h:75
Test * test
Definition: test.h:63
int32 m_textLine
Definition: test.h:127
const int32 k_maxContactPoints
Definition: test.h:66
Definition: test.h:80
virtual void EndContact(b2Contact *contact) override
Called when two fixtures cease to touch.
Definition: test.h:107
const char * category
Definition: test.h:147
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
int32 m_pointCount
Definition: test.h:125
A 2D column vector.
Definition: b2_math.h:41
#define MAX_TESTS
Definition: test.h:152
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
signed int int32
Definition: b2_types.h:28
b2AABB m_worldAABB
Definition: test.h:123
int g_testCount
Definition: test.cpp:456
float separation
Definition: test.h:77
b2Body * m_groundBody
Definition: test.h:122
virtual void KeyboardUp(int key)
Definition: test.h:91
b2Body * m_bomb
Definition: test.h:129
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
virtual void BeginContact(b2Contact *contact) override
Called when two fixtures begin to touch.
Definition: test.h:106
Profiling data. Times are in milliseconds.
Definition: b2_time_step.h:29
#define B2_NOT_USED(x)
Definition: b2_common.h:36
bool m_bombSpawning
Definition: test.h:132
b2Vec2 m_bombSpawnPoint
Definition: test.h:131
b2Profile m_totalProfile
Definition: test.h:137
b2MouseJoint * m_mouseJoint
Definition: test.h:130
int32 m_textIncrement
Definition: test.h:135
virtual void JointDestroyed(b2Joint *joint)
Definition: test.h:103
b2World * m_world
Definition: test.h:128
b2Profile m_maxProfile
Definition: test.h:136
b2Vec2 m_mouseWorld
Definition: test.h:133
void SayGoodbye(b2Fixture *fixture) override
Definition: test.h:60
const char * name
Definition: test.h:148
int32 m_stepCount
Definition: test.h:134
b2PointState
This is used for determining the state of contact points.
Definition: b2_collision.h:132
virtual void Keyboard(int key)
Definition: test.h:90
virtual void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) override
Definition: test.h:109
float tangentImpulse
Definition: test.h:76
An axis aligned bounding box.
Definition: b2_collision.h:168
b2Fixture * fixtureB
Definition: test.h:71
b2Fixture * fixtureA
Definition: test.h:70
Test * TestCreateFcn()
Definition: test.h:140
TestEntry g_testEntries[MAX_TESTS]
Definition: test.cpp:455
#define RAND_LIMIT
Definition: test.h:34


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21