Ticket #8104: sdl_gl.cpp

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

SDL with OpenGL (final)

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 SDL_Rect blackrect2 = {0, _screenStart, _screenWidth, 15};
290
291 // If the shake position changed, fill the dirty area with blackness
292 if (_currentShakePos != _newShakePos) {
293 SDL_Rect blackrect = {0, _screenStart, _screenWidth, _newShakePos+_screenStart};
294 SDL_FillRect(sdl_tmpscreen, &blackrect, 0);
295
296 fb2gl.blit16(sdl_tmpscreen,1,&blackrect,0,0);
297
298 _currentShakePos = _newShakePos;
299
300 _forceFull = true;
301 }
302
303 // Make sure the mouse is drawn, if it should be drawn.
304 draw_mouse();
305
306 // Check whether the palette was changed in the meantime and update the
307 // screen surface accordingly.
308 if (_paletteDirtyEnd != 0) {
309 SDL_SetColors(_screen, _currentPalette + _paletteDirtyStart,
310 _paletteDirtyStart,
311 _paletteDirtyEnd - _paletteDirtyStart);
312
313 _paletteDirtyEnd = 0;
314
315 _forceFull = true;
316 }
317
318 // Force a full redraw if requested
319 if (_forceFull) {
320 _num_dirty_rects = 1;
321
322 _dirty_rect_list[0].x = 0;
323 _dirty_rect_list[0].y = 0;
324 _dirty_rect_list[0].w = _screenWidth;
325 _dirty_rect_list[0].h = _screenHeight;
326 }
327
328 // Only draw anything if necessary
329 if (_num_dirty_rects > 0) {
330
331 SDL_Rect *r;
332 SDL_Rect *last_rect = _dirty_rect_list + _num_dirty_rects;
333
334 // Convert appropriate parts of the 8bpp image into 16bpp
335 if (!_overlay_visible) {
336 SDL_Rect dst;
337 for(r = _dirty_rect_list; r != last_rect; ++r) {
338 dst = *r;
339// dst.x++; // Shift rect by one since 2xSai needs to acces the data around
340// dst.y++; // any pixel to scale it, and we want to avoid mem access crashes.
341 if (SDL_BlitSurface(_screen, r, sdl_tmpscreen, &dst) != 0)
342 error("SDL_BlitSurface failed: %s", SDL_GetError());
343 }
344 }
345
346 // Almost the same thing as SDL_UpdateRects
347 fb2gl.blit16(sdl_tmpscreen,_num_dirty_rects,_dirty_rect_list,0,
348 _currentShakePos+_screenStart);
349
350
351 SDL_FillRect(sdl_tmpscreen, &blackrect2, 0);
352 fb2gl.blit16(sdl_tmpscreen,1,&blackrect2,0,_screenHeight);
353
354 fb2gl.display();
355 }
356
357 _num_dirty_rects = 0;
358 _forceFull = false;
359}
360
361void OSystem_SDL_Normal::hotswap_gfx_mode() {
362 /* We allocate a screen sized bitmap which contains a "backup"
363 * of the screen data during the change. Then we draw that to
364 * the new screen right after it's setup.
365 */
366
367 byte *bak_mem = (byte*)malloc(_screenWidth*_screenHeight);
368
369 get_screen_image(bak_mem);
370
371 unload_gfx_mode();
372 load_gfx_mode();
373
374 // reset palette
375 SDL_SetColors(_screen, _currentPalette, 0, 256);
376
377 // blit image
378 copy_rect(bak_mem, _screenWidth, 0, 0, _screenWidth, _screenHeight);
379 free(bak_mem);
380
381 update_screen();
382}
383
384uint32 OSystem_SDL_Normal::property(int param, Property *value) {
385
386 if (param == PROP_TOGGLE_FULLSCREEN) {
387// assert(sdl_hwscreen != 0);
388 _full_screen ^= true;
389
390// if (!SDL_WM_ToggleFullScreen(sdl_hwscreen)) {
391 // if ToggleFullScreen fails, achieve the same effect with hotswap gfx mode
392// hotswap_gfx_mode();
393
394// }
395 SDL_WM_ToggleFullScreen(fb2gl.screen);
396 return 1;
397 } else if (param == PROP_OVERLAY_IS_565) {
398 assert(sdl_tmpscreen != 0);
399 return (sdl_tmpscreen->format->Rmask != 0x7C00);
400 }
401
402 return OSystem_SDL_Common::property(param, value);
403}
404
405
406void OSystem_SDL_Normal::show_overlay()
407{
408 // hide the mouse
409 undraw_mouse();
410
411 _overlay_visible = true;
412 clear_overlay();
413}
414
415void OSystem_SDL_Normal::hide_overlay()
416{
417 // hide the mouse
418 undraw_mouse();
419
420 _overlay_visible = false;
421 _forceFull = true;
422}
423
424void OSystem_SDL_Normal::clear_overlay()
425{
426 if (!_overlay_visible)
427 return;
428
429 // hide the mouse
430 undraw_mouse();
431
432 // Clear the overlay by making the game screen "look through" everywhere.
433 SDL_Rect src, dst;
434 src.x = src.y = 0;
435// dst.x = dst.y = 1;
436 dst.x = dst.y = 0;
437 src.w = dst.w = _screenWidth;
438 src.h = dst.h = _screenHeight;
439 if (SDL_BlitSurface(_screen, &src, sdl_tmpscreen, &dst) != 0)
440 error("SDL_BlitSurface failed: %s", SDL_GetError());
441
442 _forceFull = true;
443}
444
445void OSystem_SDL_Normal::grab_overlay(int16 *buf, int pitch)
446{
447 if (!_overlay_visible)
448 return;
449
450 if (sdl_tmpscreen == NULL)
451 return;
452
453 // hide the mouse
454 undraw_mouse();
455
456 if (SDL_LockSurface(sdl_tmpscreen) == -1)
457 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
458
459 int16 *src = (int16 *)sdl_tmpscreen->pixels + TMP_SCREEN_WIDTH + 1;
460 int h = _screenHeight;
461 do {
462 memcpy(buf, src, _screenWidth*2);
463 src += TMP_SCREEN_WIDTH;
464 buf += pitch;
465 } while (--h);
466
467 SDL_UnlockSurface(sdl_tmpscreen);
468}
469
470void OSystem_SDL_Normal::copy_rect_overlay(const int16 *buf, int pitch, int x, int y, int w, int h)
471{
472 if (!_overlay_visible)
473 return;
474
475 if (sdl_tmpscreen == NULL)
476 return;
477
478 // Clip the coordinates
479 if (x < 0) { w+=x; buf-=x; x = 0; }
480 if (y < 0) { h+=y; buf-=y*pitch; y = 0; }
481 if (w > _screenWidth-x) { w = _screenWidth - x; }
482 if (h > _screenHeight-y) { h = _screenHeight - y; }
483 if (w <= 0 || h <= 0)
484 return;
485
486 // Mark the modified region as dirty
487 cksum_valid = false;
488 add_dirty_rect(x, y, w, h);
489
490 /* FIXME: undraw mouse only if the draw rect intersects with the mouse rect */
491 undraw_mouse();
492
493 if (SDL_LockSurface(sdl_tmpscreen) == -1)
494 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
495
496 int16 *dst = (int16 *)sdl_tmpscreen->pixels + (y+1) * TMP_SCREEN_WIDTH + (x+1);
497 do {
498 memcpy(dst, buf, w*2);
499 dst += TMP_SCREEN_WIDTH;
500 buf += pitch;
501 } while (--h);
502
503 SDL_UnlockSurface(sdl_tmpscreen);
504}
505
506