b2DebugDraw to Box2D with HGE
  • LegionaryuLegionaryu December 2008
    Hi guys,

    I'm going to use Box2D and HGE in my future projects, and I can't find anything about b2DebugDraw Class used in HGE.

    Then I put myself in the front of my computer and do this little thing, HGEDebugDraw class.

    This class is supposed to be used like DebugDraw class in Box2D TestBed examples.

    Here the code of HGEDebugDraw.h
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    /*
    * Copyright (c) 2008 Vitaliano Palmieri Neto
    *
    * This software is provided 'as-is', without any express or implied
    * warranty. In no event will the authors be held liable for any damages
    * arising from the use of this software.
    * Permission is granted to anyone to use this software for any purpose,
    * including commercial applications, and to alter it and redistribute it
    * freely, subject to the following restrictions:
    * 1. The origin of this software must not be misrepresented; you must not
    * claim that you wrote the original software. If you use this software
    * in a product, an acknowledgment in the product documentation would be
    * appreciated but is not required.
    * 2. Altered source versions must be plainly marked as such, and must not be
    * misrepresented as being the original software.
    * 3. This notice may not be removed or altered from any source distribution.
    */

     
    #ifndef RENDER_H
    #define RENDER_H
     
    #include "Box2D.h"
    #include <hge>
    #include <hgevector>
    #include <hgecolor>
    #include <iostream>
    #include <vector>
     
    struct b2AABB;
     
    class HGEDebugDraw :
    public b2DebugDraw
    {
    private:
    HGE* hge;
    public:
    HGEDebugDraw(){};
    HGEDebugDraw(HGE* draw):hge(draw){};
    void SetHGE(HGE* draw){ hge = draw;};
    void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
     
    void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
     
    void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);
     
    void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);
     
    void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
     
    void DrawXForm(const b2XForm& xf);
    };
     
    void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color);
    void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
     
    void DrawString(int x, int y, const char* string, ...);
    void DrawAABB(b2AABB* aabb, const b2Color& color);
     
    #endif


    And here the code of HGEDebugDraw.cpp
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    /*
    * Copyright (c) 2008 Vitaliano Palmieri Neto
    *
    * This software is provided 'as-is', without any express or implied
    * warranty. In no event will the authors be held liable for any damages
    * arising from the use of this software.
    * Permission is granted to anyone to use this software for any purpose,
    * including commercial applications, and to alter it and redistribute it
    * freely, subject to the following restrictions:
    * 1. The origin of this software must not be misrepresented; you must not
    * claim that you wrote the original software. If you use this software
    * in a product, an acknowledgment in the product documentation would be
    * appreciated but is not required.
    * 2. Altered source versions must be plainly marked as such, and must not be
    * misrepresented as being the original software.
    * 3. This notice may not be removed or altered from any source distribution.
    */

     
    #include "HGEDebugDraw.h"
     
    void HGEDebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
    {
    if(!hge) return;
     
    float adjustW = hge->System_GetState(HGE_SCREENWIDTH)/2.0f;
    float adjustH = hge->System_GetState(HGE_SCREENHEIGHT)/2.0f;
     
    //hge->Gfx_BeginScene();
    for (int32 i = 0, j = 1; i <vertexCount>=vertexCount)?0:j;
    hge->Gfx_RenderLine(adjustW + vertices[i].x, adjustH - vertices[i].y, adjustW + vertices[j].x, adjustH - vertices[j].y, ARGB(255,color.r*255,color.g*255,color.b*255));
    }
    //hge->Gfx_EndScene();
    }
     
    void HGEDebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
    {
    if(!hge) return;
     
    float adjustW = hge->System_GetState(HGE_SCREENWIDTH)/2.0f;
    float adjustH = hge->System_GetState(HGE_SCREENHEIGHT)/2.0f;
     
    //hge->Gfx_BeginScene();
    for (int32 i = 0, j = 1; i <vertexCount>=vertexCount)?0:j;
    hge->Gfx_RenderLine(adjustW + vertices[i].x, adjustH - vertices[i].y, adjustW + vertices[j].x, adjustH - vertices[j].y, ARGB(200,color.r*128,color.g*128,color.b*128));
    }
    //hge->Gfx_EndScene();
    }
     
    void HGEDebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
    {
    if(!hge) return;
     
    float adjustW = hge->System_GetState(HGE_SCREENWIDTH)/2.0f;
    float adjustH = hge->System_GetState(HGE_SCREENHEIGHT)/2.0f;
     
    int NUMPOINTS = 24+(int)radius/20;//24;
    const float PI = 3.14159f;
     
    std::vector<b2Vec2> vertices;
    //hgeVector Circle[NUMPOINTS + 1];
    int i;//,j;
    float X;
    float Y;
    float Theta;
    float WedgeAngle; //Size of angle between two points on the circle (single wedge)
     
    //Precompute WedgeAngle
    WedgeAngle = (float)((2*PI) / NUMPOINTS);
     
    //Set up vertices for a circle
    //Used <= in the for statement to ensure last point meets first point (closed circle)
    for(i=0; i<NUMPOINTS>Gfx_BeginScene();
    for (int32 k = 0, j = 1; k <signed>=(signed)vertices.size())?0:j;
    hge->Gfx_RenderLine(adjustW + vertices[k].x, adjustH - vertices[k].y, adjustW + vertices[j].x, adjustH - vertices[j].y, ARGB(255,color.r*255,color.g*255,color.b*255));
    }
    //hge->Gfx_EndScene();
    }
     
    void HGEDebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
    {
    if(!hge) return;
     
    float adjustW = hge->System_GetState(HGE_SCREENWIDTH)/2.0f;
    float adjustH = hge->System_GetState(HGE_SCREENHEIGHT)/2.0f;
     
    int NUMPOINTS = 24+(int)radius/20;//24;
    const float PI = 3.14159f;
     
    std::vector<b2Vec2> vertices;
    //hgeVector Circle[NUMPOINTS + 1];
    int i;//,j;
    float X;
    float Y;
    float Theta;
    float WedgeAngle; //Size of angle between two points on the circle (single wedge)
     
    //Precompute WedgeAngle
    WedgeAngle = (float)((2*PI) / NUMPOINTS);
     
    //Set up vertices for a circle
    //Used <= in the for statement to ensure last point meets first point (closed circle)
    for(i=0; i<NUMPOINTS>Gfx_BeginScene();
     
    for (int32 k = 0, j = 1; k <signed>=(signed)vertices.size())?0:j;
    hge->Gfx_RenderLine(adjustW + vertices[k].x, adjustH - vertices[k].y, adjustW + vertices[j].x, adjustH - vertices[j].y, ARGB(200,color.r*128,color.g*128,color.b*128));
    }
    //hge->Gfx_EndScene();
    }
     
    void HGEDebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
    {
    if(!hge) return;
     
    float adjustW = hge->System_GetState(HGE_SCREENWIDTH)/2.0f;
    float adjustH = hge->System_GetState(HGE_SCREENHEIGHT)/2.0f;
     
    //hge->Gfx_BeginScene();
    hge->Gfx_RenderLine(adjustW + p1.x, adjustH - p1.y, adjustW + p2.x, adjustH - p2.y, ARGB(255,color.r*255,color.g*255,color.b*255));
    //hge->Gfx_EndScene();
    }
     
    void HGEDebugDraw::DrawXForm(const b2XForm& xf)
    {
    if(!hge) return;
     
    float adjustW = hge->System_GetState(HGE_SCREENWIDTH)/2.0f;
    float adjustH = hge->System_GetState(HGE_SCREENHEIGHT)/2.0f;
     
    b2Vec2 p1 = xf.position, p2;
    const float32 k_axisScale = 0.4f;
    //hge->Gfx_BeginScene();
     
    p2 = p1 + k_axisScale * xf.R.col1;
    hge->Gfx_RenderLine(adjustW + p1.x, adjustH - p1.y, adjustW + p2.x, adjustH - p2.y, ARGB(255,255,0,0));
     
    p2 = p1 + k_axisScale * xf.R.col2;
    hge->Gfx_RenderLine(adjustW + p1.x, adjustH - p1.y, adjustW + p2.x, adjustH - p2.y, ARGB(255,0,255,0));
     
    //hge->Gfx_EndScene();
    }


    Also have an example, the link is above (source included):

    http://www.4shared.com/file/74801132/3f7e12af/HGEDebugDraw.html
    http://www.megaupload.com/pt/?d=DYSLH7RC
    http://rapidshare.com/files/170951751/HGEDebugDraw.zip
    http://sites.google.com/site/legionaryu/files/HGEDebugDraw.zip

    Yeah, I know! :D Lot of mirrors.

    Thanks
  • SoulSoul July 2009
    thanks )

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Apply for Membership

In this Discussion

Who's Online (1)