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
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
B2_NOT_USED
#define B2_NOT_USED(x)
Definition: b2_common.h:36
Test::ShiftOrigin
void ShiftOrigin(const b2Vec2 &newOrigin)
Definition: test.cpp:450
TestCreateFcn
Test * TestCreateFcn()
Definition: test.h:140
Test::m_points
ContactPoint m_points[k_maxContactPoints]
Definition: test.h:124
MAX_TESTS
#define MAX_TESTS
Definition: test.h:152
Test::m_stepCount
int32 m_stepCount
Definition: test.h:134
g_testCount
int g_testCount
Definition: test.cpp:456
Test::ShiftMouseDown
void ShiftMouseDown(const b2Vec2 &p)
Definition: test.cpp:202
ContactPoint::normalImpulse
float normalImpulse
Definition: test.h:75
ContactPoint::tangentImpulse
float tangentImpulse
Definition: test.h:76
Test::m_destructionListener
DestructionListener m_destructionListener
Definition: test.h:126
Test::m_mouseWorld
b2Vec2 m_mouseWorld
Definition: test.h:133
ContactPoint::position
b2Vec2 position
Definition: test.h:73
b2ContactImpulse
Definition: b2_world_callbacks.h:70
TestEntry
Definition: test.h:145
Test::Keyboard
virtual void Keyboard(int key)
Definition: test.h:90
b2ContactListener
Definition: b2_world_callbacks.h:86
Test::m_bomb
b2Body * m_bomb
Definition: test.h:129
Test::EndContact
virtual void EndContact(b2Contact *contact) override
Called when two fixtures cease to touch.
Definition: test.h:107
Test::JointDestroyed
virtual void JointDestroyed(b2Joint *joint)
Definition: test.h:103
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2DestructionListener
Definition: b2_world_callbacks.h:41
Test::UpdateUI
virtual void UpdateUI()
Definition: test.h:89
Test::MouseDown
virtual void MouseDown(const b2Vec2 &p)
Definition: test.cpp:144
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
Test::BeginContact
virtual void BeginContact(b2Contact *contact) override
Called when two fixtures begin to touch.
Definition: test.h:106
DestructionListener::SayGoodbye
void SayGoodbye(b2Fixture *fixture) override
Definition: test.h:60
Test::MouseMove
virtual void MouseMove(const b2Vec2 &p)
Definition: test.cpp:228
Test::Test
Test()
Definition: test.cpp:39
b2PointState
b2PointState
This is used for determining the state of contact points.
Definition: b2_collision.h:132
ContactPoint
Definition: test.h:68
b2Joint
Definition: b2_joint.h:110
Test::~Test
virtual ~Test()
Definition: test.cpp:66
ContactPoint::separation
float separation
Definition: test.h:77
Test::m_groundBody
b2Body * m_groundBody
Definition: test.h:122
b2Contact
Definition: b2_contact.h:88
ContactPoint::normal
b2Vec2 normal
Definition: test.h:72
Test::BoundaryListener
friend class BoundaryListener
Definition: test.h:119
k_maxContactPoints
const int32 k_maxContactPoints
Definition: test.h:66
Test::CompleteBombSpawn
void CompleteBombSpawn(const b2Vec2 &p)
Definition: test.cpp:188
Test::m_maxProfile
b2Profile m_maxProfile
Definition: test.h:136
b2Fixture
Definition: b2_fixture.h:116
RandomFloat
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
DestructionListener::test
Test * test
Definition: test.h:63
g_testEntries
TestEntry g_testEntries[MAX_TESTS]
Definition: test.cpp:455
Test::DrawTitle
void DrawTitle(const char *string)
Definition: test.cpp:106
Test::m_pointCount
int32 m_pointCount
Definition: test.h:125
Test::m_bombSpawnPoint
b2Vec2 m_bombSpawnPoint
Definition: test.h:131
Test::LaunchBomb
void LaunchBomb()
Definition: test.cpp:238
Test::m_worldAABB
b2AABB m_worldAABB
Definition: test.h:123
Settings
Definition: settings.h:25
draw.h
Test::ContactListener
friend class ContactListener
Definition: test.h:120
ContactPoint::fixtureA
b2Fixture * fixtureA
Definition: test.h:70
TestEntry::category
const char * category
Definition: test.h:147
b2Manifold
Definition: b2_collision.h:99
ContactPoint::fixtureB
b2Fixture * fixtureB
Definition: test.h:71
box2d.h
TestEntry::createFcn
TestCreateFcn * createFcn
Definition: test.h:149
RAND_LIMIT
#define RAND_LIMIT
Definition: test.h:34
b2AABB
An axis aligned bounding box.
Definition: b2_collision.h:168
DestructionListener
Definition: test.h:57
Test::m_textLine
int32 m_textLine
Definition: test.h:127
b2Profile
Profiling data. Times are in milliseconds.
Definition: b2_time_step.h:29
Test::PreSolve
virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition: test.cpp:73
Test::SpawnBomb
void SpawnBomb(const b2Vec2 &worldPt)
Definition: test.cpp:182
int32
signed int int32
Definition: b2_types.h:28
TestEntry::name
const char * name
Definition: test.h:148
Test
Definition: test.h:80
Test::m_totalProfile
b2Profile m_totalProfile
Definition: test.h:137
Test::KeyboardUp
virtual void KeyboardUp(int key)
Definition: test.h:91
b2World
Definition: b2_world.h:46
Test::PostSolve
virtual void PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) override
Definition: test.h:109
Test::m_bombSpawning
bool m_bombSpawning
Definition: test.h:132
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2MouseJoint
Definition: b2_mouse_joint.h:65
Test::m_mouseJoint
b2MouseJoint * m_mouseJoint
Definition: test.h:130
Test::m_world
b2World * m_world
Definition: test.h:128
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
ContactPoint::state
b2PointState state
Definition: test.h:74
Test::MouseUp
virtual void MouseUp(const b2Vec2 &p)
Definition: test.cpp:214


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08