Ticket #7970: sdl_gl.2.cpp

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

Fullscreen Toggle and Gamma Correction

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/sdl.cpp,v 1.124 2002/06/04 18:18:43 bbrox Exp $
20 *
21 */
22
23#include "stdafx.h"
24#include "scumm.h"
25#include "mididrv.h"
26#include "SDL_thread.h"
27#include "gameDetector.h"
28
29#include "scummvm.xpm"
30
31#include <SDL.h>
32
33/* Use OpenGL 1.1 */
34#define OGL_1_1
35
36#include "fb2opengl11.h"
37
38
39#define MAX(a,b) (((a)<(b)) ? (b) : (a))
40#define MIN(a,b) (((a)>(b)) ? (b) : (a))
41
42class OSystem_SDL : public OSystem {
43public:
44 // Set colors of the palette
45 void set_palette(const byte *colors, uint start, uint num);
46
47 // Set the size of the video bitmap.
48 // Typically, 320x200
49 void init_size(uint w, uint h);
50
51 // Draw a bitmap to screen.
52 // The screen will not be updated to reflect the new bitmap
53 void copy_rect(const byte *buf, int pitch, int x, int y, int w, int h);
54
55 // Update the dirty areas of the screen
56 void update_screen();
57
58 // Either show or hide the mouse cursor
59 bool show_mouse(bool visible);
60
61 // Set the position of the mouse cursor
62 void set_mouse_pos(int x, int y);
63
64 // Set the bitmap that's used when drawing the cursor.
65 void set_mouse_cursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y);
66
67 // Shaking is used in SCUMM. Set current shake position.
68 void set_shake_pos(int shake_pos);
69
70 // Get the number of milliseconds since the program was started.
71 uint32 get_msecs();
72
73 // Delay for a specified amount of milliseconds
74 void delay_msecs(uint msecs);
75
76 // Create a thread
77 void *create_thread(ThreadProc *proc, void *param);
78
79 // Get the next event.
80 // Returns true if an event was retrieved.
81 bool poll_event(Event *event);
82
83 // Set function that generates samples
84 bool set_sound_proc(void *param, SoundProc *proc, byte sound);
85
86 // Poll cdrom status
87 // Returns true if cd audio is playing
88 bool poll_cdrom();
89
90 // Play cdrom audio track
91 void play_cdrom(int track, int num_loops, int start_frame, int end_frame);
92
93 // Stop cdrom audio track
94 void stop_cdrom();
95
96 // Update cdrom audio status
97 void update_cdrom();
98
99 // Quit
100 void quit();
101
102 // Set a parameter
103 uint32 property(int param, Property *value);
104
105 // Add a callback timer
106 void set_timer(int timer, int (*callback)(int));
107
108 // Mutex handling
109 void *create_mutex(void);
110 void lock_mutex(void *mutex);
111 void unlock_mutex(void *mutex);
112 void delete_mutex(void *mutex);
113
114 static OSystem *create(int gfx_mode, bool full_screen);
115
116private:
117 typedef void TwoXSaiProc(uint8 *srcPtr, uint32 srcPitch, uint8 *deltaPtr,
118 uint8 *dstPtr, uint32 dstPitch, int width, int height);
119
120 SDL_Surface *sdl_screen;
121 SDL_Surface *sdl_tmpscreen;
122 SDL_CD *cdrom;
123
124 enum {
125 DF_WANT_RECT_OPTIM = 1 << 0,
126 DF_REAL_8BIT = 1 << 1,
127 DF_SEPARATE_TEMPSCREEN = 1 << 2,
128 DF_UPDATE_EXPAND_1_PIXEL = 1 << 3
129 };
130
131 int _mode;
132 bool _full_screen;
133 bool _mouse_visible;
134 bool _mouse_drawn;
135 uint32 _mode_flags;
136
137 bool force_full; //Force full redraw on next update_screen
138 bool cksum_valid;
139
140 enum {
141 NUM_DIRTY_RECT = 100,
142
143 MAX_MOUSE_W = 40,
144 MAX_MOUSE_H = 40,
145 MAX_SCALING = 3
146 };
147
148 int SCREEN_WIDTH, SCREEN_HEIGHT, CKSUM_NUM;
149 SDL_Rect *dirty_rect_list;
150 int num_dirty_rects;
151 uint32 *dirty_checksums;
152
153 int scaling;
154
155 /* CD Audio */
156 int cd_track, cd_num_loops, cd_start_frame, cd_end_frame;
157 Uint32 cd_end_time, cd_stop_time, cd_next_second;
158
159 struct MousePos {
160 int16 x,y,w,h;
161 };
162
163 byte *_mouse_data;
164 byte *_mouse_backup;
165 MousePos _mouse_cur_state;
166 MousePos _mouse_old_state;
167 int16 _mouse_hotspot_x;
168 int16 _mouse_hotspot_y;
169 int _current_shake_pos;
170 int _new_shake_pos;
171 TwoXSaiProc *_sai_func;
172 SDL_Color *_cur_pal;
173
174 uint _palette_changed_first, _palette_changed_last;
175
176 OSystem_SDL() : _current_shake_pos(0), _new_shake_pos(0) {}
177
178 void add_dirty_rgn_auto(const byte *buf);
179 void mk_checksums(const byte *buf);
180
181 static void fill_sound(void *userdata, Uint8 * stream, int len);
182
183 void add_dirty_rect(int x, int y, int w, int h);
184
185 void draw_mouse();
186 void undraw_mouse();
187
188 void load_gfx_mode();
189 void unload_gfx_mode();
190
191 void hotswap_gfx_mode();
192
193 void get_320x200_image(byte *buf);
194
195 void setup_icon();
196};
197
198int Init_2xSaI (uint32 BitFormat);
199void _2xSaI(uint8 *srcPtr, uint32 srcPitch, uint8 *deltaPtr, uint8 *dstPtr,
200 uint32 dstPitch, int width, int height);
201void Super2xSaI(uint8 *srcPtr, uint32 srcPitch, uint8 *deltaPtr,
202 uint8 *dstPtr, uint32 dstPitch, int width, int height);
203void SuperEagle(uint8 *srcPtr, uint32 srcPitch, uint8 *deltaPtr,
204 uint8 *dstPtr, uint32 dstPitch, int width, int height);
205void AdvMame2x(uint8 *srcPtr, uint32 srcPitch, uint8 *null,
206 uint8 *dstPtr, uint32 dstPitch, int width, int height);
207void Normal1x(uint8 *srcPtr, uint32 srcPitch, uint8 *null,
208 uint8 *dstPtr, uint32 dstPitch, int width, int height);
209void Normal2x(uint8 *srcPtr, uint32 srcPitch, uint8 *null,
210 uint8 *dstPtr, uint32 dstPitch, int width, int height);
211void Normal3x(uint8 *srcPtr, uint32 srcPitch, uint8 *null,
212 uint8 *dstPtr, uint32 dstPitch, int width, int height);
213
214void atexit_proc() {
215 SDL_ShowCursor(SDL_ENABLE);
216 SDL_Quit();
217}
218
219OSystem *OSystem_SDL::create(int gfx_mode, bool full_screen) {
220 OSystem_SDL *syst = new OSystem_SDL();
221 syst->_mode = gfx_mode;
222 syst->_full_screen = full_screen;
223
224 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) ==-1) {
225 error("Could not initialize SDL: %s.\n", SDL_GetError());
226 }
227
228#ifdef WIN32 /* Use waveout on win32, not */
229 SDL_AudioInit("waveout"); /* dsound - unfortunatly dsound */
230#endif /* doesn't do COOPERATIVE mode*/
231
232 SDL_ShowCursor(SDL_DISABLE);
233
234 /* Setup the icon */
235 syst->setup_icon();
236
237 /* Clean up on exit */
238 atexit(atexit_proc);
239
240 return syst;
241}
242
243void OSystem_SDL::set_timer(int timer, int (*callback)(int)) {
244 SDL_SetTimer(timer, (SDL_TimerCallback) callback);
245}
246OSystem *OSystem_SDL_create(int gfx_mode, bool full_screen) {
247 return OSystem_SDL::create(gfx_mode, full_screen);
248}
249
250void OSystem_SDL::set_palette(const byte *colors, uint start, uint num) {
251 const byte *b = colors;
252 uint i;
253 SDL_Color *base = _cur_pal + start;
254 for(i=0;i!=num;i++) {
255 fb2gl_palette(i+start,b[0],b[1],b[2]);
256 b += 4;
257 }
258
259 if (start < _palette_changed_first)
260 _palette_changed_first = start;
261
262 if (start + num > _palette_changed_last)
263 _palette_changed_last = start + num;
264}
265
266void OSystem_SDL::load_gfx_mode() {
267 force_full = true;
268 scaling = 1;
269 _mode_flags = 0;
270
271 _sai_func = NULL;
272 sdl_tmpscreen = NULL;
273
274 /* It's easier to work with 8 bit (256 colors) */
275 _mode_flags |= DF_REAL_8BIT;
276
277 sdl_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 8, 0, 0, 0, 0);
278 if (sdl_screen == NULL)
279 error("sdl_screen failed failed");
280
281 _sai_func = Normal1x;
282
283 _mode_flags = DF_WANT_RECT_OPTIM | DF_REAL_8BIT;
284
285 #ifdef OGL_1_1
286 fb2gl_init(640,480,0,70, FB2GL_FS | FB2GL_320 | FB2GL_PITCH | FB2GL_RGBA | FB2GL_EXPAND);
287 #else
288 fb2gl_init(640,480,0,70, FB2GL_FS | FB2GL_320 | FB2GL_PITCH);
289 #endif
290
291 SDL_SetGamma(1.25,1.25,1.25);
292
293 sdl_tmpscreen = sdl_screen;
294}
295
296void OSystem_SDL::unload_gfx_mode() {
297 SDL_FreeSurface(sdl_screen);
298 sdl_screen = NULL;
299
300 if (_mode_flags & DF_SEPARATE_TEMPSCREEN) {
301 free((uint16*)sdl_tmpscreen->pixels);
302 SDL_FreeSurface(sdl_tmpscreen);
303 }
304 sdl_tmpscreen = NULL;
305}
306
307void OSystem_SDL::init_size(uint w, uint h) {
308 //if (w != SCREEN_WIDTH && h != SCREEN_HEIGHT)
309 // error("320x200 is the only game resolution supported");
310
311 SCREEN_WIDTH = w;
312 SCREEN_HEIGHT = h;
313 CKSUM_NUM = (SCREEN_WIDTH*SCREEN_HEIGHT/(8*8));
314 /* allocate palette, it needs to be persistent across
315 * driver changes, so i'll alloc it here */
316 _cur_pal = (SDL_Color*)calloc(sizeof(SDL_Color), 256);
317
318 dirty_rect_list = (SDL_Rect*)calloc(NUM_DIRTY_RECT, sizeof(SDL_Rect));
319 _mouse_backup = (byte*)malloc(MAX_MOUSE_W * MAX_MOUSE_H * MAX_SCALING);
320 dirty_checksums = (uint32*)calloc(CKSUM_NUM*2, sizeof(uint32));
321
322 load_gfx_mode();
323}
324
325void OSystem_SDL::copy_rect(const byte *buf, int pitch, int x, int y, int w, int h) {
326 if (sdl_screen == NULL)
327 return;
328
329 if (pitch == SCREEN_WIDTH && x==0 && y==0 && w==SCREEN_WIDTH && h==SCREEN_HEIGHT && _mode_flags&DF_WANT_RECT_OPTIM) {
330 /* Special, optimized case for full screen updates.
331 * It tries to determine what areas were actually changed,
332 * and just updates those, on the actual display. */
333 add_dirty_rgn_auto(buf);
334 } else {
335 /* Clip the coordinates */
336 if (x < 0) { w+=x; buf-=x; x = 0; }
337 if (y < 0) { h+=y; buf-=y*pitch; y = 0; }
338 if (w > SCREEN_WIDTH-x) { w = SCREEN_WIDTH - x; }
339 if (h > SCREEN_HEIGHT-y) { h = SCREEN_HEIGHT - y; }
340
341 if (w <= 0 || h <= 0)
342 return;
343
344 cksum_valid = false;
345 add_dirty_rect(x, y, w, h);
346 }
347
348 /* FIXME: undraw mouse only if the draw rect intersects with the mouse rect */
349 if (_mouse_drawn)
350 undraw_mouse();
351
352 if (SDL_LockSurface(sdl_screen) == -1)
353 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
354
355 byte *dst = (byte *)sdl_screen->pixels + y * SCREEN_WIDTH + x;
356 do {
357 memcpy(dst, buf, w);
358 dst += SCREEN_WIDTH;
359 buf += pitch;
360 } while (--h);
361
362 SDL_UnlockSurface(sdl_screen);
363}
364
365
366void OSystem_SDL::add_dirty_rect(int x, int y, int w, int h) {
367 if (force_full)
368 return;
369
370 if (num_dirty_rects == NUM_DIRTY_RECT)
371 force_full = true;
372 else {
373 SDL_Rect *r = &dirty_rect_list[num_dirty_rects++];
374
375 /* Update the dirty region by 1 pixel for graphics drivers
376 * that "smear" the screen */
377 if (_mode_flags & DF_UPDATE_EXPAND_1_PIXEL) {
378 x--;
379 y--;
380 w+=2;
381 h+=2;
382 }
383
384 /* clip */
385 if (x < 0) { w+=x; x=0; }
386 if (y < 0) { h+=y; y=0; }
387 if (w > SCREEN_WIDTH-x) { w = SCREEN_WIDTH - x; }
388 if (h > SCREEN_HEIGHT-y) { h = SCREEN_HEIGHT - y; }
389
390 r->x = x;
391 r->y = y;
392 r->w = w;
393 r->h = h;
394 }
395}
396
397#define ROL(a,n) a = (a<<(n)) | (a>>(32-(n)))
398#define DOLINE(x) a ^= ((uint32*)buf)[0+(x)*(SCREEN_WIDTH/4)]; b ^= ((uint32*)buf)[1+(x)*(SCREEN_WIDTH/4)]
399void OSystem_SDL::mk_checksums(const byte *buf) {
400 uint32 *sums = dirty_checksums;
401 uint x,y;
402 const uint last_x = (uint)SCREEN_WIDTH/8;
403 const uint last_y = (uint)SCREEN_HEIGHT/8;
404
405 /* the 8x8 blocks in buf are enumerated starting in the top left corner and
406 * reading each line at a time from left to right */
407 for(y=0; y != last_y; y++, buf+=SCREEN_WIDTH*(8-1))
408 for(x=0; x != last_x; x++, buf+=8) {
409 uint32 a = x;
410 uint32 b = y;
411
412 DOLINE(0); ROL(a,13); ROL(b,11);
413 DOLINE(2); ROL(a,13); ROL(b,11);
414 DOLINE(4); ROL(a,13); ROL(b,11);
415 DOLINE(6); ROL(a,13); ROL(b,11);
416
417 a*=0xDEADBEEF;
418 b*=0xBAADF00D;
419
420 DOLINE(1); ROL(a,13); ROL(b,11);
421 DOLINE(3); ROL(a,13); ROL(b,11);
422 DOLINE(5); ROL(a,13); ROL(b,11);
423 DOLINE(7); ROL(a,13); ROL(b,11);
424
425 /* output the checksum for this block */
426 *sums++=a+b;
427 }
428}
429#undef DOLINE
430#undef ROL
431
432
433void OSystem_SDL::add_dirty_rgn_auto(const byte *buf) {
434 assert( ((uint32)buf & 3) == 0);
435
436 /* generate a table of the checksums */
437 mk_checksums(buf);
438
439 if (!cksum_valid) {
440 force_full = true;
441 cksum_valid = true;
442 }
443
444 /* go through the checksum list, compare it with the previous checksums,
445 and add all dirty rectangles to a list. try to combine small rectangles
446 into bigger ones in a simple way */
447 if (!force_full) {
448 int x,y,w;
449 uint32 *ck = dirty_checksums;
450
451 for(y=0; y!=SCREEN_HEIGHT/8; y++) {
452 for(x=0; x!=SCREEN_WIDTH/8; x++,ck++) {
453 if (ck[0] != ck[CKSUM_NUM]) {
454 /* found a dirty 8x8 block, now go as far to the right as possible,
455 and at the same time, unmark the dirty status by setting old to new. */
456 w=0;
457 do {
458 ck[w+CKSUM_NUM] = ck[w];
459 w++;
460 } while (x+w != SCREEN_WIDTH/8 && ck[w] != ck[w+CKSUM_NUM]);
461
462 add_dirty_rect(x*8, y*8, w*8, 8);
463
464 if (force_full)
465 goto get_out;
466 }
467 }
468 }
469 } else {
470 get_out:;
471 /* Copy old checksums to new */
472 memcpy(dirty_checksums + CKSUM_NUM, dirty_checksums, CKSUM_NUM * sizeof(uint32));
473 }
474}
475
476void OSystem_SDL::update_screen() {
477
478 /* First make sure the mouse is drawn, if it should be drawn. */
479 draw_mouse();
480
481 /* If the shake position changed, fill the dirty area with blackness */
482 if (_current_shake_pos != _new_shake_pos) {
483
484 _current_shake_pos = _new_shake_pos;
485
486 }
487
488 /* Palette update in case we are in "real" 8 bit color mode.
489 * Must take place after the screen data was updated, since with
490 * "real" 8bit mode, palatte changes may be visible immediatly,
491 * and we want to avoid any ugly effects.
492 */
493 if (_palette_changed_last != 0) {
494 fb2gl_set_palette(_palette_changed_first,
495 _palette_changed_last - _palette_changed_first);
496
497 _palette_changed_last = 0;
498 }
499
500 fb2gl_update(sdl_tmpscreen->pixels,320,200,320,0,_current_shake_pos);
501
502}
503
504bool OSystem_SDL::show_mouse(bool visible) {
505 if (_mouse_visible == visible)
506 return visible;
507
508 bool last = _mouse_visible;
509 _mouse_visible = visible;
510
511 if (visible)
512 draw_mouse();
513 else
514 undraw_mouse();
515
516 return last;
517}
518
519void OSystem_SDL::set_mouse_pos(int x, int y) {
520 if (x != _mouse_cur_state.x || y != _mouse_cur_state.y) {
521 _mouse_cur_state.x = x;
522 _mouse_cur_state.y = y;
523 undraw_mouse();
524 }
525}
526
527void OSystem_SDL::set_mouse_cursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y) {
528 _mouse_cur_state.w = w;
529 _mouse_cur_state.h = h;
530
531 _mouse_hotspot_x = hotspot_x;
532 _mouse_hotspot_y = hotspot_y;
533
534 _mouse_data = (byte*)buf;
535
536 undraw_mouse();
537}
538
539void OSystem_SDL::set_shake_pos(int shake_pos) {
540 _new_shake_pos = shake_pos;
541}
542
543uint32 OSystem_SDL::get_msecs() {
544 return SDL_GetTicks();
545}
546
547void OSystem_SDL::delay_msecs(uint msecs) {
548 SDL_Delay(msecs);
549}
550
551void *OSystem_SDL::create_thread(ThreadProc *proc, void *param) {
552 return SDL_CreateThread(proc, param);
553}
554
555int mapKey(int key, byte mod)
556{
557 if (key >= SDLK_F1 && key <= SDLK_F9) {
558 return key - SDLK_F1 + 315;
559 } else if (key >= 'a' && key <= 'z' && mod & KMOD_SHIFT) {
560 key &= ~0x20;
561 } else if (key >= SDLK_NUMLOCK && key <= SDLK_EURO)
562 return 0;
563 return key;
564}
565
566bool OSystem_SDL::poll_event(Event *event) {
567 SDL_Event ev;
568
569 for(;;) {
570 if (!SDL_PollEvent(&ev))
571 return false;
572
573 switch(ev.type) {
574 case SDL_KEYDOWN: {
575 byte b = 0;
576 if (ev.key.keysym.mod & KMOD_SHIFT) b |= KBD_SHIFT;
577 if (ev.key.keysym.mod & KMOD_CTRL) b |= KBD_CTRL;
578 if (ev.key.keysym.mod & KMOD_ALT) b |= KBD_ALT;
579 event->kbd.flags = b;
580
581 /* internal keypress? */
582 if (b == KBD_ALT && ev.key.keysym.sym==SDLK_RETURN) {
583 property(PROP_TOGGLE_FULLSCREEN, NULL);
584 break;
585 }
586
587 if (b == KBD_CTRL && ev.key.keysym.sym=='z') {
588 quit();
589 break;
590 }
591
592 if (b == (KBD_CTRL|KBD_ALT) &&
593 (ev.key.keysym.sym>='1') && (ev.key.keysym.sym<='7')) {
594 Property prop;
595 prop.gfx_mode = ev.key.keysym.sym - '1';
596 property(PROP_SET_GFX_MODE, &prop);
597 break;
598 }
599
600
601 event->event_code = EVENT_KEYDOWN;
602 event->kbd.keycode = ev.key.keysym.sym;
603 event->kbd.ascii = mapKey(ev.key.keysym.sym, ev.key.keysym.mod);
604 return true;
605 }
606
607 case SDL_MOUSEMOTION:
608 event->event_code = EVENT_MOUSEMOVE;
609 event->mouse.x = ev.motion.x;
610 event->mouse.y = ev.motion.y;
611
612 event->mouse.x /= scaling;
613 event->mouse.y /= scaling;
614
615 return true;
616
617 case SDL_MOUSEBUTTONDOWN:
618 if (ev.button.button == SDL_BUTTON_LEFT)
619 event->event_code = EVENT_LBUTTONDOWN;
620 else if (ev.button.button == SDL_BUTTON_RIGHT)
621 event->event_code = EVENT_RBUTTONDOWN;
622 else
623 break;
624 event->mouse.x = ev.button.x;
625 event->mouse.y = ev.button.y;
626 event->mouse.x /= scaling;
627 event->mouse.y /= scaling;
628
629 return true;
630
631 case SDL_MOUSEBUTTONUP:
632 if (ev.button.button == SDL_BUTTON_LEFT)
633 event->event_code = EVENT_LBUTTONUP;
634 else if (ev.button.button == SDL_BUTTON_RIGHT)
635 event->event_code = EVENT_RBUTTONUP;
636 else
637 break;
638 event->mouse.x = ev.button.x;
639 event->mouse.y = ev.button.y;
640 event->mouse.x /= scaling;
641 event->mouse.y /= scaling;
642 return true;
643
644 case SDL_QUIT:
645 quit();
646 }
647 }
648}
649
650bool OSystem_SDL::set_sound_proc(void *param, SoundProc *proc, byte format) {
651 SDL_AudioSpec desired;
652
653 /* only one format supported at the moment */
654
655 desired.freq = SAMPLES_PER_SEC;
656 desired.format = AUDIO_S16SYS;
657 desired.channels = 2;
658 desired.samples = 2048;
659 desired.callback = proc;
660 desired.userdata = param;
661 if (SDL_OpenAudio(&desired, NULL) != 0) {
662 return false;
663 }
664 SDL_PauseAudio(0);
665 return true;
666}
667
668
669/* retrieve the 320x200 bitmap currently being displayed */
670void OSystem_SDL::get_320x200_image(byte *buf) {
671 /* make sure the mouse is gone */
672 undraw_mouse();
673
674 if (SDL_LockSurface(sdl_screen) == -1)
675 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
676
677 memcpy(buf, sdl_screen->pixels, SCREEN_WIDTH*SCREEN_HEIGHT);
678
679 SDL_UnlockSurface(sdl_screen);
680}
681
682void OSystem_SDL::hotswap_gfx_mode() {
683 /* hmm, need to allocate a 320x200 bitmap
684 * which will contain the "backup" of the screen during the change.
685 * then draw that to the new screen right after it's setup.
686 */
687
688 byte *bak_mem = (byte*)malloc(SCREEN_WIDTH*SCREEN_HEIGHT);
689
690 get_320x200_image(bak_mem);
691
692 unload_gfx_mode();
693 load_gfx_mode();
694
695 fb2gl_set_palette(0,256);
696 fb2gl_update(sdl_tmpscreen->pixels,320,200,320,0,_current_shake_pos);
697
698 /* blit image */
699 OSystem_SDL::copy_rect(bak_mem, SCREEN_WIDTH, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
700 free(bak_mem);
701
702 OSystem_SDL::update_screen();
703}
704
705uint32 OSystem_SDL::property(int param, Property *value) {
706 switch(param) {
707
708 case PROP_TOGGLE_FULLSCREEN:
709 _full_screen ^= true;
710 SDL_WM_ToggleFullScreen(screen);
711 return 1;
712
713 case PROP_GET_FULLSCREEN:
714 return _full_screen;
715
716 case PROP_SET_WINDOW_CAPTION:
717 SDL_WM_SetCaption(value->caption, value->caption);
718 return 1;
719
720 case PROP_OPEN_CD:
721 if (SDL_InitSubSystem(SDL_INIT_CDROM) == -1)
722 cdrom = NULL;
723 else {
724 cdrom = SDL_CDOpen(value->cd_num);
725 /* Did if open? Check if cdrom is NULL */
726 if (!cdrom) {
727 warning("Couldn't open drive: %s\n", SDL_GetError());
728 }
729 }
730 break;
731
732 case PROP_SET_GFX_MODE:
733 if (value->gfx_mode >= 7)
734 return 0;
735
736 _mode = value->gfx_mode;
737 hotswap_gfx_mode();
738
739 return 1;
740
741 case PROP_SHOW_DEFAULT_CURSOR:
742 SDL_ShowCursor(value->show_cursor ? SDL_ENABLE : SDL_DISABLE);
743 break;
744
745 case PROP_GET_SAMPLE_RATE:
746 return SAMPLES_PER_SEC;
747 }
748
749 return 0;
750}
751
752void OSystem_SDL::quit() {
753 if(cdrom) {
754 SDL_CDStop(cdrom);
755 SDL_CDClose(cdrom);
756 }
757 unload_gfx_mode();
758 exit(1);
759}
760
761void OSystem_SDL::draw_mouse() {
762 if (_mouse_drawn || !_mouse_visible)
763 return;
764
765 int x = _mouse_cur_state.x - _mouse_hotspot_x;
766 int y = _mouse_cur_state.y - _mouse_hotspot_y;
767 int w = _mouse_cur_state.w;
768 int h = _mouse_cur_state.h;
769 byte color;
770 byte *src = _mouse_data; // Image representing the mouse
771 byte *bak = _mouse_backup; // Surface used to backup the area obscured by the mouse
772 byte *dst; // Surface we are drawing into
773
774 // clip the mouse rect, and addjust the src pointer accordingly
775 if (x < 0) {
776 w += x;
777 src -= x;
778 x = 0;
779 }
780 if (y < 0) {
781 h += y;
782 src -= y * _mouse_cur_state.w;
783 y = 0;
784 }
785 if (w > SCREEN_WIDTH - x)
786 w = SCREEN_WIDTH - x;
787 if (h > SCREEN_HEIGHT - y)
788 h = SCREEN_HEIGHT - y;
789
790 // Store the bounding box so that undraw mouse can restore the area the
791 // mouse currently covers to its original content.
792 _mouse_old_state.x = x;
793 _mouse_old_state.y = y;
794 _mouse_old_state.w = w;
795 _mouse_old_state.h = h;
796
797 // Quick check to see if anything has to be drawn at all
798 if (w <= 0 || h <= 0)
799 return;
800
801 // Draw the mouse cursor; backup the covered area in "bak"
802
803 if (SDL_LockSurface(sdl_screen) == -1)
804 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
805
806 add_dirty_rect(x, y, w, h);
807
808 dst = (byte *)sdl_screen->pixels + y * SCREEN_WIDTH + x;
809 while (h > 0) {
810 int width = w;
811 while (width > 0) {
812 *bak++ = *dst;
813 color = *src++;
814 if (color != 0xFF) // 0xFF = transparent, don't draw
815 *dst = color;
816 dst++;
817 width--;
818 }
819 src += _mouse_cur_state.w - w;
820 bak += MAX_MOUSE_W - w;
821 dst += SCREEN_WIDTH - w;
822 h--;
823 }
824
825 SDL_UnlockSurface(sdl_screen);
826
827 // Finally, set the flag to indicate the mouse has been drawn
828 _mouse_drawn = true;
829}
830
831void OSystem_SDL::undraw_mouse() {
832 if (!_mouse_drawn)
833 return;
834 _mouse_drawn = false;
835
836 if (SDL_LockSurface(sdl_screen) == -1)
837 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
838
839 byte *dst, *bak = _mouse_backup;
840 const int old_mouse_x = _mouse_old_state.x;
841 const int old_mouse_y = _mouse_old_state.y;
842 const int old_mouse_w = _mouse_old_state.w;
843 const int old_mouse_h = _mouse_old_state.h;
844 int x,y;
845
846 // No need to do clipping here, since draw_mouse() did that already
847
848 dst = (byte *)sdl_screen->pixels + old_mouse_y * SCREEN_WIDTH + old_mouse_x;
849 for (y = 0; y < old_mouse_h; ++y, bak += MAX_MOUSE_W, dst += SCREEN_WIDTH) {
850 for (x = 0; x < old_mouse_w; ++x) {
851 dst[x] = bak[x];
852 }
853 }
854
855 add_dirty_rect(old_mouse_x, old_mouse_y, old_mouse_w, old_mouse_h);
856
857 SDL_UnlockSurface(sdl_screen);
858}
859
860void OSystem_SDL::stop_cdrom() { /* Stop CD Audio in 1/10th of a second */
861 cd_stop_time = SDL_GetTicks() + 100;
862 cd_num_loops = 0;
863
864}
865
866void OSystem_SDL::play_cdrom(int track, int num_loops, int start_frame, int end_frame) {
867 if (!num_loops && !start_frame)
868 return;
869
870 if (!cdrom)
871 return;
872
873 if (end_frame > 0)
874 end_frame+=5;
875
876 cd_track = track;
877 cd_num_loops = num_loops;
878 cd_start_frame = start_frame;
879
880 SDL_CDStatus(cdrom);
881 SDL_CDPlayTracks(cdrom, track, start_frame, 0, end_frame);
882 cd_end_frame = end_frame;
883 cd_stop_time = 0;
884 cd_end_time = SDL_GetTicks() + cdrom->track[track].length * 1000 / CD_FPS;
885}
886
887bool OSystem_SDL::poll_cdrom() {
888 if (!cdrom)
889 return false;
890
891 return (cd_num_loops != 0 && (SDL_GetTicks() < cd_end_time || SDL_CDStatus(cdrom) != CD_STOPPED));
892}
893
894void OSystem_SDL::update_cdrom() {
895 if (!cdrom)
896 return;
897
898 if (cd_stop_time != 0 && SDL_GetTicks() >= cd_stop_time) {
899 SDL_CDStop(cdrom);
900 cd_num_loops = 0;
901 cd_stop_time = 0;
902 return;
903 }
904
905 if (cd_num_loops == 0 || SDL_GetTicks() < cd_end_time)
906 return;
907
908 if (cd_num_loops != 1 && SDL_CDStatus(cdrom) != CD_STOPPED) {
909 // Wait another second for it to be done
910 cd_end_time += 1000;
911 return;
912 }
913
914 if (cd_num_loops > 0)
915 cd_num_loops--;
916
917 if (cd_num_loops != 0) {
918 SDL_CDPlayTracks(cdrom, cd_track, cd_start_frame, 0, cd_end_frame);
919 cd_end_time = SDL_GetTicks() + cdrom->track[cd_track].length * 1000 / CD_FPS;
920 }
921}
922
923void OSystem_SDL::setup_icon() {
924 int w, h, ncols, nbytes, i;
925 unsigned int rgba[256], icon[32 * 32];
926 unsigned char mask[32][4];
927
928 sscanf(scummvm_icon[0], "%d %d %d %d", &w, &h, &ncols, &nbytes);
929 if ((w != 32) || (h != 32) || (ncols > 255) || (nbytes > 1)) {
930 warning("Could not load the icon (%d %d %d %d)", w, h, ncols, nbytes);
931 return;
932 }
933 for (i = 0; i < ncols; i++) {
934 unsigned char code;
935 char color[32];
936 unsigned int col;
937 sscanf(scummvm_icon[1 + i], "%c c %s", &code, color);
938 if (!strcmp(color, "None"))
939 col = 0x00000000;
940 else if (!strcmp(color, "black"))
941 col = 0xFF000000;
942 else if (color[0] == '#') {
943 sscanf(color + 1, "%06x", &col);
944 col |= 0xFF000000;
945 } else {
946 warning("Could not load the icon (%d %s - %s) ", code, color, scummvm_icon[1 + i]);
947 return;
948 }
949
950 rgba[code] = col;
951 }
952 memset(mask, 0, sizeof(mask));
953 for (h = 0; h < 32; h++) {
954 char *line = scummvm_icon[1 + ncols + h];
955 for (w = 0; w < 32; w++) {
956 icon[w + 32 * h] = rgba[line[w]];
957 if (rgba[line[w]] & 0xFF000000) {
958 mask[h][w >> 3] |= 1 << (7 - (w & 0x07));
959 }
960 }
961 }
962
963 SDL_Surface *sdl_surf = SDL_CreateRGBSurfaceFrom(icon, 32, 32, 32, 32 * 4, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000);
964 SDL_WM_SetIcon(sdl_surf, (unsigned char *) mask);
965}
966
967void *OSystem_SDL::create_mutex(void) {
968 return (void *) SDL_CreateMutex();
969}
970
971void OSystem_SDL::lock_mutex(void *mutex) {
972 SDL_mutexP((SDL_mutex *) mutex);
973}
974
975void OSystem_SDL::unlock_mutex(void *mutex) {
976 SDL_mutexV((SDL_mutex *) mutex);
977}
978
979void OSystem_SDL::delete_mutex(void *mutex) {
980 SDL_DestroyMutex((SDL_mutex *) mutex);
981}
982
983
984#ifdef USE_NULL_DRIVER
985
986/* NULL video driver */
987class OSystem_NULL : public OSystem {
988public:
989 void set_palette(const byte *colors, uint start, uint num) {}
990 void init_size(uint w, uint h);
991 void copy_rect(const byte *buf, int pitch, int x, int y, int w, int h) {}
992 void update_screen() {}
993 bool show_mouse(bool visible) { return false; }
994 void set_mouse_pos(int x, int y) {}
995 void set_mouse_cursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y) {}
996 void set_shake_pos(int shake_pos) {}
997 uint32 get_msecs();
998 void delay_msecs(uint msecs);
999 void *create_thread(ThreadProc *proc, void *param) { return NULL; }
1000 bool poll_event(Event *event) { return false; }
1001 bool set_sound_proc(void *param, SoundProc *proc, byte sound) {}
1002 void quit() { exit(1); }
1003 uint32 property(int param, Property *value) { return 0; }
1004 static OSystem *create(int gfx_mode, bool full_screen);
1005private:
1006
1007 uint msec_start;
1008
1009 uint32 get_ticks();
1010};
1011
1012void OSystem_NULL::init_size(uint w, uint h, byte sound) {
1013 msec_start = get_ticks();
1014}
1015
1016uint32 OSystem_NULL::get_ticks() {
1017 uint a = 0;
1018#ifdef WIN32
1019 a = GetTickCount();
1020#endif
1021
1022#ifdef UNIX
1023 struct timeval tv;
1024 gettimeofday(&tv, NULL);
1025 a = tv.tv_sec * 1000 + tv.tv_usec/1000;
1026#endif
1027
1028 return a;
1029}
1030
1031void OSystem_NULL::delay_msecs(uint msecs) {
1032#ifdef WIN32
1033 Sleep(msecs);
1034#endif
1035#ifdef UNIX
1036 usleep(msecs*1000);
1037#endif
1038}
1039
1040uint32 OSystem_NULL::get_msecs() {
1041 return get_ticks() - msec_start;
1042}
1043
1044OSystem *OSystem_NULL_create() {
1045 return new OSystem_NULL();
1046}
1047#else /* USE_NULL_DRIVER */
1048
1049OSystem *OSystem_NULL_create() {
1050 return NULL;
1051}
1052
1053#endif