Ticket #8101: sdl_gl.cpp

File sdl_gl.cpp, 12.2 KB (added by SF/luke_br, 22 years ago)

SDL Object for OpenGL support

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