forked from clickteam-plugin/TileMapViewport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayRunObject.cpp
440 lines (352 loc) · 18.2 KB
/
DisplayRunObject.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
#include "Common.h"
#include "Helpers.h"
#define assignSubLayerSettingLink(name, minCellSize) \
assignSubLayerLink(layer.settings.subLayerLink, name, minCellSize)
#define assignSubLayerCallbackLink(name, minCellSize) \
assignSubLayerLink(rdPtr->layerCallback.link, name, minCellSize)
#define assignSubLayerLink(source, name, minCellSize) \
{ \
if (source.##name != 0xff && source.##name < layer.subLayers.size() && \
layer.subLayers[source.##name].getCellSize() >= minCellSize) { \
sl_##name = &layer.subLayers[source.##name]; \
} \
}
// Clips a row of tiles rendered onto a viewport to render only within the
// viewport boundaries
inline void clipBlitTiles(int & destPos, int viewportSize, int & srcPos, int & srcSize, int tileSize)
{
srcSize = tileSize;
// Clip left
if (destPos < 0) {
srcPos -= destPos;
srcSize += destPos;
destPos = 0;
}
// Clip right
if (destPos + srcSize > viewportSize)
srcSize -= tileSize - (viewportSize - destPos);
}
short WINAPI DLLExport DisplayRunObject(LPRDATA rdPtr)
{
if (rdPtr->autoScroll) {
rdPtr->cameraX = rdPtr->rHo.hoAdRunHeader->rh3.rh3DisplayX;
rdPtr->cameraY = rdPtr->rHo.hoAdRunHeader->rh3.rh3DisplayY;
}
// No attached parent... nothing to draw...
if (!rdPtr->p)
return 0;
// Whether or not we use a temporary surface (-> coordinate shifting)
bool tempSurf = rdPtr->surface != 0;
// Get output surface
cSurface * ps = WinGetSurface((int)rdPtr->rHo.hoAdRunHeader->rhIdEditWin);
cSurface * target = tempSurf ? rdPtr->surface : ps;
if (!target)
return 0;
// On-screen coords
int viewportX = rdPtr->rHo.hoRect.left;
int viewportY = rdPtr->rHo.hoRect.top;
int viewportWidth = rdPtr->rHo.hoImgWidth;
int viewportHeight = rdPtr->rHo.hoImgHeight;
// Whether we have to perform pixel clipping on blitted tiles
bool clip = !tempSurf && rdPtr->accurateClip;
// Rendering flags for advanced blitting
DWORD blitFlags = rdPtr->blitFlags;
#ifdef HWABETA
blitFlags |= BLTF_SAFESRC;
#endif
// Clear background
if (!rdPtr->transparent) {
if (tempSurf) {
target->Fill(rdPtr->background);
}
else {
target->Fill(viewportX, viewportY, viewportWidth, viewportHeight, rdPtr->background);
}
}
double time = rdPtr->animTime;
float zoom = rdPtr->zoom;
float zoomPointX = rdPtr->zoomPointX;
float zoomPointY = rdPtr->zoomPointY;
unsigned layerCount = rdPtr->p->layers->size();
if (!layerCount)
return 0;
// Cache all tileset pointers for faster access
cSurface * tilesetCache[256] = {};
for (unsigned i = 0; i < rdPtr->p->tilesets->size(); ++i)
tilesetCache[i] = (*rdPtr->p->tilesets)[i].texture;
unsigned minLayer = max(0, min(layerCount - 1, rdPtr->minLayer));
unsigned maxLayer = max(0, min(layerCount - 1, rdPtr->maxLayer));
if (minLayer <= maxLayer && maxLayer < layerCount) {
for (unsigned i = minLayer; i <= maxLayer; ++i) {
// Get a pointer to the iterated layer
Layer & layer = (*rdPtr->p->layers)[i];
// Store layer settings locally
LayerSettings settings = layer.settings;
// Sub-layer links (assigned via callback)
SubLayer *sl_tileset = 0, *sl_scaleX = 0, *sl_scaleY = 0, *sl_angle = 0,
*sl_animation = 0, *sl_animationFrame = 0;
// If necessary, load the sub-layers for per-tile render info
assignSubLayerSettingLink(tileset, 1);
assignSubLayerSettingLink(animation, 1);
assignSubLayerSettingLink(animationFrame, 1);
// Perform layer callback and update sub-layer links if necessary...
if (rdPtr->layerCallback.use) {
rdPtr->layerCallback.index = i;
rdPtr->layerCallback.settings = &settings;
// Reset layer links
memset(&rdPtr->layerCallback.link, 0xff, sizeof(rdPtr->layerCallback.link));
// Perform callback
generateEvent(3);
generateEvent(4);
// Assign linked sub-layer pointers
assignSubLayerCallbackLink(tileset, 1);
assignSubLayerCallbackLink(animation, 1);
assignSubLayerCallbackLink(scaleX, 4);
assignSubLayerCallbackLink(scaleY, 4);
assignSubLayerCallbackLink(angle, 4);
}
// No data to draw or simply invisible
if (!layer.isValid() || !settings.visible)
continue;
// We'll need these so often...
unsigned short tileWidth = settings.tileWidth;
unsigned short tileHeight = settings.tileHeight;
// On-screen tile width
float renderTileWidth = tileWidth * zoom;
float renderTileHeight = tileHeight * zoom;
#ifdef HWABETA
POINT tileCenter = {int(renderTileWidth / 2), int(renderTileHeight / 2)};
#endif
// Can't render
if (!tileWidth || !tileHeight)
continue;
// Store the layer size
int layerWidth = layer.getWidth();
int layerHeight = layer.getHeight();
// On-screen coordinate
float drawX = layer.getScreenX(rdPtr->cameraX);
float drawY = layer.getScreenY(rdPtr->cameraY);
float viewportXBias = viewportWidth * zoomPointX;
float viewportYBias = viewportHeight * zoomPointY;
drawX = (drawX - viewportXBias) * zoom + viewportXBias;
drawY = (drawY - viewportYBias) * zoom + viewportYBias;
// See if the layer is visible at all
if ((!settings.wrapX && (drawX >= viewportWidth || drawX + renderTileWidth * layerWidth < 0)) ||
(!settings.wrapY &&
(drawY >= viewportHeight || drawY + renderTileHeight * layerHeight < 0)))
continue;
// Region to draw
int x1 = 0;
int y1 = 0;
int x2 = layerWidth; // Exclusive
int y2 = layerHeight;
// Additional tiles to render outside of the visible area
int borderX = 1 + (rdPtr->tileCallback.use ? rdPtr->tileCallback.borderX : 0);
int borderY = 1 + (rdPtr->tileCallback.use ? rdPtr->tileCallback.borderY : 0);
int pixelBorderX = int(borderX * renderTileWidth);
int pixelBorderY = int(borderY * renderTileHeight);
// Optimize drawing region
while (drawX <= -pixelBorderX)
drawX += renderTileWidth, x1++;
while (drawY <= -pixelBorderY)
drawY += renderTileHeight, y1++;
while (drawX >= viewportWidth + pixelBorderX - (x2 - x1) * renderTileWidth)
x2--;
while (drawY >= viewportHeight + pixelBorderY - (y2 - y1) * renderTileWidth)
y2--;
// Wrapping
if (settings.wrapX) {
// TODO: Find optimization
if (zoom != 1.0f)
while (drawX > -renderTileWidth * (borderX - 1))
drawX -= renderTileWidth * layerWidth, x1 -= layerWidth;
else
while (drawX > -renderTileWidth * (borderX - 1))
drawX -= renderTileWidth, x1++;
while (drawX < viewportWidth - renderTileWidth * (x2 - x1 - borderX + 1))
x2++;
signmodPair(x1, x2, 0, layerWidth);
}
if (settings.wrapY) {
// TODO: Find optimization
if (zoom != 1.0f)
while (drawY > -renderTileHeight * (borderY - 1))
drawY -= renderTileHeight * layerHeight, y1 -= layerHeight;
else
while (drawY > -renderTileHeight * (borderY - 1))
drawY -= renderTileHeight, y1++;
while (drawY < viewportWidth - renderTileWidth * (y2 - y1 - borderY + 1))
y2++;
signmodPair(y1, y2, 0, layerHeight);
}
// Draw to screen: Add on-screen offset
if (!tempSurf) {
drawX += viewportX;
drawY += viewportY;
}
// If can render
if (x2 - x1 > 0 && y2 - y1 > 0) {
// Initialize default render settings of tile
TileSettings origTileSettings;
if (sl_scaleX || sl_scaleY || sl_angle)
origTileSettings.transform = true;
// Settings storage that can be modified in a tile callback
TileSettings tileSettings = origTileSettings;
// Per-tile offset (for callbacks)
int offsetX = 0, offsetY = 0;
// For every visible tile...
int screenY = 0;
for (int unwrappedY = y1; unwrappedY < y2; ++unwrappedY) {
int screenX = 0;
for (int unwrappedX = x1; unwrappedX < x2; ++unwrappedX) {
// Calculate wrapped coordinate (in case wrapping is
// enabled)
int x = unwrappedX % layerWidth;
int y = unwrappedY % layerHeight;
// Get this tile's address
Tile tile = *layer.getTile(x, y);
// Calculate position and render the tile, exit when
// impossible
do {
if (tile.id == Tile::EMPTY)
break;
// By default, use the layer tileset
unsigned char tilesetIndex = settings.tileset;
// Default animation
unsigned char animation = 0;
unsigned frameOffset = 0;
// Sub-layer links
if (sl_tileset)
tilesetIndex = *sl_tileset->getCell(x, y);
if (sl_animation)
sl_animation->getCellSafe(x, y, &animation);
if (sl_animationFrame)
sl_animationFrame->getCellSafe(x, y, &frameOffset);
if (sl_scaleX)
sl_scaleX->getCellSafe(x, y, &tileSettings.scaleX);
if (sl_scaleY)
sl_scaleY->getCellSafe(x, y, &tileSettings.scaleY);
if (sl_angle)
sl_angle->getCellSafe(x, y, &tileSettings.angle);
// Determine tile opacity
BlitOp blitOp = settings.opacity < 0.999f ? BOP_BLEND : BOP_COPY;
float opacity = settings.opacity;
LPARAM blitParam = 128 - int(opacity * 128);
// We use callbacks, so let the programmer do stuff
if (rdPtr->tileCallback.use) {
// Reset the tile settings - the last callback
// might have changed
// them
tileSettings = origTileSettings;
rdPtr->tileCallback.settings = &tileSettings;
tileSettings.tileset = tilesetIndex;
// Allow modification of tile values
rdPtr->tileCallback.tile = &tile;
// Grant access to tile position
rdPtr->tileCallback.x = x;
rdPtr->tileCallback.y = y;
// Call condition so the programmer can modify
// the values
generateEvent(1);
// Decided not to render tile...
if (!tileSettings.visible)
break;
// Apply tileset index
tilesetIndex = tileSettings.tileset;
// Apply offset
offsetX = tileSettings.offsetX;
offsetY = tileSettings.offsetY;
// Apply opacity
opacity *= tileSettings.opacity;
// Compute blit operation
if (tileSettings.tint != WHITE) {
blitOp = BOP_RGBAFILTER;
int rgb = tileSettings.tint & 0xffffff;
rgb = ((rgb & 0xff) << 16) | (rgb & 0xff00) |
((rgb & 0xff0000) >> 16);
blitParam = rgb | (int(opacity * 255) << 24);
}
else if (opacity < 0.999) {
blitOp = BOP_BLEND;
blitParam = 128 - int(opacity * 128);
}
}
// Animate if there is more than 1 tile in the
// selected animation
rdPtr->anim[animation].apply(tile, time, frameOffset);
// Get the computed tileset's surface
cSurface * tileSurf = tilesetCache[tilesetIndex];
if (!tileSurf)
break;
#ifdef HWABETA
// If zoomed in, act as if tiles were transformed
if (zoom != 1.0f)
tileSettings.transform = true;
// HWA only: tile angle and scale on callback.
// Disables accurate clipping!
if (tileSettings.transform) {
float scaleX = zoom * tileSettings.scaleX;
float scaleY = zoom * tileSettings.scaleY;
float angle = tileSettings.angle * 360.0f;
float blitX, blitY;
blitX = drawX + (screenX + offsetX + tileCenter.x) * zoom;
blitY = drawY + (screenY + offsetY + tileCenter.y) * zoom;
tileSurf->BlitEx(*target, blitX, blitY, scaleX, scaleY,
tile.x * tileWidth, tile.y * tileHeight,
tileWidth, tileHeight, &tileCenter, angle,
BMODE_TRANSP, blitOp, blitParam, blitFlags);
break;
}
#endif
// Blit from the surface of the tileset with the tile's index in the layer tilesets
if (!clip) {
tileSurf->Blit(*target, (int)drawX + screenX + offsetX,
(int)drawY + screenY + offsetY, (tile.x) * tileWidth,
tile.y * tileHeight, tileWidth, tileHeight,
BMODE_TRANSP, blitOp, blitParam, blitFlags);
}
// Before blitting, perform clipping so that we won't draw outside of the viewport...
else {
tileSurf->Blit(*target, (int)drawX + screenX + offsetX,
(int)drawY + screenY + offsetY, tile.x * tileWidth,
tile.y * tileHeight, tileWidth, tileHeight,
BMODE_TRANSP, blitOp, blitParam, blitFlags);
int dX, dY, sX, sY, sW, sH;
dX = (int)drawX + screenX + offsetX - viewportX;
dY = (int)drawY + screenY + offsetY - viewportY;
sX = tile.x * tileWidth;
sY = tile.y * tileHeight;
clipBlitTiles(dX, viewportWidth, sX, sW, tileWidth);
clipBlitTiles(dY, viewportHeight, sY, sH, tileHeight);
// Draw if still visible
if (dX < viewportWidth && dY < viewportHeight && sW > 0 && sH > 0) {
// Apply viewport position
dX += viewportX;
dY += viewportY;
tileSurf->Blit(*target, dX, dY, sX, sY, sW, sH,
BMODE_TRANSP, blitOp, blitParam, blitFlags);
}
}
} while (0);
// Calculate next on-screen X
screenX += tileWidth;
}
// Calculate next on-screen Y
screenY += tileHeight;
}
}
}
}
// Finish up
if (tempSurf) {
rdPtr->surface->Blit(*ps, viewportX, viewportY, BMODE_OPAQUE,
BlitOp(rdPtr->rs.rsEffect & EFFECT_MASK), rdPtr->rs.rsEffectParam, 0);
}
// Update window region
WinAddZone(rdPtr->rHo.hoAdRunHeader->rhIdEditWin, &rdPtr->rHo.hoRect);
// Clear pointers...
rdPtr->tileCallback.settings = 0;
rdPtr->tileCallback.tile = 0;
rdPtr->layerCallback.settings = 0;
return 0;
}