b2DebugDraw class to Box2D with HGE

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
Code:
/*
* 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.h>
#include <hgevector.h>
#include <hgecolor.h>
#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
Code:
/*
* 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; ++i, j++)
   {
      //glVertex2f(vertices[i].x, vertices[i].y);
      j = (j>=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; ++i, j++)
   {
      //glVertex2f(vertices[i].x, vertices[i].y);
      j = (j>=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; i++)
   {
      //Calculate theta for this vertex
      Theta = i * WedgeAngle;
      
      //Compute X and Y locations
      X = (float)(center.x + radius * cos(Theta));
      Y = (float)(center.y - radius * sin(Theta));

      vertices.push_back(b2Vec2(X, Y));//Circle[i] = hgeVector(X, Y);
   }

   //hge->Gfx_BeginScene();
   for (int32 k = 0, j = 1; k < (signed)vertices.size(); ++k, ++j)
   {
      //glVertex2f(vertices[i].x, vertices[i].y);
      j = (j>=(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; i++)
   {
      //Calculate theta for this vertex
      Theta = i * WedgeAngle;
      
      //Compute X and Y locations
      X = (float)(center.x + radius * cos(Theta));
      Y = (float)(center.y - radius * sin(Theta));

      vertices.push_back(b2Vec2(X, Y));//Circle[i] = hgeVector(X, Y);
   }

   //hge->Gfx_BeginScene();
   
   for (int32 k = 0, j = 1; k < (signed)vertices.size(); ++k, ++j)
   {
      //glVertex2f(vertices[i].x, vertices[i].y);
      j = (j>=(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/3f ... gDraw.html
http://www.megaupload.com/pt/?d=DYSLH7RC
http://rapidshare.com/files/170951751/HGEDebugDraw.zip
http://sites.google.com/site/legionaryu ... ugDraw.zip

Yeah, I know! 😀 Lot of mirrors.

Thanks
I haven't tried this, as Im not an HGE user, but I know several people have wanted to use both engines, so thanks, I'm sure this will be really helpful.

How about you make a patch to put this in the Contrib/ directory? Possibly, a whole mini program that runs (one of) the Testbed tests, do demonstrate it's full use?
Hey BorisTheBrave,

Thats is a very good idea!
I'll convert the TestBed example from OGL to HGE.

Actually, the Box2D HelloWorld example is already converted.
Here is the code:
Code:
/*
* 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 "Box2D.h"
#include "hge.h"
#include "hgesprite.h"
#include "hgefont.h"
#include "hgeparticle.h"
#include "HGEDebugDraw.h"

HGEDebugDraw DebugDraw;

//Flags use to manage Drawing in Box2D
uint32 flagsDraw = HGEDebugDraw::e_shapeBit|HGEDebugDraw::e_jointBit|HGEDebugDraw::e_aabbBit;

b2World *world = NULL;
b2Body *body = NULL;
b2Body *groundBody = NULL;

// Prepare for simulation. Typically we use a time step of 1/60 of a
// second (60Hz) and 10 iterations. This provides a high quality simulation
// in most game scenarios.
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;

// Pointer to the HGE interface.
// Helper classes require this to work.
HGE *hge=0;

bool FrameFunc()
{
   float dt=hge->Timer_GetDelta();

   // Process keys
   if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
   
   // Moves the world
   world->Step(timeStep, iterations);

   return false;
}

bool RenderFunc()
{
   // Set the flags needed for Drawing
   DebugDraw.SetFlags(flagsDraw);
   hge->Gfx_BeginScene();
   hge->Gfx_Clear(0);

   // Draw the stopped world
   world->Step(0,0);

   hge->Gfx_EndScene();

   // Clean the flags to don't Draw
   DebugDraw.SetFlags(0);
   return false;
}

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
   hge = hgeCreate(HGE_VERSION);

   hge->System_SetState(HGE_LOGFILE, "TestBedHGE.log");
   hge->System_SetState(HGE_HIDEMOUSE, false);
   hge->System_SetState(HGE_USESOUND, false);
   hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
   hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
   hge->System_SetState(HGE_TITLE, "TestBedHGE");
   hge->System_SetState(HGE_FPS, 100);
   hge->System_SetState(HGE_WINDOWED, true);
   hge->System_SetState(HGE_SCREENWIDTH, 800);
   hge->System_SetState(HGE_SCREENHEIGHT, 600);
   hge->System_SetState(HGE_SCREENBPP, 32);

   b2AABB worldAABB;
   worldAABB.lowerBound.Set(-100.0f, -100.0f);
   worldAABB.upperBound.Set(100.0f, 100.0f);

   // Define the gravity vector.
   b2Vec2 gravity(0.0f, -10.0f);

   // Do we want to let bodies sleep?
   bool doSleep = true;

   // Construct a world object, which will hold and simulate the rigid bodies.
   world = new b2World(worldAABB, gravity, doSleep);

   // Define the ground body.
   b2BodyDef groundBodyDef;
   groundBodyDef.position.Set(0.0f, -90.0f);

   // Call the body factory which allocates memory for the ground body
   // from a pool and creates the ground box shape (also from a pool).
   // The body is also added to the world.
   groundBody = world->CreateBody(&groundBodyDef);

   // Define the ground box shape.
   b2PolygonDef groundShapeDef;//b2PolygonDef groundShapeDef;

   // The extents are the half-widths of the box.
   groundShapeDef.SetAsBox(50.0f, 10.0f);

   // Add the ground shape to the ground body.
   groundBody->CreateShape(&groundShapeDef);

   // Define the dynamic body. We set its position and call the body factory.
   b2BodyDef bodyDef;
   bodyDef.position.Set(0.0f, 90.0f);
   body = world->CreateBody(&bodyDef);

   // Define another box shape for our dynamic body.
   b2CircleDef shapeDef;//b2PolygonDef shapeDef;
   shapeDef.radius = 4.0f;

   // Set the box density to be non-zero, so it will be dynamic.
   shapeDef.density = 1.0f;

   // Override the default friction.
   shapeDef.friction = 0.3f;

   shapeDef.restitution = 0.2f;

   // Add the shape to the body.
   body->CreateShape(&shapeDef);

   // Now tell the dynamic body to compute it's mass properties base
   // on its shape.
   body->SetMassFromShapes();

   // Now set hge pointer for DebugDraw class
   DebugDraw.SetHGE(hge);
   // We need to clean the current flags
   DebugDraw.SetFlags(0);

   world->SetDebugDraw(&DebugDraw);

   if(hge->System_Initiate()) {
      
      // Let's rock now!
      hge->System_Start();
   }

   delete world;

   // Clean up and shutdown
   hge->System_Shutdown();
   hge->Release();
   return 0;
}


All ideas are wellcome!

Original thread: https://box2d.org/forum/viewtopic.php?t=1873