/
FrameBufferTurtleSpaceRenderer.cpp
408 lines (314 loc) · 11.1 KB
/
FrameBufferTurtleSpaceRenderer.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* TOBY -- An abstract interpreter engine and system for learning.
* Copyright (C) 1999 Ryan C. Gordon.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "platform/renderers/fbrenderer/FrameBufferTurtleSpaceRenderer.h"
#include "SDL.h" // !!!
FrameBufferTurtleSpaceRenderer::FrameBufferTurtleSpaceRenderer(void)
: TurtleSpaceRenderer(), defaultSurface(NULL)
{
} // Constructor
FrameBufferTurtleSpaceRenderer::~FrameBufferTurtleSpaceRenderer(void)
{
deleteSurface();
} // Destructor
bool FrameBufferTurtleSpaceRenderer::resize(int _w, int _h)
{
if (screenResize(_w, _h) == false)
return(false);
toby_uint32 *oldbuf = NULL;
toby_uint32 *p = getSurface();
// need an extra copy so we don't createSurface() twice in a row.
if (p != NULL)
{
oldbuf = new toby_uint32[w * h];
memcpy(oldbuf, p, (w * h) * sizeof (toby_uint32));
deleteSurface();
} // if
createSurface(_w, _h);
p = getSurface();
if (p == NULL)
{
if (oldbuf == NULL)
delete[] oldbuf;
return(false);
} // if
blankPixels(p, _w, _h);
if (oldbuf != NULL)
{
// !!! copy old framebuffer to new, centered.
delete[] oldbuf;
} // if
w = _w;
h = _h;
return(true);
} // FrameBufferTurtleSpaceRenderer::resize
void FrameBufferTurtleSpaceRenderer::notifyGrabbed(void)
{
} // FrameBufferTurtleSpaceRenderer::notifyGrabbed(void)
void FrameBufferTurtleSpaceRenderer::notifyUngrabbed(void)
{
} // FrameBufferTurtleSpaceRenderer::notifyUngrabbed
void FrameBufferTurtleSpaceRenderer::blankPixels(toby_uint32 *p, int _w, int _h)
{
// !!! This could optimize, I bet.
int max = _w * _h;
for (int i = 0; i < max; i++)
p[i] = 0xFF000000; // Full alpha, no red or green or blue.
} // FrameBufferTurtleSpaceRenderer::blankPixels
double FrameBufferTurtleSpaceRenderer::getTurtleSpaceWidth(void)
{
return(w);
} // FrameBufferTurtleSpaceRenderer::getTurtleSpaceWidth
double FrameBufferTurtleSpaceRenderer::getTurtleSpaceHeight(void)
{
return(h);
} // FrameBufferTurtleSpaceRenderer::getTurtleSpaceHeight
double FrameBufferTurtleSpaceRenderer::getDesiredTurtleWidth(void)
{
return(w * 0.02);
} // FrameBufferTurtleSpaceRenderer::getDesiredTurtleWidth
double FrameBufferTurtleSpaceRenderer::getDesiredTurtleHeight(void)
{
return(getDesiredTurtleWidth());
} // FrameBufferTurtleSpaceRenderer::getDesiredTurtleHeight
void FrameBufferTurtleSpaceRenderer::renderString(double x, double y,
double angle,
float r, float b,
float g, float a,
const char *str)
{
} // FrameBufferTurtleSpaceRenderer::renderString
#define LINEBLITCHUNKS 10
void FrameBufferTurtleSpaceRenderer::renderLine(double _x1, double _y1,
double _x2, double _y2,
float r, float b,
float g, float a)
{
// This is a standard Bresenham line-drawing algorithm, but the specific
// implementation was borrowed, optimized, and mangled from
// SGE's DoLine code:
// http://www.etek.chalmers.se/~e8cal1/sge/
toby_uint32 pval = constructPixelValue(r, g, b, a);
int x1 = TobyGeometry::roundDoubleToInt(_x1);
int y1 = TobyGeometry::roundDoubleToInt(_y1);
int x2 = TobyGeometry::roundDoubleToInt(_x2);
int y2 = TobyGeometry::roundDoubleToInt(_y2);
FrameBufferRect rects[LINEBLITCHUNKS];
toby_uint32 *p = getSurface();
_D(("LFB: rendering line...(%d, %d)-(%d, %d), (%f, %f, %f, %f)...\n",
x1, y1, x2, y2, r, g, b, a));
int dx, dy, sdx, sdy, x, y, px, py, incr;
dx = x2 - x1;
dy = y2 - y1;
sdx = (dx < 0) ? -1 : 1;
sdy = (dy < 0) ? -1 : 1;
dx = sdx * dx + 1;
dy = sdy * dy + 1;
x = y = 0;
px = x1;
py = y1;
if ((dx == 1) && (dy == 1))
{
_D(("LFB: Single pixel line fastpath.\n"));
*(((toby_uint32 *) p) + ((py * w) + px)) = pval;
putToScreen(p, px, py, 1, 1);
return;
} // if
else if (dx == 1)
{
_D(("LFB: Vertical line fastpath.\n"));
toby_uint32 *linep = ((toby_uint32 *) p) + ((py * w) + px);
incr = w * sdy;
for (y = 0; y < dy; y++)
{
*linep = pval;
linep += incr;
} // for
putToScreen(p, (x1 < x2) ? x1 : x2, (y1 < y2) ? y1 : y2, 1, dy);
return;
} // else if
else if (dy == 1)
{
_D(("LFB: Horizontal line fastpath.\n"));
toby_uint32 *linep = ((toby_uint32 *) p) + ((py * w) + px);
incr = sdx;
for (x = 0; x < dx; x++)
{
*linep = pval;
linep += incr;
} // for
putToScreen(p, (x1 < x2) ? x1 : x2, (y1 < y2) ? y1 : y2, dx, 1);
return;
} // else if
else if (dx >= dy)
{
_D(("LFB: Line with major axis of X.\n"));
int lastX = px;
int thisY = py;
int rectidx = 0;
for (x = 0; x < dx; x++)
{
*( ((toby_uint32 *) p) + ((py * w) + px) ) = pval;
y += dy;
if (y >= dx) // run length completed.
{
if (rectidx != -1)
{
rects[rectidx].x = (lastX < px) ? lastX : px;
rects[rectidx].y = thisY;
rects[rectidx].w = (px - lastX) * sdx;
rects[rectidx].h = 1;
rectidx++;
if (rectidx >= LINEBLITCHUNKS)
rectidx = -1;
} // if
y -= dx;
py += sdy;
lastX = px;
thisY = py;
} // if
px += sdx;
} // for
if (rectidx != -1)
putMultToScreen(p, rectidx, rects);
else
goto tobylfb_blit_slanted_line;
return;
} // else if
else
{
_D(("LFB: Line with major axis of Y.\n"));
int lastY = py;
int thisX = px;
int rectidx = 0;
for (y = 0; y < dy; y++)
{
*( ((toby_uint32 *) p) + ((py * w) + px) ) = pval;
x += dx;
if (x >= dy) // run length completed.
{
if (rectidx != -1)
{
rects[rectidx].x = thisX;
rects[rectidx].y = (lastY < py) ? lastY : py;
rects[rectidx].w = 1;
rects[rectidx].h = (py - lastY) * sdy;
rectidx++;
if (rectidx >= LINEBLITCHUNKS)
rectidx = -1;
} // if
x -= dy;
px += sdx;
lastY = py;
thisX = px;
} // if
py += sdy;
} // for
if (rectidx != -1)
putMultToScreen(p, rectidx, rects);
else
goto tobylfb_blit_slanted_line;
return;
} // else
assert(0); // should have return'd or goto'd by here.
tobylfb_blit_slanted_line:
// !!! This isn't rendering right in all cases.
float chunkX = ((float) dx) / ((float) LINEBLITCHUNKS);
float chunkY = ((float) dy) / ((float) LINEBLITCHUNKS);
float yincr;
float fx, fy;
fx = (float) ((x1 < x2) ? x1 : x2);
if ( ((sdx > 0) && (sdy > 0)) || // line from q1 to q4.
((sdx < 0) && (sdy < 0)) )
{
fy = (float) ((y1 < y2) ? y1 : y2);
yincr = chunkY;
} // if
else // line from q3 to q2.
{
fy = (float) (((y1 > y2) ? y1 : y2)) - chunkY;
yincr = -chunkY;
} // else
for (int i = 0; i < LINEBLITCHUNKS; i++)
{
rects[i].x = (int) (fx);
rects[i].y = (int) (fy);
rects[i].w = (int) (chunkX + 0.5);
rects[i].h = (int) (chunkY + 0.5);
if (rects[i].w == 0)
rects[i].w++;
if (rects[i].h == 0)
rects[i].h++;
fx += chunkX;
fy += yincr;
} // for
putMultToScreen(p, LINEBLITCHUNKS, rects);
} // FrameBufferTurtleSpaceRenderer::renderLine
void FrameBufferTurtleSpaceRenderer::renderTurtle(Turtle *turtle) throw (ExecException *)
{
_D(("LFB: renderTurtle() called. NOT IMPLEMENTED.\n"));
} // FrameBufferTurtleSpaceRenderer::renderTurtle
void FrameBufferTurtleSpaceRenderer::blankTurtle(Turtle *turtle) throw (ExecException *)
{
_D(("LFB: blankTurtle() called. NOT IMPLEMENTED.\n"));
} // FrameBufferTurtleSpaceRenderer::blankTurtle
void FrameBufferTurtleSpaceRenderer::cleanup(void) throw (ExecException *)
{
blankPixels(getSurface(), w, h);
} // FrameBufferTurtleSpaceRenderer::cleanup
inline toby_uint32 FrameBufferTurtleSpaceRenderer::constructPixelValue(
float r,
float g,
float b,
float a)
{
// !!! check this.
return(
(((int) (a * 255.0)) << 24) |
(((int) (r * 255.0)) << 16) |
(((int) (b * 255.0)) << 8) |
(((int) (g * 255.0)) )
);
#if 0
_D(("%f, %f, %f, %f == ", r, b, g, a));
for (unsigned int i = 1 << 31; i != 0; i >>= 1)
_D(("%d", (int) ((pval & i) / i)));
_D(("\n"));
#endif
} // FrameBufferTurtleSpaceRenderer::constructPixelValue
void FrameBufferTurtleSpaceRenderer::createSurface(int _w, int _h)
{
defaultSurface = new toby_uint32[_w * _h];
} // FrameBufferTurtleSpaceRenderer::createSurface
void FrameBufferTurtleSpaceRenderer::deleteSurface(void)
{
if (defaultSurface != NULL)
delete[] defaultSurface;
} // FrameBufferTurtleSpaceRenderer::deleteSurface
toby_uint32 *FrameBufferTurtleSpaceRenderer::getSurface(void)
{
return(defaultSurface);
} // FrameBufferTurtleSpaceRenderer::getSurface
void FrameBufferTurtleSpaceRenderer::putMultToScreen(toby_uint32 *pix,
int rectCount,
FrameBufferRect *rects)
{
for (int i = 0; i < rectCount; i++)
putToScreen(pix, rects[i].x, rects[i].y, rects[i].w, rects[i].h);
} // FrameBufferTurtleSpaceRenderer::putMultToScreen
// end of FrameBufferTurtleSpaceRenderer.cpp ...