Ticket #8102: sdl_gl.cpp

File sdl_gl.cpp, 12.7 KB (added by SF/luke_br, 18 years ago)

SDL with OpenGL

Line 
1/* ScummVM - Scumm Interpreter
2 * Copyright (C) 2001 Ludvig Strigeus
3 * Copyright (C) 2001/2002 The ScummVM project
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * $Header: /cvsroot/scummvm/scummvm/backends/sdl/sdl.cpp,v 1.10 2002/10/14 11:02:27 fingolfin Exp $
20 *
21 */
22
23#include "sdl-common.h"
24#include "common/scaler.h"
25#include "common/util.h"
26#include "common/engine.h" // Only #included for error() and warning()
27
28#ifdef WIN32
29int glColorTable(int, int, int, int, int, void *) { return 0; }
30int glGetColorTable(int, int, int, void *) { return 0; }
31#endif
32
33#include "fb2opengl.h"
34int _screenStart = 30;
35
36class OSystem_SDL_Normal : public OSystem_SDL_Common {
37public:
38 OSystem_SDL_Normal() : sdl_tmpscreen(0), sdl_hwscreen(0), _overlay_visible(false) {}
39
40 // Set colors of the palette
41 void set_palette(const byte *colors, uint start, uint num);
42
43 // Update the dirty areas of the screen
44 void update_screen();
45
46 // Set a parameter
47 uint32 property(int param, Property *value);
48
49 // Overlay
50 virtual void show_overlay();
51 virtual void hide_overlay();
52 virtual void clear_overlay();
53 virtual void grab_overlay(int16 *buf, int pitch);
54 virtual void copy_rect_overlay(const int16 *buf, int pitch, int x, int y, int w, int h);
55
56protected:
57 FB2GL fb2gl;
58 typedef void ScalerProc(uint8 *srcPtr, uint32 srcPitch, uint8 *deltaPtr,
59 uint8 *dstPtr, uint32 dstPitch, int width, int height);
60
61 SDL_Surface *sdl_tmpscreen; // temporary screen (for scalers/overlay)
62 SDL_Surface *sdl_hwscreen; // hardware screen
63 bool _overlay_visible;
64
65 ScalerProc *_scaler_proc;
66
67 virtual void draw_mouse();
68 virtual void undraw_mouse();
69
70 virtual void load_gfx_mode();
71 virtual void unload_gfx_mode();
72 void hotswap_gfx_mode();
73
74 int TMP_SCREEN_WIDTH;
75};
76
77OSystem_SDL_Common *OSystem_SDL_Common::create() {
78 return new OSystem_SDL_Normal();
79}
80
81void OSystem_SDL_Normal::set_palette(const byte *colors, uint start, uint num) {
82 const byte *b = colors;
83 uint i;
84 SDL_Color *base = _currentPalette + start;
85 for(i = 0; i < num; i++) {
86 base[i].r = b[0];
87 base[i].g = b[1];
88 base[i].b = b[2];
89 b += 4;
90 }
91
92 if (start < _paletteDirtyStart)
93 _paletteDirtyStart = start;
94
95 if (start + num > _paletteDirtyEnd)
96 _paletteDirtyEnd = start + num;
97}
98
99void OSystem_SDL_Normal::draw_mouse() {
100 if (!_overlay_visible) {
101 OSystem_SDL_Common::draw_mouse();
102 }
103
104 if (_mouseDrawn || !_mouseVisible)
105 return;
106
107 int x = _mouse_cur_state.x - _mouseHotspotX;
108 int y = _mouse_cur_state.y - _mouseHotspotY;
109 int w = _mouse_cur_state.w;
110 int h = _mouse_cur_state.h;
111 byte color;
112 byte *src = _mouseData; // Image representing the mouse
113 uint16 *bak = (uint16*)_mouseBackup; // Surface used to backup the area obscured by the mouse
114 uint16 *dst; // Surface we are drawing into
115
116 // clip the mouse rect, and addjust the src pointer accordingly
117 if (x < 0) {
118 w += x;
119 src -= x;
120 x = 0;
121 }
122 if (y < 0) {
123 h += y;
124 src -= y * _mouse_cur_state.w;
125 y = 0;
126 }
127
128 // Quick check to see if anything has to be drawn at all
129 if (w <= 0 || h <= 0)
130 return;
131
132 if (w > _screenWidth - x)
133 w = _screenWidth - x;
134 if (h > _screenHeight - y)
135 h = _screenHeight - y;
136
137 // Store the bounding box so that undraw mouse can restore the area the
138 // mouse currently covers to its original content.
139 _mouse_old_state.x = x;
140 _mouse_old_state.y = y;
141 _mouse_old_state.w = w;
142 _mouse_old_state.h = h;
143
144 // Draw the mouse cursor; backup the covered area in "bak"
145
146 if (SDL_LockSurface(sdl_tmpscreen) == -1)
147 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
148
149 dst = (uint16 *)sdl_tmpscreen->pixels + (y+1) * TMP_SCREEN_WIDTH + (x+1);
150 while (h > 0) {
151 int width = w;
152 while (width > 0) {
153 *bak++ = *dst;
154 color = *src++;
155 if (color != 0xFF) // 0xFF = transparent, don't draw
156 *dst = RGB_TO_16(_currentPalette[color].r, _currentPalette[color].g, _currentPalette[color].b);
157 dst++;
158 width--;
159 }
160 src += _mouse_cur_state.w - w;
161 bak += MAX_MOUSE_W - w;
162 dst += TMP_SCREEN_WIDTH - w;
163 h--;
164 }
165
166 SDL_UnlockSurface(sdl_tmpscreen);
167
168 // Mark as dirty
169 add_dirty_rect(x, y, w, h);
170
171 // Finally, set the flag to indicate the mouse has been drawn
172 _mouseDrawn = true;
173}
174
175void OSystem_SDL_Normal::undraw_mouse() {
176 if (!_overlay_visible) {
177 OSystem_SDL_Common::undraw_mouse();
178 }
179
180 if (!_mouseDrawn)
181 return;
182 _mouseDrawn = false;
183
184 if (SDL_LockSurface(sdl_tmpscreen) == -1)
185 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
186
187 uint16 *dst, *bak = (uint16 *)_mouseBackup;
188 const int old_mouse_x = _mouse_old_state.x;
189 const int old_mouse_y = _mouse_old_state.y;
190 const int old_mouse_w = _mouse_old_state.w;
191 const int old_mouse_h = _mouse_old_state.h;
192 int x, y;
193
194 // No need to do clipping here, since draw_mouse() did that already
195
196 dst = (uint16 *)sdl_tmpscreen->pixels + (old_mouse_y+1) * TMP_SCREEN_WIDTH + (old_mouse_x+1);
197 for (y = 0; y < old_mouse_h; ++y, bak += MAX_MOUSE_W, dst += TMP_SCREEN_WIDTH) {
198 for (x = 0; x < old_mouse_w; ++x) {
199 dst[x] = bak[x];
200 }
201 }
202
203 add_dirty_rect(old_mouse_x, old_mouse_y, old_mouse_w, old_mouse_h);
204
205 SDL_UnlockSurface(sdl_tmpscreen);
206}
207
208void OSystem_SDL_Normal::load_gfx_mode() {
209 _forceFull = true;
210 _mode_flags = DF_WANT_RECT_OPTIM | DF_UPDATE_EXPAND_1_PIXEL;
211 _scaleFactor = 2;
212
213 sdl_tmpscreen = NULL;
214 TMP_SCREEN_WIDTH = (_screenWidth + 3);
215// TMP_SCREEN_WIDTH = (_screenWidth);
216
217 //
218 // Create the surface that contains the 8 bit game data
219 //
220 _screen = SDL_CreateRGBSurface(SDL_SWSURFACE, _screenWidth, _screenHeight, 8, 0, 0, 0, 0);
221 if (_screen == NULL)
222 error("_screen failed");
223
224
225 //
226 // Create the surface that contains the scaled graphics in 16 bit mode
227 //
228
229// SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
230// if (fb2gl.screen->format->Rmask == 0x7C00)
231// SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
232// else
233// SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 6 );
234// SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
235// SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
236
237 int gl_flags = FB2GL_320 | FB2GL_RGBA | FB2GL_EXPAND;
238 if (_full_screen) gl_flags |= (FB2GL_FS);
239 // 640x480 screen resolution
240 fb2gl.init(640,480,0,_screenStart? 0: 70,gl_flags);
241
242 SDL_SetGamma(1.25,1.25,1.25);
243
244
245 //
246 // Create the surface used for the graphics in 16 bit before scaling, and also the overlay
247 //
248
249 // Need some extra bytes around when using 2xSaI
250 uint16 *tmp_screen = (uint16*)calloc(TMP_SCREEN_WIDTH*(_screenHeight+3),sizeof(uint16));
251 sdl_tmpscreen = SDL_CreateRGBSurfaceFrom(tmp_screen,
252 TMP_SCREEN_WIDTH, _screenHeight + 3, 16, TMP_SCREEN_WIDTH*2,
253 fb2gl.screen->format->Rmask,
254 fb2gl.screen->format->Gmask,
255 fb2gl.screen->format->Bmask,
256 fb2gl.screen->format->Amask);
257
258
259 if (sdl_tmpscreen == NULL)
260 error("sdl_tmpscreen failed");
261
262 // keyboard cursor control, some other better place for it?
263 km.x_max = _screenWidth * _scaleFactor - 1;
264 km.y_max = _screenHeight * _scaleFactor - 1;
265 km.delay_time = 25;
266 km.last_time = 0;
267
268}
269
270void OSystem_SDL_Normal::unload_gfx_mode() {
271 if (_screen) {
272 SDL_FreeSurface(_screen);
273 _screen = NULL;
274 }
275
276 if (sdl_hwscreen) {
277 SDL_FreeSurface(sdl_hwscreen);
278 sdl_hwscreen = NULL;
279 }
280
281 if (sdl_tmpscreen) {
282 free((uint16*)sdl_tmpscreen->pixels);
283 SDL_FreeSurface(sdl_tmpscreen);
284 sdl_tmpscreen = NULL;
285 }
286}
287
288void OSystem_SDL_Normal::update_screen() {
289
290 // If the shake position changed, fill the dirty area with blackness
291 if (_currentShakePos != _newShakePos) {
292 SDL_Rect blackrect = {0, _screenStart, _screenWidth, _newShakePos+_screenStart};
293 SDL_FillRect(sdl_tmpscreen, &blackrect, 0);
294
295 fb2gl.blit16(sdl_tmpscreen,1,&blackrect,0,0);
296
297 _currentShakePos = _newShakePos;
298
299 _forceFull = true;
300 }
301
302 // Make sure the mouse is drawn, if it should be drawn.
303 draw_mouse();
304
305 // Check whether the palette was changed in the meantime and update the
306 // screen surface accordingly.
307 if (_paletteDirtyEnd != 0) {
308 SDL_SetColors(_screen, _currentPalette + _paletteDirtyStart,
309 _paletteDirtyStart,
310 _paletteDirtyEnd - _paletteDirtyStart);
311
312 _paletteDirtyEnd = 0;
313
314 _forceFull = true;
315 }
316
317 // Force a full redraw if requested
318 if (_forceFull) {
319 _num_dirty_rects = 1;
320
321 _dirty_rect_list[0].x = 0;
322 _dirty_rect_list[0].y = 0;
323 _dirty_rect_list[0].w = _screenWidth;
324 _dirty_rect_list[0].h = _screenHeight;
325 }
326
327 // Only draw anything if necessary
328 if (_num_dirty_rects > 0) {
329
330 SDL_Rect *r;
331 SDL_Rect *last_rect = _dirty_rect_list + _num_dirty_rects;
332
333 // Convert appropriate parts of the 8bpp image into 16bpp
334 if (!_overlay_visible) {
335 SDL_Rect dst;
336 for(r = _dirty_rect_list; r != last_rect; ++r) {
337 dst = *r;
338 dst.x++; // Shift rect by one since 2xSai needs to acces the data around
339 dst.y++; // any pixel to scale it, and we want to avoid mem access crashes.
340 if (SDL_BlitSurface(_screen, r, sdl_tmpscreen, &dst) != 0)
341 error("SDL_BlitSurface failed: %s", SDL_GetError());
342 }
343 }
344
345 // Almost the same thing as SDL_UpdateRects
346 fb2gl.blit16(sdl_tmpscreen,_num_dirty_rects,_dirty_rect_list,0,
347 _currentShakePos+_screenStart);
348 fb2gl.display();
349 }
350
351 _num_dirty_rects = 0;
352 _forceFull = false;
353}
354
355void OSystem_SDL_Normal::hotswap_gfx_mode() {
356 /* We allocate a screen sized bitmap which contains a "backup"
357 * of the screen data during the change. Then we draw that to
358 * the new screen right after it's setup.
359 */
360
361 byte *bak_mem = (byte*)malloc(_screenWidth*_screenHeight);
362
363 get_screen_image(bak_mem);
364
365 unload_gfx_mode();
366 load_gfx_mode();
367
368 // reset palette
369 SDL_SetColors(_screen, _currentPalette, 0, 256);
370
371 // blit image
372 copy_rect(bak_mem, _screenWidth, 0, 0, _screenWidth, _screenHeight);
373 free(bak_mem);
374
375 update_screen();
376}
377
378uint32 OSystem_SDL_Normal::property(int param, Property *value) {
379
380 if (param == PROP_TOGGLE_FULLSCREEN) {
381// assert(sdl_hwscreen != 0);
382 _full_screen ^= true;
383
384// if (!SDL_WM_ToggleFullScreen(sdl_hwscreen)) {
385 // if ToggleFullScreen fails, achieve the same effect with hotswap gfx mode
386// hotswap_gfx_mode();
387
388// }
389 SDL_WM_ToggleFullScreen(fb2gl.screen);
390 return 1;
391 } else if (param == PROP_OVERLAY_IS_565) {
392 assert(sdl_tmpscreen != 0);
393 return (sdl_tmpscreen->format->Rmask != 0x7C00);
394 }
395
396 return OSystem_SDL_Common::property(param, value);
397}
398
399
400void OSystem_SDL_Normal::show_overlay()
401{
402 // hide the mouse
403 undraw_mouse();
404
405 _overlay_visible = true;
406 clear_overlay();
407}
408
409void OSystem_SDL_Normal::hide_overlay()
410{
411 // hide the mouse
412 undraw_mouse();
413
414 _overlay_visible = false;
415 _forceFull = true;
416}
417
418void OSystem_SDL_Normal::clear_overlay()
419{
420 if (!_overlay_visible)
421 return;
422
423 // hide the mouse
424 undraw_mouse();
425
426 // Clear the overlay by making the game screen "look through" everywhere.
427 SDL_Rect src, dst;
428 src.x = src.y = 0;
429 dst.x = dst.y = 1;
430 src.w = dst.w = _screenWidth;
431 src.h = dst.h = _screenHeight;
432 if (SDL_BlitSurface(_screen, &src, sdl_tmpscreen, &dst) != 0)
433 error("SDL_BlitSurface failed: %s", SDL_GetError());
434
435 _forceFull = true;
436}
437
438void OSystem_SDL_Normal::grab_overlay(int16 *buf, int pitch)
439{
440 if (!_overlay_visible)
441 return;
442
443 if (sdl_tmpscreen == NULL)
444 return;
445
446 // hide the mouse
447 undraw_mouse();
448
449 if (SDL_LockSurface(sdl_tmpscreen) == -1)
450 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
451
452 int16 *src = (int16 *)sdl_tmpscreen->pixels + TMP_SCREEN_WIDTH + 1;
453 int h = _screenHeight;
454 do {
455 memcpy(buf, src, _screenWidth*2);
456 src += TMP_SCREEN_WIDTH;
457 buf += pitch;
458 } while (--h);
459
460 SDL_UnlockSurface(sdl_tmpscreen);
461}
462
463void OSystem_SDL_Normal::copy_rect_overlay(const int16 *buf, int pitch, int x, int y, int w, int h)
464{
465 if (!_overlay_visible)
466 return;
467
468 if (sdl_tmpscreen == NULL)
469 return;
470
471 // Clip the coordinates
472 if (x < 0) { w+=x; buf-=x; x = 0; }
473 if (y < 0) { h+=y; buf-=y*pitch; y = 0; }
474 if (w > _screenWidth-x) { w = _screenWidth - x; }
475 if (h > _screenHeight-y) { h = _screenHeight - y; }
476 if (w <= 0 || h <= 0)
477 return;
478
479 // Mark the modified region as dirty
480 cksum_valid = false;
481 add_dirty_rect(x, y, w, h);
482
483 /* FIXME: undraw mouse only if the draw rect intersects with the mouse rect */
484 undraw_mouse();
485
486 if (SDL_LockSurface(sdl_tmpscreen) == -1)
487 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
488
489 int16 *dst = (int16 *)sdl_tmpscreen->pixels + (y+1) * TMP_SCREEN_WIDTH + (x+1);
490 do {
491 memcpy(dst, buf, w*2);
492 dst += TMP_SCREEN_WIDTH;
493 buf += pitch;
494 } while (--h);
495
496 SDL_UnlockSurface(sdl_tmpscreen);
497}
498
499