snes mouse support
[drnoksnes] / platform / sdlv.cpp
index 0f173b7..5cdc1aa 100644 (file)
@@ -18,6 +18,8 @@
                abort(); \
        } while (0);
 
+struct gui GUI;
+
 static SDL_Surface *screen;
 
 static SDL_Rect windowSize, screenSize;
@@ -105,40 +107,44 @@ static void freeVideoSurface()
 static void setupVideoSurface()
 {
        // Real surface area.
-       unsigned realWidth = IMAGE_WIDTH;
-       unsigned realHeight = IMAGE_HEIGHT;
-       // SDL Window/Surface size (bigger, so we can get mouse events there).
-       unsigned srfWidth, srfHeight;
+       const unsigned gameWidth = IMAGE_WIDTH;
+       const unsigned gameHeight = IMAGE_HEIGHT;
 
 #ifdef MAEMO
        if ((Config.fullscreen && !gotScreenSize) ||
                (!Config.fullscreen && !gotWindowSize)) {
                // Do a first try, in order to get window/screen size
-               screen = SDL_SetVideoMode(realWidth, realHeight, 16,
+               screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
                        SDL_SWSURFACE | SDL_RESIZABLE |
                        (Config.fullscreen ? SDL_FULLSCREEN : 0));
                if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
                calculateScreenSize();
        }
        if (Config.fullscreen) {
-               srfWidth = screenSize.w;
-               srfHeight = screenSize.h;
+               GUI.Width = screenSize.w;
+               GUI.Height = screenSize.h;
        } else {
-               srfWidth = windowSize.w;
-               srfHeight = windowSize.h;
+               GUI.Width = windowSize.w;
+               GUI.Height = windowSize.h;
        }
        
-       // By now, just assume xsp == fullscreen. This has to change.
-       Config.xsp = Config.fullscreen;
-       if (!Config.xsp) {
+       // So, can we enable Xsp?
+       if (gameWidth * 2 < GUI.Width && gameHeight * 2 < GUI.Height) {
+               Config.xsp = true;
+       } else  {
+               Config.xsp = false;
                setDoubling(false); // Before switching video modes; avoids flicker.
        }
 #else
-       srfWidth = realWidth;
-       srfHeight = realHeight;
+       GUI.Width = gameWidth;
+       GUI.Height = gameHeight;
 #endif
 
-       screen = SDL_SetVideoMode(srfWidth, srfHeight,
+       // Safeguard
+       if (gameHeight > GUI.Height || gameWidth > GUI.Width)
+               DIE("Video is larger than window size!");
+
+       screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
                                                                Settings.SixteenBit ? 16 : 8,
                                                                SDL_SWSURFACE |
                                                                (Config.fullscreen ? SDL_FULLSCREEN : 0));
@@ -156,15 +162,14 @@ static void setupVideoSurface()
 #ifdef MAEMO
        if (Config.xsp) {
                setDoubling(true);
-               centerRectangle(renderArea, srfWidth, srfHeight,
-                       realWidth * 2, realHeight * 2);
-               renderArea.w /= 2;
-               renderArea.h /= 2;
+               centerRectangle(renderArea, GUI.Width, GUI.Height,
+                       gameWidth * 2, gameHeight * 2);
        } else {
-               centerRectangle(renderArea, srfWidth, srfHeight, realWidth, realHeight);
+               centerRectangle(renderArea, GUI.Width, GUI.Height,
+                       gameWidth, gameHeight);
        }
 #else
-       centerRectangle(renderArea, srfWidth, srfHeight, realWidth, realHeight);
+       centerRectangle(renderArea, GUI.Width, GUI.Height, gameWidth, gameHeight);
 #endif
        
        GFX.Screen = ((uint8*) screen->pixels)
@@ -178,8 +183,21 @@ static void setupVideoSurface()
        GFX.PPL = GFX.Pitch >> 1;
        GFX.PPLx2 = GFX.Pitch;
 
+       GUI.RenderX = renderArea.x;
+       GUI.RenderY = renderArea.y;
+       GUI.RenderW = renderArea.w;
+       GUI.RenderH = renderArea.h;
+
+#ifdef MAEMO
+       if (Config.xsp) {
+               // Do not update 2x the area.
+               renderArea.w /= 2;
+               renderArea.h /= 2;
+       }
+#endif
+
        printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s %s\n",
-               realWidth, realHeight,
+               gameWidth, gameHeight,
                screen->w, screen->h, screen->format->BitsPerPixel,
                Config.fullscreen ? "fullscreen" : "windowed",
                Config.xsp ? "with pixel doubling" : "");
@@ -246,13 +264,8 @@ bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
 {
        if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
 
-       if (Config.xsp) {       
-               width *= 2;
-               height *= 2;
-       }
-
        SDL_UpdateRects(screen, 1, &renderArea);
-       
+
        return TRUE;
 }