-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.cpp
313 lines (256 loc) · 8.97 KB
/
render.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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL: https://www.switchlink.se/svn/picture/screen.cpp $
* $Id: screen.cpp 33 2008-09-17 10:59:23Z johndoe $
*
*/
#include "common/system.h"
#include "picture/picture.h"
#include "picture/render.h"
#include "picture/resource.h"
namespace Picture {
Common::Rect makeRect(int16 x, int16 y, int16 width, int16 height) {
Common::Rect rect;
rect.left = x;
rect.top = y;
rect.setWidth(width);
rect.setHeight(height);
return rect;
}
RenderQueue::RenderQueue(PictureEngine *vm) : _vm(vm) {
_currQueue = new RenderQueueArray();
_prevQueue = new RenderQueueArray();
_updateUta = new MicroTileArray(640, 400);
}
RenderQueue::~RenderQueue() {
delete _currQueue;
delete _prevQueue;
delete _updateUta;
}
void RenderQueue::addSprite(SpriteDrawItem &sprite) {
RenderQueueItem item;
item.type = kSprite;
item.flags = kRefresh;
item.rect = makeRect(sprite.x - _vm->_cameraX, sprite.y - _vm->_cameraY, sprite.width, sprite.height);
item.priority = sprite.priority;
item.sprite = sprite;
item.sprite.x -= _vm->_cameraX;
item.sprite.y -= _vm->_cameraY;
// Add sprite sorted by priority
RenderQueueArray::iterator iter = _currQueue->begin();
while (iter != _currQueue->end() && (*iter).priority <= item.priority) {
iter++;
}
_currQueue->insert(iter, item);
}
void RenderQueue::addText(int16 x, int16 y, byte color, uint fontResIndex, byte *text, int len) {
Font font(_vm->_res->load(fontResIndex)->data);
RenderQueueItem item;
item.type = kText;
item.flags = kRefresh;
item.rect = makeRect(x, y, font.getTextWidth(text), font.getHeight());
item.priority = 1000;
item.text.color = color;
item.text.fontResIndex = fontResIndex;
item.text.text = text;
item.text.len = len;
_currQueue->push_back(item);
}
void RenderQueue::addMask(SegmapMaskRect &mask) {
RenderQueueItem item;
item.type = kMask;
item.flags = kRefresh;
item.rect = makeRect(mask.x - _vm->_cameraX, mask.y - _vm->_cameraY, mask.width, mask.height);
item.priority = mask.priority;
item.mask = mask;
// Only add the mask if a sprite intersects its rect
if (rectIntersectsItem(item.rect)) {
RenderQueueArray::iterator iter = _currQueue->begin();
while (iter != _currQueue->end() && (*iter).priority <= item.priority) {
iter++;
}
_currQueue->insert(iter, item);
}
}
void RenderQueue::update() {
bool doFullRefresh = _vm->_screen->_fullRefresh;
_updateUta->clear();
if (!doFullRefresh) {
for (RenderQueueArray::iterator iter = _currQueue->begin(); iter != _currQueue->end(); iter++) {
RenderQueueItem *item = &(*iter);
RenderQueueItem *prevItem = findItemInQueue(_prevQueue, *item);
if (prevItem) {
if (hasItemChanged(*prevItem, *item)) {
item->flags = kRefresh;
addDirtyRect(prevItem->rect);
} else {
item->flags = kUnchanged;
}
} else {
item->flags = kRefresh;
}
}
for (RenderQueueArray::iterator iter = _prevQueue->begin(); iter != _prevQueue->end(); iter++) {
RenderQueueItem *prevItem = &(*iter);
RenderQueueItem *item = findItemInQueue(_currQueue, *prevItem);
if (!item) {
prevItem->flags = kRemoved;
addDirtyRect(prevItem->rect);
}
}
restoreDirtyBackground();
for (RenderQueueArray::iterator iter = _currQueue->begin(); iter != _currQueue->end(); iter++) {
RenderQueueItem *item = &(*iter);
if (item->flags != kUnchanged)
invalidateItemsByRect(item->rect, item);
}
} else {
byte *destp = _vm->_screen->_frontScreen;
byte *srcp = _vm->_screen->_backScreen + _vm->_cameraX + _vm->_cameraY * _vm->_sceneWidth;
int16 w = MIN<int16>(640, _vm->_sceneWidth);
int16 h = MIN<int16>(400, _vm->_cameraHeight);
while (h--) {
memcpy(destp, srcp, w);
destp += 640;
srcp += _vm->_sceneWidth;
}
_vm->_screen->_fullRefresh = false;
}
for (RenderQueueArray::iterator iter = _currQueue->begin(); iter != _currQueue->end(); iter++) {
const RenderQueueItem *item = &(*iter);
if (item->flags == kRefresh || doFullRefresh) {
switch (item->type) {
case kSprite:
_vm->_screen->drawSprite(item->sprite);
break;
case kText:
_vm->_screen->drawString(item->rect.left, item->rect.top, item->text.color, item->text.fontResIndex,
item->text.text, item->text.len, NULL, true);
break;
case kMask:
_vm->_screen->drawSurface(item->rect.left, item->rect.top, item->mask.surface);
break;
default:
break;
}
if (!doFullRefresh)
addDirtyRect(item->rect);
}
}
if (doFullRefresh) {
clear();
_vm->_system->copyRectToScreen((const byte *)_vm->_screen->_frontScreen, 640, 0, 0, 640, _vm->_cameraHeight);
} else {
updateDirtyRects();
}
SWAP(_currQueue, _prevQueue);
_currQueue->clear();
}
void RenderQueue::clear() {
_prevQueue->clear();
_currQueue->clear();
}
bool RenderQueue::rectIntersectsItem(const Common::Rect &rect) {
for (RenderQueueArray::iterator iter = _currQueue->begin(); iter != _currQueue->end(); iter++) {
const RenderQueueItem *item = &(*iter);
if (rect.intersects(item->rect))
return true;
}
return false;
}
RenderQueueItem *RenderQueue::findItemInQueue(RenderQueueArray *queue, const RenderQueueItem &item) {
/* This checks if the given item also exists in the previously drawn frame.
The state of the item (position, color etc) is handled elsewhere.
*/
for (RenderQueueArray::iterator iter = queue->begin(); iter != queue->end(); iter++) {
RenderQueueItem *prevItem = &(*iter);
if (prevItem->type == item.type) {
switch (item.type) {
case kSprite:
if (prevItem->sprite.resIndex == item.sprite.resIndex &&
prevItem->sprite.frameNum == item.sprite.frameNum)
return prevItem;
break;
case kText:
if (prevItem->text.text == item.text.text &&
prevItem->text.len == item.text.len)
return prevItem;
break;
case kMask:
if (prevItem->mask.surface == item.mask.surface)
return prevItem;
break;
}
}
}
return NULL; // Not found
}
bool RenderQueue::hasItemChanged(const RenderQueueItem &item1, const RenderQueueItem &item2) {
if (item1.type != item2.type)
return true;
if (item1.rect.left != item2.rect.left ||
item1.rect.top != item2.rect.top ||
item1.rect.right != item2.rect.right ||
item1.rect.bottom != item2.rect.bottom)
return true;
if (item1.type == kText && item1.text.color != item2.text.color)
return true;
return false;
}
void RenderQueue::invalidateItemsByRect(const Common::Rect &rect, const RenderQueueItem *item) {
for (RenderQueueArray::iterator iter = _currQueue->begin(); iter != _currQueue->end(); iter++) {
RenderQueueItem *subItem = &(*iter);
if (item != subItem &&
subItem->flags == kUnchanged &&
rect.intersects(subItem->rect)) {
subItem->flags = kRefresh;
invalidateItemsByRect(subItem->rect, subItem);
}
}
}
void RenderQueue::addDirtyRect(const Common::Rect &rect) {
_updateUta->addRect(rect);
}
void RenderQueue::restoreDirtyBackground() {
int n_rects = 0;
Common::Rect *rects = _updateUta->getRectangles(&n_rects, 0, 0, 639, _vm->_cameraHeight - 1);
for (int i = 0; i < n_rects; i++) {
byte *destp = _vm->_screen->_frontScreen + rects[i].left + rects[i].top * 640;
byte *srcp = _vm->_screen->_backScreen + (_vm->_cameraX + rects[i].left) + (_vm->_cameraY + rects[i].top) * _vm->_sceneWidth;
int16 w = rects[i].width();
int16 h = rects[i].height();
while (h--) {
memcpy(destp, srcp, w);
destp += 640;
srcp += _vm->_sceneWidth;
}
invalidateItemsByRect(rects[i], NULL);
}
delete[] rects;
}
void RenderQueue::updateDirtyRects() {
int n_rects = 0;
Common::Rect *rects = _updateUta->getRectangles(&n_rects, 0, 0, 639, _vm->_cameraHeight - 1);
for (int i = 0; i < n_rects; i++) {
_vm->_system->copyRectToScreen((const byte *)_vm->_screen->_frontScreen + rects[i].left + rects[i].top * 640,
640, rects[i].left, rects[i].top, rects[i].width(), rects[i].height());
}
delete[] rects;
}
} // End of namespace Picture