/
Video.cpp
452 lines (382 loc) · 11.3 KB
/
Video.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
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB Project
*
* 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.
*
*
*/
#include "Video.h"
#include "win32def.h"
#include "Interface.h"
#include "Palette.h"
#include "Sprite2D.h"
#include <cmath>
namespace GemRB {
const TypeID Video::ID = { "Video" };
Video::Video(void)
: Viewport(), CursorPos(), fadeColor()
{
CursorIndex = VID_CUR_UP;
Cursor[VID_CUR_UP] = NULL;
Cursor[VID_CUR_DOWN] = NULL;
Cursor[VID_CUR_DRAG] = NULL;
EvntManager = NULL;
// MOUSE_GRAYED and MOUSE_DISABLED are the first 2 bits so shift the config value away from those.
// we care only about 2 bits at the moment so mask out the remainder
MouseFlags = ((core->MouseFeedback & 0x3) << 2);
// Initialize gamma correction tables
for (int i = 0; i < 256; i++) {
Gamma22toGamma10[i] = (unsigned char)(0.5 + (pow (i/255.0, 2.2/1.0) * 255.0));
Gamma10toGamma22[i] = (unsigned char)(0.5 + (pow (i/255.0, 1.0/2.2) * 255.0));
}
}
Video::~Video(void)
{
}
bool Video::ToggleFullscreenMode()
{
return SetFullscreenMode(!fullscreen);
}
/** Set Event Manager */
void Video::SetEventMgr(EventMgr* evnt)
{
//if 'evnt' is NULL then no Event Manager will be used
EvntManager = evnt;
}
// Flips given sprite vertically (up-down). If MirrorAnchor=true,
// flips its anchor (i.e. origin//base point) as well
// returns new sprite
Sprite2D* Video::MirrorSpriteVertical(const Sprite2D* sprite, bool MirrorAnchor)
{
if (!sprite)
return NULL;
Sprite2D* dest = sprite->copy();
if (sprite->pixels != dest->pixels) {
assert(!sprite->BAM);
// if the sprite pixel buffers are not the same we need to manually mirror the pixels
for (int x = 0; x < dest->Width; x++) {
unsigned char * dst = ( unsigned char * ) dest->pixels + x;
unsigned char * src = dst + ( dest->Height - 1 ) * dest->Width;
for (int y = 0; y < dest->Height / 2; y++) {
unsigned char swp = *dst;
*dst = *src;
*src = swp;
dst += dest->Width;
src -= dest->Width;
}
}
} else {
// if the pixel buffers are the same then either there are no pixels (NULL)
// or the sprites support sharing pixel data and we only need to set a render flag on the copy
dest->renderFlags |= RENDER_FLIP_VERTICAL;
}
dest->XPos = sprite->XPos;
if (MirrorAnchor)
dest->YPos = sprite->Height - sprite->YPos;
else
dest->YPos = sprite->YPos;
return dest;
}
// Flips given sprite horizontally (left-right). If MirrorAnchor=true,
// flips its anchor (i.e. origin//base point) as well
Sprite2D* Video::MirrorSpriteHorizontal(const Sprite2D* sprite, bool MirrorAnchor)
{
if (!sprite)
return NULL;
Sprite2D* dest = sprite->copy();
if (sprite->pixels != dest->pixels) {
assert(!sprite->BAM);
// if the sprite pixel buffers are not the same we need to manually mirror the pixels
for (int y = 0; y < dest->Height; y++) {
unsigned char * dst = (unsigned char *) dest->pixels + ( y * dest->Width );
unsigned char * src = dst + dest->Width - 1;
for (int x = 0; x < dest->Width / 2; x++) {
unsigned char swp=*dst;
*dst++ = *src;
*src-- = swp;
}
}
} else {
// if the pixel buffers are the same then either there are no pixels (NULL)
// or the sprites support sharing pixel data and we only need to set a render flag on the copy
dest->renderFlags |= RENDER_FLIP_HORIZONTAL;
}
if (MirrorAnchor)
dest->XPos = sprite->Width - sprite->XPos;
else
dest->XPos = sprite->XPos;
dest->YPos = sprite->YPos;
return dest;
}
void Video::SetCursor(Sprite2D* cur, enum CursorType curIdx)
{
if (cur) {
//cur will be assigned in the end, increase refcount
cur->acquire();
//setting a dragged sprite cursor, it will 'stick' until cleared
if (curIdx == VID_CUR_DRAG)
CursorIndex = VID_CUR_DRAG;
} else {
//clearing the dragged sprite cursor, replace it with the normal cursor
if (curIdx == VID_CUR_DRAG)
CursorIndex = VID_CUR_UP;
}
//decrease refcount of the previous cursor
if (Cursor[curIdx])
FreeSprite(Cursor[curIdx]);
Cursor[curIdx] = cur;
}
/** Mouse is invisible and cannot interact */
void Video::SetMouseEnabled(int enabled)
{
if (enabled) {
MouseFlags &= ~MOUSE_DISABLED;
} else {
MouseFlags |= MOUSE_DISABLED;
}
}
/** Mouse cursor is grayed and doesn't click (but visible and movable) */
void Video::SetMouseGrayed(bool grayed)
{
if (grayed) {
MouseFlags |= MOUSE_GRAYED;
} else {
MouseFlags &= ~MOUSE_GRAYED;
}
}
/** Get the fullscreen mode */
bool Video::GetFullscreenMode() const
{
return fullscreen;
}
void Video::BlitTiled(Region rgn, const Sprite2D* img, bool anchor)
{
int xrep = ( rgn.w + img->Width - 1 ) / img->Width;
int yrep = ( rgn.h + img->Height - 1 ) / img->Height;
for (int y = 0; y < yrep; y++) {
for (int x = 0; x < xrep; x++) {
BlitSprite(img, rgn.x + (x*img->Width),
rgn.y + (y*img->Height), anchor, &rgn);
}
}
}
//Sprite conversion, creation
Sprite2D* Video::CreateAlpha( const Sprite2D *sprite)
{
if (!sprite)
return 0;
unsigned int *pixels = (unsigned int *) malloc (sprite->Width * sprite->Height * 4);
int i=0;
for (int y = 0; y < sprite->Height; y++) {
for (int x = 0; x < sprite->Width; x++) {
int sum = 0;
int cnt = 0;
for (int xx=x-3;xx<=x+3;xx++) {
for(int yy=y-3;yy<=y+3;yy++) {
if (((xx==x-3) || (xx==x+3)) &&
((yy==y-3) || (yy==y+3))) continue;
if (xx < 0 || xx >= sprite->Width) continue;
if (yy < 0 || yy >= sprite->Height) continue;
cnt++;
if (sprite->IsPixelTransparent(xx, yy))
sum++;
}
}
int tmp=255 - (sum * 255 / cnt);
tmp = tmp * tmp / 255;
pixels[i++]=tmp;
}
}
return CreateSprite( sprite->Width, sprite->Height, 32, 0xFF000000,
0x00FF0000, 0x0000FF00, 0x000000FF, pixels );
}
Sprite2D* Video::SpriteScaleDown( const Sprite2D* sprite, unsigned int ratio )
{
unsigned int Width = sprite->Width / ratio;
unsigned int Height = sprite->Height / ratio;
unsigned int* pixels = (unsigned int *) malloc( Width * Height * 4 );
int i = 0;
for (unsigned int y = 0; y < Height; y++) {
for (unsigned int x = 0; x < Width; x++) {
Color c = SpriteGetPixelSum( sprite, x, y, ratio );
*(pixels + i++) = c.r + (c.g << 8) + (c.b << 16) + (c.a << 24);
}
}
Sprite2D* small = CreateSprite( Width, Height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000,
0xff000000, pixels, false, 0 );
small->XPos = sprite->XPos / ratio;
small->YPos = sprite->YPos / ratio;
return small;
}
//TODO light could be elliptical in the original engine
//is it difficult?
Sprite2D* Video::CreateLight(int radius, int intensity)
{
if(!radius) return NULL;
Point p, q;
int a;
void* pixels = malloc( radius * radius * 4 * 4);
int i = 0;
for (p.y = -radius; p.y < radius; p.y++) {
for (p.x = -radius; p.x < radius; p.x++) {
a = intensity*(radius-(signed) Distance(p,q))/radius;
if(a<0) a=0;
else if(a>255) a = 255;
*((unsigned int*)pixels + i++) = 0xffffff + ((a/2) << 24);
}
}
Sprite2D* light = CreateSprite( radius*2, radius*2, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, pixels);
light->XPos = radius;
light->YPos = radius;
return light;
}
Color Video::SpriteGetPixelSum(const Sprite2D* sprite, unsigned short xbase, unsigned short ybase, unsigned int ratio)
{
Color sum;
unsigned int count = ratio*ratio;
unsigned int r=0, g=0, b=0, a=0;
for (unsigned int x = 0; x < ratio; x++) {
for (unsigned int y = 0; y < ratio; y++) {
Color c = sprite->GetPixel( xbase*ratio+x, ybase*ratio+y );
r += Gamma22toGamma10[c.r];
g += Gamma22toGamma10[c.g];
b += Gamma22toGamma10[c.b];
a += Gamma22toGamma10[c.a];
}
}
sum.r = Gamma10toGamma22[r / count];
sum.g = Gamma10toGamma22[g / count];
sum.b = Gamma10toGamma22[b / count];
sum.a = Gamma10toGamma22[a / count];
return sum;
}
//Viewport specific
Region Video::GetViewport() const
{
return Viewport;
}
void Video::SetMovieFont(Font *stfont, Palette *pal)
{
subtitlefont = stfont;
subtitlepal = pal;
}
void Video::SetViewport(int x, int y, unsigned int w, unsigned int h)
{
if (x>width)
x=width;
xCorr = x;
if (y>height)
y=height;
yCorr = y;
if (w>(unsigned int) width)
w=0;
Viewport.w = w;
if (h>(unsigned int) height)
h=0;
Viewport.h = h;
}
void Video::MoveViewportTo(int x, int y)
{
if (x != Viewport.x || y != Viewport.y) {
core->GetAudioDrv()->UpdateListenerPos( (x - xCorr) + width / 2, (y - yCorr)
+ height / 2 );
Viewport.x = x;
Viewport.y = y;
}
}
void Video::FreeSprite(Sprite2D*& spr)
{
if (spr) {
spr->release();
spr = NULL;
}
}
void Video::InitSpriteCover(SpriteCover* sc, int flags)
{
int i;
sc->flags = flags;
sc->pixels = new unsigned char[sc->Width * sc->Height];
for (i = 0; i < sc->Width*sc->Height; ++i)
sc->pixels[i] = 0;
}
// flags: 0 - never dither (full cover)
// 1 - dither if polygon wants it
// 2 - always dither
void Video::AddPolygonToSpriteCover(SpriteCover* sc, Wall_Polygon* poly)
{
// possible TODO: change the cover to use a set of intervals per line?
// advantages: faster
// disadvantages: makes the blitter much more complex
int xoff = sc->worldx - sc->XPos;
int yoff = sc->worldy - sc->YPos;
std::list<Trapezoid>::iterator iter;
for (iter = poly->trapezoids.begin(); iter != poly->trapezoids.end();
++iter)
{
int y_top = iter->y1 - yoff; // inclusive
int y_bot = iter->y2 - yoff; // exclusive
if (y_top < 0) y_top = 0;
if ( y_bot > sc->Height) y_bot = sc->Height;
if (y_top >= y_bot) continue; // clipped
int ledge = iter->left_edge;
int redge = iter->right_edge;
Point& a = poly->points[ledge];
Point& b = poly->points[(ledge+1)%(poly->count)];
Point& c = poly->points[redge];
Point& d = poly->points[(redge+1)%(poly->count)];
unsigned char* line = sc->pixels + (y_top)*sc->Width;
for (int sy = y_top; sy < y_bot; ++sy) {
int py = sy + yoff;
// TODO: maybe use a 'real' line drawing algorithm to
// compute these values faster.
int lt = (b.x * (py - a.y) + a.x * (b.y - py))/(b.y - a.y);
int rt = (d.x * (py - c.y) + c.x * (d.y - py))/(d.y - c.y) + 1;
lt -= xoff;
rt -= xoff;
if (lt < 0) lt = 0;
if (rt > sc->Width) rt = sc->Width;
if (lt >= rt) { line += sc->Width; continue; } // clipped
int dither;
if (sc->flags == 1) {
dither = poly->wall_flag & WF_DITHER;
} else {
dither = sc->flags;
}
if (dither) {
unsigned char* pix = line + lt;
unsigned char* end = line + rt;
if ((lt + xoff + sy + yoff) % 2) pix++; // CHECKME: aliasing?
for (; pix < end; pix += 2)
*pix = 1;
} else {
// we hope memset is faster
// condition: lt < rt is true
memset (line+lt, 1, rt-lt);
}
line += sc->Width;
}
}
}
void Video::DestroySpriteCover(SpriteCover* sc)
{
delete[] sc->pixels;
sc->pixels = NULL;
}
void Video::GetMousePos(int &x, int &y)
{
x = CursorPos.x;
y = CursorPos.y;
}
}