Debian Codegraph @

RecastDemo/Source/SampleInterfaces.cpp

raw symbols SCIP
1 #include <math.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include "SampleInterfaces.h"
5 #include "Recast.h"
6 #include "RecastDebugDraw.h"
7 #include "DetourDebugDraw.h"
8 #include "PerfTimer.h"
9 #include "SDL.h"
10 #include "SDL_opengl.h"
11
12 #ifdef WIN32
13 # define snprintf _snprintf
14 #endif
15
16 ////////////////////////////////////////////////////////////////////////////////////////////////////
17
18 BuildContext::BuildContext() :
19 m_messageCount(0),
20 m_textPoolSize(0)
21 {
22 memset(m_messages, 0, sizeof(char*) * MAX_MESSAGES);
23
24 resetTimers();
25 }
26
27 // Virtual functions for custom implementations.
28 void BuildContext::doResetLog()
29 {
30 m_messageCount = 0;
31 m_textPoolSize = 0;
32 }
33
34 void BuildContext::doLog(const rcLogCategory category, const char* msg, const int len)
35 {
36 if (!len) return;
37 if (m_messageCount >= MAX_MESSAGES)
38 return;
39 char* dst = &m_textPool[m_textPoolSize];
40 int n = TEXT_POOL_SIZE - m_textPoolSize;
41 if (n < 2)
42 return;
43 char* cat = dst;
44 char* text = dst+1;
45 const int maxtext = n-1;
46 // Store category
47 *cat = (char)category;
48 // Store message
49 const int count = rcMin(len+1, maxtext);
50 memcpy(text, msg, count);
51 text[count-1] = '\0';
52 m_textPoolSize += 1 + count;
53 m_messages[m_messageCount++] = dst;
54 }
55
56 void BuildContext::doResetTimers()
57 {
58 for (int i = 0; i < RC_MAX_TIMERS; ++i)
59 m_accTime[i] = -1;
60 }
61
62 void BuildContext::doStartTimer(const rcTimerLabel label)
63 {
64 m_startTime[label] = getPerfTime();
65 }
66
67 void BuildContext::doStopTimer(const rcTimerLabel label)
68 {
69 const TimeVal endTime = getPerfTime();
70 const TimeVal deltaTime = endTime - m_startTime[label];
71 if (m_accTime[label] == -1)
72 m_accTime[label] = deltaTime;
73 else
74 m_accTime[label] += deltaTime;
75 }
76
77 int BuildContext::doGetAccumulatedTime(const rcTimerLabel label) const
78 {
79 return getPerfTimeUsec(m_accTime[label]);
80 }
81
82 void BuildContext::dumpLog(const char* format, ...)
83 {
84 // Print header.
85 va_list ap;
86 va_start(ap, format);
87 vprintf(format, ap);
88 va_end(ap);
89 printf("\n");
90
91 // Print messages
92 const int TAB_STOPS[4] = { 28, 36, 44, 52 };
93 for (int i = 0; i < m_messageCount; ++i)
94 {
95 const char* msg = m_messages[i]+1;
96 int n = 0;
97 while (*msg)
98 {
99 if (*msg == '\t')
100 {
101 int count = 1;
102 for (int j = 0; j < 4; ++j)
103 {
104 if (n < TAB_STOPS[j])
105 {
106 count = TAB_STOPS[j] - n;
107 break;
108 }
109 }
110 while (--count)
111 {
112 putchar(' ');
113 n++;
114 }
115 }
116 else
117 {
118 putchar(*msg);
119 n++;
120 }
121 msg++;
122 }
123 putchar('\n');
124 }
125 }
126
127 int BuildContext::getLogCount() const
128 {
129 return m_messageCount;
130 }
131
132 const char* BuildContext::getLogText(const int i) const
133 {
134 return m_messages[i]+1;
135 }
136
137 ////////////////////////////////////////////////////////////////////////////////////////////////////
138
139 class GLCheckerTexture
140 {
141 unsigned int m_texId;
142 public:
143 GLCheckerTexture() : m_texId(0)
144 {
145 }
146
147 ~GLCheckerTexture()
148 {
149 if (m_texId != 0)
150 glDeleteTextures(1, &m_texId);
151 }
152 void bind()
153 {
154 if (m_texId == 0)
155 {
156 // Create checker pattern.
157 const unsigned int col0 = duRGBA(215,215,215,255);
158 const unsigned int col1 = duRGBA(255,255,255,255);
159 static const int TSIZE = 64;
160 unsigned int data[TSIZE*TSIZE];
161
162 glGenTextures(1, &m_texId);
163 glBindTexture(GL_TEXTURE_2D, m_texId);
164
165 int level = 0;
166 int size = TSIZE;
167 while (size > 0)
168 {
169 for (int y = 0; y < size; ++y)
170 for (int x = 0; x < size; ++x)
171 data[x+y*size] = (x==0 || y==0) ? col0 : col1;
172 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, size,size, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
173 size /= 2;
174 level++;
175 }
176
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
179 }
180 else
181 {
182 glBindTexture(GL_TEXTURE_2D, m_texId);
183 }
184 }
185 };
186 static GLCheckerTexture g_tex;
187
188
189 void DebugDrawGL::depthMask(bool state)
190 {
191 glDepthMask(state ? GL_TRUE : GL_FALSE);
192 }
193
194 void DebugDrawGL::texture(bool state)
195 {
196 if (state)
197 {
198 glEnable(GL_TEXTURE_2D);
199 g_tex.bind();
200 }
201 else
202 {
203 glDisable(GL_TEXTURE_2D);
204 }
205 }
206
207 void DebugDrawGL::begin(duDebugDrawPrimitives prim, float size)
208 {
209 switch (prim)
210 {
211 case DU_DRAW_POINTS:
212 glPointSize(size);
213 glBegin(GL_POINTS);
214 break;
215 case DU_DRAW_LINES:
216 glLineWidth(size);
217 glBegin(GL_LINES);
218 break;
219 case DU_DRAW_TRIS:
220 glBegin(GL_TRIANGLES);
221 break;
222 case DU_DRAW_QUADS:
223 glBegin(GL_QUADS);
224 break;
225 };
226 }
227
228 void DebugDrawGL::vertex(const float* pos, unsigned int color)
229 {
230 glColor4ubv((GLubyte*)&color);
231 glVertex3fv(pos);
232 }
233
234 void DebugDrawGL::vertex(const float x, const float y, const float z, unsigned int color)
235 {
236 glColor4ubv((GLubyte*)&color);
237 glVertex3f(x,y,z);
238 }
239
240 void DebugDrawGL::vertex(const float* pos, unsigned int color, const float* uv)
241 {
242 glColor4ubv((GLubyte*)&color);
243 glTexCoord2fv(uv);
244 glVertex3fv(pos);
245 }
246
247 void DebugDrawGL::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v)
248 {
249 glColor4ubv((GLubyte*)&color);
250 glTexCoord2f(u,v);
251 glVertex3f(x,y,z);
252 }
253
254 void DebugDrawGL::end()
255 {
256 glEnd();
257 glLineWidth(1.0f);
258 glPointSize(1.0f);
259 }
260
261 ////////////////////////////////////////////////////////////////////////////////////////////////////
262
263 FileIO::FileIO() :
264 m_fp(0),
265 m_mode(-1)
266 {
267 }
268
269 FileIO::~FileIO()
270 {
271 if (m_fp) fclose(m_fp);
272 }
273
274 bool FileIO::openForWrite(const char* path)
275 {
276 if (m_fp) return false;
277 m_fp = fopen(path, "wb");
278 if (!m_fp) return false;
279 m_mode = 1;
280 return true;
281 }
282
283 bool FileIO::openForRead(const char* path)
284 {
285 if (m_fp) return false;
286 m_fp = fopen(path, "rb");
287 if (!m_fp) return false;
288 m_mode = 2;
289 return true;
290 }
291
292 bool FileIO::isWriting() const
293 {
294 return m_mode == 1;
295 }
296
297 bool FileIO::isReading() const
298 {
299 return m_mode == 2;
300 }
301
302 bool FileIO::write(const void* ptr, const size_t size)
303 {
304 if (!m_fp || m_mode != 1) return false;
305 fwrite(ptr, size, 1, m_fp);
306 return true;
307 }
308
309 bool FileIO::read(void* ptr, const size_t size)
310 {
311 if (!m_fp || m_mode != 2) return false;
312 size_t readLen = fread(ptr, size, 1, m_fp);
313 return readLen == 1;
314 }
315
316