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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright (c) 2006-2013 Erin Catto http://www.box2d.org
*
* 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 "DebugDraw.h"

#include <stdio.h>
#include <stdarg.h>
#include <algorithm>
#include <initializer_list>

#include <fsAppCore/renderer/fsCLine.h>
#include <fsAppCore/src/fsCEntity.h>
#include <fsAppCore/src/fsCAppCoreMain.h>

#include <gfHogCore/gfIHogLevel.h>
#include <fsCore/src/fsNCoreUtils.h>
#include <fsAppCore/fsINode.h>
#include <fsAppCore/fsIDisplay.h>

#define BUFFER_OFFSET(x)  ((const void*) (x))

DebugDraw g_debugDraw;
Camera g_camera;

//
b2Vec2 Camera::ConvertScreenToWorld(const b2Vec2& ps)
{
    float32 w = float32(m_width);
    float32 h = float32(m_height);
	float32 u = ps.x / w;
	float32 v = (h - ps.y) / h;

	float32 ratio = w / h;
	b2Vec2 extents(ratio * 25.0f, 25.0f);
	extents *= m_zoom;

	b2Vec2 lower = m_center - extents;
	b2Vec2 upper = m_center + extents;

	b2Vec2 pw;
	pw.x = (1.0f - u) * lower.x + u * upper.x;
	pw.y = (1.0f - v) * lower.y + v * upper.y;
	return pw;
}

//
b2Vec2 Camera::ConvertWorldToScreen(const b2Vec2& pw)
{
	float32 w = float32(m_width);
	float32 h = float32(m_height);
	float32 ratio = w / h;
	b2Vec2 extents(ratio * 25.0f, 25.0f);
	extents *= m_zoom;

	b2Vec2 lower = m_center - extents;
	b2Vec2 upper = m_center + extents;

	float32 u = (pw.x - lower.x) / (upper.x - lower.x);
	float32 v = (pw.y - lower.y) / (upper.y - lower.y);

	b2Vec2 ps;
	ps.x = u * w;
	ps.y = (1.0f - v) * h;
	return ps;
}

// Convert from world coordinates to normalized device coordinates.
// http://www.songho.ca/opengl/gl_projectionmatrix.html
void Camera::BuildProjectionMatrix(float32* m, float32 zBias)
{
	float32 w = float32(m_width);
	float32 h = float32(m_height);
	float32 ratio = w / h;
	b2Vec2 extents(ratio * 25.0f, 25.0f);
	extents *= m_zoom;

	b2Vec2 lower = m_center - extents;
	b2Vec2 upper = m_center + extents;

	m[0] = 2.0f / (upper.x - lower.x);
	m[1] = 0.0f;
	m[2] = 0.0f;
	m[3] = 0.0f;

	m[4] = 0.0f;
	m[5] = 2.0f / (upper.y - lower.y);
	m[6] = 0.0f;
	m[7] = 0.0f;

	m[8] = 0.0f;
	m[9] = 0.0f;
	m[10] = 1.0f;
	m[11] = 0.0f;

	m[12] = -(upper.x + lower.x) / (upper.x - lower.x);
	m[13] = -(upper.y + lower.y) / (upper.y - lower.y);
	m[14] = zBias;
	m[15] = 1.0f;
}

//
DebugDraw::DebugDraw()
{
}

//
DebugDraw::~DebugDraw()
{
}

//
void DebugDraw::Create()
{	
	for (int i = 0; i < 128 ; ++i)
	{
		fsCLine* line = new fsCLine(nullptr);
		mLines.emplace_back(line);
	}
	mCurrentLine = mLines.begin();
}

//
void DebugDraw::Destroy()
{
	fsNCoreUtils::clearSequenceContainerOfPtrs(mLines);
}

//
void DebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
}

//
void DebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
	fsVec2d v[8];

#if defined fsPLATFORM_CX
	fsS32 flipY = -1;
#else
	fsS32 flipY = 1;
#endif


	v[0].x = v[7].x = vertices[0].x * 115.f;
	v[0].y = v[7].y = flipY * vertices[0].y * 115.f;
	v[1].x = v[2].x = vertices[1].x * 115.f;
	v[1].y = v[2].y = flipY * vertices[1].y * 115.f;
	v[3].x = v[4].x = vertices[2].x * 115.f;
	v[3].y = v[4].y = flipY * vertices[2].y * 115.f;
	v[5].x = v[6].x = vertices[3].x * 115.f;
	v[5].y = v[6].y = flipY * vertices[3].y * 115.f;

	(*mCurrentLine)->vertSet(v, 8);

	if (fsCEntity* const hogLevel = fsCAppCoreMain::instanceGet()->sceneGet()->firstEntityWith<gfIHogLevel>())
	{
		(*mCurrentLine)->matrixApply(hogLevel->renderMatrixGet());
	}

	(*mCurrentLine)->matrixApply(fsCAppCoreMain::instanceGet()->displayGet()->renderMatrixGet());
	(*mCurrentLine)->colourSet(fsCColour(0.0f, 1.0f, 0.0f, 1.0f));

	(*mCurrentLine)->render();
	++mCurrentLine;

	if (mCurrentLine == mLines.end())
	{
		mCurrentLine = mLines.begin();
	}
}

//
void DebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
}

//
void DebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
}

//
void DebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
}

//
void DebugDraw::DrawTransform(const b2Transform& xf)
{
}

//
void DebugDraw::DrawPoint(const b2Vec2& p, float32 size, const b2Color& color)
{
}

//
void DebugDraw::DrawString(int x, int y, const char *string, ...)<--- Either there is a missing 'override', or the member function 'DebugDraw::DrawString' can be static.
{
}

//
void DebugDraw::DrawString(const b2Vec2& pw, const char *string, ...)<--- Either there is a missing 'override', or the member function 'DebugDraw::DrawString' can be static.
{
}

//
void DebugDraw::DrawAABB(b2AABB* aabb, const b2Color& c)<--- Either there is a missing 'override', or the member function 'DebugDraw::DrawAABB' can be static.
{
}

//
void DebugDraw::Flush()
{
	mCurrentLine = mLines.begin();
	return;
	std::for_each(std::begin(mLines), std::end(mLines),<--- Statements following 'return' will never be executed.
		[this](fsCLine* line)
	{
		line->render();
//		mLines[mCurrentLine]->vertSet(nullptr, 0);

	});

}