Ticket #7970: sdl_gl.cpp

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

New SDL/OpenGL output mode (choose in Makefile)

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_tmpscreen = sdl_screen;
292}
293
294void OSystem_SDL::unload_gfx_mode() {
295 SDL_FreeSurface(sdl_screen);
296 sdl_screen = NULL;
297
298 if (_mode_flags & DF_SEPARATE_TEMPSCREEN) {
299 free((uint16*)sdl_tmpscreen->pixels);
300 SDL_FreeSurface(sdl_tmpscreen);
301 }
302 sdl_tmpscreen = NULL;
303}
304
305void OSystem_SDL::init_size(uint w, uint h) {
306 //if (w != SCREEN_WIDTH && h != SCREEN_HEIGHT)
307 // error("320x200 is the only game resolution supported");
308
309 SCREEN_WIDTH = w;
310 SCREEN_HEIGHT = h;
311 CKSUM_NUM = (SCREEN_WIDTH*SCREEN_HEIGHT/(8*8));
312 /* allocate palette, it needs to be persistent across
313 * driver changes, so i'll alloc it here */
314 _cur_pal = (SDL_Color*)calloc(sizeof(SDL_Color), 256);
315
316 dirty_rect_list = (SDL_Rect*)calloc(NUM_DIRTY_RECT, sizeof(SDL_Rect));
317 _mouse_backup = (byte*)malloc(MAX_MOUSE_W * MAX_MOUSE_H * MAX_SCALING);
318 dirty_checksums = (uint32*)calloc(CKSUM_NUM*2, sizeof(uint32));
319
320 load_gfx_mode();
321}
322
323void OSystem_SDL::copy_rect(const byte *buf, int pitch, int x, int y, int w, int h) {
324 if (sdl_screen == NULL)
325 return;
326
327 if (pitch == SCREEN_WIDTH && x==0 && y==0 && w==SCREEN_WIDTH && h==SCREEN_HEIGHT && _mode_flags&DF_WANT_RECT_OPTIM) {
328 /* Special, optimized case for full screen updates.
329 * It tries to determine what areas were actually changed,
330 * and just updates those, on the actual display. */
331 add_dirty_rgn_auto(buf);
332 } else {
333 /* Clip the coordinates */
334 if (x < 0) { w+=x; buf-=x; x = 0; }
335 if (y < 0) { h+=y; buf-=y*pitch; y = 0; }
336 if (w > SCREEN_WIDTH-x) { w = SCREEN_WIDTH - x; }
337 if (h > SCREEN_HEIGHT-y) { h = SCREEN_HEIGHT - y; }
338
339 if (w <= 0 || h <= 0)
340 return;
341
342 cksum_valid = false;
343 add_dirty_rect(x, y, w, h);
344 }
345
346 /* FIXME: undraw mouse only if the draw rect intersects with the mouse rect */
347 if (_mouse_drawn)
348 undraw_mouse();
349
350 if (SDL_LockSurface(sdl_screen) == -1)
351 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
352
353 byte *dst = (byte *)sdl_screen->pixels + y * SCREEN_WIDTH + x;
354 do {
355 memcpy(dst, buf, w);
356 dst += SCREEN_WIDTH;
357 buf += pitch;
358 } while (--h);
359
360 SDL_UnlockSurface(sdl_screen);
361}
362
363
364void OSystem_SDL::add_dirty_rect(int x, int y, int w, int h) {
365 if (force_full)
366 return;
367
368 if (num_dirty_rects == NUM_DIRTY_RECT)
369 force_full = true;
370 else {
371 SDL_Rect *r = &dirty_rect_list[num_dirty_rects++];
372
373 /* Update the dirty region by 1 pixel for graphics drivers
374 * that "smear" the screen */
375 if (_mode_flags & DF_UPDATE_EXPAND_1_PIXEL) {
376 x--;
377 y--;
378 w+=2;
379 h+=2;
380 }
381
382 /* clip */
383 if (x < 0) { w+=x; x=0; }
384 if (y < 0) { h+=y; y=0; }
385 if (w > SCREEN_WIDTH-x) { w = SCREEN_WIDTH - x; }
386 if (h > SCREEN_HEIGHT-y) { h = SCREEN_HEIGHT - y; }
387
388 r->x = x;
389 r->y = y;
390 r->w = w;
391 r->h = h;
392 }
393}
394
395#define ROL(a,n) a = (a<<(n)) | (a>>(32-(n)))
396#define DOLINE(x) a ^= ((uint32*)buf)[0+(x)*(SCREEN_WIDTH/4)]; b ^= ((uint32*)buf)[1+(x)*(SCREEN_WIDTH/4)]
397void OSystem_SDL::mk_checksums(const byte *buf) {
398 uint32 *sums = dirty_checksums;
399 uint x,y;
400 const uint last_x = (uint)SCREEN_WIDTH/8;
401 const uint last_y = (uint)SCREEN_HEIGHT/8;
402
403 /* the 8x8 blocks in buf are enumerated starting in the top left corner and
404 * reading each line at a time from left to right */
405 for(y=0; y != last_y; y++, buf+=SCREEN_WIDTH*(8-1))
406 for(x=0; x != last_x; x++, buf+=8) {
407 uint32 a = x;
408 uint32 b = y;
409
410 DOLINE(0); ROL(a,13); ROL(b,11);
411 DOLINE(2); ROL(a,13); ROL(b,11);
412 DOLINE(4); ROL(a,13); ROL(b,11);
413 DOLINE(6); ROL(a,13); ROL(b,11);
414
415 a*=0xDEADBEEF;
416 b*=0xBAADF00D;
417
418 DOLINE(1); ROL(a,13); ROL(b,11);
419 DOLINE(3); ROL(a,13); ROL(b,11);
420 DOLINE(5); ROL(a,13); ROL(b,11);
421 DOLINE(7); ROL(a,13); ROL(b,11);
422
423 /* output the checksum for this block */
424 *sums++=a+b;
425 }
426}
427#undef DOLINE
428#undef ROL
429
430
431void OSystem_SDL::add_dirty_rgn_auto(const byte *buf) {
432 assert( ((uint32)buf & 3) == 0);
433
434 /* generate a table of the checksums */
435 mk_checksums(buf);
436
437 if (!cksum_valid) {
438 force_full = true;
439 cksum_valid = true;
440 }
441
442 /* go through the checksum list, compare it with the previous checksums,
443 and add all dirty rectangles to a list. try to combine small rectangles
444 into bigger ones in a simple way */
445 if (!force_full) {
446 int x,y,w;
447 uint32 *ck = dirty_checksums;
448
449 for(y=0; y!=SCREEN_HEIGHT/8; y++) {
450 for(x=0; x!=SCREEN_WIDTH/8; x++,ck++) {
451 if (ck[0] != ck[CKSUM_NUM]) {
452 /* found a dirty 8x8 block, now go as far to the right as possible,
453 and at the same time, unmark the dirty status by setting old to new. */
454 w=0;
455 do {
456 ck[w+CKSUM_NUM] = ck[w];
457 w++;
458 } while (x+w != SCREEN_WIDTH/8 && ck[w] != ck[w+CKSUM_NUM]);
459
460 add_dirty_rect(x*8, y*8, w*8, 8);
461
462 if (force_full)
463 goto get_out;
464 }
465 }
466 }
467 } else {
468 get_out:;
469 /* Copy old checksums to new */
470 memcpy(dirty_checksums + CKSUM_NUM, dirty_checksums, CKSUM_NUM * sizeof(uint32));
471 }
472}
473
474void OSystem_SDL::update_screen() {
475
476 /* First make sure the mouse is drawn, if it should be drawn. */
477 draw_mouse();
478
479 /* If the shake position changed, fill the dirty area with blackness */
480 if (_current_shake_pos != _new_shake_pos) {
481
482 // fb2gl_update(sdl_tmpscreen->pixels,320,200,320,0,_current_shake_pos);
483 _current_shake_pos = _new_shake_pos;
484
485 }
486
487 /* Palette update in case we are in "real" 8 bit color mode.
488 * Must take place after the screen data was updated, since with
489 * "real" 8bit mode, palatte changes may be visible immediatly,
490 * and we want to avoid any ugly effects.
491 */
492 if (_palette_changed_last != 0) {
493 fb2gl_set_palette(_palette_changed_first,
494 _palette_changed_last - _palette_changed_first);
495
496 _palette_changed_last = 0;
497 }
498
499 fb2gl_update(sdl_tmpscreen->pixels,320,200,320,0,_current_shake_pos);
500
501}
502
503bool OSystem_SDL::show_mouse(bool visible) {
504 if (_mouse_visible == visible)
505 return visible;
506
507 bool last = _mouse_visible;
508 _mouse_visible = visible;
509
510 if (visible)
511 draw_mouse();
512 else
513 undraw_mouse();
514
515 return last;
516}
517
518void OSystem_SDL::set_mouse_pos(int x, int y) {
519 if (x != _mouse_cur_state.x || y != _mouse_cur_state.y) {
520 _mouse_cur_state.x = x;
521 _mouse_cur_state.y = y;
522 undraw_mouse();
523 }
524}
525
526void OSystem_SDL::set_mouse_cursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y) {
527 _mouse_cur_state.w = w;
528 _mouse_cur_state.h = h;
529
530 _mouse_hotspot_x = hotspot_x;
531 _mouse_hotspot_y = hotspot_y;
532
533 _mouse_data = (byte*)buf;
534
535 undraw_mouse();
536}
537
538void OSystem_SDL::set_shake_pos(int shake_pos) {
539 _new_shake_pos = shake_pos;
540}
541
542uint32 OSystem_SDL::get_msecs() {
543 return SDL_GetTicks();
544}
545
546void OSystem_SDL::delay_msecs(uint msecs) {
547 SDL_Delay(msecs);
548}
549
550void *OSystem_SDL::create_thread(ThreadProc *proc, void *param) {
551 return SDL_CreateThread(proc, param);
552}
553
554int mapKey(int key, byte mod)
555{
556 if (key >= SDLK_F1 && key <= SDLK_F9) {
557 return key - SDLK_F1 + 315;
558 } else if (key >= 'a' && key <= 'z' && mod & KMOD_SHIFT) {
559 key &= ~0x20;
560 } else if (key >= SDLK_NUMLOCK && key <= SDLK_EURO)
561 return 0;
562 return key;
563}
564
565bool OSystem_SDL::poll_event(Event *event) {
566 SDL_Event ev;
567
568 for(;;) {
569 if (!SDL_PollEvent(&ev))
570 return false;
571
572 switch(ev.type) {
573 case SDL_KEYDOWN: {
574 byte b = 0;
575 if (ev.key.keysym.mod & KMOD_SHIFT) b |= KBD_SHIFT;
576 if (ev.key.keysym.mod & KMOD_CTRL) b |= KBD_CTRL;
577 if (ev.key.keysym.mod & KMOD_ALT) b |= KBD_ALT;
578 event->kbd.flags = b;
579
580 /* internal keypress? */
581 if (b == KBD_ALT && ev.key.keysym.sym==SDLK_RETURN) {
582 property(PROP_TOGGLE_FULLSCREEN, NULL);
583 break;
584 }
585
586 if (b == KBD_CTRL && ev.key.keysym.sym=='z') {
587 quit();
588 break;
589 }
590
591 if (b == (KBD_CTRL|KBD_ALT) &&
592 (ev.key.keysym.sym>='1') && (ev.key.keysym.sym<='7')) {
593 Property prop;
594 prop.gfx_mode = ev.key.keysym.sym - '1';
595 property(PROP_SET_GFX_MODE, &prop);
596 break;
597 }
598
599
600 event->event_code = EVENT_KEYDOWN;
601 event->kbd.keycode = ev.key.keysym.sym;
602 event->kbd.ascii = mapKey(ev.key.keysym.sym, ev.key.keysym.mod);
603 return true;
604 }
605
606 case SDL_MOUSEMOTION:
607 event->event_code = EVENT_MOUSEMOVE;
608 event->mouse.x = ev.motion.x;
609 event->mouse.y = ev.motion.y;
610
611 event->mouse.x /= scaling;
612 event->mouse.y /= scaling;
613
614 return true;
615
616 case SDL_MOUSEBUTTONDOWN:
617 if (ev.button.button == SDL_BUTTON_LEFT)
618 event->event_code = EVENT_LBUTTONDOWN;
619 else if (ev.button.button == SDL_BUTTON_RIGHT)
620 event->event_code = EVENT_RBUTTONDOWN;
621 else
622 break;
623 event->mouse.x = ev.button.x;
624 event->mouse.y = ev.button.y;
625 event->mouse.x /= scaling;
626 event->mouse.y /= scaling;
627
628 return true;
629
630 case SDL_MOUSEBUTTONUP:
631 if (ev.button.button == SDL_BUTTON_LEFT)
632 event->event_code = EVENT_LBUTTONUP;
633 else if (ev.button.button == SDL_BUTTON_RIGHT)
634 event->event_code = EVENT_RBUTTONUP;
635 else
636 break;
637 event->mouse.x = ev.button.x;
638 event->mouse.y = ev.button.y;
639 event->mouse.x /= scaling;
640 event->mouse.y /= scaling;
641 return true;
642
643 case SDL_QUIT:
644 quit();
645 }
646 }
647}
648
649bool OSystem_SDL::set_sound_proc(void *param, SoundProc *proc, byte format) {
650 SDL_AudioSpec desired;
651
652 /* only one format supported at the moment */
653
654 desired.freq = SAMPLES_PER_SEC;
655 desired.format = AUDIO_S16SYS;
656 desired.channels = 2;
657 desired.samples = 2048;
658 desired.callback = proc;
659 desired.userdata = param;
660 if (SDL_OpenAudio(&desired, NULL) != 0) {
661 return false;
662 }
663 SDL_PauseAudio(0);
664 return true;
665}
666
667
668/* retrieve the 320x200 bitmap currently being displayed */
669void OSystem_SDL::get_320x200_image(byte *buf) {
670 /* make sure the mouse is gone */
671 undraw_mouse();
672
673 if (SDL_LockSurface(sdl_screen) == -1)
674 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
675
676 memcpy(buf, sdl_screen->pixels, SCREEN_WIDTH*SCREEN_HEIGHT);
677
678 SDL_UnlockSurface(sdl_screen);
679}
680
681void OSystem_SDL::hotswap_gfx_mode() {
682 /* hmm, need to allocate a 320x200 bitmap
683 * which will contain the "backup" of the screen during the change.
684 * then draw that to the new screen right after it's setup.
685 */
686
687 byte *bak_mem = (byte*)malloc(SCREEN_WIDTH*SCREEN_HEIGHT);
688
689 get_320x200_image(bak_mem);
690
691 unload_gfx_mode();
692 load_gfx_mode();
693
694 fb2gl_set_palette(0,256);
695 fb2gl_update(sdl_tmpscreen->pixels,320,200,320,0,_current_shake_pos);
696
697 /* blit image */
698 OSystem_SDL::copy_rect(bak_mem, SCREEN_WIDTH, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
699 free(bak_mem);
700
701 OSystem_SDL::update_screen();
702}
703
704uint32 OSystem_SDL::property(int param, Property *value) {
705 switch(param) {
706
707 case PROP_TOGGLE_FULLSCREEN:
708 _full_screen ^= true;
709
710 return 1;
711
712 case PROP_GET_FULLSCREEN:
713 return _full_screen;
714
715 case PROP_SET_WINDOW_CAPTION:
716 SDL_WM_SetCaption(value->caption, value->caption);
717 return 1;
718
719 case PROP_OPEN_CD:
720 if (SDL_InitSubSystem(SDL_INIT_CDROM) == -1)
721 cdrom = NULL;
722 else {
723 cdrom = SDL_CDOpen(value->cd_num);
724 /* Did if open? Check if cdrom is NULL */
725 if (!cdrom) {
726 warning("Couldn't open drive: %s\n", SDL_GetError());
727 }
728 }
729 break;
730
731 case PROP_SET_GFX_MODE:
732 if (value->gfx_mode >= 7)
733 return 0;
734
735 _mode = value->gfx_mode;
736 hotswap_gfx_mode();
737
738 return 1;
739
740 case PROP_SHOW_DEFAULT_CURSOR:
741 SDL_ShowCursor(value->show_cursor ? SDL_ENABLE : SDL_DISABLE);
742 break;
743
744 case PROP_GET_SAMPLE_RATE:
745 return SAMPLES_PER_SEC;
746 }
747
748 return 0;
749}
750
751void OSystem_SDL::quit() {
752 if(cdrom) {
753 SDL_CDStop(cdrom);
754 SDL_CDClose(cdrom);
755 }
756 unload_gfx_mode();
757 exit(1);
758}
759
760void OSystem_SDL::draw_mouse() {
761 if (_mouse_drawn || !_mouse_visible)
762 return;
763
764 int x = _mouse_cur_state.x - _mouse_hotspot_x;
765 int y = _mouse_cur_state.y - _mouse_hotspot_y;
766 int w = _mouse_cur_state.w;
767 int h = _mouse_cur_state.h;
768 byte color;
769 byte *src = _mouse_data; // Image representing the mouse
770 byte *bak = _mouse_backup; // Surface used to backup the area obscured by the mouse
771 byte *dst; // Surface we are drawing into
772
773 // clip the mouse rect, and addjust the src pointer accordingly
774 if (x < 0) {
775 w += x;
776 src -= x;
777 x = 0;
778 }
779 if (y < 0) {
780 h += y;
781 src -= y * _mouse_cur_state.w;
782 y = 0;
783 }
784 if (w > SCREEN_WIDTH - x)
785 w = SCREEN_WIDTH - x;
786 if (h > SCREEN_HEIGHT - y)
787 h = SCREEN_HEIGHT - y;
788
789 // Store the bounding box so that undraw mouse can restore the area the
790 // mouse currently covers to its original content.
791 _mouse_old_state.x = x;
792 _mouse_old_state.y = y;
793 _mouse_old_state.w = w;
794 _mouse_old_state.h = h;
795
796 // Quick check to see if anything has to be drawn at all
797 if (w <= 0 || h <= 0)
798 return;
799
800 // Draw the mouse cursor; backup the covered area in "bak"
801
802 if (SDL_LockSurface(sdl_screen) == -1)
803 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
804
805 add_dirty_rect(x, y, w, h);
806
807 dst = (byte *)sdl_screen->pixels + y * SCREEN_WIDTH + x;
808 while (h > 0) {
809 int width = w;
810 while (width > 0) {
811 *bak++ = *dst;
812 color = *src++;
813 if (color != 0xFF) // 0xFF = transparent, don't draw
814 *dst = color;
815 dst++;
816 width--;
817 }
818 src += _mouse_cur_state.w - w;
819 bak += MAX_MOUSE_W - w;
820 dst += SCREEN_WIDTH - w;
821 h--;
822 }
823
824 SDL_UnlockSurface(sdl_screen);
825
826 // Finally, set the flag to indicate the mouse has been drawn
827 _mouse_drawn = true;
828}
829
830void OSystem_SDL::undraw_mouse() {
831 if (!_mouse_drawn)
832 return;
833 _mouse_drawn = false;
834
835 if (SDL_LockSurface(sdl_screen) == -1)
836 error("SDL_LockSurface failed: %s.\n", SDL_GetError());
837
838 byte *dst, *bak = _mouse_backup;
839 const int old_mouse_x = _mouse_old_state.x;
840 const int old_mouse_y = _mouse_old_state.y;
841 const int old_mouse_w = _mouse_old_state.w;
842 const int old_mouse_h = _mouse_old_state.h;
843 int x,y;
844
845 // No need to do clipping here, since draw_mouse() did that already
846
847 dst = (byte *)sdl_screen->pixels + old_mouse_y * SCREEN_WIDTH + old_mouse_x;
848 for (y = 0; y < old_mouse_h; ++y, bak += MAX_MOUSE_W, dst += SCREEN_WIDTH) {
849 for (x = 0; x < old_mouse_w; ++x) {
850 dst[x] = bak[x];
851 }
852 }
853
854 add_dirty_rect(old_mouse_x, old_mouse_y, old_mouse_w, old_mouse_h);
855
856 SDL_UnlockSurface(sdl_screen);
857}
858
859void OSystem_SDL::stop_cdrom() { /* Stop CD Audio in 1/10th of a second */
860 cd_stop_time = SDL_GetTicks() + 100;
861 cd_num_loops = 0;
862
863}
864
865void OSystem_SDL::play_cdrom(int track, int num_loops, int start_frame, int end_frame) {
866 if (!num_loops && !start_frame)
867 return;
868
869 if (!cdrom)
870 return;
871
872 if (end_frame > 0)
873 end_frame+=5;
874
875 cd_track = track;
876 cd_num_loops = num_loops;
877 cd_start_frame = start_frame;
878
879 SDL_CDStatus(cdrom);
880 SDL_CDPlayTracks(cdrom, track, start_frame, 0, end_frame);
881 cd_end_frame = end_frame;
882 cd_stop_time = 0;
883 cd_end_time = SDL_GetTicks() + cdrom->track[track].length * 1000 / CD_FPS;
884}
885
886bool OSystem_SDL::poll_cdrom() {
887 if (!cdrom)
888 return false;
889
890 return (cd_num_loops != 0 && (SDL_GetTicks() < cd_end_time || SDL_CDStatus(cdrom) != CD_STOPPED));
891}
892
893void OSystem_SDL::update_cdrom() {
894 if (!cdrom)
895 return;
896
897 if (cd_stop_time != 0 && SDL_GetTicks() >= cd_stop_time) {
898 SDL_CDStop(cdrom);
899 cd_num_loops = 0;
900 cd_stop_time = 0;
901 return;
902 }
903
904 if (cd_num_loops == 0 || SDL_GetTicks() < cd_end_time)
905 return;
906
907 if (cd_num_loops != 1 && SDL_CDStatus(cdrom) != CD_STOPPED) {
908 // Wait another second for it to be done
909 cd_end_time += 1000;
910 return;
911 }
912
913 if (cd_num_loops > 0)
914 cd_num_loops--;
915
916 if (cd_num_loops != 0) {
917 SDL_CDPlayTracks(cdrom, cd_track, cd_start_frame, 0, cd_end_frame);
918 cd_end_time = SDL_GetTicks() + cdrom->track[cd_track].length * 1000 / CD_FPS;
919 }
920}
921
922void OSystem_SDL::setup_icon() {
923 int w, h, ncols, nbytes, i;
924 unsigned int rgba[256], icon[32 * 32];
925 unsigned char mask[32][4];
926
927 sscanf(scummvm_icon[0], "%d %d %d %d", &w, &h, &ncols, &nbytes);
928 if ((w != 32) || (h != 32) || (ncols > 255) || (nbytes > 1)) {
929 warning("Could not load the icon (%d %d %d %d)", w, h, ncols, nbytes);
930 return;
931 }
932 for (i = 0; i < ncols; i++) {
933 unsigned char code;
934 char color[32];
935 unsigned int col;
936 sscanf(scummvm_icon[1 + i], "%c c %s", &code, color);
937 if (!strcmp(color, "None"))
938 col = 0x00000000;
939 else if (!strcmp(color, "black"))
940 col = 0xFF000000;
941 else if (color[0] == '#') {
942 sscanf(color + 1, "%06x", &col);
943 col |= 0xFF000000;
944 } else {
945 warning("Could not load the icon (%d %s - %s) ", code, color, scummvm_icon[1 + i]);
946 return;
947 }
948
949 rgba[code] = col;
950 }
951 memset(mask, 0, sizeof(mask));
952 for (h = 0; h < 32; h++) {
953 char *line = scummvm_icon[1 + ncols + h];
954 for (w = 0; w < 32; w++) {
955 icon[w + 32 * h] = rgba[line[w]];
956 if (rgba[line[w]] & 0xFF000000) {
957 mask[h][w >> 3] |= 1 << (7 - (w & 0x07));
958 }
959 }
960 }
961
962 SDL_Surface *sdl_surf = SDL_CreateRGBSurfaceFrom(icon, 32, 32, 32, 32 * 4, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000);
963 SDL_WM_SetIcon(sdl_surf, (unsigned char *) mask);
964}
965
966void *OSystem_SDL::create_mutex(void) {
967 return (void *) SDL_CreateMutex();
968}
969
970void OSystem_SDL::lock_mutex(void *mutex) {
971 SDL_mutexP((SDL_mutex *) mutex);
972}
973
974void OSystem_SDL::unlock_mutex(void *mutex) {
975 SDL_mutexV((SDL_mutex *) mutex);
976}
977
978void OSystem_SDL::delete_mutex(void *mutex) {
979 SDL_DestroyMutex((SDL_mutex *) mutex);
980}
981
982
983#ifdef USE_NULL_DRIVER
984
985/* NULL video driver */
986class OSystem_NULL : public OSystem {
987public:
988 void set_palette(const byte *colors, uint start, uint num) {}
989 void init_size(uint w, uint h);
990 void copy_rect(const byte *buf, int pitch, int x, int y, int w, int h) {}
991 void update_screen() {}
992 bool show_mouse(bool visible) { return false; }
993 void set_mouse_pos(int x, int y) {}
994 void set_mouse_cursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y) {}
995 void set_shake_pos(int shake_pos) {}
996 uint32 get_msecs();
997 void delay_msecs(uint msecs);
998 void *create_thread(ThreadProc *proc, void *param) { return NULL; }
999 bool poll_event(Event *event) { return false; }
1000 bool set_sound_proc(void *param, SoundProc *proc, byte sound) {}
1001 void quit() { exit(1); }
1002 uint32 property(int param, Property *value) { return 0; }
1003 static OSystem *create(int gfx_mode, bool full_screen);
1004private:
1005
1006 uint msec_start;
1007
1008 uint32 get_ticks();
1009};
1010
1011void OSystem_NULL::init_size(uint w, uint h, byte sound) {
1012 msec_start = get_ticks();
1013}
1014
1015uint32 OSystem_NULL::get_ticks() {
1016 uint a = 0;
1017#ifdef WIN32
1018 a = GetTickCount();
1019#endif
1020
1021#ifdef UNIX
1022 struct timeval tv;
1023 gettimeofday(&tv, NULL);
1024 a = tv.tv_sec * 1000 + tv.tv_usec/1000;
1025#endif
1026
1027 return a;
1028}
1029
1030void OSystem_NULL::delay_msecs(uint msecs) {
1031#ifdef WIN32
1032 Sleep(msecs);
1033#endif
1034#ifdef UNIX
1035 usleep(msecs*1000);
1036#endif
1037}
1038
1039uint32 OSystem_NULL::get_msecs() {
1040 return get_ticks() - msec_start;
1041}
1042
1043OSystem *OSystem_NULL_create() {
1044 return new OSystem_NULL();
1045}
1046#else /* USE_NULL_DRIVER */
1047
1048OSystem *OSystem_NULL_create() {
1049 return NULL;
1050}
1051
1052#endif