removing lots of unused code
[drnoksnes] / platform / sdlv.cpp
index fbd06e8..7504231 100644 (file)
@@ -7,11 +7,9 @@
 
 #include "snes9x.h"
 #include "platform.h"
-#include "display.h"
 #include "gfx.h"
 #include "ppu.h"
 #include "sdlv.h"
-#include "scaler.h"
 
 #define DIE(format, ...) do { \
                fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
@@ -27,8 +25,11 @@ static SDL_Rect windowSize, screenSize;
 static bool gotWindowSize, gotScreenSize;
 
 /** The current scaler object */
-static Scaler* scaler;
+Scaler* scaler;
 
+/** Use the current window size to calculate screen size.
+       Useful on "single window" platforms, like Hildon.
+ */
 static void calculateScreenSize()
 {
        SDL_SysWMinfo wminfo;
@@ -58,8 +59,10 @@ static void calculateScreenSize()
        }
 }
 
+/** Sets the main window title */
 void S9xSetTitle(const char *title)
 {
+       // This is a Maemo specific hack, but works on most platforms.
        SDL_SysWMinfo info;
        SDL_VERSION(&info.version);
        if ( SDL_GetWMInfo(&info) ) {
@@ -93,9 +96,13 @@ static void setupVideoSurface()
        const unsigned gameHeight = IMAGE_HEIGHT;
 
 #ifdef MAEMO
+       // Under Maemo we know that the window manager will automatically
+       // resize our windows to fullscreen.
+       // Thus we can use that to detect the screen size.
+       // Of course, this causes flicker, so we try to avoid it when
+       // changing between modes.
        if ((Config.fullscreen && !gotScreenSize) ||
                (!Config.fullscreen && !gotWindowSize)) {
-               // Do a first try, in order to get window/screen size
                screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
                        SDL_SWSURFACE | SDL_RESIZABLE |
                        (Config.fullscreen ? SDL_FULLSCREEN : 0));
@@ -114,44 +121,39 @@ static void setupVideoSurface()
        GUI.Height = gameHeight;
 #endif
 #if CONF_EXIT_BUTTON
-       exitReset();
+       ExitBtnReset();
 #endif
 
        // Safeguard
        if (gameHeight > GUI.Height || gameWidth > GUI.Width)
                DIE("Video is larger than window size!");
 
-       const ScalerFactory* sFactory =
-               searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
+       const ScalerFactory* sFactory = searchForScaler(16, gameWidth, gameHeight);
+
+       screen = SDL_SetVideoMode(GUI.Width, GUI.Height, 16,
+               SDL_SWSURFACE | (Config.fullscreen ? SDL_FULLSCREEN : 0));
 
-       screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
-                                                               Settings.SixteenBit ? 16 : 8,
-                                                               SDL_SWSURFACE |
-                                                               (Config.fullscreen ? SDL_FULLSCREEN : 0));
        if (!screen)
                DIE("SDL_SetVideoMode: %s", SDL_GetError());
        
        SDL_ShowCursor(SDL_DISABLE);
 
-#if CONF_HD
-       hdSetupFullscreen(Config.fullscreen);
-#endif
-
        scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
 
-       // We get pitch surface values from SDL
-       GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
-       GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
-       GFX.PixSize = screen->format->BitsPerPixel / 8;
-       
+       // Each scaler may have its own pitch
+       GFX.Pitch = scaler->getDrawBufferPitch();
+       GFX.ZPitch = GFX.Pitch / 2;
+       // gfx & tile.cpp depend on the zbuffer pitch being always half of the color buffer pitch.
+       // Which is a pity, since the color buffer might be much larger.
+
        GFX.Screen = scaler->getDrawBuffer();
        GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
        GFX.ZBuffer =  (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
        GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
 
        GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
-       GFX.PPL = GFX.Pitch >> 1;
-       GFX.PPLx2 = GFX.Pitch;
+       GFX.DepthDelta = GFX.SubZBuffer - GFX.ZBuffer;
+       GFX.PPL = GFX.Pitch / (screen->format->BitsPerPixel / 8);
 
        scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
        scaler->getRatio(GUI.ScaleX, GUI.ScaleY);
@@ -172,22 +174,17 @@ static void drawOnscreenControls()
        if (Config.touchscreenShow) {
                scaler->pause();
                SDL_FillRect(screen, NULL, 0);
-               S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
-                                                       screen->pixels, screen->pitch);
+               S9xInputScreenDraw(screen->pixels, screen->pitch);
                SDL_Flip(screen);
                scaler->resume();
        }
 }
 
-void S9xInitDisplay(int argc, const char ** argv)
+void S9xInitDisplay(int argc, char ** argv)
 {      
        if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
                DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
 
-#if CONF_HD
-       hdSetup();
-#endif
-
        setupVideoSurface();
        drawOnscreenControls();
 }
@@ -206,52 +203,57 @@ void S9xVideoToggleFullscreen()
        drawOnscreenControls();
 }
 
-void S9xVideoOutputFocus(bool hasFocus)
+bool videoEventFilter(const SDL_Event& event)
 {
-#if MAEMO
-       if (scaler) {
-               if (hasFocus) {
-                       scaler->resume();
-               } else {
-                       scaler->pause();
+       // If we're in power save mode, and this is a defocus event, quit.
+       if (Config.saver) {
+               if (event.type == SDL_ACTIVEEVENT &&
+                  (event.active.state & SDL_APPINPUTFOCUS) &&
+                  !event.active.gain) {
+                       S9xDoAction(kActionQuit);
+                       return true;
                }
        }
-#endif
-}
 
-// This is here for completeness, but palette mode is useless on N8x0
-void S9xSetPalette ()
-{
-       if (Settings.SixteenBit) return;
-       
-       SDL_Color colors[256];
-       int brightness = IPPU.MaxBrightness *138;
-       for (int i = 0; i < 256; i++)
-       {
-               colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
-               colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
-               colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
-       }
-       
-       SDL_SetColors(screen, colors, 0, 256);
+       // Forward video event to the active scaler, if any.
+       if (scaler)
+               return scaler->filter(event);
+       else
+               return false;
 }
 
-bool8_32 S9xInitUpdate ()
+/** Called before rendering a frame.
+       This function must ensure GFX.Screen points to something, but we did that
+       while initializing video output.
+       @return TRUE if we should render the frame.
+ */
+bool8 S9xInitUpdate ()
 {
        scaler->prepare();
 
        return TRUE;
 }
 
-bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
+/** Called once a complete SNES screen has been rendered into the GFX.Screen
+       memory buffer.
+
+       Now is your chance to copy the SNES rendered screen to the
+       host computer's screen memory. The problem is that you have to cope with
+       different sized SNES rendered screens. Width is always 256, unless you're
+       supporting SNES hi-res. screen modes (Settings.SupportHiRes is TRUE), in
+       which case it can be 256 or 512. The height parameter can be either 224 or
+       239 if you're only supporting SNES lo-res. screen modes, or 224, 239, 448 or
+       478 if hi-res. SNES screen modes are being supported.
+ */
+// TODO Above.
+bool8 S9xDeinitUpdate (int width, int height)
 {
        scaler->finish();
 
 #if CONF_EXIT_BUTTON
-       if (exitRequiresDraw()) {
+       if (ExitBtnRequiresDraw()) {
                scaler->pause();
-               exitDraw(screen);
-               SDL_Flip(screen);
+               ExitBtnDraw(screen);
                scaler->resume();
        }
 #endif