1 #include "PhysicsDebugDrawer.h"
2 #include "../SharedDefines.h"
3 #include "../Scene/SceneNodes.h"
4 
5 #include <SDL2/SDL2_gfxPrimitives.h>
6 
7 // Conversion warnings from Box2D -> SDL2_gfx
8 #pragma warning(disable: 4244)
9 
PhysicsDebugDrawer()10 PhysicsDebugDrawer::PhysicsDebugDrawer()
11 {
12     SetFlags(b2Draw::e_shapeBit);
13 }
14 
DrawPolygon(const b2Vec2 * vertices,int32 vertexCount,const b2Color & color)15 void PhysicsDebugDrawer::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
16 {
17     Point cameraPos = m_pCamera->GetPosition();
18 
19     std::vector<int16> vaX;
20     std::vector<int16> vaY;
21 
22     for (int32 vertexIdx = 0; vertexIdx < vertexCount; vertexIdx++)
23     {
24         b2Vec2 worldPos = MetersToPixels(vertices[vertexIdx]);
25 
26         vaX.push_back(worldPos.x - cameraPos.x);
27         vaY.push_back(worldPos.y - cameraPos.y);
28     }
29 
30     polygonRGBA(m_pRenderer, vaX.data(), vaY.data(), vertexCount, color.r * 255, color.g * 255, color.b * 255, color.a * 255);
31 }
32 
33 // This is ~20 times more cpu taxing than DrawPolygon
DrawSolidPolygon(const b2Vec2 * vertices,int32 vertexCount,const b2Color & color)34 void PhysicsDebugDrawer::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
35 {
36     // For now I guess
37     DrawPolygon(vertices, vertexCount, color);
38 
39     /*Point cameraPos = m_pCamera->GetPosition();
40 
41     std::vector<int16> vaX;
42     std::vector<int16> vaY;
43 
44     for (int32 vertexIdx = 0; vertexIdx < vertexCount; vertexIdx++)
45     {
46         vaX.push_back(vertices[vertexIdx].x - cameraPos.x);
47         vaY.push_back(vertices[vertexIdx].y - cameraPos.y);
48     }
49 
50     filledPolygonRGBA(m_pRenderer, vaX.data(), vaY.data(), vertexCount, color.r * 255, color.g * 255, color.b * 255, (color.a * 255) * 0.5);*/
51 }
52 
DrawCircle(const b2Vec2 & center,float32 radius,const b2Color & color)53 void PhysicsDebugDrawer::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
54 {
55     Point cameraPos = m_pCamera->GetPosition();
56     b2Vec2 worldPos = MetersToPixels(center);
57     float worldRadius = MetersToPixels(radius);
58 
59     circleRGBA(m_pRenderer, worldPos.x - cameraPos.x, worldPos.y - cameraPos.y, worldRadius, color.r * 255, color.g * 255, color.b * 255, color.a * 255);
60 }
61 
DrawSolidCircle(const b2Vec2 & center,float32 radius,const b2Vec2 & axis,const b2Color & color)62 void PhysicsDebugDrawer::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
63 {
64     DrawCircle(center, radius, color);
65 }
66 
DrawSegment(const b2Vec2 & p1,const b2Vec2 & p2,const b2Color & color)67 void PhysicsDebugDrawer::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
68 {
69 
70 }
71 
DrawTransform(const b2Transform & xf)72 void PhysicsDebugDrawer::DrawTransform(const b2Transform& xf)
73 {
74 
75 }
76 
DrawPoint(const b2Vec2 & p,float32 size,const b2Color & color)77 void PhysicsDebugDrawer::DrawPoint(const b2Vec2& p, float32 size, const b2Color& color)
78 {
79     SDL_SetRenderDrawColor(m_pRenderer, color.r * 255, color.g, color.b * 255, color.a * 255);
80 
81     SDL_RenderDrawPoint(m_pRenderer, p.x, p.y);
82 }
83 
PrepareForDraw(SDL_Renderer * pRenderer,std::shared_ptr<CameraNode> pCamera)84 void PhysicsDebugDrawer::PrepareForDraw(SDL_Renderer* pRenderer, std::shared_ptr<CameraNode> pCamera)
85 {
86     assert(pRenderer);
87     assert(pCamera);
88 
89     m_pRenderer = pRenderer;
90     m_pCamera = pCamera;
91 }