/
SDL12Video.cpp
317 lines (284 loc) · 8.48 KB
/
SDL12Video.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
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003-2012 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 "SDL12Video.h"
#include "Game.h"
#include "Interface.h"
using namespace GemRB;
SDL12VideoDriver::SDL12VideoDriver(void)
{
overlay = NULL;
}
SDL12VideoDriver::~SDL12VideoDriver(void)
{
DestroyMovieScreen();
}
int SDL12VideoDriver::Init(void)
{
int ret = SDLVideoDriver::Init();
if (ret==GEM_OK) {
SDL_EnableUNICODE( 1 );
SDL_EnableKeyRepeat( 500, 50 );
#if TARGET_OS_MAC
// Apple laptops have single buttons,
// but actually produce more then left mouse events with that single button
// this may limit people actually using very old single button mice, but who cares :)
setenv("SDL_HAS3BUTTONMOUSE", "SDL_HAS3BUTTONMOUSE", 1);
#endif
}
return ret;
}
int SDL12VideoDriver::CreateDisplay(int w, int h, int b, bool fs, const char* title)
{
bpp=b;
fullscreen=fs;
Log(MESSAGE, "SDL 1.2 Driver", "Creating display");
ieDword flags = SDL_SWSURFACE;
if (fullscreen) {
flags |= SDL_FULLSCREEN;
}
Log(MESSAGE, "SDL 1.2 Driver", "SDL_SetVideoMode...");
disp = SDL_SetVideoMode( w, h, bpp, flags );
SDL_WM_SetCaption( title, 0 );
if (disp == NULL) {
Log(ERROR, "SDL 1.2 Driver", "%s", SDL_GetError());
return GEM_ERROR;
}
Log(MESSAGE, "SDL 1.2 Driver", "Checking for HardWare Acceleration...");
const SDL_VideoInfo* vi = SDL_GetVideoInfo();
if (!vi) {
Log(WARNING, "SDL 1.2 Driver", "No Hardware Acceleration available.");
}
width = disp->w;
height = disp->h;
Viewport.w = width;
Viewport.h = height;
Log(MESSAGE, "SDL 1.2 Driver", "Creating Main Surface...");
SDL_Surface* tmp = SDL_CreateRGBSurface( SDL_SWSURFACE, width, height,
bpp, 0, 0, 0, 0 );
Log(MESSAGE, "SDL 1.2 Driver", "Creating Back Buffer...");
backBuf = SDL_DisplayFormat( tmp );
Log(MESSAGE, "SDL 1.2 Driver", "Creating Extra Buffer...");
extra = SDL_DisplayFormat( tmp );
SDL_LockSurface( extra );
long val = SDL_MapRGBA( extra->format, fadeColor.r, fadeColor.g, fadeColor.b, 0 );
SDL_FillRect( extra, NULL, val );
SDL_UnlockSurface( extra );
SDL_FreeSurface( tmp );
return GEM_OK;
}
void SDL12VideoDriver::InitMovieScreen(int &w, int &h, bool yuv)
{
if (yuv) {
if (overlay) {
SDL_FreeYUVOverlay(overlay);
}
// BIKPlayer outputs PIX_FMT_YUV420P which is YV12
overlay = SDL_CreateYUVOverlay(w, h, SDL_YV12_OVERLAY, disp);
}
if (SDL_LockSurface( disp ) == 0) {
memset( disp->pixels, 0,
disp->w * disp->h * disp->format->BytesPerPixel );
SDL_UnlockSurface( disp );
SDL_Flip( disp );
w = disp->w;
h = disp->h;
} else {
print("Couldn't lock surface: %s\n", SDL_GetError());
}
//setting the subtitle region to the bottom 1/4th of the screen
subtitleregion.w = w;
subtitleregion.h = h/4;
subtitleregion.x = 0;
subtitleregion.y = h-h/4;
}
void SDL12VideoDriver::DestroyMovieScreen()
{
if (overlay) {
SDL_FreeYUVOverlay(overlay);
overlay = NULL;
}
}
void SDL12VideoDriver::showFrame(unsigned char* buf, unsigned int bufw,
unsigned int bufh, unsigned int sx, unsigned int sy, unsigned int w,
unsigned int h, unsigned int dstx, unsigned int dsty,
int g_truecolor, unsigned char *pal, ieDword titleref)
{
int i;
SDL_Surface* sprite;
SDL_Rect srcRect, destRect;
assert( bufw == w && bufh == h );
if (g_truecolor) {
sprite = SDL_CreateRGBSurfaceFrom( buf, bufw, bufh, 16, 2 * bufw,
0x7C00, 0x03E0, 0x001F, 0 );
} else {
sprite = SDL_CreateRGBSurfaceFrom( buf, bufw, bufh, 8, bufw, 0, 0, 0, 0 );
for (i = 0; i < 256; i++) {
sprite->format->palette->colors[i].r = ( *pal++ ) << 2;
sprite->format->palette->colors[i].g = ( *pal++ ) << 2;
sprite->format->palette->colors[i].b = ( *pal++ ) << 2;
sprite->format->palette->colors[i].unused = 0;
}
}
srcRect.x = sx;
srcRect.y = sy;
srcRect.w = w;
srcRect.h = h;
destRect.x = dstx;
destRect.y = dsty;
destRect.w = w;
destRect.h = h;
SDL_Rect rect = RectFromRegion(subtitleregion);
SDL_FillRect(disp, &rect, 0);
SDL_BlitSurface( sprite, &srcRect, disp, &destRect );
if (titleref>0)
DrawMovieSubtitle( titleref );
SDL_Flip( disp );
SDL_FreeSurface( sprite );
}
// sets brightness and contrast
// FIXME:SetGammaRamp doesn't seem to work
// WARNING: SDL 1.2.13 crashes in SetGamma on Windows (it was fixed in SDL's #3533 Revision)
void SDL12VideoDriver::SetGamma(int brightness, int /*contrast*/)
{
SDL_SetGamma(0.8+brightness/50.0,0.8+brightness/50.0,0.8+brightness/50.0);
}
bool SDL12VideoDriver::SetFullscreenMode(bool set)
{
if (fullscreen != set) {
fullscreen=set;
// FIXME: SDL_WM_ToggleFullScreen only works on X11. use SDL_SetVideoMode()
SDL_WM_ToggleFullScreen( disp );
//readjust mouse to original position
MoveMouse(CursorPos.x, CursorPos.y);
//synchronise internal variable
core->GetDictionary()->SetAt( "Full Screen", (ieDword) fullscreen );
return true;
}
return false;
}
int SDL12VideoDriver::SwapBuffers(void)
{
SDL_BlitSurface( backBuf, NULL, disp, NULL );
if (fadeColor.a) {
SDL_SetAlpha( extra, SDL_SRCALPHA, fadeColor.a );
SDL_Rect src = {
0, 0, (Uint16)Viewport.w, (Uint16)Viewport.h
};
SDL_Rect dst = {
xCorr, yCorr, 0, 0
};
SDL_BlitSurface( extra, &src, disp, &dst );
}
/** This causes the tooltips/cursors to be rendered directly to display */
SDL_Surface* tmp = backBuf;
backBuf = disp; // FIXME: UGLY HACK!
int ret = SDLVideoDriver::SwapBuffers();
backBuf = tmp;
SDL_Flip( disp );
return ret;
}
bool SDL12VideoDriver::ToggleGrabInput()
{
if (SDL_GRAB_OFF == SDL_WM_GrabInput( SDL_GRAB_QUERY )) {
SDL_WM_GrabInput( SDL_GRAB_ON );
return true;
}
else {
SDL_WM_GrabInput( SDL_GRAB_OFF );
MoveMouse(CursorPos.x, CursorPos.y);
return false;
}
}
/* no idea how elaborate this should be*/
void SDL12VideoDriver::MoveMouse(unsigned int x, unsigned int y)
{
SDL_WarpMouse(x,y);
}
void SDL12VideoDriver::showYUVFrame(unsigned char** buf, unsigned int *strides,
unsigned int /*bufw*/, unsigned int bufh,
unsigned int w, unsigned int h,
unsigned int dstx, unsigned int dsty,
ieDword titleref) {
assert( /* bufw == w && */ bufh == h );
SDL_Rect destRect;
SDL_LockYUVOverlay(overlay);
for (unsigned int plane = 0; plane < 3; plane++) {
unsigned char *data = buf[plane];
unsigned int size = overlay->pitches[plane];
if (strides[plane] < size) {
size = strides[plane];
}
unsigned int srcoffset = 0, destoffset = 0;
for (unsigned int i = 0; i < ((plane == 0) ? bufh : (bufh / 2)); i++) {
memcpy(overlay->pixels[plane] + destoffset,
data + srcoffset, size);
srcoffset += strides[plane];
destoffset += overlay->pitches[plane];
}
}
SDL_UnlockYUVOverlay(overlay);
destRect.x = dstx;
destRect.y = dsty;
destRect.w = w;
destRect.h = h;
SDL_Rect rect = RectFromRegion(subtitleregion);
SDL_FillRect(disp, &rect, 0);
SDL_DisplayYUVOverlay(overlay, &destRect);
if (titleref>0)
DrawMovieSubtitle( titleref );
}
int SDL12VideoDriver::ProcessEvent(const SDL_Event & event)
{
switch (event.type) {
case SDL_ACTIVEEVENT:
if (core->ConsolePopped) {
break;
}
if (event.active.state == SDL_APPMOUSEFOCUS) {
if (!event.active.gain)
EvntManager->OnSpecialKeyPress( GEM_MOUSEOUT );
}
break;
default:
return SDLVideoDriver::ProcessEvent(event);
}
return GEM_OK;
}
void SDL12VideoDriver::ShowSoftKeyboard()
{
if(core->UseSoftKeyboard){
Log(WARNING, "SDL 1.2 Driver", "SDL 1.2 doesn't support a software keyboard");
}
}
void SDL12VideoDriver::HideSoftKeyboard()
{
if(core->UseSoftKeyboard){
Log(WARNING, "SDL 1.2 Driver", "SDL 1.2 doesn't support a software keyboard");
}
}
// Private methods
bool SDL12VideoDriver::SetSurfaceAlpha(SDL_Surface* surface, unsigned short alpha)
{
return (SDL_SetAlpha( surface, SDL_SRCALPHA | SDL_RLEACCEL, alpha ) == 0);
}
#include "plugindef.h"
GEMRB_PLUGIN(0xDBAAB50, "SDL1 Video Driver")
PLUGIN_DRIVER(SDL12VideoDriver, "sdl")
END_PLUGIN()