The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

104 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. Box2DRenderer::Box2DRenderer() noexcept : graphics (nullptr)
  18. {
  19. SetFlags (e_shapeBit);
  20. }
  21. void Box2DRenderer::render (Graphics& g, b2World& world,
  22. float left, float top, float right, float bottom,
  23. const Rectangle<float>& target)
  24. {
  25. graphics = &g;
  26. g.addTransform (AffineTransform::fromTargetPoints (left, top, target.getX(), target.getY(),
  27. right, top, target.getRight(), target.getY(),
  28. left, bottom, target.getX(), target.getBottom()));
  29. world.SetDebugDraw (this);
  30. world.DrawDebugData();
  31. }
  32. Colour Box2DRenderer::getColour (const b2Color& c) const
  33. {
  34. return Colour::fromFloatRGBA (c.r, c.g, c.b, 1.0f);
  35. }
  36. float Box2DRenderer::getLineThickness() const
  37. {
  38. return 0.1f;
  39. }
  40. static void createPath (Path& p, const b2Vec2* vertices, int32 vertexCount)
  41. {
  42. p.startNewSubPath (vertices[0].x, vertices[0].y);
  43. for (int i = 1; i < vertexCount; ++i)
  44. p.lineTo (vertices[i].x, vertices[i].y);
  45. }
  46. void Box2DRenderer::DrawPolygon (const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  47. {
  48. graphics->setColour (getColour (color));
  49. Path p;
  50. createPath (p, vertices, vertexCount);
  51. graphics->strokePath (p, PathStrokeType (getLineThickness()));
  52. }
  53. void Box2DRenderer::DrawSolidPolygon (const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  54. {
  55. graphics->setColour (getColour (color));
  56. Path p;
  57. createPath (p, vertices, vertexCount);
  58. graphics->fillPath (p);
  59. }
  60. void Box2DRenderer::DrawCircle (const b2Vec2& center, float32 radius, const b2Color& color)
  61. {
  62. graphics->setColour (getColour (color));
  63. graphics->drawEllipse (center.x - radius, center.y - radius,
  64. radius * 2.0f, radius * 2.0f,
  65. getLineThickness());
  66. }
  67. void Box2DRenderer::DrawSolidCircle (const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
  68. {
  69. graphics->setColour (getColour (color));
  70. graphics->fillEllipse (center.x - radius, center.y - radius,
  71. radius * 2.0f, radius * 2.0f);
  72. }
  73. void Box2DRenderer::DrawSegment (const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
  74. {
  75. graphics->setColour (getColour (color));
  76. graphics->drawLine (p1.x, p1.y, p2.x, p2.y, getLineThickness());
  77. }
  78. void Box2DRenderer::DrawTransform (const b2Transform&)
  79. {
  80. }