Skip to content

Commit 1bc2113

Browse files
committed
fixes
1 parent b288fdd commit 1bc2113

File tree

6 files changed

+59
-326
lines changed

6 files changed

+59
-326
lines changed

samples/draw.c

Lines changed: 16 additions & 290 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ b2Vec2 ConvertWorldToScreen( Camera* camera, b2Vec2 worldPoint )
109109
// Convert from world coordinates to normalized device coordinates.
110110
// http://www.songho.ca/opengl/gl_projectionmatrix.html
111111
// This also includes the view transform
112-
void BuildProjectionMatrix( Camera* camera, float* m, float zBias )
112+
static void BuildProjectionMatrix( Camera* camera, float* m, float zBias )
113113
{
114114
float ratio = camera->m_width / camera->m_height;
115115
b2Vec2 extents = { camera->m_zoom * ratio, camera->m_zoom };
@@ -140,26 +140,26 @@ void BuildProjectionMatrix( Camera* camera, float* m, float zBias )
140140
m[15] = 1.0f;
141141
}
142142

143-
void BuildProjectionMatrixNoZoom( Camera* camera, float* m )
143+
static void MakeOrthographicMatrix( float* m, float left, float right, float bottom, float top, float near, float far )
144144
{
145-
m[0] = camera->m_height / camera->m_width;
145+
m[0] = 2.0f / (right - left);
146146
m[1] = 0.0f;
147147
m[2] = 0.0f;
148148
m[3] = 0.0f;
149149

150150
m[4] = 0.0f;
151-
m[5] = 1.0f;
151+
m[5] = 2.0f / (top - bottom);
152152
m[6] = 0.0f;
153153
m[7] = 0.0f;
154154

155155
m[8] = 0.0f;
156156
m[9] = 0.0f;
157-
m[10] = -1.0f;
157+
m[10] = -2.0f / (far - near);
158158
m[11] = 0.0f;
159159

160-
m[12] = 0.0f;
161-
m[13] = 0.0f;
162-
m[14] = 0.0f;
160+
m[12] = -( right + left ) / ( right - left );
161+
m[13] = -( top + bottom ) / ( top - bottom );
162+
m[14] = -( far + near ) / ( far - near );
163163
m[15] = 1.0f;
164164
}
165165

@@ -207,7 +207,7 @@ typedef struct
207207
uint32_t m_programId;
208208
} Font;
209209

210-
Font CreateFont( int fontSize, const char* trueTypeFile )
210+
Font CreateFont( const char* trueTypeFile, float fontSize )
211211
{
212212
Font font = { 0 };
213213

@@ -342,13 +342,13 @@ void AddText( Font* font, float x, float y, b2HexColor color, const char* text )
342342
void FlushText( Font* font, Camera* camera )
343343
{
344344
float projectionMatrix[16];
345-
BuildProjectionMatrixNoZoom( camera, projectionMatrix );
345+
MakeOrthographicMatrix( projectionMatrix, 0.0f, camera->m_width, camera->m_height, 0.0f, -1.0f, 1.0f );
346346

347347
glUseProgram( font->m_programId );
348348

349349
glEnable( GL_BLEND );
350350
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
351-
glDisable( GL_DEPTH_TEST );
351+
//glDisable( GL_DEPTH_TEST );
352352

353353
int slot = 0;
354354
glActiveTexture( GL_TEXTURE0 + slot );
@@ -389,7 +389,7 @@ void FlushText( Font* font, Camera* camera )
389389
glBindTexture( GL_TEXTURE_2D, 0 );
390390

391391
glDisable( GL_BLEND );
392-
glEnable( GL_DEPTH_TEST );
392+
//glEnable( GL_DEPTH_TEST );
393393

394394
CheckOpenGL();
395395

@@ -1391,7 +1391,7 @@ Draw* CreateDraw( void )
13911391
draw->m_circles = CreateSolidCircles();
13921392
draw->m_capsules = CreateCapsules();
13931393
draw->m_polygons = CreatePolygons();
1394-
draw->font = CreateFont( 13, "samples/data/droid_sans.ttf" );
1394+
draw->font = CreateFont( "samples/data/droid_sans.ttf", 18.0f );
13951395
return draw;
13961396
}
13971397

@@ -1474,7 +1474,7 @@ void DrawBounds( Draw* draw, b2AABB aabb, b2HexColor color )
14741474
AddLine( &draw->m_lines, p4, p1, color );
14751475
}
14761476

