aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/sdl
diff options
context:
space:
mode:
Diffstat (limited to 'backends/platform/sdl')
-rw-r--r--backends/platform/sdl/build.rules9
-rw-r--r--backends/platform/sdl/events.cpp510
-rw-r--r--backends/platform/sdl/graphics.cpp1745
-rw-r--r--backends/platform/sdl/module.mk12
-rw-r--r--backends/platform/sdl/sdl-common.h392
-rw-r--r--backends/platform/sdl/sdl.cpp488
6 files changed, 3156 insertions, 0 deletions
diff --git a/backends/platform/sdl/build.rules b/backends/platform/sdl/build.rules
new file mode 100644
index 0000000000..14cfe0ebdf
--- /dev/null
+++ b/backends/platform/sdl/build.rules
@@ -0,0 +1,9 @@
+# Build settings for the SDL backend
+MODULES += backends/sdl
+DEFINES += -DUNIX
+INCLUDES += `sdl-config --cflags`
+LIBS += `sdl-config --libs`
+
+# Uncomment this in addition to the above if you compile on Mac OS X
+# DEFINES += -DMACOSX
+# LIBS += -framework QuickTime -framework AudioUnit
diff --git a/backends/platform/sdl/events.cpp b/backends/platform/sdl/events.cpp
new file mode 100644
index 0000000000..4fa02907db
--- /dev/null
+++ b/backends/platform/sdl/events.cpp
@@ -0,0 +1,510 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2001 Ludvig Strigeus
+ * Copyright (C) 2001-2006 The ScummVM project
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#include "backends/platform/sdl/sdl-common.h"
+#include "common/util.h"
+
+// FIXME move joystick defines out and replace with confile file options
+// we should really allow users to map any key to a joystick button
+#define JOY_DEADZONE 3200
+
+#ifndef __SYMBIAN32__ // Symbian wants dialog joystick i.e cursor for movement/selection
+ #define JOY_ANALOG
+#endif
+
+// #define JOY_INVERT_Y
+#define JOY_XAXIS 0
+#define JOY_YAXIS 1
+// buttons
+#define JOY_BUT_LMOUSE 0
+#define JOY_BUT_RMOUSE 2
+#define JOY_BUT_ESCAPE 3
+#define JOY_BUT_PERIOD 1
+#define JOY_BUT_SPACE 4
+#define JOY_BUT_F5 5
+
+
+
+
+static int mapKey(SDLKey key, SDLMod mod, Uint16 unicode)
+{
+ if (key >= SDLK_F1 && key <= SDLK_F9) {
+ return key - SDLK_F1 + 315;
+ } else if (key >= SDLK_KP0 && key <= SDLK_KP9) {
+ return key - SDLK_KP0 + '0';
+ } else if (key >= SDLK_UP && key <= SDLK_PAGEDOWN) {
+ return key;
+ } else if (unicode) {
+ return unicode;
+ } else if (key >= 'a' && key <= 'z' && mod & KMOD_SHIFT) {
+ return key & ~0x20;
+ } else if (key >= SDLK_NUMLOCK && key <= SDLK_EURO) {
+ return 0;
+ }
+ return key;
+}
+
+void OSystem_SDL::fillMouseEvent(Event &event, int x, int y) {
+ event.mouse.x = x;
+ event.mouse.y = y;
+
+ // Update the "keyboard mouse" coords
+ _km.x = x;
+ _km.y = y;
+
+ // Adjust for the screen scaling
+ if (!_overlayVisible) {
+ event.mouse.x /= _scaleFactor;
+ event.mouse.y /= _scaleFactor;
+ if (_adjustAspectRatio)
+ event.mouse.y = aspect2Real(event.mouse.y);
+ }
+}
+
+void OSystem_SDL::handleKbdMouse() {
+ uint32 curTime = getMillis();
+ if (curTime >= _km.last_time + _km.delay_time) {
+ _km.last_time = curTime;
+ if (_km.x_down_count == 1) {
+ _km.x_down_time = curTime;
+ _km.x_down_count = 2;
+ }
+ if (_km.y_down_count == 1) {
+ _km.y_down_time = curTime;
+ _km.y_down_count = 2;
+ }
+
+ if (_km.x_vel || _km.y_vel) {
+ if (_km.x_down_count) {
+ if (curTime > _km.x_down_time + _km.delay_time * 12) {
+ if (_km.x_vel > 0)
+ _km.x_vel++;
+ else
+ _km.x_vel--;
+ } else if (curTime > _km.x_down_time + _km.delay_time * 8) {
+ if (_km.x_vel > 0)
+ _km.x_vel = 5;
+ else
+ _km.x_vel = -5;
+ }
+ }
+ if (_km.y_down_count) {
+ if (curTime > _km.y_down_time + _km.delay_time * 12) {
+ if (_km.y_vel > 0)
+ _km.y_vel++;
+ else
+ _km.y_vel--;
+ } else if (curTime > _km.y_down_time + _km.delay_time * 8) {
+ if (_km.y_vel > 0)
+ _km.y_vel = 5;
+ else
+ _km.y_vel = -5;
+ }
+ }
+
+ _km.x += _km.x_vel;
+ _km.y += _km.y_vel;
+
+ if (_km.x < 0) {
+ _km.x = 0;
+ _km.x_vel = -1;
+ _km.x_down_count = 1;
+ } else if (_km.x > _km.x_max) {
+ _km.x = _km.x_max;
+ _km.x_vel = 1;
+ _km.x_down_count = 1;
+ }
+
+ if (_km.y < 0) {
+ _km.y = 0;
+ _km.y_vel = -1;
+ _km.y_down_count = 1;
+ } else if (_km.y > _km.y_max) {
+ _km.y = _km.y_max;
+ _km.y_vel = 1;
+ _km.y_down_count = 1;
+ }
+
+ SDL_WarpMouse(_km.x, _km.y);
+ }
+ }
+}
+
+static byte SDLModToOSystemKeyFlags(SDLMod mod) {
+ byte b = 0;
+#ifdef LINUPY
+ // Yopy has no ALT key, steal the SHIFT key
+ // (which isn't used much anyway)
+ if (mod & KMOD_SHIFT)
+ b |= OSystem::KBD_ALT;
+#else
+ if (mod & KMOD_SHIFT)
+ b |= OSystem::KBD_SHIFT;
+ if (mod & KMOD_ALT)
+ b |= OSystem::KBD_ALT;
+#endif
+ if (mod & KMOD_CTRL)
+ b |= OSystem::KBD_CTRL;
+
+ return b;
+}
+
+bool OSystem_SDL::pollEvent(Event &event) {
+ SDL_Event ev;
+ int axis;
+ byte b = 0;
+
+ handleKbdMouse();
+
+ // If the screen mode changed, send an EVENT_SCREEN_CHANGED
+ if (_modeChanged) {
+ _modeChanged = false;
+ event.type = EVENT_SCREEN_CHANGED;
+ return true;
+ }
+
+ while(SDL_PollEvent(&ev)) {
+ switch(ev.type) {
+ case SDL_KEYDOWN:{
+ b = event.kbd.flags = SDLModToOSystemKeyFlags(SDL_GetModState());
+
+ // Alt-Return and Alt-Enter toggle full screen mode
+ if (b == KBD_ALT && (ev.key.keysym.sym == SDLK_RETURN
+ || ev.key.keysym.sym == SDLK_KP_ENTER)) {
+ setFullscreenMode(!_fullscreen);
+#ifdef USE_OSD
+ if (_fullscreen)
+ displayMessageOnOSD("Fullscreen mode");
+ else
+ displayMessageOnOSD("Windowed mode");
+#endif
+
+ break;
+ }
+
+ // Alt-S: Create a screenshot
+ if (b == KBD_ALT && ev.key.keysym.sym == 's') {
+ char filename[20];
+
+ for (int n = 0;; n++) {
+ SDL_RWops *file;
+
+ sprintf(filename, "scummvm%05d.bmp", n);
+ file = SDL_RWFromFile(filename, "r");
+ if (!file)
+ break;
+ SDL_RWclose(file);
+ }
+ if (saveScreenshot(filename))
+ printf("Saved '%s'\n", filename);
+ else
+ printf("Could not save screenshot!\n");
+ break;
+ }
+
+ // Ctrl-m toggles mouse capture
+ if (b == KBD_CTRL && ev.key.keysym.sym == 'm') {
+ toggleMouseGrab();
+ break;
+ }
+
+#ifdef MACOSX
+ // On Macintosh', Cmd-Q quits
+ if ((ev.key.keysym.mod & KMOD_META) && ev.key.keysym.sym == 'q') {
+ event.type = EVENT_QUIT;
+ return true;
+ }
+#elif defined(UNIX)
+ // On other unices, Control-Q quits
+ if ((ev.key.keysym.mod & KMOD_CTRL) && ev.key.keysym.sym == 'q') {
+ event.type = EVENT_QUIT;
+ return true;
+ }
+#else
+ // Ctrl-z and Alt-X quit
+ if ((b == KBD_CTRL && ev.key.keysym.sym == 'z') || (b == KBD_ALT && ev.key.keysym.sym == 'x')) {
+ event.type = EVENT_QUIT;
+ return true;
+ }
+#endif
+
+ // Ctrl-Alt-<key> will change the GFX mode
+ if ((b & (KBD_CTRL|KBD_ALT)) == (KBD_CTRL|KBD_ALT)) {
+
+ handleScalerHotkeys(ev.key);
+ break;
+ }
+ const bool event_complete = remapKey(ev,event);
+
+ if (event_complete)
+ return true;
+
+ event.type = EVENT_KEYDOWN;
+ event.kbd.keycode = ev.key.keysym.sym;
+ event.kbd.ascii = mapKey(ev.key.keysym.sym, ev.key.keysym.mod, ev.key.keysym.unicode);
+
+ return true;
+ }
+ case SDL_KEYUP:
+ {
+ const bool event_complete = remapKey(ev,event);
+
+ if (event_complete)
+ return true;
+
+ event.type = EVENT_KEYUP;
+ event.kbd.keycode = ev.key.keysym.sym;
+ event.kbd.ascii = mapKey(ev.key.keysym.sym, ev.key.keysym.mod, ev.key.keysym.unicode);
+ b = event.kbd.flags = SDLModToOSystemKeyFlags(SDL_GetModState());
+
+ // Ctrl-Alt-<key> will change the GFX mode
+ if ((b & (KBD_CTRL|KBD_ALT)) == (KBD_CTRL|KBD_ALT)) {
+ // Swallow these key up events
+ break;
+ }
+
+ return true;
+ }
+ case SDL_MOUSEMOTION:
+ event.type = EVENT_MOUSEMOVE;
+ fillMouseEvent(event, ev.motion.x, ev.motion.y);
+
+ setMousePos(event.mouse.x, event.mouse.y);
+ return true;
+
+ case SDL_MOUSEBUTTONDOWN:
+ if (ev.button.button == SDL_BUTTON_LEFT)
+ event.type = EVENT_LBUTTONDOWN;
+ else if (ev.button.button == SDL_BUTTON_RIGHT)
+ event.type = EVENT_RBUTTONDOWN;
+#if defined(SDL_BUTTON_WHEELUP) && defined(SDL_BUTTON_WHEELDOWN)
+ else if (ev.button.button == SDL_BUTTON_WHEELUP)
+ event.type = EVENT_WHEELUP;
+ else if (ev.button.button == SDL_BUTTON_WHEELDOWN)
+ event.type = EVENT_WHEELDOWN;
+#endif
+ else
+ break;
+
+ fillMouseEvent(event, ev.button.x, ev.button.y);
+
+ return true;
+
+ case SDL_MOUSEBUTTONUP:
+ if (ev.button.button == SDL_BUTTON_LEFT)
+ event.type = EVENT_LBUTTONUP;
+ else if (ev.button.button == SDL_BUTTON_RIGHT)
+ event.type = EVENT_RBUTTONUP;
+ else
+ break;
+ fillMouseEvent(event, ev.button.x, ev.button.y);
+
+ return true;
+
+ case SDL_JOYBUTTONDOWN:
+ if (ev.jbutton.button == JOY_BUT_LMOUSE) {
+ event.type = EVENT_LBUTTONDOWN;
+ fillMouseEvent(event, _km.x, _km.y);
+ } else if (ev.jbutton.button == JOY_BUT_RMOUSE) {
+ event.type = EVENT_RBUTTONDOWN;
+ fillMouseEvent(event, _km.x, _km.y);
+ } else {
+ event.type = EVENT_KEYDOWN;
+ switch (ev.jbutton.button) {
+ case JOY_BUT_ESCAPE:
+ event.kbd.keycode = SDLK_ESCAPE;
+ event.kbd.ascii = mapKey(SDLK_ESCAPE, ev.key.keysym.mod, 0);
+ break;
+ case JOY_BUT_PERIOD:
+ event.kbd.keycode = SDLK_PERIOD;
+ event.kbd.ascii = mapKey(SDLK_PERIOD, ev.key.keysym.mod, 0);
+ break;
+ case JOY_BUT_SPACE:
+ event.kbd.keycode = SDLK_SPACE;
+ event.kbd.ascii = mapKey(SDLK_SPACE, ev.key.keysym.mod, 0);
+ break;
+ case JOY_BUT_F5:
+ event.kbd.keycode = SDLK_F5;
+ event.kbd.ascii = mapKey(SDLK_F5, ev.key.keysym.mod, 0);
+ break;
+ }
+ }
+ return true;
+
+ case SDL_JOYBUTTONUP:
+ if (ev.jbutton.button == JOY_BUT_LMOUSE) {
+ event.type = EVENT_LBUTTONUP;
+ fillMouseEvent(event, _km.x, _km.y);
+ } else if (ev.jbutton.button == JOY_BUT_RMOUSE) {
+ event.type = EVENT_RBUTTONUP;
+ fillMouseEvent(event, _km.x, _km.y);
+ } else {
+ event.type = EVENT_KEYUP;
+ switch (ev.jbutton.button) {
+ case JOY_BUT_ESCAPE:
+ event.kbd.keycode = SDLK_ESCAPE;
+ event.kbd.ascii = mapKey(SDLK_ESCAPE, ev.key.keysym.mod, 0);
+ break;
+ case JOY_BUT_PERIOD:
+ event.kbd.keycode = SDLK_PERIOD;
+ event.kbd.ascii = mapKey(SDLK_PERIOD, ev.key.keysym.mod, 0);
+ break;
+ case JOY_BUT_SPACE:
+ event.kbd.keycode = SDLK_SPACE;
+ event.kbd.ascii = mapKey(SDLK_SPACE, ev.key.keysym.mod, 0);
+ break;
+ case JOY_BUT_F5:
+ event.kbd.keycode = SDLK_F5;
+ event.kbd.ascii = mapKey(SDLK_F5, ev.key.keysym.mod, 0);
+ break;
+ }
+ }
+ return true;
+
+ case SDL_JOYAXISMOTION:
+ axis = ev.jaxis.value;
+ if ( axis > JOY_DEADZONE) {
+ axis -= JOY_DEADZONE;
+ event.type = EVENT_MOUSEMOVE;
+ } else if ( axis < -JOY_DEADZONE ) {
+ axis += JOY_DEADZONE;
+ event.type = EVENT_MOUSEMOVE;
+ } else
+ axis = 0;
+
+ if ( ev.jaxis.axis == JOY_XAXIS) {
+#ifdef JOY_ANALOG
+ _km.x_vel = axis/2000;
+ _km.x_down_count = 0;
+#else
+ if (axis != 0) {
+ _km.x_vel = (axis > 0) ? 1:-1;
+ _km.x_down_count = 1;
+ } else {
+ _km.x_vel = 0;
+ _km.x_down_count = 0;
+ }
+#endif
+
+ } else if (ev.jaxis.axis == JOY_YAXIS) {
+#ifndef JOY_INVERT_Y
+ axis = -axis;
+#endif
+#ifdef JOY_ANALOG
+ _km.y_vel = -axis / 2000;
+ _km.y_down_count = 0;
+#else
+ if (axis != 0) {
+ _km.y_vel = (-axis > 0) ? 1: -1;
+ _km.y_down_count = 1;
+ } else {
+ _km.y_vel = 0;
+ _km.y_down_count = 0;
+ }
+#endif
+ }
+
+ fillMouseEvent(event, _km.x, _km.y);
+
+ return true;
+
+ case SDL_VIDEOEXPOSE:
+ _forceFull = true;
+ break;
+
+ case SDL_QUIT:
+ event.type = EVENT_QUIT;
+ return true;
+ }
+ }
+ return false;
+}
+
+bool OSystem_SDL::remapKey(SDL_Event &ev,Event &event) {
+#ifdef LINUPY
+ // On Yopy map the End button to quit
+ if ((ev.key.keysym.sym == 293)) {
+ event.type = EVENT_QUIT;
+ return true;
+ }
+ // Map menu key to f5 (scumm menu)
+ if (ev.key.keysym.sym == 306) {
+ event.type = EVENT_KEYDOWN;
+ event.kbd.keycode = SDLK_F5;
+ event.kbd.ascii = mapKey(SDLK_F5, ev.key.keysym.mod, 0);
+ return true;
+ }
+ // Map action key to action
+ if (ev.key.keysym.sym == 291) {
+ event.type = EVENT_KEYDOWN;
+ event.kbd.keycode = SDLK_TAB;
+ event.kbd.ascii = mapKey(SDLK_TAB, ev.key.keysym.mod, 0);
+ return true;
+ }
+ // Map OK key to skip cinematic
+ if (ev.key.keysym.sym == 292) {
+ event.type = EVENT_KEYDOWN;
+ event.kbd.keycode = SDLK_ESCAPE;
+ event.kbd.ascii = mapKey(SDLK_ESCAPE, ev.key.keysym.mod, 0);
+ return true;
+ }
+#endif
+
+#ifdef QTOPIA
+ // Quit on fn+backspace on zaurus
+ if (ev.key.keysym.sym == 127) {
+ event.type = EVENT_QUIT;
+ return true;
+ }
+
+ // Map menu key (f11) to f5 (scumm menu)
+ if (ev.key.keysym.sym == SDLK_F11) {
+ event.type = EVENT_KEYDOWN;
+ event.kbd.keycode = SDLK_F5;
+ event.kbd.ascii = mapKey(SDLK_F5, ev.key.keysym.mod, 0);
+ }
+ // Nap center (space) to tab (default action )
+ // I wanted to map the calendar button but the calendar comes up
+ //
+ else if (ev.key.keysym.sym == SDLK_SPACE) {
+ event.type = EVENT_KEYDOWN;
+ event.kbd.keycode = SDLK_TAB;
+ event.kbd.ascii = mapKey(SDLK_TAB, ev.key.keysym.mod, 0);
+ }
+ // Since we stole space (pause) above we'll rebind it to the tab key on the keyboard
+ else if (ev.key.keysym.sym == SDLK_TAB) {
+ event.type = EVENT_KEYDOWN;
+ event.kbd.keycode = SDLK_SPACE;
+ event.kbd.ascii = mapKey(SDLK_SPACE, ev.key.keysym.mod, 0);
+ } else {
+ // Let the events fall through if we didn't change them, this may not be the best way to
+ // set it up, but i'm not sure how sdl would like it if we let if fall through then redid it though.
+ // and yes i have an huge terminal size so i dont wrap soon enough.
+ event.type = EVENT_KEYDOWN;
+ event.kbd.keycode = ev.key.keysym.sym;
+ event.kbd.ascii = mapKey(ev.key.keysym.sym, ev.key.keysym.mod, ev.key.keysym.unicode);
+ }
+#endif
+ return false;
+}
+
diff --git a/backends/platform/sdl/graphics.cpp b/backends/platform/sdl/graphics.cpp
new file mode 100644
index 0000000000..eb560c5769
--- /dev/null
+++ b/backends/platform/sdl/graphics.cpp
@@ -0,0 +1,1745 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2001 Ludvig Strigeus
+ * Copyright (C) 2001-2006 The ScummVM project
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#include "backends/platform/sdl/sdl-common.h"
+#include "common/util.h"
+#include "graphics/font.h"
+#include "graphics/fontman.h"
+#include "graphics/scaler.h"
+#include "graphics/surface.h"
+
+static const OSystem::GraphicsMode s_supportedGraphicsModes[] = {
+ {"1x", "Normal (no scaling)", GFX_NORMAL},
+ {"2x", "2x", GFX_DOUBLESIZE},
+ {"3x", "3x", GFX_TRIPLESIZE},
+ {"2xsai", "2xSAI", GFX_2XSAI},
+ {"super2xsai", "Super2xSAI", GFX_SUPER2XSAI},
+ {"supereagle", "SuperEagle", GFX_SUPEREAGLE},
+ {"advmame2x", "AdvMAME2x", GFX_ADVMAME2X},
+ {"advmame3x", "AdvMAME3x", GFX_ADVMAME3X},
+#ifndef DISABLE_HQ_SCALERS
+ {"hq2x", "HQ2x", GFX_HQ2X},
+ {"hq3x", "HQ3x", GFX_HQ3X},
+#endif
+ {"tv2x", "TV2x", GFX_TV2X},
+ {"dotmatrix", "DotMatrix", GFX_DOTMATRIX},
+ {0, 0, 0}
+};
+
+// Table of relative scalers magnitudes
+// [definedScale - 1][_scaleFactor - 1]
+static ScalerProc *scalersMagn[3][3] = {
+#ifndef DISABLE_SCALERS
+ { Normal1x, AdvMame2x, AdvMame3x },
+ { Normal1x, Normal1x, Normal1o5x },
+ { Normal1x, Normal1x, Normal1x }
+#else // remove dependencies on other scalers
+ { Normal1x, Normal1x, Normal1x },
+ { Normal1x, Normal1x, Normal1x },
+ { Normal1x, Normal1x, Normal1x }
+#endif
+};
+
+static const int s_gfxModeSwitchTable[][4] = {
+ { GFX_NORMAL, GFX_DOUBLESIZE, GFX_TRIPLESIZE, -1 },
+ { GFX_NORMAL, GFX_ADVMAME2X, GFX_ADVMAME3X, -1 },
+ { GFX_NORMAL, GFX_HQ2X, GFX_HQ3X, -1 },
+ { GFX_NORMAL, GFX_2XSAI, -1, -1 },
+ { GFX_NORMAL, GFX_SUPER2XSAI, -1, -1 },
+ { GFX_NORMAL, GFX_SUPEREAGLE, -1, -1 },
+ { GFX_NORMAL, GFX_TV2X, -1, -1 },
+ { GFX_NORMAL, GFX_DOTMATRIX, -1, -1 }
+ };
+
+#ifndef DISABLE_SCALERS
+static int cursorStretch200To240(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY);
+#endif
+
+const OSystem::GraphicsMode *OSystem_SDL::getSupportedGraphicsModes() const {
+ return s_supportedGraphicsModes;
+}
+
+int OSystem_SDL::getDefaultGraphicsMode() const {
+ return GFX_DOUBLESIZE;
+}
+
+void OSystem_SDL::beginGFXTransaction(void) {
+ assert (_transactionMode == kTransactionNone);
+
+ _transactionMode = kTransactionActive;
+
+ _transactionDetails.modeChanged = false;
+ _transactionDetails.sizeChanged = false;
+ _transactionDetails.arChanged = false;
+ _transactionDetails.fsChanged = false;
+
+ _transactionDetails.needHotswap = false;
+ _transactionDetails.needUpdatescreen = false;
+ _transactionDetails.needUnload = false;
+
+ _transactionDetails.normal1xScaler = false;
+}
+
+void OSystem_SDL::endGFXTransaction(void) {
+ // for each engine we run initCommonGFX() as first thing in the transaction
+ // and initSize() is called later. If user runs launcher at 320x200 with
+ // 2x overlay, setting to Nomral1x sclaler in that case will be suppressed
+ // and backend is forced to 2x
+ //
+ // This leads to bad results such as 1280x960 window for 640x480 engines.
+ // To prevent that we rerun setGraphicsMode() if there was 1x scaler request
+ if (_transactionDetails.normal1xScaler)
+ setGraphicsMode(GFX_NORMAL);
+
+ assert (_transactionMode == kTransactionActive);
+
+ _transactionMode = kTransactionCommit;
+ if (_transactionDetails.modeChanged)
+ setGraphicsMode(_transactionDetails.mode);
+
+ if (_transactionDetails.sizeChanged)
+ initSize(_transactionDetails.w, _transactionDetails.h);
+
+ if (_transactionDetails.arChanged)
+ setAspectRatioCorrection(_transactionDetails.ar);
+
+ if (_transactionDetails.needUnload) {
+ unloadGFXMode();
+ loadGFXMode();
+ clearOverlay();
+ } else {
+ if (!_transactionDetails.fsChanged) {
+ if (_transactionDetails.needHotswap)
+ hotswapGFXMode();
+ else if (_transactionDetails.needUpdatescreen)
+ internUpdateScreen();
+ }
+ }
+
+ if (_transactionDetails.fsChanged)
+ setFullscreenMode(_transactionDetails.fs);
+
+ _transactionMode = kTransactionNone;
+}
+
+bool OSystem_SDL::setGraphicsMode(int mode) {
+ Common::StackLock lock(_graphicsMutex);
+
+ int newScaleFactor = 1;
+ ScalerProc *newScalerProc;
+
+ switch(mode) {
+ case GFX_NORMAL:
+ newScaleFactor = 1;
+ newScalerProc = Normal1x;
+ break;
+#ifndef DISABLE_SCALERS
+ case GFX_DOUBLESIZE:
+ newScaleFactor = 2;
+ newScalerProc = Normal2x;
+ break;
+ case GFX_TRIPLESIZE:
+ newScaleFactor = 3;
+ newScalerProc = Normal3x;
+ break;
+
+ case GFX_2XSAI:
+ newScaleFactor = 2;
+ newScalerProc = _2xSaI;
+ break;
+ case GFX_SUPER2XSAI:
+ newScaleFactor = 2;
+ newScalerProc = Super2xSaI;
+ break;
+ case GFX_SUPEREAGLE:
+ newScaleFactor = 2;
+ newScalerProc = SuperEagle;
+ break;
+ case GFX_ADVMAME2X:
+ newScaleFactor = 2;
+ newScalerProc = AdvMame2x;
+ break;
+ case GFX_ADVMAME3X:
+ newScaleFactor = 3;
+ newScalerProc = AdvMame3x;
+ break;
+#ifndef DISABLE_HQ_SCALERS
+ case GFX_HQ2X:
+ newScaleFactor = 2;
+ newScalerProc = HQ2x;
+ break;
+ case GFX_HQ3X:
+ newScaleFactor = 3;
+ newScalerProc = HQ3x;
+ break;
+#endif
+ case GFX_TV2X:
+ newScaleFactor = 2;
+ newScalerProc = TV2x;
+ break;
+ case GFX_DOTMATRIX:
+ newScaleFactor = 2;
+ newScalerProc = DotMatrix;
+ break;
+#endif // DISABLE_SCALERS
+
+ default:
+ warning("unknown gfx mode %d", mode);
+ return false;
+ }
+
+ _transactionDetails.normal1xScaler = (mode == GFX_NORMAL);
+
+ _mode = mode;
+ _scalerProc = newScalerProc;
+
+ if (_transactionMode == kTransactionActive) {
+ _transactionDetails.mode = mode;
+ _transactionDetails.modeChanged = true;
+
+ if (newScaleFactor != _scaleFactor) {
+ _transactionDetails.needHotswap = true;
+ _scaleFactor = newScaleFactor;
+ }
+
+ _transactionDetails.needUpdatescreen = true;
+
+ return true;
+ }
+
+ // NOTE: This should not be executed at transaction commit
+ // Otherwise there is some unsolicited setGraphicsMode() call
+ // which should be properly removed
+ if (newScaleFactor != _scaleFactor) {
+ assert(_transactionMode != kTransactionCommit);
+
+ _scaleFactor = newScaleFactor;
+ hotswapGFXMode();
+ }
+
+ // Determine the "scaler type", i.e. essentially an index into the
+ // s_gfxModeSwitchTable array defined in events.cpp.
+ if (_mode != GFX_NORMAL) {
+ for (int i = 0; i < ARRAYSIZE(s_gfxModeSwitchTable); i++) {
+ if (s_gfxModeSwitchTable[i][1] == _mode || s_gfxModeSwitchTable[i][2] == _mode) {
+ _scalerType = i;
+ break;
+ }
+ }
+ }
+
+ if (!_screen)
+ return true;
+
+ // Blit everything to the screen
+ _forceFull = true;
+
+ // Even if the old and new scale factors are the same, we may have a
+ // different scaler for the cursor now.
+ blitCursor();
+
+ if (_transactionMode != kTransactionCommit)
+ internUpdateScreen();
+
+ // Make sure that an EVENT_SCREEN_CHANGED gets sent later
+ _modeChanged = true;
+
+ return true;
+}
+
+int OSystem_SDL::getGraphicsMode() const {
+ assert (_transactionMode == kTransactionNone);
+ return _mode;
+}
+
+void OSystem_SDL::initSize(uint w, uint h) {
+ // Avoid redundant res changes
+ if ((int)w == _screenWidth && (int)h == _screenHeight &&
+ _transactionMode != kTransactionCommit)
+ return;
+
+ _screenWidth = w;
+ _screenHeight = h;
+
+ _cksumNum = (_screenWidth * _screenHeight / (8 * 8));
+
+ if (_transactionMode == kTransactionActive) {
+ _transactionDetails.w = w;
+ _transactionDetails.h = h;
+ _transactionDetails.sizeChanged = true;
+
+ _transactionDetails.needUnload = true;
+
+ return;
+ }
+
+ free(_dirtyChecksums);
+ _dirtyChecksums = (uint32 *)calloc(_cksumNum * 2, sizeof(uint32));
+
+ if (_transactionMode != kTransactionCommit) {
+ unloadGFXMode();
+ loadGFXMode();
+
+ // if initSize() gets called in the middle, overlay is not transparent
+ clearOverlay();
+ }
+}
+
+void OSystem_SDL::loadGFXMode() {
+ assert(_inited);
+ _forceFull = true;
+ _modeFlags |= DF_UPDATE_EXPAND_1_PIXEL;
+
+ int hwW, hwH;
+
+#ifndef __MAEMO__
+ _overlayWidth = _screenWidth * _scaleFactor;
+ _overlayHeight = _screenHeight * _scaleFactor;
+
+ if (_screenHeight != 200)
+ _adjustAspectRatio = false;
+
+ if (_adjustAspectRatio)
+ _overlayHeight = real2Aspect(_overlayHeight);
+
+ hwW = _screenWidth * _scaleFactor;
+ hwH = effectiveScreenHeight();
+#else
+ hwW = _overlayWidth;
+ hwH = _overlayHeight;
+#endif
+
+ //
+ // Create the surface that contains the 8 bit game data
+ //
+ _screen = SDL_CreateRGBSurface(SDL_SWSURFACE, _screenWidth, _screenHeight, 8, 0, 0, 0, 0);
+ if (_screen == NULL)
+ error("allocating _screen failed");
+
+ //
+ // Create the surface that contains the scaled graphics in 16 bit mode
+ //
+
+ _hwscreen = SDL_SetVideoMode(hwW, hwH, 16,
+ _fullscreen ? (SDL_FULLSCREEN|SDL_SWSURFACE) : SDL_SWSURFACE
+ );
+ if (_hwscreen == NULL) {
+ // DON'T use error(), as this tries to bring up the debug
+ // console, which WON'T WORK now that _hwscreen is hosed.
+
+ // FIXME: We should be able to continue the game without
+ // shutting down or bringing up the debug console, but at
+ // this point we've already screwed up all our member vars.
+ // We need to find a way to call SDL_SetVideoMode *before*
+ // that happens and revert to all the old settings if we
+ // can't pull off the switch to the new settings.
+ //
+ // Fingolfin says: the "easy" way to do that is not to modify
+ // the member vars before we are sure everything is fine. Think
+ // of "transactions, commit, rollback" style... we use local vars
+ // in place of the member vars, do everything etc. etc.. In case
+ // of a failure, rollback is trivial. Only if everything worked fine
+ // do we "commit" the changed values to the member vars.
+ warning("SDL_SetVideoMode says we can't switch to that mode (%s)", SDL_GetError());
+ quit();
+ }
+
+ //
+ // Create the surface used for the graphics in 16 bit before scaling, and also the overlay
+ //
+
+ // Distinguish 555 and 565 mode
+ if (_hwscreen->format->Rmask == 0x7C00)
+ InitScalers(555);
+ else
+ InitScalers(565);
+
+ // Need some extra bytes around when using 2xSaI
+ _tmpscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _screenWidth + 3, _screenHeight + 3,
+ 16,
+ _hwscreen->format->Rmask,
+ _hwscreen->format->Gmask,
+ _hwscreen->format->Bmask,
+ _hwscreen->format->Amask);
+
+ if (_tmpscreen == NULL)
+ error("allocating _tmpscreen failed");
+
+ _overlayscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, _overlayWidth, _overlayHeight,
+ 16,
+ _hwscreen->format->Rmask,
+ _hwscreen->format->Gmask,
+ _hwscreen->format->Bmask,
+ _hwscreen->format->Amask);
+
+ if (_overlayscreen == NULL)
+ error("allocating _overlayscreen failed");
+
+ _tmpscreen2 = SDL_CreateRGBSurface(SDL_SWSURFACE, _overlayWidth + 3, _overlayHeight + 3,
+ 16,
+ _hwscreen->format->Rmask,
+ _hwscreen->format->Gmask,
+ _hwscreen->format->Bmask,
+ _hwscreen->format->Amask);
+
+ if (_tmpscreen2 == NULL)
+ error("allocating _tmpscreen2 failed");
+
+#ifdef USE_OSD
+ _osdSurface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA,
+ _hwscreen->w,
+ _hwscreen->h,
+ 16,
+ _hwscreen->format->Rmask,
+ _hwscreen->format->Gmask,
+ _hwscreen->format->Bmask,
+ _hwscreen->format->Amask);
+ if (_osdSurface == NULL)
+ error("allocating _osdSurface failed");
+ SDL_SetColorKey(_osdSurface, SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA, kOSDColorKey);
+#endif
+
+ // keyboard cursor control, some other better place for it?
+ _km.x_max = _screenWidth * _scaleFactor - 1;
+ _km.y_max = effectiveScreenHeight() - 1;
+ _km.delay_time = 25;
+ _km.last_time = 0;
+}
+
+void OSystem_SDL::unloadGFXMode() {
+ if (_screen) {
+ SDL_FreeSurface(_screen);
+ _screen = NULL;
+ }
+
+ if (_hwscreen) {
+ SDL_FreeSurface(_hwscreen);
+ _hwscreen = NULL;
+ }
+
+ if (_tmpscreen) {
+ SDL_FreeSurface(_tmpscreen);
+ _tmpscreen = NULL;
+ }
+
+ if (_tmpscreen2) {
+ SDL_FreeSurface(_tmpscreen2);
+ _tmpscreen2 = NULL;
+ }
+
+ if (_overlayscreen) {
+ SDL_FreeSurface(_overlayscreen);
+ _overlayscreen = NULL;
+ }
+
+#ifdef USE_OSD
+ if (_osdSurface) {
+ SDL_FreeSurface(_osdSurface);
+ _osdSurface = NULL;
+ }
+#endif
+}
+
+void OSystem_SDL::hotswapGFXMode() {
+ if (!_screen)
+ return;
+
+ // Keep around the old _screen & _overlayscreen so we can restore the screen data
+ // after the mode switch.
+ SDL_Surface *old_screen = _screen;
+ SDL_Surface *old_overlayscreen = _overlayscreen;
+
+ // Release the HW screen surface
+ SDL_FreeSurface(_hwscreen);
+
+ SDL_FreeSurface(_tmpscreen);
+ SDL_FreeSurface(_tmpscreen2);
+
+#ifdef USE_OSD
+ // Release the OSD surface
+ SDL_FreeSurface(_osdSurface);
+#endif
+
+ // Setup the new GFX mode
+ loadGFXMode();
+
+ // reset palette
+ SDL_SetColors(_screen, _currentPalette, 0, 256);
+
+ // Restore old screen content
+ SDL_BlitSurface(old_screen, NULL, _screen, NULL);
+ SDL_BlitSurface(old_overlayscreen, NULL, _overlayscreen, NULL);
+
+ // Free the old surfaces
+ SDL_FreeSurface(old_screen);
+ SDL_FreeSurface(old_overlayscreen);
+
+ // Update cursor to new scale
+ blitCursor();
+
+ // Blit everything to the screen
+ internUpdateScreen();
+
+ // Make sure that an EVENT_SCREEN_CHANGED gets sent later
+ _modeChanged = true;
+}
+
+void OSystem_SDL::updateScreen() {
+ assert (_transactionMode == kTransactionNone);
+
+ Common::StackLock lock(_graphicsMutex); // Lock the mutex until this function ends
+
+ internUpdateScreen();
+}
+
+void OSystem_SDL::internUpdateScreen() {
+ SDL_Surface *srcSurf, *origSurf;
+ int height, width;
+ ScalerProc *scalerProc;
+ int scale1;
+
+#if defined (DEBUG) && ! defined(_WIN32_WCE) // definitions not available for non-DEBUG here. (needed this to compile in SYMBIAN32 & linux?)
+ assert(_hwscreen != NULL);
+ assert(_hwscreen->map->sw_data != NULL);
+#endif
+
+ // If the shake position changed, fill the dirty area with blackness
+ if (_currentShakePos != _newShakePos) {
+ SDL_Rect blackrect = {0, 0, _screenWidth * _scaleFactor, _newShakePos * _scaleFactor};
+
+ if (_adjustAspectRatio && !_overlayVisible)
+ blackrect.h = real2Aspect(blackrect.h - 1) + 1;
+
+ SDL_FillRect(_hwscreen, &blackrect, 0);
+
+ _currentShakePos = _newShakePos;
+
+ _forceFull = true;
+ }
+
+ // Check whether the palette was changed in the meantime and update the
+ // screen surface accordingly.
+ if (_paletteDirtyEnd != 0) {
+ SDL_SetColors(_screen, _currentPalette + _paletteDirtyStart,
+ _paletteDirtyStart,
+ _paletteDirtyEnd - _paletteDirtyStart);
+
+ _paletteDirtyEnd = 0;
+
+ _forceFull = true;
+ }
+
+#ifdef USE_OSD
+ // OSD visible (i.e. non-transparent)?
+ if (_osdAlpha != SDL_ALPHA_TRANSPARENT) {
+ // Updated alpha value
+ const int diff = SDL_GetTicks() - _osdFadeStartTime;
+ if (diff > 0) {
+ if (diff >= kOSDFadeOutDuration) {
+ // Back to full transparency
+ _osdAlpha = SDL_ALPHA_TRANSPARENT;
+ } else {
+ // Do a linear fade out...
+ const int startAlpha = SDL_ALPHA_TRANSPARENT + kOSDInitialAlpha * (SDL_ALPHA_OPAQUE - SDL_ALPHA_TRANSPARENT) / 100;
+ _osdAlpha = startAlpha + diff * (SDL_ALPHA_TRANSPARENT - startAlpha) / kOSDFadeOutDuration;
+ }
+ SDL_SetAlpha(_osdSurface, SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA, _osdAlpha);
+ _forceFull = true;
+ }
+ }
+#endif
+
+ if (!_overlayVisible) {
+ origSurf = _screen;
+ srcSurf = _tmpscreen;
+ width = _screenWidth;
+ height = _screenHeight;
+ scalerProc = _scalerProc;
+ scale1 = _scaleFactor;
+ } else {
+ origSurf = _overlayscreen;
+ srcSurf = _tmpscreen2;
+ width = _overlayWidth;
+ height = _overlayHeight;
+ scalerProc = Normal1x;
+
+ scale1 = 1;
+ }
+
+ // Force a full redraw if requested
+ if (_forceFull) {
+ _numDirtyRects = 1;
+ _dirtyRectList[0].x = 0;
+ _dirtyRectList[0].y = 0;
+ _dirtyRectList[0].w = width;
+ _dirtyRectList[0].h = height;
+ } else
+ undrawMouse();
+
+ // Only draw anything if necessary
+ if (_numDirtyRects > 0) {
+
+ SDL_Rect *r;
+ SDL_Rect dst;
+ uint32 srcPitch, dstPitch;
+ SDL_Rect *lastRect = _dirtyRectList + _numDirtyRects;
+
+ if (scalerProc == Normal1x && !_adjustAspectRatio && 0) {
+ for (r = _dirtyRectList; r != lastRect; ++r) {
+ dst = *r;
+
+ dst.y += _currentShakePos;
+ if (SDL_BlitSurface(origSurf, r, _hwscreen, &dst) != 0)
+ error("SDL_BlitSurface failed: %s", SDL_GetError());
+ }
+ } else {
+ for (r = _dirtyRectList; r != lastRect; ++r) {
+ dst = *r;
+ dst.x++; // Shift rect by one since 2xSai needs to acces the data around
+ dst.y++; // any pixel to scale it, and we want to avoid mem access crashes.
+
+ if (SDL_BlitSurface(origSurf, r, srcSurf, &dst) != 0)
+ error("SDL_BlitSurface failed: %s", SDL_GetError());
+ }
+
+ SDL_LockSurface(srcSurf);
+ SDL_LockSurface(_hwscreen);
+
+ srcPitch = srcSurf->pitch;
+ dstPitch = _hwscreen->pitch;
+
+ for (r = _dirtyRectList; r != lastRect; ++r) {
+ register int dst_y = r->y + _currentShakePos;
+ register int dst_h = 0;
+ register int orig_dst_y = 0;
+ register int rx1 = r->x * scale1;
+
+ if (dst_y < height) {
+ dst_h = r->h;
+ if (dst_h > height - dst_y)
+ dst_h = height - dst_y;
+
+ orig_dst_y = dst_y;
+ dst_y = dst_y * scale1;
+
+ if (_adjustAspectRatio && !_overlayVisible)
+ dst_y = real2Aspect(dst_y);
+
+ assert(scalerProc != NULL);
+ scalerProc((byte *)srcSurf->pixels + (r->x * 2 + 2) + (r->y + 1) * srcPitch, srcPitch,
+ (byte *)_hwscreen->pixels + rx1 * 2 + dst_y * dstPitch, dstPitch, r->w, dst_h);
+ }
+
+ r->x = rx1;
+ r->y = dst_y;
+ r->w = r->w * scale1;
+ r->h = dst_h * scale1;
+
+#ifndef DISABLE_SCALERS
+ if (_adjustAspectRatio && orig_dst_y < height && !_overlayVisible)
+ r->h = stretch200To240((uint8 *) _hwscreen->pixels, dstPitch, r->w, r->h, r->x, r->y, orig_dst_y * scale1);
+#endif
+ }
+ SDL_UnlockSurface(srcSurf);
+ SDL_UnlockSurface(_hwscreen);
+ }
+
+ // Readjust the dirty rect list in case we are doing a full update.
+ // This is necessary if shaking is active.
+ if (_forceFull) {
+ _dirtyRectList[0].y = 0;
+ _dirtyRectList[0].h = effectiveScreenHeight();
+ }
+
+ drawMouse();
+
+#ifdef USE_OSD
+ if (_osdAlpha != SDL_ALPHA_TRANSPARENT) {
+ SDL_BlitSurface(_osdSurface, 0, _hwscreen, 0);
+ }
+#endif
+ // Finally, blit all our changes to the screen
+ SDL_UpdateRects(_hwscreen, _numDirtyRects, _dirtyRectList);
+ } else {
+ drawMouse();
+ if (_numDirtyRects)
+ SDL_UpdateRects(_hwscreen, _numDirtyRects, _dirtyRectList);
+ }
+
+ _numDirtyRects = 0;
+ _forceFull = false;
+}
+
+bool OSystem_SDL::saveScreenshot(const char *filename) {
+ assert(_hwscreen != NULL);
+
+ Common::StackLock lock(_graphicsMutex); // Lock the mutex until this function ends
+ return SDL_SaveBMP(_hwscreen, filename) == 0;
+}
+
+void OSystem_SDL::setFullscreenMode(bool enable) {
+ Common::StackLock lock(_graphicsMutex);
+
+ if (_fullscreen != enable || _transactionMode == kTransactionCommit) {
+ assert(_hwscreen != 0);
+ _fullscreen = enable;
+
+ if (_transactionMode == kTransactionActive) {
+ _transactionDetails.fs = enable;
+ _transactionDetails.fsChanged = true;
+
+ _transactionDetails.needHotswap = true;
+
+ return;
+ }
+
+#if (defined(MACOSX) && !SDL_VERSION_ATLEAST(1, 2, 6)) || defined(__MAEMO__)
+ // On OS X, SDL_WM_ToggleFullScreen is currently not implemented. Worse,
+ // before SDL 1.2.6 it always returned -1 (which would indicate a
+ // successful switch). So we simply don't call it at all and use
+ // hotswapGFXMode() directly to switch to fullscreen mode.
+ hotswapGFXMode();
+#else
+ if (!SDL_WM_ToggleFullScreen(_hwscreen)) {
+ // if ToggleFullScreen fails, achieve the same effect with hotswap gfx mode
+ hotswapGFXMode();
+ } else {
+ // Blit everything to the screen
+ internUpdateScreen();
+
+ // Make sure that an EVENT_SCREEN_CHANGED gets sent later
+ _modeChanged = true;
+ }
+#endif
+ }
+}
+
+void OSystem_SDL::setAspectRatioCorrection(bool enable) {
+ if ((_screenHeight == 200 && _adjustAspectRatio != enable) ||
+ _transactionMode == kTransactionCommit) {
+ Common::StackLock lock(_graphicsMutex);
+
+ //assert(_hwscreen != 0);
+ _adjustAspectRatio = enable;
+
+ if (_transactionMode == kTransactionActive) {
+ _transactionDetails.ar = enable;
+ _transactionDetails.arChanged = true;
+
+ _transactionDetails.needHotswap = true;
+
+ return;
+ } else {
+ if (_transactionMode != kTransactionCommit)
+ hotswapGFXMode();
+ }
+
+ // Make sure that an EVENT_SCREEN_CHANGED gets sent later
+ _modeChanged = true;
+ }
+}
+
+void OSystem_SDL::clearScreen() {
+ assert (_transactionMode == kTransactionNone);
+
+ // Try to lock the screen surface
+ if (SDL_LockSurface(_screen) == -1)
+ error("SDL_LockSurface failed: %s", SDL_GetError());
+
+ byte *dst = (byte *)_screen->pixels;
+
+ // Clear the screen
+ memset(dst, 0, _screenWidth * _screenHeight);
+
+ // Unlock the screen surface
+ SDL_UnlockSurface(_screen);
+}
+
+void OSystem_SDL::copyRectToScreen(const byte *src, int pitch, int x, int y, int w, int h) {
+ assert (_transactionMode == kTransactionNone);
+ assert(src);
+
+ if (_screen == NULL)
+ return;
+
+ Common::StackLock lock(_graphicsMutex); // Lock the mutex until this function ends
+
+ assert(x >= 0 && x < _screenWidth);
+ assert(y >= 0 && y < _screenHeight);
+ assert(h > 0 && y + h <= _screenHeight);
+ assert(w > 0 && x + w <= _screenWidth);
+
+ if (((long)src & 3) == 0 && pitch == _screenWidth && x == 0 && y == 0 &&
+ w == _screenWidth && h == _screenHeight && _modeFlags & DF_WANT_RECT_OPTIM) {
+ /* Special, optimized case for full screen updates.
+ * It tries to determine what areas were actually changed,
+ * and just updates those, on the actual display. */
+ addDirtyRgnAuto(src);
+ } else {
+ /* Clip the coordinates */
+ if (x < 0) {
+ w += x;
+ src -= x;
+ x = 0;
+ }
+
+ if (y < 0) {
+ h += y;
+ src -= y * pitch;
+ y = 0;
+ }
+
+ if (w > _screenWidth - x) {
+ w = _screenWidth - x;
+ }
+
+ if (h > _screenHeight - y) {
+ h = _screenHeight - y;
+ }
+
+ if (w <= 0 || h <= 0)
+ return;
+
+ _cksumValid = false;
+ addDirtyRect(x, y, w, h);
+ }
+
+ // Try to lock the screen surface
+ if (SDL_LockSurface(_screen) == -1)
+ error("SDL_LockSurface failed: %s", SDL_GetError());
+
+ byte *dst = (byte *)_screen->pixels + y * _screenWidth + x;
+
+ if (_screenWidth == pitch && pitch == w) {
+ memcpy(dst, src, h*w);
+ } else {
+ do {
+ memcpy(dst, src, w);
+ src += pitch;
+ dst += _screenWidth;
+ } while (--h);
+ }
+
+ // Unlock the screen surface
+ SDL_UnlockSurface(_screen);
+}
+
+bool OSystem_SDL::grabRawScreen(Graphics::Surface *surf) {
+ assert(_screen);
+ assert(surf);
+
+ Common::StackLock lock(_graphicsMutex); // Lock the mutex until this function ends
+
+ surf->create(_screenWidth, _screenHeight, _screen->format->BytesPerPixel);
+
+ // Try to lock the screen surface
+ if (SDL_LockSurface(_screen) == -1)
+ error("SDL_LockSurface failed: %s", SDL_GetError());
+
+ memcpy(surf->pixels, _screen->pixels, _screenWidth * _screenHeight * _screen->format->BytesPerPixel);
+
+ // Unlock the screen surface
+ SDL_UnlockSurface(_screen);
+
+ return true;
+}
+
+void OSystem_SDL::addDirtyRect(int x, int y, int w, int h, bool realCoordinates) {
+ if (_forceFull)
+ return;
+
+ if (_numDirtyRects == NUM_DIRTY_RECT) {
+ _forceFull = true;
+ return;
+ }
+
+ int height, width;
+
+ if (!_overlayVisible && !realCoordinates) {
+ width = _screenWidth;
+ height = _screenHeight;
+ } else {
+ width = _overlayWidth;
+ height = _overlayHeight;
+ }
+
+ // Extend the dirty region by 1 pixel for scalers
+ // that "smear" the screen, e.g. 2xSAI
+ if ((_modeFlags & DF_UPDATE_EXPAND_1_PIXEL) && !realCoordinates) {
+ x--;
+ y--;
+ w+=2;
+ h+=2;
+ }
+
+ // clip
+ if (x < 0) {
+ w += x;
+ x = 0;
+ }
+
+ if (y < 0) {
+ h += y;
+ y=0;
+ }
+
+ if (w > width - x) {
+ w = width - x;
+ }
+
+ if (h > height - y) {
+ h = height - y;
+ }
+
+#ifndef DISABLE_SCALERS
+ if (_adjustAspectRatio && !_overlayVisible && !realCoordinates) {
+ makeRectStretchable(x, y, w, h);
+ }
+#endif
+
+ if (w == width && h == height) {
+ _forceFull = true;
+ return;
+ }
+
+ if (w > 0 && h > 0) {
+ SDL_Rect *r = &_dirtyRectList[_numDirtyRects++];
+
+ r->x = x;
+ r->y = y;
+ r->w = w;
+ r->h = h;
+ }
+}
+
+
+void OSystem_SDL::makeChecksums(const byte *buf) {
+ assert(buf);
+ uint32 *sums = _dirtyChecksums;
+ uint x,y;
+ const uint last_x = (uint)_screenWidth / 8;
+ const uint last_y = (uint)_screenHeight / 8;
+
+ const uint BASE = 65521; /* largest prime smaller than 65536 */
+
+ /* the 8x8 blocks in buf are enumerated starting in the top left corner and
+ * reading each line at a time from left to right */
+ for(y = 0; y != last_y; y++, buf += _screenWidth * (8 - 1))
+ for(x = 0; x != last_x; x++, buf += 8) {
+ // Adler32 checksum algorithm (from RFC1950, used by gzip and zlib).
+ // This computes the Adler32 checksum of a 8x8 pixel block. Note
+ // that we can do the modulo operation (which is the slowest part)
+ // of the algorithm) at the end, instead of doing each iteration,
+ // since we only have 64 iterations in total - and thus s1 and
+ // s2 can't overflow anyway.
+ uint32 s1 = 1;
+ uint32 s2 = 0;
+ const byte *ptr = buf;
+ for (int subY = 0; subY < 8; subY++) {
+ for (int subX = 0; subX < 8; subX++) {
+ s1 += ptr[subX];
+ s2 += s1;
+ }
+ ptr += _screenWidth;
+ }
+
+ s1 %= BASE;
+ s2 %= BASE;
+
+ /* output the checksum for this block */
+ *sums++ = (s2 << 16) + s1;
+ }
+}
+
+void OSystem_SDL::addDirtyRgnAuto(const byte *buf) {
+ assert(buf);
+ assert(((long)buf & 3) == 0);
+
+ /* generate a table of the checksums */
+ makeChecksums(buf);
+
+ if (!_cksumValid) {
+ _forceFull = true;
+ _cksumValid = true;
+ }
+
+ /* go through the checksum list, compare it with the previous checksums,
+ and add all dirty rectangles to a list. try to combine small rectangles
+ into bigger ones in a simple way */
+ if (!_forceFull) {
+ int x, y, w;
+ uint32 *ck = _dirtyChecksums;
+
+ for(y = 0; y != _screenHeight / 8; y++) {
+ for(x = 0; x != _screenWidth / 8; x++, ck++) {
+ if (ck[0] != ck[_cksumNum]) {
+ /* found a dirty 8x8 block, now go as far to the right as possible,
+ and at the same time, unmark the dirty status by setting old to new. */
+ w=0;
+ do {
+ ck[w + _cksumNum] = ck[w];
+ w++;
+ } while (x + w != _screenWidth / 8 && ck[w] != ck[w + _cksumNum]);
+
+ addDirtyRect(x * 8, y * 8, w * 8, 8);
+
+ if (_forceFull)
+ goto get_out;
+ }
+ }
+ }
+ } else {
+ get_out:;
+ /* Copy old checksums to new */
+ memcpy(_dirtyChecksums + _cksumNum, _dirtyChecksums, _cksumNum * sizeof(uint32));
+ }
+}
+
+int16 OSystem_SDL::getHeight() {
+ return _screenHeight;
+}
+
+int16 OSystem_SDL::getWidth() {
+ return _screenWidth;
+}
+
+void OSystem_SDL::setPalette(const byte *colors, uint start, uint num) {
+ assert(colors);
+ const byte *b = colors;
+ uint i;
+ SDL_Color *base = _currentPalette + start;
+ for (i = 0; i < num; i++) {
+ base[i].r = b[0];
+ base[i].g = b[1];
+ base[i].b = b[2];
+ b += 4;
+ }
+
+ if (start < _paletteDirtyStart)
+ _paletteDirtyStart = start;
+
+ if (start + num > _paletteDirtyEnd)
+ _paletteDirtyEnd = start + num;
+
+ // Some games blink cursors with palette
+ if (_cursorPaletteDisabled)
+ blitCursor();
+}
+
+void OSystem_SDL::grabPalette(byte *colors, uint start, uint num) {
+ assert(colors);
+ const SDL_Color *base = _currentPalette + start;
+
+ for (uint i = 0; i < num; ++i) {
+ colors[i * 4] = base[i].r;
+ colors[i * 4 + 1] = base[i].g;
+ colors[i * 4 + 2] = base[i].b;
+ colors[i * 4 + 3] = 0xFF;
+ }
+}
+
+void OSystem_SDL::setCursorPalette(const byte *colors, uint start, uint num) {
+ assert(colors);
+ const byte *b = colors;
+ uint i;
+ SDL_Color *base = _cursorPalette + start;
+ for (i = 0; i < num; i++) {
+ base[i].r = b[0];
+ base[i].g = b[1];
+ base[i].b = b[2];
+ b += 4;
+ }
+
+ _cursorPaletteDisabled = false;
+
+ blitCursor();
+}
+
+void OSystem_SDL::setShakePos(int shake_pos) {
+ assert (_transactionMode == kTransactionNone);
+
+ _newShakePos = shake_pos;
+}
+
+
+#pragma mark -
+#pragma mark --- Overlays ---
+#pragma mark -
+
+void OSystem_SDL::showOverlay() {
+ assert (_transactionMode == kTransactionNone);
+
+ int x, y;
+
+ if (_overlayVisible)
+ return;
+
+ _overlayVisible = true;
+
+ // Since resolution could change, put mouse to adjusted position
+ // Fixes bug #1349059
+ x = _mouseCurState.x * _scaleFactor;
+ if (_adjustAspectRatio)
+ y = real2Aspect(_mouseCurState.y) * _scaleFactor;
+ else
+ y = _mouseCurState.y * _scaleFactor;
+
+ warpMouse(x, y);
+
+ clearOverlay();
+}
+
+void OSystem_SDL::hideOverlay() {
+ assert (_transactionMode == kTransactionNone);
+
+ if (!_overlayVisible)
+ return;
+
+ int x, y;
+
+ _overlayVisible = false;
+
+ // Since resolution could change, put mouse to adjusted position
+ // Fixes bug #1349059
+ x = _mouseCurState.x / _scaleFactor;
+ y = _mouseCurState.y / _scaleFactor;
+ if (_adjustAspectRatio)
+ y = aspect2Real(y);
+
+ warpMouse(x, y);
+
+ clearOverlay();
+
+ _forceFull = true;
+}
+
+void OSystem_SDL::clearOverlay() {
+ //assert (_transactionMode == kTransactionNone);
+
+ Common::StackLock lock(_graphicsMutex); // Lock the mutex until this function ends
+
+ if (!_overlayVisible)
+ return;
+
+ // Clear the overlay by making the game screen "look through" everywhere.
+ SDL_Rect src, dst;
+ src.x = src.y = 0;
+ dst.x = dst.y = 1;
+ src.w = dst.w = _screenWidth;
+ src.h = dst.h = _screenHeight;
+ if (SDL_BlitSurface(_screen, &src, _tmpscreen, &dst) != 0)
+ error("SDL_BlitSurface failed: %s", SDL_GetError());
+
+ SDL_LockSurface(_tmpscreen);
+ SDL_LockSurface(_overlayscreen);
+ _scalerProc((byte *)(_tmpscreen->pixels) + _tmpscreen->pitch + 2, _tmpscreen->pitch,
+ (byte *)_overlayscreen->pixels, _overlayscreen->pitch, _screenWidth, _screenHeight);
+
+#ifndef DISABLE_SCALERS
+ if (_adjustAspectRatio)
+ stretch200To240((uint8 *)_overlayscreen->pixels, _overlayscreen->pitch,
+ _overlayWidth, _screenHeight * _scaleFactor, 0, 0, 0);
+#endif
+ SDL_UnlockSurface(_tmpscreen);
+ SDL_UnlockSurface(_overlayscreen);
+
+ _forceFull = true;
+}
+
+void OSystem_SDL::grabOverlay(OverlayColor *buf, int pitch) {
+ assert (_transactionMode == kTransactionNone);
+
+ if (_overlayscreen == NULL)
+ return;
+
+ if (SDL_LockSurface(_overlayscreen) == -1)
+ error("SDL_LockSurface failed: %s", SDL_GetError());
+
+ byte *src = (byte *)_overlayscreen->pixels;
+ int h = _overlayHeight;
+ do {
+ memcpy(buf, src, _overlayWidth * 2);
+ src += _overlayscreen->pitch;
+ buf += pitch;
+ } while (--h);
+
+ SDL_UnlockSurface(_overlayscreen);
+}
+
+void OSystem_SDL::copyRectToOverlay(const OverlayColor *buf, int pitch, int x, int y, int w, int h) {
+ assert (_transactionMode == kTransactionNone);
+
+ if (_overlayscreen == NULL)
+ return;
+
+ // Clip the coordinates
+ if (x < 0) {
+ w += x;
+ buf -= x;
+ x = 0;
+ }
+
+ if (y < 0) {
+ h += y; buf -= y * pitch;
+ y = 0;
+ }
+
+ if (w > _overlayWidth - x) {
+ w = _overlayWidth - x;
+ }
+
+ if (h > _overlayHeight - y) {
+ h = _overlayHeight - y;
+ }
+
+ if (w <= 0 || h <= 0)
+ return;
+
+ // Mark the modified region as dirty
+ _cksumValid = false;
+ addDirtyRect(x, y, w, h);
+
+ if (SDL_LockSurface(_overlayscreen) == -1)
+ error("SDL_LockSurface failed: %s", SDL_GetError());
+
+ byte *dst = (byte *)_overlayscreen->pixels + y * _overlayscreen->pitch + x * 2;
+ do {
+ memcpy(dst, buf, w * 2);
+ dst += _overlayscreen->pitch;
+ buf += pitch;
+ } while (--h);
+
+ SDL_UnlockSurface(_overlayscreen);
+}
+
+OverlayColor OSystem_SDL::RGBToColor(uint8 r, uint8 g, uint8 b) {
+ return SDL_MapRGB(_overlayscreen->format, r, g, b);
+}
+
+void OSystem_SDL::colorToRGB(OverlayColor color, uint8 &r, uint8 &g, uint8 &b) {
+ SDL_GetRGB(color, _overlayscreen->format, &r, &g, &b);
+}
+
+
+#pragma mark -
+#pragma mark --- Mouse ---
+#pragma mark -
+
+bool OSystem_SDL::showMouse(bool visible) {
+ if (_mouseVisible == visible)
+ return visible;
+
+ bool last = _mouseVisible;
+ _mouseVisible = visible;
+
+ return last;
+}
+
+void OSystem_SDL::setMousePos(int x, int y) {
+ if (x != _mouseCurState.x || y != _mouseCurState.y) {
+ _mouseCurState.x = x;
+ _mouseCurState.y = y;
+ }
+}
+
+void OSystem_SDL::warpMouse(int x, int y) {
+ int y1 = y;
+
+ if (_adjustAspectRatio && !_overlayVisible)
+ y1 = real2Aspect(y);
+
+ if (_mouseCurState.x != x || _mouseCurState.y != y) {
+ if (!_overlayVisible)
+ SDL_WarpMouse(x * _scaleFactor, y1 * _scaleFactor);
+ else
+ SDL_WarpMouse(x, y1);
+
+ // SDL_WarpMouse() generates a mouse movement event, so
+ // setMousePos() would be called eventually. However, the
+ // cannon script in CoMI calls this function twice each time
+ // the cannon is reloaded. Unless we update the mouse position
+ // immediately the second call is ignored, causing the cannon
+ // to change its aim.
+
+ setMousePos(x, y);
+ }
+}
+
+void OSystem_SDL::setMouseCursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y, byte keycolor, int cursorTargetScale) {
+ if (w == 0 || h == 0)
+ return;
+
+ _mouseCurState.hotX = hotspot_x;
+ _mouseCurState.hotY = hotspot_y;
+
+ _mouseKeyColor = keycolor;
+
+ _cursorTargetScale = cursorTargetScale;
+
+ if (_mouseCurState.w != (int)w || _mouseCurState.h != (int)h) {
+ _mouseCurState.w = w;
+ _mouseCurState.h = h;
+
+ if (_mouseOrigSurface)
+ SDL_FreeSurface(_mouseOrigSurface);
+
+ // Allocate bigger surface because AdvMame2x adds black pixel at [0,0]
+ _mouseOrigSurface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA,
+ _mouseCurState.w + 2,
+ _mouseCurState.h + 2,
+ 16,
+ _hwscreen->format->Rmask,
+ _hwscreen->format->Gmask,
+ _hwscreen->format->Bmask,
+ _hwscreen->format->Amask);
+
+ if (_mouseOrigSurface == NULL)
+ error("allocating _mouseOrigSurface failed");
+ SDL_SetColorKey(_mouseOrigSurface, SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA, kMouseColorKey);
+ }
+
+ free(_mouseData);
+
+ _mouseData = (byte *)malloc(w * h);
+ memcpy(_mouseData, buf, w * h);
+ blitCursor();
+}
+
+void OSystem_SDL::blitCursor() {
+ byte *dstPtr;
+ const byte *srcPtr = _mouseData;
+ byte color;
+ int w, h, i, j;
+
+ if (!_mouseOrigSurface || !_mouseData)
+ return;
+
+ w = _mouseCurState.w;
+ h = _mouseCurState.h;
+
+ SDL_LockSurface(_mouseOrigSurface);
+
+ // Make whole surface transparent
+ for (i = 0; i < h + 2; i++) {
+ dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch * i;
+ for (j = 0; j < w + 2; j++) {
+ *(uint16 *)dstPtr = kMouseColorKey;
+ dstPtr += 2;
+ }
+ }
+
+ // Draw from [1,1] since AdvMame2x adds artefact at 0,0
+ dstPtr = (byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch + 2;
+
+ SDL_Color *palette;
+
+ if (_cursorPaletteDisabled)
+ palette = _currentPalette;
+ else
+ palette = _cursorPalette;
+
+ for (i = 0; i < h; i++) {
+ for (j = 0; j < w; j++) {
+ color = *srcPtr;
+ if (color != _mouseKeyColor) { // transparent, don't draw
+ *(uint16 *)dstPtr = SDL_MapRGB(_mouseOrigSurface->format,
+ palette[color].r, palette[color].g, palette[color].b);
+ }
+ dstPtr += 2;
+ srcPtr++;
+ }
+ dstPtr += _mouseOrigSurface->pitch - w * 2;
+ }
+
+ int rW, rH;
+
+ if (_cursorTargetScale >= _scaleFactor) {
+ // The cursor target scale is greater or equal to the scale at
+ // which the rest of the screen is drawn. We do not downscale
+ // the cursor image, we draw it at its original size. It will
+ // appear too large on screen.
+
+ rW = w;
+ rH = h;
+ _mouseCurState.rHotX = _mouseCurState.hotX;
+ _mouseCurState.rHotY = _mouseCurState.hotY;
+
+ // The virtual dimensions may be larger than the original.
+
+ _mouseCurState.vW = w * _cursorTargetScale / _scaleFactor;
+ _mouseCurState.vH = h * _cursorTargetScale / _scaleFactor;
+ _mouseCurState.vHotX = _mouseCurState.hotX * _cursorTargetScale /
+ _scaleFactor;
+ _mouseCurState.vHotY = _mouseCurState.hotY * _cursorTargetScale /
+ _scaleFactor;
+ } else {
+ // The cursor target scale is smaller than the scale at which
+ // the rest of the screen is drawn. We scale up the cursor
+ // image to make it appear correct.
+
+ rW = w * _scaleFactor / _cursorTargetScale;
+ rH = h * _scaleFactor / _cursorTargetScale;
+ _mouseCurState.rHotX = _mouseCurState.hotX * _scaleFactor /
+ _cursorTargetScale;
+ _mouseCurState.rHotY = _mouseCurState.hotY * _scaleFactor /
+ _cursorTargetScale;
+
+ // The virtual dimensions will be the same as the original.
+
+ _mouseCurState.vW = w;
+ _mouseCurState.vH = h;
+ _mouseCurState.vHotX = _mouseCurState.hotX;
+ _mouseCurState.vHotY = _mouseCurState.hotY;
+ }
+
+ int rH1 = rH; // store original to pass to aspect-correction function later
+ if (_adjustAspectRatio && _cursorTargetScale == 1) {
+ rH = real2Aspect(rH - 1) + 1;
+ _mouseCurState.rHotY = real2Aspect(_mouseCurState.rHotY);
+ }
+
+ if (_mouseCurState.rW != rW || _mouseCurState.rH != rH) {
+ _mouseCurState.rW = rW;
+ _mouseCurState.rH = rH;
+
+ if (_mouseSurface)
+ SDL_FreeSurface(_mouseSurface);
+
+ _mouseSurface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA,
+ _mouseCurState.rW,
+ _mouseCurState.rH,
+ 16,
+ _hwscreen->format->Rmask,
+ _hwscreen->format->Gmask,
+ _hwscreen->format->Bmask,
+ _hwscreen->format->Amask);
+
+ if (_mouseSurface == NULL)
+ error("allocating _mouseSurface failed");
+
+ SDL_SetColorKey(_mouseSurface, SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA, kMouseColorKey);
+ }
+
+ SDL_LockSurface(_mouseSurface);
+
+ ScalerProc *scalerProc;
+
+ // If possible, use the same scaler for the cursor as for the rest of
+ // the game. This only works well with the non-blurring scalers so we
+ // actually only use the 1x, 1.5x, 2x and AdvMame scalers.
+
+ if (_cursorTargetScale == 1 && (_mode == GFX_DOUBLESIZE || _mode == GFX_TRIPLESIZE))
+ scalerProc = _scalerProc;
+ else
+ scalerProc = scalersMagn[_cursorTargetScale - 1][_scaleFactor - 1];
+
+ scalerProc((byte *)_mouseOrigSurface->pixels + _mouseOrigSurface->pitch + 2,
+ _mouseOrigSurface->pitch, (byte *)_mouseSurface->pixels, _mouseSurface->pitch,
+ _mouseCurState.w, _mouseCurState.h);
+
+#ifndef DISABLE_SCALERS
+ if (_adjustAspectRatio && _cursorTargetScale == 1)
+ cursorStretch200To240((uint8 *)_mouseSurface->pixels, _mouseSurface->pitch, rW, rH1, 0, 0, 0);
+#endif
+
+ SDL_UnlockSurface(_mouseSurface);
+ SDL_UnlockSurface(_mouseOrigSurface);
+}
+
+#ifndef DISABLE_SCALERS
+// Basically it is kVeryFastAndUglyAspectMode of stretch200To240 from
+// common/scale/aspect.cpp
+static int cursorStretch200To240(uint8 *buf, uint32 pitch, int width, int height, int srcX, int srcY, int origSrcY) {
+ int maxDstY = real2Aspect(origSrcY + height - 1);
+ int y;
+ const uint8 *startSrcPtr = buf + srcX * 2 + (srcY - origSrcY) * pitch;
+ uint8 *dstPtr = buf + srcX * 2 + maxDstY * pitch;
+
+ for (y = maxDstY; y >= srcY; y--) {
+ const uint8 *srcPtr = startSrcPtr + aspect2Real(y) * pitch;
+
+ if (srcPtr == dstPtr)
+ break;
+ memcpy(dstPtr, srcPtr, width * 2);
+ dstPtr -= pitch;
+ }
+
+ return 1 + maxDstY - srcY;
+}
+#endif
+
+void OSystem_SDL::toggleMouseGrab() {
+ if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF)
+ SDL_WM_GrabInput(SDL_GRAB_ON);
+ else
+ SDL_WM_GrabInput(SDL_GRAB_OFF);
+}
+
+void OSystem_SDL::undrawMouse() {
+ const int x = _mouseBackup.x;
+ const int y = _mouseBackup.y;
+
+ // When we switch bigger overlay off mouse jumps. Argh!
+ // This is intended to prevent undrawing offscreen mouse
+ if (!_overlayVisible && (x >= _screenWidth || y >= _screenHeight)) {
+ return;
+ }
+
+ if (_mouseBackup.w != 0 && _mouseBackup.h != 0)
+ addDirtyRect(x, y, _mouseBackup.w, _mouseBackup.h);
+}
+
+void OSystem_SDL::drawMouse() {
+ if (!_mouseVisible || !_mouseSurface) {
+ _mouseBackup.x = _mouseBackup.y = _mouseBackup.w = _mouseBackup.h = 0;
+ return;
+ }
+
+ SDL_Rect dst;
+ int scale;
+ int width, height;
+ int hotX, hotY;
+
+ dst.x = _mouseCurState.x;
+ dst.y = _mouseCurState.y;
+
+ if (!_overlayVisible) {
+ scale = _scaleFactor;
+ width = _screenWidth;
+ height = _screenHeight;
+ dst.w = _mouseCurState.vW;
+ dst.h = _mouseCurState.vH;
+ hotX = _mouseCurState.vHotX;
+ hotY = _mouseCurState.vHotY;
+ } else {
+ scale = 1;
+ width = _overlayWidth;
+ height = _overlayHeight;
+ dst.w = _mouseCurState.rW;
+ dst.h = _mouseCurState.rH;
+ hotX = _mouseCurState.rHotX;
+ hotY = _mouseCurState.rHotY;
+ }
+
+ // The mouse is undrawn using virtual coordinates, i.e. they may be
+ // scaled and aspect-ratio corrected.
+
+ _mouseBackup.x = dst.x - hotX;
+ _mouseBackup.y = dst.y - hotY;
+ _mouseBackup.w = dst.w;
+ _mouseBackup.h = dst.h;
+
+ // We draw the pre-scaled cursor image, so now we need to adjust for
+ // scaling, shake position and aspect ratio correction manually.
+
+ if (!_overlayVisible) {
+ dst.y += _currentShakePos;
+ }
+
+ if (_adjustAspectRatio && !_overlayVisible)
+ dst.y = real2Aspect(dst.y);
+
+ dst.x = scale * dst.x - _mouseCurState.rHotX;
+ dst.y = scale * dst.y - _mouseCurState.rHotY;
+ dst.w = _mouseCurState.rW;
+ dst.h = _mouseCurState.rH;
+
+ // Note that SDL_BlitSurface() and addDirtyRect() will both perform any
+ // clipping necessary
+
+ if (SDL_BlitSurface(_mouseSurface, NULL, _hwscreen, &dst) != 0)
+ error("SDL_BlitSurface failed: %s", SDL_GetError());
+
+ // The screen will be updated using real surface coordinates, i.e.
+ // they will not be scaled or aspect-ratio corrected.
+
+ addDirtyRect(dst.x, dst.y, dst.w, dst.h, true);
+}
+
+#pragma mark -
+#pragma mark --- On Screen Display ---
+#pragma mark -
+
+#ifdef USE_OSD
+void OSystem_SDL::displayMessageOnOSD(const char *msg) {
+ assert (_transactionMode == kTransactionNone);
+ assert(msg);
+
+ uint i;
+
+ // Lock the OSD surface for drawing
+ if (SDL_LockSurface(_osdSurface))
+ error("displayMessageOnOSD: SDL_LockSurface failed: %s", SDL_GetError());
+
+ Graphics::Surface dst;
+ dst.pixels = _osdSurface->pixels;
+ dst.w = _osdSurface->w;
+ dst.h = _osdSurface->h;
+ dst.pitch = _osdSurface->pitch;
+ dst.bytesPerPixel = _osdSurface->format->BytesPerPixel;
+
+ // The font we are going to use:
+ const Graphics::Font *font = FontMan.getFontByUsage(Graphics::FontManager::kOSDFont);
+
+ // Clear everything with the "transparent" color, i.e. the colorkey
+ SDL_FillRect(_osdSurface, 0, kOSDColorKey);
+
+ // Split the message into separate lines.
+ Common::StringList lines;
+ const char *ptr;
+ for (ptr = msg; *ptr; ++ptr) {
+ if (*ptr == '\n') {
+ lines.push_back(Common::String(msg, ptr - msg));
+ msg = ptr + 1;
+ }
+ }
+ lines.push_back(Common::String(msg, ptr - msg));
+
+ // Determine a rect which would contain the message string (clipped to the
+ // screen dimensions).
+ const int vOffset = 6;
+ const int lineSpacing = 1;
+ const int lineHeight = font->getFontHeight() + 2 * lineSpacing;
+ int width = 0;
+ int height = lineHeight * lines.size() + 2 * vOffset;
+ for (i = 0; i < lines.size(); i++) {
+ width = MAX(width, font->getStringWidth(lines[i]) + 14);
+ }
+
+ // Clip the rect
+ if (width > dst.w)
+ width = dst.w;
+ if (height > dst.h)
+ height = dst.h;
+
+ // Draw a dark gray rect
+ // TODO: Rounded corners ? Border?
+ SDL_Rect osdRect;
+ osdRect.x = (dst.w - width) / 2;
+ osdRect.y = (dst.h - height) / 2;
+ osdRect.w = width;
+ osdRect.h = height;
+ SDL_FillRect(_osdSurface, &osdRect, SDL_MapRGB(_osdSurface->format, 64, 64, 64));
+
+ // Render the message, centered, and in white
+ for (i = 0; i < lines.size(); i++) {
+ font->drawString(&dst, lines[i],
+ osdRect.x, osdRect.y + i * lineHeight + vOffset + lineSpacing, osdRect.w,
+ SDL_MapRGB(_osdSurface->format, 255, 255, 255),
+ Graphics::kTextAlignCenter);
+ }
+
+ // Finished drawing, so unlock the OSD surface again
+ SDL_UnlockSurface(_osdSurface);
+
+ // Init the OSD display parameters, and the fade out
+ _osdAlpha = SDL_ALPHA_TRANSPARENT + kOSDInitialAlpha * (SDL_ALPHA_OPAQUE - SDL_ALPHA_TRANSPARENT) / 100;
+ _osdFadeStartTime = SDL_GetTicks() + kOSDFadeOutDelay;
+ SDL_SetAlpha(_osdSurface, SDL_RLEACCEL | SDL_SRCCOLORKEY | SDL_SRCALPHA, _osdAlpha);
+
+ // Ensure a full redraw takes place next time the screen is updated
+ _forceFull = true;
+}
+#endif
+
+
+#pragma mark -
+#pragma mark --- Misc ---
+#pragma mark -
+
+void OSystem_SDL::handleScalerHotkeys(const SDL_KeyboardEvent &key) {
+ // Ctrl-Alt-a toggles aspect ratio correction
+ if (key.keysym.sym == 'a') {
+ setFeatureState(kFeatureAspectRatioCorrection, !_adjustAspectRatio);
+#ifdef USE_OSD
+ char buffer[128];
+ if (_adjustAspectRatio)
+ sprintf(buffer, "Enabled aspect ratio correction\n%d x %d -> %d x %d",
+ _screenWidth, _screenHeight,
+ _hwscreen->w, _hwscreen->h
+ );
+ else
+ sprintf(buffer, "Disabled aspect ratio correction\n%d x %d -> %d x %d",
+ _screenWidth, _screenHeight,
+ _hwscreen->w, _hwscreen->h
+ );
+ displayMessageOnOSD(buffer);
+#endif
+
+ return;
+ }
+
+ int newMode = -1;
+ int factor = _scaleFactor - 1;
+
+ // Increase/decrease the scale factor
+ if (key.keysym.sym == SDLK_EQUALS || key.keysym.sym == SDLK_PLUS || key.keysym.sym == SDLK_MINUS ||
+ key.keysym.sym == SDLK_KP_PLUS || key.keysym.sym == SDLK_KP_MINUS) {
+ factor += (key.keysym.sym == SDLK_MINUS || key.keysym.sym == SDLK_KP_MINUS) ? -1 : +1;
+ if (0 <= factor && factor <= 3) {
+ newMode = s_gfxModeSwitchTable[_scalerType][factor];
+ }
+ }
+
+ const bool isNormalNumber = (SDLK_1 <= key.keysym.sym && key.keysym.sym <= SDLK_9);
+ const bool isKeypadNumber = (SDLK_KP1 <= key.keysym.sym && key.keysym.sym <= SDLK_KP9);
+ if (isNormalNumber || isKeypadNumber) {
+ _scalerType = key.keysym.sym - (isNormalNumber ? SDLK_1 : SDLK_KP1);
+ if (_scalerType >= ARRAYSIZE(s_gfxModeSwitchTable))
+ return;
+
+ while (s_gfxModeSwitchTable[_scalerType][factor] < 0) {
+ assert(factor > 0);
+ factor--;
+ }
+ newMode = s_gfxModeSwitchTable[_scalerType][factor];
+ }
+
+ if (newMode >= 0) {
+ setGraphicsMode(newMode);
+#ifdef USE_OSD
+ if (_osdSurface) {
+ const char *newScalerName = 0;
+ const GraphicsMode *g = getSupportedGraphicsModes();
+ while (g->name) {
+ if (g->id == _mode) {
+ newScalerName = g->description;
+ break;
+ }
+ g++;
+ }
+ if (newScalerName) {
+ char buffer[128];
+ sprintf(buffer, "Active graphics filter: %s\n%d x %d -> %d x %d",
+ newScalerName,
+ _screenWidth, _screenHeight,
+ _hwscreen->w, _hwscreen->h
+ );
+ displayMessageOnOSD(buffer);
+ }
+ }
+#endif
+
+ }
+
+}
diff --git a/backends/platform/sdl/module.mk b/backends/platform/sdl/module.mk
new file mode 100644
index 0000000000..18e56bc4fe
--- /dev/null
+++ b/backends/platform/sdl/module.mk
@@ -0,0 +1,12 @@
+MODULE := backends/platform/sdl
+
+MODULE_OBJS := \
+ events.o \
+ graphics.o \
+ sdl.o
+
+MODULE_DIRS += \
+ backends/platform/sdl/
+
+# We don't use the rules.mk here on purpose
+OBJS := $(addprefix $(MODULE)/, $(MODULE_OBJS)) $(OBJS)
diff --git a/backends/platform/sdl/sdl-common.h b/backends/platform/sdl/sdl-common.h
new file mode 100644
index 0000000000..f4c9383c63
--- /dev/null
+++ b/backends/platform/sdl/sdl-common.h
@@ -0,0 +1,392 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2001 Ludvig Strigeus
+ * Copyright (C) 2001-2006 The ScummVM project
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#ifndef SDL_COMMON_H
+#define SDL_COMMON_H
+
+#include <SDL.h>
+
+#include "common/stdafx.h"
+#include "common/scummsys.h"
+#include "common/system.h"
+#include "graphics/scaler.h"
+#include "backends/intern.h"
+
+
+#if !defined(_WIN32_WCE) && !defined(__SYMBIAN32__)
+// Uncomment this to enable the 'on screen display' code.
+#define USE_OSD 1
+#endif
+
+
+enum {
+ GFX_NORMAL = 0,
+ GFX_DOUBLESIZE = 1,
+ GFX_TRIPLESIZE = 2,
+ GFX_2XSAI = 3,
+ GFX_SUPER2XSAI = 4,
+ GFX_SUPEREAGLE = 5,
+ GFX_ADVMAME2X = 6,
+ GFX_ADVMAME3X = 7,
+ GFX_HQ2X = 8,
+ GFX_HQ3X = 9,
+ GFX_TV2X = 10,
+ GFX_DOTMATRIX = 11
+};
+
+
+class OSystem_SDL : public OSystem {
+public:
+ OSystem_SDL();
+ virtual ~OSystem_SDL();
+
+ virtual void initBackend();
+
+ void beginGFXTransaction(void);
+ void endGFXTransaction(void);
+
+ // Set the size of the video bitmap.
+ // Typically, 320x200
+ virtual void initSize(uint w, uint h); // overloaded by CE backend
+
+ // Set colors of the palette
+ void setPalette(const byte *colors, uint start, uint num);
+
+ // Get colors of the palette
+ void grabPalette(byte *colors, uint start, uint num);
+
+ // Draw a bitmap to screen.
+ // The screen will not be updated to reflect the new bitmap
+ virtual void copyRectToScreen(const byte *src, int pitch, int x, int y, int w, int h); // overloaded by CE backend (FIXME)
+
+ // Copies the screen to a buffer
+ bool grabRawScreen(Graphics::Surface *surf);
+
+ // Clear the screen
+ void clearScreen();
+
+ // Update the dirty areas of the screen
+ void updateScreen();
+
+ // Either show or hide the mouse cursor
+ bool showMouse(bool visible);
+
+ // Warp the mouse cursor. Where set_mouse_pos() only informs the
+ // backend of the mouse cursor's current position, this function
+ // actually moves the cursor to the specified position.
+ virtual void warpMouse(int x, int y); // overloaded by CE backend (FIXME)
+
+ // Set the bitmap that's used when drawing the cursor.
+ virtual void setMouseCursor(const byte *buf, uint w, uint h, int hotspot_x, int hotspot_y, byte keycolor, int cursorTargetScale); // overloaded by CE backend (FIXME)
+
+ // Set colors of cursor palette
+ void setCursorPalette(const byte *colors, uint start, uint num);
+
+ // Disables or enables cursor palette
+ void disableCursorPalette(bool disable) {
+ _cursorPaletteDisabled = disable;
+ blitCursor();
+ };
+
+ // Shaking is used in SCUMM. Set current shake position.
+ void setShakePos(int shake_pos);
+
+ // Get the number of milliseconds since the program was started.
+ uint32 getMillis();
+
+ // Delay for a specified amount of milliseconds
+ void delayMillis(uint msecs);
+
+ // Get the next event.
+ // Returns true if an event was retrieved.
+ virtual bool pollEvent(Event &event); // overloaded by CE backend
+
+ // Set function that generates samples
+ virtual bool setSoundCallback(SoundProc proc, void *param); // overloaded by CE backend
+
+ void clearSoundCallback();
+
+ // Poll CD status
+ // Returns true if cd audio is playing
+ bool pollCD();
+
+ // Play CD audio track
+ void playCD(int track, int num_loops, int start_frame, int duration);
+
+ // Stop CD audio track
+ void stopCD();
+
+ // Update CD audio status
+ void updateCD();
+
+ // Quit
+ virtual void quit(); // overloaded by CE backend
+
+
+ // Add a callback timer
+ void setTimerCallback(TimerProc callback, int timer);
+
+ // Mutex handling
+ MutexRef createMutex();
+ void lockMutex(MutexRef mutex);
+ void unlockMutex(MutexRef mutex);
+ void deleteMutex(MutexRef mutex);
+
+ // Overlay
+ virtual void showOverlay(); // WinCE FIXME
+ virtual void hideOverlay(); // WinCE FIXME
+ virtual void clearOverlay();
+ virtual void grabOverlay(OverlayColor *buf, int pitch);
+ virtual void copyRectToOverlay(const OverlayColor *buf, int pitch, int x, int y, int w, int h); // WinCE FIXME
+ virtual int16 getHeight();
+ virtual int16 getWidth();
+ virtual int16 getOverlayHeight() { return _overlayHeight; }
+ virtual int16 getOverlayWidth() { return _overlayWidth; }
+
+ // Methods that convert RGB to/from colors suitable for the overlay.
+ virtual OverlayColor RGBToColor(uint8 r, uint8 g, uint8 b);
+ virtual void colorToRGB(OverlayColor color, uint8 &r, uint8 &g, uint8 &b);
+
+
+ virtual const GraphicsMode *getSupportedGraphicsModes() const;
+ virtual int getDefaultGraphicsMode() const;
+ virtual bool setGraphicsMode(int mode);
+ virtual int getGraphicsMode() const;
+
+ virtual void setWindowCaption(const char *caption);
+ virtual bool openCD(int drive);
+ virtual int getOutputSampleRate() const;
+
+ virtual bool hasFeature(Feature f);
+ virtual void setFeatureState(Feature f, bool enable);
+ virtual bool getFeatureState(Feature f);
+
+#ifdef USE_OSD
+ void displayMessageOnOSD(const char *msg);
+#endif
+
+protected:
+ bool _inited;
+
+#ifdef USE_OSD
+ SDL_Surface *_osdSurface;
+ Uint8 _osdAlpha; // Transparency level of the OSD
+ uint32 _osdFadeStartTime; // When to start the fade out
+ enum {
+ kOSDFadeOutDelay = 2 * 1000, // Delay before the OSD is faded out (in milliseconds)
+ kOSDFadeOutDuration = 500, // Duration of the OSD fade out (in milliseconds)
+ kOSDColorKey = 1,
+ kOSDInitialAlpha = 80 // Initial alpha level, in percent
+ };
+#endif
+
+ // hardware screen
+ SDL_Surface *_hwscreen;
+
+ // unseen game screen
+ SDL_Surface *_screen;
+ int _screenWidth, _screenHeight;
+
+ // temporary screen (for scalers)
+ SDL_Surface *_tmpscreen;
+ SDL_Surface *_tmpscreen2;
+
+ // overlay
+ SDL_Surface *_overlayscreen;
+ int _overlayWidth, _overlayHeight;
+ bool _overlayVisible;
+
+ // Audio
+ int _samplesPerSec;
+
+ // CD Audio
+ SDL_CD *_cdrom;
+ int _cdTrack, _cdNumLoops, _cdStartFrame, _cdDuration;
+ uint32 _cdEndTime, _cdStopTime;
+
+ enum {
+ DF_WANT_RECT_OPTIM = 1 << 0,
+ DF_UPDATE_EXPAND_1_PIXEL = 1 << 1
+ };
+
+ enum {
+ kTransactionNone = 0,
+ kTransactionCommit = 1,
+ kTransactionActive = 2
+ };
+
+ struct TransactionDetails {
+ int mode;
+ bool modeChanged;
+ int w;
+ int h;
+ bool sizeChanged;
+ bool fs;
+ bool fsChanged;
+ bool ar;
+ bool arChanged;
+ bool needHotswap;
+ bool needUpdatescreen;
+ bool needUnload;
+ bool needToggle;
+ bool normal1xScaler;
+ };
+ TransactionDetails _transactionDetails;
+
+ /** Force full redraw on next updateScreen */
+ bool _forceFull;
+ ScalerProc *_scalerProc;
+ int _scalerType;
+ int _scaleFactor;
+ int _mode;
+ int _transactionMode;
+ bool _fullscreen;
+
+ /** Current video mode flags (see DF_* constants) */
+ uint32 _modeFlags;
+ bool _modeChanged;
+
+ /** True if aspect ratio correction is enabled. */
+ bool _adjustAspectRatio;
+
+ enum {
+ NUM_DIRTY_RECT = 100,
+
+ MAX_MOUSE_W = 80,
+ MAX_MOUSE_H = 80,
+ MAX_SCALING = 3
+ };
+
+ // Dirty rect management
+ SDL_Rect _dirtyRectList[NUM_DIRTY_RECT];
+ int _numDirtyRects;
+ uint32 *_dirtyChecksums;
+ bool _cksumValid;
+ int _cksumNum;
+
+ // Keyboard mouse emulation. Disabled by fingolfin 2004-12-18.
+ // I am keeping the rest of the code in for now, since the joystick
+ // code (or rather, "hack") uses it, too.
+ struct KbdMouse {
+ int16 x, y, x_vel, y_vel, x_max, y_max, x_down_count, y_down_count;
+ uint32 last_time, delay_time, x_down_time, y_down_time;
+ };
+
+ struct MousePos {
+ // The mouse position, using either virtual (game) or real
+ // (overlay) coordinates.
+ int16 x, y;
+
+ // The size and hotspot of the original cursor image.
+ int16 w, h;
+ int16 hotX, hotY;
+
+ // The size and hotspot of the pre-scaled cursor image, in real
+ // coordinates.
+ int16 rW, rH;
+ int16 rHotX, rHotY;
+
+ // The size and hotspot of the pre-scaled cursor image, in game
+ // coordinates.
+ int16 vW, vH;
+ int16 vHotX, vHotY;
+
+ MousePos() : x(0), y(0), w(0), h(0), hotX(0), hotY(0),
+ rW(0), rH(0), rHotX(0), rHotY(0), vW(0), vH(0),
+ vHotX(0), vHotY(0)
+ { }
+ };
+
+ // mouse
+ KbdMouse _km;
+ bool _mouseVisible;
+ bool _mouseDrawn;
+ byte *_mouseData;
+ SDL_Rect _mouseBackup;
+ MousePos _mouseCurState;
+ byte _mouseKeyColor;
+ int _cursorTargetScale;
+ bool _cursorPaletteDisabled;
+ SDL_Surface *_mouseOrigSurface;
+ SDL_Surface *_mouseSurface;
+ enum {
+ kMouseColorKey = 1
+ };
+
+ // joystick
+ SDL_Joystick *_joystick;
+
+ // Shake mode
+ int _currentShakePos;
+ int _newShakePos;
+
+ // Palette data
+ SDL_Color *_currentPalette;
+ uint _paletteDirtyStart, _paletteDirtyEnd;
+
+ // Cursor palette data
+ SDL_Color *_cursorPalette;
+
+ /**
+ * Mutex which prevents multiple threads from interfering with each other
+ * when accessing the screen.
+ */
+ MutexRef _graphicsMutex;
+
+
+ void addDirtyRgnAuto(const byte *buf);
+ void makeChecksums(const byte *buf);
+
+ virtual void addDirtyRect(int x, int y, int w, int h, bool realCoordinates = false); // overloaded by CE backend
+
+ virtual void drawMouse(); // overloaded by CE backend
+ virtual void undrawMouse(); // overloaded by CE backend (FIXME)
+ virtual void blitCursor(); // overloaded by CE backend (FIXME)
+
+ /** Set the position of the virtual mouse cursor. */
+ void setMousePos(int x, int y);
+ virtual void fillMouseEvent(Event &event, int x, int y); // overloaded by CE backend
+ void toggleMouseGrab();
+
+ virtual void internUpdateScreen(); // overloaded by CE backend
+
+ virtual void loadGFXMode(); // overloaded by CE backend
+ virtual void unloadGFXMode(); // overloaded by CE backend
+ virtual void hotswapGFXMode(); // overloaded by CE backend
+
+ void setFullscreenMode(bool enable);
+ void setAspectRatioCorrection(bool enable);
+
+ virtual bool saveScreenshot(const char *filename); // overloaded by CE backend
+
+ int effectiveScreenHeight() const { return (_adjustAspectRatio ? 240 : _screenHeight) * _scaleFactor; }
+
+ void setupIcon();
+ void handleKbdMouse();
+
+ virtual bool remapKey(SDL_Event &ev, Event &event);
+
+ void handleScalerHotkeys(const SDL_KeyboardEvent &key);
+};
+
+#endif
diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
new file mode 100644
index 0000000000..b6d2d675df
--- /dev/null
+++ b/backends/platform/sdl/sdl.cpp
@@ -0,0 +1,488 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2001 Ludvig Strigeus
+ * Copyright (C) 2001-2006 The ScummVM project
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#include "backends/platform/sdl/sdl-common.h"
+#include "common/config-manager.h"
+#include "common/util.h"
+#include "base/main.h"
+
+#include "icons/scummvm.xpm"
+
+#if defined(__SYMBIAN32__)
+#include "SymbianOs.h"
+#endif
+
+#if !defined(_WIN32_WCE) && !defined(__MAEMO__)
+
+#if defined (WIN32)
+int __stdcall WinMain(HINSTANCE /*hInst*/, HINSTANCE /*hPrevInst*/, LPSTR /*lpCmdLine*/, int /*iShowCmd*/) {
+ SDL_SetModuleHandle(GetModuleHandle(NULL));
+ return main(__argc, __argv);
+}
+#endif
+
+int main(int argc, char *argv[]) {
+
+#if defined(__SYMBIAN32__)
+ //
+ // Set up redirects for stdout/stderr under Windows and Symbian.
+ // Code copied from SDL_main.
+ //
+
+ // Symbian does not like any output to the console through any *print* function
+ char STDOUT_FILE[256], STDERR_FILE[256]; // shhh, don't tell anybody :)
+ strcpy(STDOUT_FILE, Symbian::GetExecutablePath());
+ strcpy(STDERR_FILE, Symbian::GetExecutablePath());
+ strcat(STDOUT_FILE, "scummvm.stdout.txt");
+ strcat(STDERR_FILE, "scummvm.stderr.txt");
+
+ /* Flush the output in case anything is queued */
+ fclose(stdout);
+ fclose(stderr);
+
+ /* Redirect standard input and standard output */
+ FILE *newfp = freopen(STDOUT_FILE, "w", stdout);
+ if (newfp == NULL) { /* This happens on NT */
+#if !defined(stdout)
+ stdout = fopen(STDOUT_FILE, "w");
+#else
+ newfp = fopen(STDOUT_FILE, "w");
+ if (newfp) {
+ *stdout = *newfp;
+ }
+#endif
+ }
+ newfp = freopen(STDERR_FILE, "w", stderr);
+ if (newfp == NULL) { /* This happens on NT */
+#if !defined(stderr)
+ stderr = fopen(STDERR_FILE, "w");
+#else
+ newfp = fopen(STDERR_FILE, "w");
+ if (newfp) {
+ *stderr = *newfp;
+ }
+#endif
+ }
+ setbuf(stderr, NULL); /* No buffering */
+
+#endif // defined(__SYMBIAN32__)
+
+ // Create our OSystem instance
+#if defined(__SYMBIAN32__)
+ g_system = new OSystem_SDL_Symbian();
+#else
+ g_system = new OSystem_SDL();
+#endif
+ assert(g_system);
+
+ // Invoke the actual ScummVM main entry point:
+ int res = scummvm_main(argc, argv);
+ g_system->quit(); // TODO: Consider removing / replacing this!
+ return res;
+}
+#endif
+
+void OSystem_SDL::initBackend() {
+ assert(!_inited);
+
+ int joystick_num = ConfMan.getInt("joystick_num");
+ uint32 sdlFlags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
+
+ if (ConfMan.hasKey("disable_sdl_parachute"))
+ sdlFlags |= SDL_INIT_NOPARACHUTE;
+
+#ifdef _WIN32_WCE
+ if (ConfMan.hasKey("use_GDI") && ConfMan.getBool("use_GDI")) {
+ SDL_VideoInit("windib", 0);
+ sdlFlags ^= SDL_INIT_VIDEO;
+ }
+#endif
+
+ if (joystick_num > -1)
+ sdlFlags |= SDL_INIT_JOYSTICK;
+
+ if (SDL_Init(sdlFlags) == -1) {
+ error("Could not initialize SDL: %s", SDL_GetError());
+ }
+
+ _graphicsMutex = createMutex();
+
+ SDL_ShowCursor(SDL_DISABLE);
+
+ // Enable unicode support if possible
+ SDL_EnableUNICODE(1);
+
+ _cksumValid = false;
+#if !defined(_WIN32_WCE) && !defined(__SYMBIAN32__) && !defined(DISABLE_SCALERS)
+ _mode = GFX_DOUBLESIZE;
+ _scaleFactor = 2;
+ _scalerProc = Normal2x;
+ _fullscreen = ConfMan.getBool("fullscreen");
+ _adjustAspectRatio = ConfMan.getBool("aspect_ratio");
+#else // for small screen platforms
+ _mode = GFX_NORMAL;
+ _scaleFactor = 1;
+ _scalerProc = Normal1x;
+
+#if !defined(_WIN32_WCE) && !defined(__SYMBIAN32__)
+ _fullscreen = ConfMan.getBool("fullscreen");
+#else
+ _fullscreen = true;
+#endif
+
+ _adjustAspectRatio = false;
+#endif
+ _scalerType = 0;
+ _modeFlags = 0;
+
+#if !defined(MACOSX) && !defined(__SYMBIAN32__) // Don't set icon on OS X, as we use a nicer external icon there
+ setupIcon(); // Don't for Symbian: it uses the EScummVM.aif file for the icon
+#endif
+
+ // enable joystick
+ if (joystick_num > -1 && SDL_NumJoysticks() > 0) {
+ printf("Using joystick: %s\n", SDL_JoystickName(0));
+ _joystick = SDL_JoystickOpen(joystick_num);
+ }
+
+ _inited = true;
+}
+
+OSystem_SDL::OSystem_SDL()
+ :
+#ifdef USE_OSD
+ _osdSurface(0), _osdAlpha(SDL_ALPHA_TRANSPARENT), _osdFadeStartTime(0),
+#endif
+ _hwscreen(0), _screen(0), _screenWidth(0), _screenHeight(0),
+ _tmpscreen(0), _overlayWidth(0), _overlayHeight(0),
+ _overlayVisible(false),
+ _overlayscreen(0), _tmpscreen2(0),
+ _samplesPerSec(0),
+ _cdrom(0), _scalerProc(0), _modeChanged(false), _dirtyChecksums(0),
+ _mouseVisible(false), _mouseDrawn(false), _mouseData(0), _mouseSurface(0),
+ _mouseOrigSurface(0), _cursorTargetScale(1), _cursorPaletteDisabled(true),
+ _joystick(0),
+ _currentShakePos(0), _newShakePos(0),
+ _paletteDirtyStart(0), _paletteDirtyEnd(0),
+ _graphicsMutex(0), _transactionMode(kTransactionNone) {
+
+ // allocate palette storage
+ _currentPalette = (SDL_Color *)calloc(sizeof(SDL_Color), 256);
+ _cursorPalette = (SDL_Color *)calloc(sizeof(SDL_Color), 256);
+
+ _mouseBackup.x = _mouseBackup.y = _mouseBackup.w = _mouseBackup.h = 0;
+
+ // reset mouse state
+ memset(&_km, 0, sizeof(_km));
+ memset(&_mouseCurState, 0, sizeof(_mouseCurState));
+
+ _inited = false;
+}
+
+OSystem_SDL::~OSystem_SDL() {
+ free(_dirtyChecksums);
+ free(_currentPalette);
+ free(_cursorPalette);
+ free(_mouseData);
+}
+
+uint32 OSystem_SDL::getMillis() {
+ return SDL_GetTicks();
+}
+
+void OSystem_SDL::delayMillis(uint msecs) {
+ SDL_Delay(msecs);
+}
+
+void OSystem_SDL::setTimerCallback(TimerProc callback, int timer) {
+ SDL_SetTimer(timer, (SDL_TimerCallback) callback);
+}
+
+void OSystem_SDL::setWindowCaption(const char *caption) {
+ SDL_WM_SetCaption(caption, caption);
+}
+
+bool OSystem_SDL::hasFeature(Feature f) {
+ return
+ (f == kFeatureFullscreenMode) ||
+ (f == kFeatureAspectRatioCorrection) ||
+ (f == kFeatureAutoComputeDirtyRects) ||
+ (f == kFeatureCursorHasPalette);
+}
+
+void OSystem_SDL::setFeatureState(Feature f, bool enable) {
+ switch (f) {
+ case kFeatureFullscreenMode:
+ setFullscreenMode(enable);
+ break;
+ case kFeatureAspectRatioCorrection:
+ setAspectRatioCorrection(enable);
+ break;
+ case kFeatureAutoComputeDirtyRects:
+ if (enable)
+ _modeFlags |= DF_WANT_RECT_OPTIM;
+ else
+ _modeFlags &= ~DF_WANT_RECT_OPTIM;
+ break;
+ default:
+ break;
+ }
+}
+
+bool OSystem_SDL::getFeatureState(Feature f) {
+ assert (_transactionMode == kTransactionNone);
+
+ switch (f) {
+ case kFeatureFullscreenMode:
+ return _fullscreen;
+ case kFeatureAspectRatioCorrection:
+ return _adjustAspectRatio;
+ case kFeatureAutoComputeDirtyRects:
+ return _modeFlags & DF_WANT_RECT_OPTIM;
+ default:
+ return false;
+ }
+}
+
+void OSystem_SDL::quit() {
+ if (_cdrom) {
+ SDL_CDStop(_cdrom);
+ SDL_CDClose(_cdrom);
+ }
+ unloadGFXMode();
+ deleteMutex(_graphicsMutex);
+
+ if (_joystick)
+ SDL_JoystickClose(_joystick);
+ SDL_ShowCursor(SDL_ENABLE);
+ SDL_Quit();
+
+ exit(0);
+}
+
+void OSystem_SDL::setupIcon() {
+ int w, h, ncols, nbytes, i;
+ unsigned int rgba[256], icon[32 * 32];
+ unsigned char mask[32][4];
+
+ sscanf(scummvm_icon[0], "%d %d %d %d", &w, &h, &ncols, &nbytes);
+ if ((w != 32) || (h != 32) || (ncols > 255) || (nbytes > 1)) {
+ warning("Could not load the icon (%d %d %d %d)", w, h, ncols, nbytes);
+ return;
+ }
+ for (i = 0; i < ncols; i++) {
+ unsigned char code;
+ char color[32];
+ unsigned int col;
+ sscanf(scummvm_icon[1 + i], "%c c %s", &code, color);
+ if (!strcmp(color, "None"))
+ col = 0x00000000;
+ else if (!strcmp(color, "black"))
+ col = 0xFF000000;
+ else if (color[0] == '#') {
+ sscanf(color + 1, "%06x", &col);
+ col |= 0xFF000000;
+ } else {
+ warning("Could not load the icon (%d %s - %s) ", code, color, scummvm_icon[1 + i]);
+ return;
+ }
+
+ rgba[code] = col;
+ }
+ memset(mask, 0, sizeof(mask));
+ for (h = 0; h < 32; h++) {
+ const char *line = scummvm_icon[1 + ncols + h];
+ for (w = 0; w < 32; w++) {
+ icon[w + 32 * h] = rgba[(int)line[w]];
+ if (rgba[(int)line[w]] & 0xFF000000) {
+ mask[h][w >> 3] |= 1 << (7 - (w & 0x07));
+ }
+ }
+ }
+
+ SDL_Surface *sdl_surf = SDL_CreateRGBSurfaceFrom(icon, 32, 32, 32, 32 * 4, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF000000);
+ SDL_WM_SetIcon(sdl_surf, (unsigned char *) mask);
+ SDL_FreeSurface(sdl_surf);
+}
+
+OSystem::MutexRef OSystem_SDL::createMutex(void) {
+ return (MutexRef) SDL_CreateMutex();
+}
+
+void OSystem_SDL::lockMutex(MutexRef mutex) {
+ SDL_mutexP((SDL_mutex *) mutex);
+}
+
+void OSystem_SDL::unlockMutex(MutexRef mutex) {
+ SDL_mutexV((SDL_mutex *) mutex);
+}
+
+void OSystem_SDL::deleteMutex(MutexRef mutex) {
+ SDL_DestroyMutex((SDL_mutex *) mutex);
+}
+
+#pragma mark -
+#pragma mark --- Audio ---
+#pragma mark -
+
+bool OSystem_SDL::setSoundCallback(SoundProc proc, void *param) {
+ SDL_AudioSpec desired;
+ SDL_AudioSpec obtained;
+
+ memset(&desired, 0, sizeof(desired));
+
+ _samplesPerSec = 0;
+
+ if (ConfMan.hasKey("output_rate"))
+ _samplesPerSec = ConfMan.getInt("output_rate");
+
+ if (_samplesPerSec <= 0)
+ _samplesPerSec = SAMPLES_PER_SEC;
+
+ // Originally, we always used 2048 samples. This loop will produce the
+ // same result at 22050 Hz, and should hopefully produce something
+ // sensible for other frequencies. Note that it must be a power of two.
+
+ uint32 samples = 0x8000;
+
+ for (;;) {
+ if ((1000 * samples) / _samplesPerSec < 100)
+ break;
+ samples >>= 1;
+ }
+
+ desired.freq = _samplesPerSec;
+ desired.format = AUDIO_S16SYS;
+ desired.channels = 2;
+ desired.samples = (uint16)samples;
+ desired.callback = proc;
+ desired.userdata = param;
+ if (SDL_OpenAudio(&desired, &obtained) != 0) {
+ warning("Could not open audio device: %s", SDL_GetError());
+ return false;
+ }
+ // Note: This should be the obtained output rate, but it seems that at
+ // least on some platforms SDL will lie and claim it did get the rate
+ // even if it didn't. Probably only happens for "weird" rates, though.
+ _samplesPerSec = obtained.freq;
+ SDL_PauseAudio(0);
+ return true;
+}
+
+void OSystem_SDL::clearSoundCallback() {
+ SDL_CloseAudio();
+}
+
+int OSystem_SDL::getOutputSampleRate() const {
+ return _samplesPerSec;
+}
+
+#pragma mark -
+#pragma mark --- CD Audio ---
+#pragma mark -
+
+bool OSystem_SDL::openCD(int drive) {
+ if (SDL_InitSubSystem(SDL_INIT_CDROM) == -1)
+ _cdrom = NULL;
+ else {
+ _cdrom = SDL_CDOpen(drive);
+ // Did it open? Check if _cdrom is NULL
+ if (!_cdrom) {
+ warning("Couldn't open drive: %s", SDL_GetError());
+ } else {
+ _cdNumLoops = 0;
+ _cdStopTime = 0;
+ _cdEndTime = 0;
+ }
+ }
+
+ return (_cdrom != NULL);
+}
+
+void OSystem_SDL::stopCD() { /* Stop CD Audio in 1/10th of a second */
+ _cdStopTime = SDL_GetTicks() + 100;
+ _cdNumLoops = 0;
+}
+
+void OSystem_SDL::playCD(int track, int num_loops, int start_frame, int duration) {
+ if (!num_loops && !start_frame)
+ return;
+
+ if (!_cdrom)
+ return;
+
+ if (duration > 0)
+ duration += 5;
+
+ _cdTrack = track;
+ _cdNumLoops = num_loops;
+ _cdStartFrame = start_frame;
+
+ SDL_CDStatus(_cdrom);
+ if (start_frame == 0 && duration == 0)
+ SDL_CDPlayTracks(_cdrom, track, 0, 1, 0);
+ else
+ SDL_CDPlayTracks(_cdrom, track, start_frame, 0, duration);
+ _cdDuration = duration;
+ _cdStopTime = 0;
+ _cdEndTime = SDL_GetTicks() + _cdrom->track[track].length * 1000 / CD_FPS;
+}
+
+bool OSystem_SDL::pollCD() {
+ if (!_cdrom)
+ return false;
+
+ return (_cdNumLoops != 0 && (SDL_GetTicks() < _cdEndTime || SDL_CDStatus(_cdrom) != CD_STOPPED));
+}
+
+void OSystem_SDL::updateCD() {
+ if (!_cdrom)
+ return;
+
+ if (_cdStopTime != 0 && SDL_GetTicks() >= _cdStopTime) {
+ SDL_CDStop(_cdrom);
+ _cdNumLoops = 0;
+ _cdStopTime = 0;
+ return;
+ }
+
+ if (_cdNumLoops == 0 || SDL_GetTicks() < _cdEndTime)
+ return;
+
+ if (_cdNumLoops != 1 && SDL_CDStatus(_cdrom) != CD_STOPPED) {
+ // Wait another second for it to be done
+ _cdEndTime += 1000;
+ return;
+ }
+
+ if (_cdNumLoops > 0)
+ _cdNumLoops--;
+
+ if (_cdNumLoops != 0) {
+ if (_cdStartFrame == 0 && _cdDuration == 0)
+ SDL_CDPlayTracks(_cdrom, _cdTrack, 0, 1, 0);
+ else
+ SDL_CDPlayTracks(_cdrom, _cdTrack, _cdStartFrame, 0, _cdDuration);
+ _cdEndTime = SDL_GetTicks() + _cdrom->track[_cdTrack].length * 1000 / CD_FPS;
+ }
+}