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.

105 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. Box2DRenderer::Box2DRenderer() noexcept : graphics (nullptr)
  19. {
  20. SetFlags (e_shapeBit);
  21. }
  22. void Box2DRenderer::render (Graphics& g, b2World& world,
  23. float left, float top, float right, float bottom,
  24. const Rectangle<float>& target)
  25. {
  26. graphics = &g;
  27. g.addTransform (AffineTransform::fromTargetPoints (left, top, target.getX(), target.getY(),
  28. right, top, target.getRight(), target.getY(),
  29. left, bottom, target.getX(), target.getBottom()));
  30. world.SetDebugDraw (this);
  31. world.DrawDebugData();
  32. }
  33. Colour Box2DRenderer::getColour (const b2Color& c) const
  34. {
  35. return Colour::fromFloatRGBA (c.r, c.g, c.b, 1.0f);
  36. }
  37. float Box2DRenderer::getLineThickness() const
  38. {
  39. return 0.1f;
  40. }
  41. static void createPath (Path& p, const b2Vec2* vertices, int32 vertexCount)
  42. {
  43. p.startNewSubPath (vertices[0].x, vertices[0].y);
  44. for (int i = 1; i < vertexCount; ++i)
  45. p.lineTo (vertices[i].x, vertices[i].y);
  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& color)
  69. {
  70. graphics->setColour (getColour (color));
  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. }