1477-
void DrawScreenString( Draw* draw, int x, int y, const char* string, ... )
1477+
void DrawScreenString( Draw* draw, float x, float y, b2HexColor color, const char* string, ... )
14781478
{
14791479
char buffer[256];
14801480
va_list arg;
@@ -1483,7 +1483,7 @@ void DrawScreenString( Draw* draw, int x, int y, const char* string, ... )
14831483
va_end( arg );
14841484

14851485
buffer[255] = 0;
1486-
AddText( &draw->font, (float)x, (float)y, b2_colorWhite, buffer );
1486+
AddText( &draw->font, x, y, color, buffer );
14871487
}
14881488

14891489
void DrawWorldString( Draw* draw, Camera* camera, b2Vec2 p, b2HexColor color, const char* string, ... )
@@ -1509,285 +1509,11 @@ void FlushDraw( Draw* draw, Camera* camera )
15091509
FlushCircles( &draw->m_hollowCircles, camera );
15101510
FlushLines( &draw->m_lines, camera );
15111511
FlushPoints( &draw->m_points, camera );
1512+
FlushText( &draw->font, camera );
15121513
CheckOpenGL();
15131514
}
15141515

15151516
void DrawBackground( Draw* draw, Camera* camera )
15161517
{
15171518
RenderBackground( &draw->m_background, camera );
15181519
}
1519-
1520-
#if 0
1521-
void DrawPolygonFcn( const b2Vec2* vertices, int vertexCount, b2HexColor color, void* context )
1522-
{
1523-
static_cast<Draw*>( context )->DrawPolygon( vertices, vertexCount, color );
1524-
}
1525-
1526-
void DrawSolidPolygonFcn( b2Transform transform, const b2Vec2* vertices, int vertexCount, float radius, b2HexColor color,
1527-
void* context )
1528-
{
1529-
static_cast<Draw*>( context )->DrawSolidPolygon( transform, vertices, vertexCount, radius, color );
1530-
}
1531-
1532-
void DrawCircleFcn( b2Vec2 center, float radius, b2HexColor color, void* context )
1533-
{
1534-
static_cast<Draw*>( context )->DrawCircle( center, radius, color );
1535-
}
1536-
1537-
void DrawSolidCircleFcn( b2Transform transform, float radius, b2HexColor color, void* context )
1538-
{
1539-
static_cast<Draw*>( context )->DrawSolidCircle( transform, b2Vec2_zero, radius, color );
1540-
}
1541-
1542-
void DrawSolidCapsuleFcn( b2Vec2 p1, b2Vec2 p2, float radius, b2HexColor color, void* context )
1543-
{
1544-
static_cast<Draw*>( context )->DrawSolidCapsule( p1, p2, radius, color );
1545-
}
1546-
1547-
void DrawSegmentFcn( b2Vec2 p1, b2Vec2 p2, b2HexColor color, void* context )
1548-
{
1549-
static_cast<Draw*>( context )->DrawLine( p1, p2, color );
1550-
}
1551-
1552-
void DrawTransformFcn( b2Transform transform, void* context )
1553-
{
1554-
static_cast<Draw*>( context )->DrawTransform( transform );
1555-
}
1556-
1557-
void DrawPointFcn( b2Vec2 p, float size, b2HexColor color, void* context )
1558-
{
1559-
static_cast<Draw*>( context )->DrawPoint( p, size, color );
1560-
}
1561-
1562-
void DrawStringFcn( b2Vec2 p, const char* s, b2HexColor color, void* context )
1563-
{
1564-
static_cast<Draw*>( context )->DrawString( p, s );
1565-
}
1566-
1567-
Draw::Draw()
1568-
{
1569-
m_camera = NULL;
1570-
m_showUI = true;
1571-
m_points = NULL;
1572-
m_lines = NULL;
1573-
m_circles = NULL;
1574-
m_solidCircles = NULL;
1575-
m_solidCapsules = NULL;
1576-
m_solidPolygons = NULL;
1577-
m_debugDraw = {};
1578-
m_regularFont = NULL;
1579-
m_mediumFont = NULL;
1580-
m_largeFont = NULL;
1581-
m_background = NULL;
1582-
}
1583-
1584-
Draw::~Draw()
1585-
{
1586-
assert( m_points == NULL );
1587-
assert( m_lines == NULL );
1588-
assert( m_circles == NULL );
1589-
assert( m_solidCircles == NULL );
1590-
assert( m_solidCapsules == NULL );
1591-
assert( m_solidPolygons == NULL );
1592-
assert( m_background == NULL );
1593-
}
1594-
1595-
void Draw::Create( Camera* camera )
1596-
{
1597-
m_camera = camera;
1598-
m_background = new Background;
1599-
m_background->Create();
1600-
m_points = new PointRender;
1601-
m_points->Create();
1602-
m_lines = new GLLines;
1603-
m_lines->Create();
1604-
m_circles = new CircleRender;
1605-
m_circles->Create();
1606-
m_solidCircles = new GLSolidCircles;
1607-
m_solidCircles->Create();
1608-
m_solidCapsules = new GLSolidCapsules;
1609-
m_solidCapsules->Create();
1610-
m_solidPolygons = new GLSolidPolygons;
1611-
m_solidPolygons->Create();
1612-
1613-
b2AABB bounds = { { -FLT_MAX, -FLT_MAX }, { FLT_MAX, FLT_MAX } };
1614-
1615-
m_debugDraw = {};
1616-
1617-
m_debugDraw.DrawPolygonFcn = DrawPolygonFcn;
1618-
m_debugDraw.DrawSolidPolygonFcn = DrawSolidPolygonFcn;
1619-
m_debugDraw.DrawCircleFcn = DrawCircleFcn;
1620-
m_debugDraw.DrawSolidCircleFcn = DrawSolidCircleFcn;
1621-
m_debugDraw.DrawSolidCapsuleFcn = DrawSolidCapsuleFcn;
1622-
m_debugDraw.DrawSegmentFcn = DrawSegmentFcn;
1623-
m_debugDraw.DrawTransformFcn = DrawTransformFcn;
1624-
m_debugDraw.DrawPointFcn = DrawPointFcn;
1625-
m_debugDraw.DrawStringFcn = DrawStringFcn;
1626-
m_debugDraw.drawingBounds = bounds;
1627-
1628-
m_debugDraw.drawShapes = true;
1629-
m_debugDraw.drawJoints = true;
1630-
m_debugDraw.drawJointExtras = false;
1631-
m_debugDraw.drawBounds = false;
1632-
m_debugDraw.drawMass = false;
1633-
m_debugDraw.drawContacts = false;
1634-
m_debugDraw.drawGraphColors = false;
1635-
m_debugDraw.drawContactNormals = false;
1636-
m_debugDraw.drawContactForces = false;
1637-
m_debugDraw.drawContactFeatures = false;
1638-
m_debugDraw.drawFrictionForces = false;
1639-
m_debugDraw.drawIslands = false;
1640-
1641-
m_debugDraw.context = this;
1642-
}
1643-
1644-
void Draw::Destroy()
1645-
{
1646-
m_background->Destroy();
1647-
delete m_background;
1648-
m_background = NULL;
1649-
1650-
m_points->Destroy();
1651-
delete m_points;
1652-
m_points = NULL;
1653-
1654-
m_lines->Destroy();
1655-
delete m_lines;
1656-
m_lines = NULL;
1657-
1658-
m_circles->Destroy();
1659-
delete m_circles;
1660-
m_circles = NULL;
1661-
1662-
m_solidCircles->Destroy();
1663-
delete m_solidCircles;
1664-
m_solidCircles = NULL;
1665-
1666-
m_solidCapsules->Destroy();
1667-
delete m_solidCapsules;
1668-
m_solidCapsules = NULL;
1669-
1670-
m_solidPolygons->Destroy();
1671-
delete m_solidPolygons;
1672-
m_solidPolygons = NULL;
1673-
}
1674-
1675-
void Draw::DrawPolygon( const b2Vec2* vertices, int vertexCount, b2HexColor color )
1676-
{
1677-
b2Vec2 p1 = vertices[vertexCount - 1];
1678-
for ( int i = 0; i < vertexCount; ++i )
1679-
{
1680-
b2Vec2 p2 = vertices[i];
1681-
m_lines->AddLine( p1, p2, color );
1682-
p1 = p2;
1683-
}
1684-
}
1685-
1686-
void Draw::DrawSolidPolygon( b2Transform transform, const b2Vec2* vertices, int vertexCount, float radius, b2HexColor color )
1687-
{
1688-
m_solidPolygons->AddPolygon( transform, vertices, vertexCount, radius, color );
1689-
}
1690-
1691-
void Draw::DrawCircle( b2Vec2 center, float radius, b2HexColor color )
1692-
{
1693-
m_circles->AddCircle( center, radius, color );
1694-
}
1695-
1696-
void Draw::DrawSolidCircle( b2Transform transform, b2Vec2 center, float radius, b2HexColor color )
1697-
{
1698-
transform.p = b2TransformPoint( transform, center );
1699-
m_solidCircles->AddCircle( transform, radius, color );
1700-
}
1701-
1702-
void Draw::DrawSolidCapsule( b2Vec2 p1, b2Vec2 p2, float radius, b2HexColor color )
1703-
{
1704-
m_solidCapsules->AddCapsule( p1, p2, radius, color );
1705-
}
1706-
1707-
void Draw::DrawLine( b2Vec2 p1, b2Vec2 p2, b2HexColor color )
1708-
{
1709-
m_lines->AddLine( p1, p2, color );
1710-
}
1711-
1712-
void Draw::DrawTransform( b2Transform transform )
1713-
{
1714-
const float k_axisScale = 0.2f;
1715-
b2Vec2 p1 = transform.p;
1716-
1717-
b2Vec2 p2 = b2MulAdd( p1, k_axisScale, b2Rot_GetXAxis( transform.q ) );
1718-
m_lines->AddLine( p1, p2, b2_colorRed );
1719-
1720-
p2 = b2MulAdd( p1, k_axisScale, b2Rot_GetYAxis( transform.q ) );
1721-
m_lines->AddLine( p1, p2, b2_colorGreen );
1722-
}
1723-
1724-
void Draw::DrawPoint( b2Vec2 p, float size, b2HexColor color )
1725-
{
1726-
#ifdef __APPLE__
1727-
size *= 2.0f;
1728-
#endif
1729-
m_points->AddPoint( p, size, color );
1730-
}
1731-
1732-
void Draw::DrawString( int x, int y, const char* string, ... )
1733-
{
1734-
// if (m_showUI == false)
1735-
//{
1736-
// return;
1737-
// }
1738-
1739-
va_list arg;
1740-
va_start( arg, string );
1741-
ImGui::Begin( "Overlay", NULL,
1742-
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize |
1743-
ImGuiWindowFlags_NoScrollbar );
1744-
ImGui::SetCursorPos( ImVec2( float( x ), float( y ) ) );
1745-
ImGui::TextColoredV( ImColor( 230, 153, 153, 255 ), string, arg );
1746-
ImGui::End();
1747-
va_end( arg );
1748-
}
1749-
1750-
void Draw::DrawString( b2Vec2 p, const char* string, ... )
1751-
{
1752-
b2Vec2 ps = m_camera->ConvertWorldToScreen( p );
1753-
1754-
va_list arg;
1755-
va_start( arg, string );
1756-
ImGui::Begin( "Overlay", NULL,
1757-
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize |
1758-
ImGuiWindowFlags_NoScrollbar );
1759-
ImGui::SetCursorPos( ImVec2( ps.x, ps.y ) );
1760-
ImGui::TextColoredV( ImColor( 230, 230, 230, 255 ), string, arg );
1761-
ImGui::End();
1762-
va_end( arg );
1763-
}
1764-
1765-
void Draw::DrawBounds( b2AABB aabb, b2HexColor c )
1766-
{
1767-
b2Vec2 p1 = aabb.lowerBound;
1768-
b2Vec2 p2 = { aabb.upperBound.x, aabb.lowerBound.y };
1769-
b2Vec2 p3 = aabb.upperBound;
1770-
b2Vec2 p4 = { aabb.lowerBound.x, aabb.upperBound.y };
1771-
1772-
m_lines->AddLine( p1, p2, c );
1773-
m_lines->AddLine( p2, p3, c );
1774-
m_lines->AddLine( p3, p4, c );
1775-
m_lines->AddLine( p4, p1, c );
1776-
}
1777-
1778-
void Draw::Flush()
1779-
{
1780-
m_solidCircles->Flush( m_camera );
1781-
m_solidCapsules->Flush( m_camera );
1782-
m_solidPolygons->Flush( m_camera );
1783-
m_circles->Flush( m_camera );
1784-
m_lines->Flush( m_camera );
1785-
m_points->Flush( m_camera );
1786-
CheckOpenGL();
1787-
}
1788-
1789-
void Draw::DrawBackground()
1790-
{
1791-
m_background->Draw( m_camera );
1792-
}
1793-
#endif

samples/draw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void DrawSolidPolygon( Draw* draw, b2Transform transform, const b2Vec2* vertices
4545
void DrawTransform( Draw* draw, b2Transform transform, float scale );
4646
void DrawBounds( Draw* draw, b2AABB aabb, b2HexColor color );
4747

48-
void DrawScreenString( Draw* draw, int x, int y, const char* string, ... );
48+
void DrawScreenString( Draw* draw, float x, float y, b2HexColor color, const char* string, ... );
4949
void DrawWorldString( Draw* draw, Camera* camera, b2Vec2 p, b2HexColor color, const char* string, ... );
5050

5151

0 commit comments

Comments
 (0)