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.

106 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. p.closeSubPath();
  46. }
  47. void Box2DRenderer::DrawPolygon (const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  48. {
  49. graphics->setColour (getColour (color));
  50. Path p;
  51. createPath (p, vertices, vertexCount);
  52. graphics->strokePath (p, PathStrokeType (getLineThickness()));
  53. }
  54. void Box2DRenderer::DrawSolidPolygon (const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  55. {
  56. graphics->setColour (getColour (color));
  57. Path p;
  58. createPath (p, vertices, vertexCount);
  59. graphics->fillPath (p);
  60. }
  61. void Box2DRenderer::DrawCircle (const b2Vec2& center, float32 radius, const b2Color& color)
  62. {
  63. graphics->setColour (getColour (color));
  64. graphics->drawEllipse (center.x - radius, center.y - radius,
  65. radius * 2.0f, radius * 2.0f,
  66. getLineThickness());
  67. }
  68. void Box2DRenderer::DrawSolidCircle (const b2Vec2& center, float32 radius, const b2Vec2& /*axis*/, const b2Color& colour)
  69. {
  70. graphics->setColour (getColour (colour));
  71. graphics->fillEllipse (center.x - radius, center.y - radius,
  72. radius * 2.0f, radius * 2.0f);
  73. }
  74. void Box2DRenderer::DrawSegment (const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
  75. {
  76. graphics->setColour (getColour (color));
  77. graphics->drawLine (p1.x, p1.y, p2.x, p2.y, getLineThickness());
  78. }
  79. void Box2DRenderer::DrawTransform (const b2Transform&)
  80. {
  81. }