X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=platform%2Fsdlv.cpp;h=98964946a54c982214f12f5bbf08483c4a7ae744;hb=cbe623ccf368fc733eb7a881a423afabe250ce8f;hp=e61a861549f8d10b3aab0267b760ba519ac0906a;hpb=909f5635bffc4c3516ddbba4f9c63c2a7333afc7;p=drnoksnes diff --git a/platform/sdlv.cpp b/platform/sdlv.cpp index e61a861..9896494 100644 --- a/platform/sdlv.cpp +++ b/platform/sdlv.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -11,6 +10,7 @@ #include "display.h" #include "gfx.h" #include "ppu.h" +#include "sdlv.h" #define DIE(format, ...) do { \ fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \ @@ -18,19 +18,52 @@ abort(); \ } while (0); -static SDL_Surface *screen; +struct gui GUI; -static void setDoubling(bool enable) +SDL_Surface* screen; + +static SDL_Rect windowSize, screenSize; +static bool gotWindowSize, gotScreenSize; + +/** The current scaler object */ +Scaler* scaler; + +/** Use the current window size to calculate screen size. + Useful on "single window" platforms, like Hildon. + */ +static void calculateScreenSize() { SDL_SysWMinfo wminfo; SDL_VERSION(&wminfo.version); + if ( SDL_GetWMInfo(&wminfo) ) { - XSPSetPixelDoubling(wminfo.info.x11.display, 0, enable ? 1 : 0); + Display *dpy = wminfo.info.x11.display; + Window w; + SDL_Rect* size; + XWindowAttributes xwa; + + if (Config.fullscreen) { + w = wminfo.info.x11.fswindow; + size = &screenSize; + gotScreenSize = true; + } else { + w = wminfo.info.x11.wmwindow; + size = &windowSize; + gotWindowSize = true; + } + + XGetWindowAttributes(dpy, w, &xwa); + size->x = xwa.x; + size->y = xwa.y; + size->w = xwa.width; + size->h = xwa.height; } } +/** 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) ) { @@ -49,62 +82,115 @@ static void freeVideoSurface() { screen = 0; // There's no need to free the screen surface. GFX.Screen = 0; - + free(GFX.SubScreen); GFX.SubScreen = 0; free(GFX.ZBuffer); GFX.ZBuffer = 0; free(GFX.SubZBuffer); GFX.SubZBuffer = 0; + + delete scaler; scaler = 0; } static void setupVideoSurface() { - int w = IMAGE_WIDTH; - int h = IMAGE_HEIGHT; - - // By now, just assume xsp == fullscreen. This has to change. - Config.xsp = Config.fullscreen; - if (Config.xsp) { - w *= 2; - h *= 2; + // Real surface area. + const unsigned gameWidth = IMAGE_WIDTH; + 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)) { + 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) { + GUI.Width = screenSize.w; + GUI.Height = screenSize.h; } else { - setDoubling(false); // Before switching video modes + GUI.Width = windowSize.w; + GUI.Height = windowSize.h; } +#else + GUI.Width = gameWidth; + GUI.Height = gameHeight; +#endif +#if CONF_EXIT_BUTTON + ExitBtnReset(); +#endif + + // Safeguard + if (gameHeight > GUI.Height || gameWidth > GUI.Width) + DIE("Video is larger than window size!"); - screen = SDL_SetVideoMode(w, h, + const ScalerFactory* sFactory = + searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight); + + 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 (Config.xsp) setDoubling(true); - - GFX.RealPitch = GFX.Pitch = screen->pitch; - - GFX.Screen = (uint8*) screen->pixels; - GFX.SubScreen = (uint8 *) malloc(GFX.RealPitch * IMAGE_HEIGHT); - GFX.ZBuffer = (uint8 *) malloc(GFX.RealPitch * IMAGE_HEIGHT); - GFX.SubZBuffer = (uint8 *) malloc(GFX.RealPitch * IMAGE_HEIGHT); - + scaler = sFactory->instantiate(screen, gameWidth, gameHeight); + + // 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.ZPitch = GFX.Pitch >> 1; - - printf("Video: %dx%d, %hu bits per pixel, %s %s\n", screen->w, screen->h, - screen->format->BitsPerPixel, + 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); + + printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n", + gameWidth, gameHeight, + screen->w, screen->h, screen->format->BitsPerPixel, Config.fullscreen ? "fullscreen" : "windowed", - Config.xsp ? "with pixel doubling" : ""); + scaler->getName()); } -void S9xInitDisplay(int argc, const char ** argv) +static void drawOnscreenControls() +{ + if (Config.touchscreenInput) { + S9xInputScreenChanged(); + } + + if (Config.touchscreenShow) { + scaler->pause(); + SDL_FillRect(screen, NULL, 0); + S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1, + screen->pixels, screen->pitch); + SDL_Flip(screen); + scaler->resume(); + } +} + +void S9xInitDisplay(int argc, char ** argv) { if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError()); - + setupVideoSurface(); + drawOnscreenControls(); } void S9xDeinitDisplay() @@ -115,19 +201,32 @@ void S9xDeinitDisplay() void S9xVideoToggleFullscreen() { - Config.fullscreen = !Config.fullscreen; freeVideoSurface(); + Config.fullscreen = !Config.fullscreen; setupVideoSurface(); + drawOnscreenControls(); } -void S9xVideoOutputFocus(bool hasFocus) +bool videoEventFilter(const SDL_Event& event) { - if (Config.xsp) { - setDoubling(hasFocus); - } + // 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; + } + } + + // Forward video event to the active scaler, if any. + if (scaler) + return scaler->filter(event); + else + return false; } -// This is here for completeness, but palette mode is useless on N8x0 +// This is here for completeness, but palette mode is mostly useless (slow). void S9xSetPalette () { if (Settings.SixteenBit) return; @@ -144,29 +243,42 @@ void S9xSetPalette () SDL_SetColors(screen, colors, 0, 256); } +/** 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_32 S9xInitUpdate () { - if(SDL_MUSTLOCK(screen)) - { - if(SDL_LockSurface(screen) < 0) { - DIE("Failed to lock SDL surface: %s", SDL_GetError()); - } - } + scaler->prepare(); return TRUE; } +/** 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_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit) { - if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); + scaler->finish(); - if (Config.xsp) { - width *= 2; - height *= 2; +#if CONF_EXIT_BUTTON + if (ExitBtnRequiresDraw()) { + scaler->pause(); + ExitBtnDraw(screen); + scaler->resume(); } +#endif - SDL_UpdateRect(screen, 0, 0, width, height); - return TRUE; }