diff --git a/testbed/CMakeLists.txt b/testbed/CMakeLists.txt index 5ef305e09..31022bf25 100644 --- a/testbed/CMakeLists.txt +++ b/testbed/CMakeLists.txt @@ -10,6 +10,7 @@ set (TESTBED_SOURCE_FILES settings.cpp test.cpp test.h + tests/benchmark_boxes.cpp tests/add_pair.cpp tests/apply_force.cpp tests/body_types.cpp diff --git a/testbed/tests/benchmark_boxes.cpp b/testbed/tests/benchmark_boxes.cpp new file mode 100644 index 000000000..bee50b457 --- /dev/null +++ b/testbed/tests/benchmark_boxes.cpp @@ -0,0 +1,100 @@ +// MIT License + +// Copyright (c) 2019 Erin Catto + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "test.h" + +class Boxes : public Test +{ +public: + Boxes() + { + float groundSize = 25.0f; + + { + b2BodyDef bd; + b2Body* ground = m_world->CreateBody(&bd); + + b2PolygonShape box; + box.SetAsBox(groundSize, 1.2f); + b2FixtureDef sd; + sd.shape = &box; + ground->CreateFixture(&sd); + + bd.angle = 0.5f * b2_pi; + bd.position = { groundSize, 2.0f * groundSize }; + ground = m_world->CreateBody(&bd); + + box.SetAsBox(2.0f * groundSize, 1.2f); + ground->CreateFixture(&sd); + + bd.position = { -groundSize, 2.0f * groundSize }; + ground = m_world->CreateBody(&bd); + ground->CreateFixture(&sd); + } + + int32_t num = 26; + float rad = 0.5f; + + float shift = rad * 2.0f; + float centerx = shift * num / 2.0f; + float centery = shift / 2.0f; + + b2BodyDef bd; + bd.type = b2_dynamicBody; + + b2FixtureDef sd; + sd.density = 1.0f; + sd.friction = 0.5f; + + b2PolygonShape cuboid; + cuboid.SetAsBox(0.5f, 0.5f); + sd.shape = &cuboid; + +#ifdef _DEBUG + int32_t numj = 5; +#else + int32_t numj = 5 * num; +#endif + + for (int32_t i = 0; i < num; ++i) + { + float x = i * shift - centerx; + + for (int32_t j = 0; j < numj; ++j) + { + float y = j * shift + centery + 2.0f; + + bd.position = { x, y }; + + b2Body* rigidBody = m_world->CreateBody(&bd); + rigidBody->CreateFixture(&sd); + } + } + } + + static Test* Create() + { + return new Boxes; + } +}; + +static int testIndex = RegisterTest("Benchmark", "Boxes", Boxes::Create);