forked from cyxx/rawgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics_soft.cpp
459 lines (410 loc) · 12 KB
/
graphics_soft.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Another World engine rewrite
* Copyright (C) 2004-2005 Gregory Montoir ([email protected])
*/
#include <math.h>
#include "graphics.h"
#include "util.h"
#include "screenshot.h"
#include "systemstub.h"
struct GraphicsSoft: Graphics {
typedef void (GraphicsSoft::*drawLine)(int16_t x1, int16_t x2, int16_t y, uint8_t col);
uint8_t *_pagePtrs[4];
uint8_t *_drawPagePtr;
int _u, _v;
int _w, _h;
int _byteDepth;
Color _pal[16];
uint16_t *_colorBuffer;
int _screenshotNum;
GraphicsSoft();
~GraphicsSoft();
int xScale(int x) const { return (x * _u) >> 16; }
int yScale(int y) const { return (y * _v) >> 16; }
void setSize(int w, int h);
void drawPolygon(uint8_t color, const QuadStrip &qs);
void drawChar(uint8_t c, uint16_t x, uint16_t y, uint8_t color);
void drawPoint(int16_t x, int16_t y, uint8_t color);
void drawLineT(int16_t x1, int16_t x2, int16_t y, uint8_t color);
void drawLineN(int16_t x1, int16_t x2, int16_t y, uint8_t color);
void drawLineP(int16_t x1, int16_t x2, int16_t y, uint8_t color);
uint8_t *getPagePtr(uint8_t page);
int getPageSize() const { return _w * _h * _byteDepth; }
void setWorkPagePtr(uint8_t page);
virtual void init(int targetW, int targetH);
virtual void setFont(const uint8_t *src, int w, int h);
virtual void setPalette(const Color *colors, int count);
virtual void setSpriteAtlas(const uint8_t *src, int w, int h, int xSize, int ySize);
virtual void drawSprite(int buffer, int num, const Point *pt);
virtual void drawBitmap(int buffer, const uint8_t *data, int w, int h, int fmt);
virtual void drawPoint(int buffer, uint8_t color, const Point *pt);
virtual void drawQuadStrip(int buffer, uint8_t color, const QuadStrip *qs);
virtual void drawStringChar(int buffer, uint8_t color, char c, const Point *pt);
virtual void clearBuffer(int num, uint8_t color);
virtual void copyBuffer(int dst, int src, int vscroll = 0);
virtual void drawBuffer(int num, SystemStub *stub);
virtual void drawRect(int num, uint8_t color, const Point *pt, int w, int h);
};
GraphicsSoft::GraphicsSoft() {
_fixUpPalette = FIXUP_PALETTE_NONE;
memset(_pagePtrs, 0, sizeof(_pagePtrs));
_colorBuffer = 0;
memset(_pal, 0, sizeof(_pal));
_screenshotNum = 1;
}
GraphicsSoft::~GraphicsSoft() {
for (int i = 0; i < 4; ++i) {
free(_pagePtrs[i]);
_pagePtrs[i] = 0;
}
free(_colorBuffer);
}
void GraphicsSoft::setSize(int w, int h) {
_u = (w << 16) / GFX_W;
_v = (h << 16) / GFX_H;
_w = w;
_h = h;
_byteDepth = _use555 ? 2 : 1;
assert(_byteDepth == 1 || _byteDepth == 2);
_colorBuffer = (uint16_t *)realloc(_colorBuffer, _w * _h * sizeof(uint16_t));
if (!_colorBuffer) {
error("Unable to allocate color buffer w %d h %d", _w, _h);
}
for (int i = 0; i < 4; ++i) {
_pagePtrs[i] = (uint8_t *)realloc(_pagePtrs[i], getPageSize());
if (!_pagePtrs[i]) {
error("Not enough memory to allocate offscreen buffers");
}
memset(_pagePtrs[i], 0, getPageSize());
}
setWorkPagePtr(2);
}
static uint32_t calcStep(const Point &p1, const Point &p2, uint16_t &dy) {
dy = p2.y - p1.y;
uint16_t delta = (dy == 0) ? 1 : dy;
return ((p2.x - p1.x) << 16) / delta;
}
void GraphicsSoft::drawPolygon(uint8_t color, const QuadStrip &quadStrip) {
QuadStrip qs = quadStrip;
if (_w != GFX_W || _h != GFX_H) {
for (int i = 0; i < qs.numVertices; ++i) {
qs.vertices[i].scale(_u, _v);
}
}
int i = 0;
int j = qs.numVertices - 1;
int16_t x2 = qs.vertices[i].x;
int16_t x1 = qs.vertices[j].x;
int16_t hliney = MIN(qs.vertices[i].y, qs.vertices[j].y);
++i;
--j;
drawLine pdl;
switch (color) {
default:
pdl = &GraphicsSoft::drawLineN;
break;
case COL_PAGE:
pdl = &GraphicsSoft::drawLineP;
break;
case COL_ALPHA:
pdl = &GraphicsSoft::drawLineT;
break;
}
uint32_t cpt1 = x1 << 16;
uint32_t cpt2 = x2 << 16;
int numVertices = qs.numVertices;
while (1) {
numVertices -= 2;
if (numVertices == 0) {
return;
}
uint16_t h;
uint32_t step1 = calcStep(qs.vertices[j + 1], qs.vertices[j], h);
uint32_t step2 = calcStep(qs.vertices[i - 1], qs.vertices[i], h);
++i;
--j;
cpt1 = (cpt1 & 0xFFFF0000) | 0x7FFF;
cpt2 = (cpt2 & 0xFFFF0000) | 0x8000;
if (h == 0) {
cpt1 += step1;
cpt2 += step2;
} else {
while (h--) {
if (hliney >= 0) {
x1 = cpt1 >> 16;
x2 = cpt2 >> 16;
if (x1 < _w && x2 >= 0) {
if (x1 < 0) x1 = 0;
if (x2 >= _w) x2 = _w - 1;
(this->*pdl)(x1, x2, hliney, color);
}
}
cpt1 += step1;
cpt2 += step2;
++hliney;
if (hliney >= _h) return;
}
}
}
}
void GraphicsSoft::drawChar(uint8_t c, uint16_t x, uint16_t y, uint8_t color) {
if (x <= GFX_W - 8 && y <= GFX_H - 8) {
x = xScale(x);
y = yScale(y);
const uint8_t *ft = _font + (c - 0x20) * 8;
const int offset = (x + y * _w) * _byteDepth;
if (_byteDepth == 1) {
for (int j = 0; j < 8; ++j) {
const uint8_t ch = ft[j];
for (int i = 0; i < 8; ++i) {
if (ch & (1 << (7 - i))) {
_drawPagePtr[offset + j * _w + i] = color;
}
}
}
} else if (_byteDepth == 2) {
const uint16_t rgbColor = _pal[color].rgb555();
for (int j = 0; j < 8; ++j) {
const uint8_t ch = ft[j];
for (int i = 0; i < 8; ++i) {
if (ch & (1 << (7 - i))) {
((uint16_t *)(_drawPagePtr + offset))[j * _w + i] = rgbColor;
}
}
}
}
}
}
static void blend_rgb555(uint16_t *dst, const uint16_t b) {
static const uint16_t RB_MASK = 0x7c1f;
static const uint16_t G_MASK = 0x03e0;
uint16_t a = *dst;
if ((a & 0x8000) == 0) { // use bit 15 to prevent additive blending
uint16_t r = 0x8000;
r |= (((a & RB_MASK) + (b & RB_MASK)) >> 1) & RB_MASK;
r |= (((a & G_MASK) + (b & G_MASK)) >> 1) & G_MASK;
*dst = r;
}
}
void GraphicsSoft::drawPoint(int16_t x, int16_t y, uint8_t color) {
x = xScale(x);
y = yScale(y);
const int offset = (y * _w + x) * _byteDepth;
if (_byteDepth == 1) {
switch (color) {
case COL_ALPHA:
_drawPagePtr[offset] |= 8;
break;
case COL_PAGE:
_drawPagePtr[offset] = *(_pagePtrs[0] + offset);
break;
default:
_drawPagePtr[offset] = color;
break;
}
} else if (_byteDepth == 2) {
switch (color) {
case COL_ALPHA:
blend_rgb555((uint16_t *)(_drawPagePtr + offset), _pal[ALPHA_COLOR_INDEX].rgb555());
break;
case COL_PAGE:
*(uint16_t *)(_drawPagePtr + offset) = *(uint16_t *)(_pagePtrs[0] + offset);
break;
default:
*(uint16_t *)(_drawPagePtr + offset) = _pal[color].rgb555();
break;
}
}
}
void GraphicsSoft::drawLineT(int16_t x1, int16_t x2, int16_t y, uint8_t color) {
int16_t xmax = MAX(x1, x2);
int16_t xmin = MIN(x1, x2);
int w = xmax - xmin + 1;
const int offset = (y * _w + xmin) * _byteDepth;
if (_byteDepth == 1) {
for (int i = 0; i < w; ++i) {
_drawPagePtr[offset + i] |= 8;
}
} else if (_byteDepth == 2) {
const uint16_t rgbColor = _pal[ALPHA_COLOR_INDEX].rgb555();
uint16_t *p = (uint16_t *)(_drawPagePtr + offset);
for (int i = 0; i < w; ++i) {
blend_rgb555(p + i, rgbColor);
}
}
}
void GraphicsSoft::drawLineN(int16_t x1, int16_t x2, int16_t y, uint8_t color) {
int16_t xmax = MAX(x1, x2);
int16_t xmin = MIN(x1, x2);
const int w = xmax - xmin + 1;
const int offset = (y * _w + xmin) * _byteDepth;
if (_byteDepth == 1) {
memset(_drawPagePtr + offset, color, w);
} else if (_byteDepth == 2) {
const uint16_t rgbColor = _pal[color].rgb555();
uint16_t *p = (uint16_t *)(_drawPagePtr + offset);
for (int i = 0; i < w; ++i) {
p[i] = rgbColor;
}
}
}
void GraphicsSoft::drawLineP(int16_t x1, int16_t x2, int16_t y, uint8_t color) {
if (_drawPagePtr == _pagePtrs[0]) {
return;
}
int16_t xmax = MAX(x1, x2);
int16_t xmin = MIN(x1, x2);
const int w = xmax - xmin + 1;
const int offset = (y * _w + xmin) * _byteDepth;
memcpy(_drawPagePtr + offset, _pagePtrs[0] + offset, w * _byteDepth);
}
uint8_t *GraphicsSoft::getPagePtr(uint8_t page) {
assert(page >= 0 && page < 4);
return _pagePtrs[page];
}
void GraphicsSoft::setWorkPagePtr(uint8_t page) {
_drawPagePtr = getPagePtr(page);
}
void GraphicsSoft::init(int targetW, int targetH) {
Graphics::init(targetW, targetH);
setSize(targetW, targetH);
}
void GraphicsSoft::setFont(const uint8_t *src, int w, int h) {
if (_is1991) {
// no-op for 1991
}
}
void GraphicsSoft::setPalette(const Color *colors, int count) {
memcpy(_pal, colors, sizeof(Color) * MIN(count, 16));
}
void GraphicsSoft::setSpriteAtlas(const uint8_t *src, int w, int h, int xSize, int ySize) {
if (_is1991) {
// no-op for 1991
}
}
void GraphicsSoft::drawSprite(int buffer, int num, const Point *pt) {
if (_is1991) {
// no-op for 1991
}
}
void GraphicsSoft::drawBitmap(int buffer, const uint8_t *data, int w, int h, int fmt) {
switch (_byteDepth) {
case 1:
if (fmt == FMT_CLUT && _w == w && _h == h) {
memcpy(getPagePtr(buffer), data, w * h);
}
break;
case 2:
if (fmt == FMT_RGB555 && _w == w && _h == h) {
memcpy(getPagePtr(buffer), data, getPageSize());
}
break;
}
}
void GraphicsSoft::drawPoint(int buffer, uint8_t color, const Point *pt) {
setWorkPagePtr(buffer);
drawPoint(pt->x, pt->y, color);
}
void GraphicsSoft::drawQuadStrip(int buffer, uint8_t color, const QuadStrip *qs) {
setWorkPagePtr(buffer);
drawPolygon(color, *qs);
}
void GraphicsSoft::drawStringChar(int buffer, uint8_t color, char c, const Point *pt) {
setWorkPagePtr(buffer);
drawChar(c, pt->x, pt->y, color);
}
void GraphicsSoft::clearBuffer(int num, uint8_t color) {
if (_byteDepth == 1) {
memset(getPagePtr(num), color, getPageSize());
} else if (_byteDepth == 2) {
const uint16_t rgbColor = _pal[color].rgb555();
uint16_t *p = (uint16_t *)getPagePtr(num);
for (int i = 0; i < _w * _h; ++i) {
p[i] = rgbColor;
}
}
}
void GraphicsSoft::copyBuffer(int dst, int src, int vscroll) {
if (vscroll == 0) {
memcpy(getPagePtr(dst), getPagePtr(src), getPageSize());
} else if (vscroll >= -199 && vscroll <= 199) {
const int dy = yScale(vscroll);
if (dy < 0) {
memcpy(getPagePtr(dst), getPagePtr(src) - dy * _w * _byteDepth, (_h + dy) * _w * _byteDepth);
} else {
memcpy(getPagePtr(dst) + dy * _w * _byteDepth, getPagePtr(src), (_h - dy) * _w * _byteDepth);
}
}
}
static void dumpBuffer555(const uint16_t *src, int w, int h, int num) {
char name[32];
snprintf(name, sizeof(name), "screenshot-%d.tga", num);
saveTGA(name, src, w, h);
debug(DBG_INFO, "Written '%s'", name);
}
static void dumpPalette555(uint16_t *dst, int w, const Color *pal) {
static const int SZ = 16;
for (int color = 0; color < 16; ++color) {
uint16_t *p = dst + (color & 7) * SZ;
for (int y = 0; y < SZ; ++y) {
for (int x = 0; x < SZ; ++x) {
p[x] = pal[color].rgb555();
}
p += w;
}
if (color == 7) {
dst += SZ * w;
}
}
}
void GraphicsSoft::drawBuffer(int num, SystemStub *stub) {
int w, h;
float ar[4];
stub->prepareScreen(w, h, ar);
if (_byteDepth == 1) {
const uint8_t *src = getPagePtr(num);
for (int i = 0; i < _w * _h; ++i) {
_colorBuffer[i] = _pal[src[i]].rgb555();
}
if (0) {
dumpPalette555(_colorBuffer, _w, _pal);
}
stub->setScreenPixels555(_colorBuffer, _w, _h);
if (_screenshot) {
dumpBuffer555(_colorBuffer, _w, _h, _screenshotNum);
++_screenshotNum;
_screenshot = false;
}
} else if (_byteDepth == 2) {
const uint16_t *src = (uint16_t *)getPagePtr(num);
stub->setScreenPixels555(src, _w, _h);
if (_screenshot) {
dumpBuffer555(src, _w, _h, _screenshotNum);
++_screenshotNum;
_screenshot = false;
}
}
stub->updateScreen();
}
void GraphicsSoft::drawRect(int num, uint8_t color, const Point *pt, int w, int h) {
assert(_byteDepth == 2);
setWorkPagePtr(num);
const uint16_t rgbColor = _pal[color].rgb555();
const int x1 = xScale(pt->x);
const int y1 = yScale(pt->y);
const int x2 = xScale(pt->x + w - 1);
const int y2 = yScale(pt->y + h - 1);
// horizontal
for (int x = x1; x <= x2; ++x) {
*(uint16_t *)(_drawPagePtr + (y1 * _w + x) * _byteDepth) = rgbColor;
*(uint16_t *)(_drawPagePtr + (y2 * _w + x) * _byteDepth) = rgbColor;
}
// vertical
for (int y = y1; y <= y2; ++y) {
*(uint16_t *)(_drawPagePtr + (y * _w + x1) * _byteDepth) = rgbColor;
*(uint16_t *)(_drawPagePtr + (y * _w + x2) * _byteDepth) = rgbColor;
}
}
Graphics *GraphicsSoft_create() {
return new GraphicsSoft();
}