#include <SDL.h>
#include <SDL_syswm.h>
-#if CONF_XSP
-# include <X11/extensions/Xsp.h>
-#endif
-
#include "snes9x.h"
#include "platform.h"
#include "display.h"
#include "gfx.h"
#include "ppu.h"
+#include "sdlv.h"
#define DIE(format, ...) do { \
fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
struct gui GUI;
-static SDL_Surface* screen;
+SDL_Surface* screen;
static SDL_Rect windowSize, screenSize;
static bool gotWindowSize, gotScreenSize;
-class Scaler;
/** The current scaler object */
-static Scaler* scaler;
-
-static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
-{
- result.x = areaW / 2 - w / 2;
- result.w = w;
- result.y = areaH / 2 - h / 2;
- result.h = h;
-}
-
-class Scaler
-{
-public:
- Scaler() { };
- virtual ~Scaler() { };
-
- virtual const char * getName() const = 0;
-
- virtual uint8* getDrawBuffer() const = 0;
- virtual unsigned int getDrawBufferPitch() const = 0;
- virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
- unsigned short & w, unsigned short & h)
- const = 0;
- virtual int getRatio() const = 0;
- virtual void prepare() = 0;
- virtual void finish() = 0;
-};
-
-class ScalerFactory
-{
-public:
- ScalerFactory() { };
- virtual ~ScalerFactory() { };
- virtual const char * getName() const = 0;
- virtual bool canEnable(int w, int h) const = 0;
- virtual Scaler* instantiate(SDL_Surface* screen, int w, int h) const = 0;
-};
-
-class DummyScaler : public Scaler
-{
- SDL_Surface * m_screen;
- SDL_Rect m_area;
-
- DummyScaler(SDL_Surface* screen, int w, int h)
- : m_screen(screen)
- {
- centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
- }
-public:
-
- ~DummyScaler()
- {
- };
-
- class Factory : public ScalerFactory
- {
- const char * getName() const
- {
- return "none";
- }
-
- bool canEnable(int w, int h) const
- {
- return true;
- }
-
- Scaler* instantiate(SDL_Surface* screen, int w, int h) const
- {
- return new DummyScaler(screen, w, h);
- }
- };
-
- static const Factory factory;
-
- const char * getName() const
- {
- return "no scaling";
- }
-
- uint8* getDrawBuffer() const
- {
- const int Bpp = screen->format->BitsPerPixel / 8;
- const int pitch = screen->pitch;
- return ((uint8*) screen->pixels)
- + (m_area.x * Bpp)
- + (m_area.y * pitch);
- };
-
- unsigned int getDrawBufferPitch() const
- {
- return screen->pitch;
- };
-
- void getRenderedGUIArea(unsigned short & x, unsigned short & y,
- unsigned short & w, unsigned short & h) const
- {
- x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
- };
-
- int getRatio() const
- {
- return 1;
- };
-
- void prepare() { };
-
- void finish()
- {
- SDL_UpdateRects(m_screen, 1, &m_area);
- };
-};
-const DummyScaler::Factory DummyScaler::factory;
-
-class SWScaler : public Scaler
-{
- SDL_Surface * m_screen;
- SDL_Rect m_area;
- uint8 * m_surface;
- const int m_w, m_h, m_Bpp;
-
- SWScaler(SDL_Surface* screen, int w, int h)
- : m_screen(screen), m_w(w), m_h(h),
- m_Bpp(m_screen->format->BitsPerPixel / 8)
- {
- centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
- m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
- }
-public:
- ~SWScaler()
- {
- free(m_surface);
- };
-
- class Factory : public ScalerFactory
- {
- const char * getName() const
- {
- return "2x";
- }
-
- bool canEnable(int w, int h) const
- {
- return w * 2 < GUI.Width && h * 2 < GUI.Height;
- }
-
- Scaler* instantiate(SDL_Surface* screen, int w, int h) const
- {
- return new SWScaler(screen, w, h);
- }
- };
-
- static const Factory factory;
-
- const char * getName() const
- {
- return "software 2x scaling";
- }
-
- uint8* getDrawBuffer() const
- {
- return m_surface;
- };
-
- unsigned int getDrawBufferPitch() const
- {
- return m_w * m_Bpp;
- };
-
- void getRenderedGUIArea(unsigned short & x, unsigned short & y,
- unsigned short & w, unsigned short & h) const
- {
- x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
- };
-
- int getRatio() const
- {
- return 2;
- };
-
- void prepare() { };
-
- void finish()
- {
- uint16 * src = reinterpret_cast<uint16*>(m_surface);
- uint16 * dst = reinterpret_cast<uint16*>(
- ((uint8*) m_screen->pixels)
- + (m_area.x * m_Bpp)
- + (m_area.y * m_screen->pitch));
- const int src_pitch = m_w;
- const int dst_pitch = m_screen->pitch / m_Bpp;
- int x, y;
-
- for (y = 0; y < m_h*2; y++) {
- for (x = 0; x < m_w*2; x+=2) {
- dst[x] = src[x/2];
- dst[x + 1] = src[x/2];
- }
- dst += dst_pitch;
- if (y&1) src += src_pitch;
- }
-
- SDL_UpdateRects(m_screen, 1, &m_area);
- };
-};
-const SWScaler::Factory SWScaler::factory;
-
-#if CONF_XSP
-class XSPScaler : public Scaler
-{
- SDL_Surface* m_screen;
- SDL_Rect m_area;
- SDL_Rect m_real_area;
-
- static void setDoubling(bool enable)
- {
- SDL_SysWMinfo wminfo;
- SDL_VERSION(&wminfo.version);
- if ( SDL_GetWMInfo(&wminfo) ) {
- Display *dpy = wminfo.info.x11.display;
- XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
- XFlush(dpy);
- }
- }
-
- XSPScaler(SDL_Surface* screen, int w, int h)
- : m_screen(screen)
- {
- centerRectangle(m_area, GUI.Width, GUI.Height,
- w * 2, h * 2);
- setDoubling(true);
-
- m_real_area.x = m_area.x;
- m_real_area.y = m_area.y;
- m_real_area.w = m_area.w / 2;
- m_real_area.h = m_area.h / 2;
- };
-public:
- ~XSPScaler()
- {
- setDoubling(false);
- };
-
- class Factory : public ScalerFactory
- {
- const char * getName() const
- {
- return "xsp";
- }
-
- bool canEnable(int w, int h) const
- {
- return w * 2 < GUI.Width && h * 2 < GUI.Height;
- };
-
- Scaler* instantiate(SDL_Surface* screen, int w, int h) const
- {
- return new XSPScaler(screen, w, h);
- }
- };
-
- static const Factory factory;
-
- const char * getName() const
- {
- return "XSP pixel doubling";
- }
-
- uint8* getDrawBuffer() const
- {
- const int Bpp = screen->format->BitsPerPixel / 8;
- const int pitch = screen->pitch;
- return ((uint8*) screen->pixels)
- + (m_area.x * Bpp)
- + (m_area.y * pitch);
- };
-
- int getDrawBufferPitch() const
- {
- return screen->pitch;
- };
-
- void getRenderedGUIArea(unsigned short & x, unsigned short & y,
- unsigned short & w, unsigned short & h) const
- {
- x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
- };
-
- int getRatio() const
- {
- return 2;
- };
-
- void prepare() { };
-
- void finish()
- {
- SDL_UpdateRects(m_screen, 1, &m_real_area);
- };
-};
-const XSPScaler::Factory XSPScaler::factory;
-#endif
-
-static const ScalerFactory* scalers[] = {
-#if CONF_XSP
- &XSPScaler::factory,
-#endif
- &SWScaler::factory,
- &DummyScaler::factory
-};
-
-static const ScalerFactory* searchForScaler(int w, int h)
-{
- const int n = sizeof(scalers) / sizeof(ScalerFactory*);
- int i;
-
- if (Config.scaler) {
- // We prefer a specific scaler
- for (i = 0; i < n; i++) {
- if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
- if (!scalers[i]->canEnable(w, h)) {
- DIE("Cannot use selected scaler");
- }
- return scalers[i];
- }
- }
- DIE("Select scaler does not exist");
- } else {
- // Just try them all
- for (i = 0; i < n; i++) {
- if (scalers[i]->canEnable(w, h)) {
- return scalers[i];
- }
- }
- DIE("Can't use any scaler");
- }
-}
+Scaler* scaler;
static void calculateScreenSize()
{
free(GFX.ZBuffer); GFX.ZBuffer = 0;
free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
- free(scaler); scaler = 0;
+ delete scaler; scaler = 0;
}
static void setupVideoSurface()
const unsigned gameWidth = IMAGE_WIDTH;
const unsigned gameHeight = IMAGE_HEIGHT;
- if (scaler) {
- delete scaler;
- scaler = 0;
- }
-
#ifdef MAEMO
if ((Config.fullscreen && !gotScreenSize) ||
(!Config.fullscreen && !gotWindowSize)) {
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!");
- const ScalerFactory* sFactory = searchForScaler(gameWidth, gameHeight);
+ const ScalerFactory* sFactory =
+ searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
Settings.SixteenBit ? 16 : 8,
GFX.PPLx2 = GFX.Pitch;
scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
- GUI.Scale = scaler->getRatio();
+ scaler->getRatio(GUI.ScaleX, GUI.ScaleY);
printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
gameWidth, gameHeight,
scaler->getName());
}
+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, const char ** argv)
{
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
setupVideoSurface();
+ drawOnscreenControls();
}
void S9xDeinitDisplay()
void S9xVideoToggleFullscreen()
{
- Config.fullscreen = !Config.fullscreen;
freeVideoSurface();
+ Config.fullscreen = !Config.fullscreen;
setupVideoSurface();
+ drawOnscreenControls();
}
-void S9xVideoOutputFocus(bool hasFocus)
+void processVideoEvent(const SDL_Event& event)
{
-#if 0 // TODO
- if (Config.xsp) {
- setDoubling(hasFocus);
- }
-#endif
+ if (scaler)
+ scaler->filter(event);
}
// This is here for completeness, but palette mode is useless on N8x0
{
scaler->finish();
+#if CONF_EXIT_BUTTON
+ if (ExitBtnRequiresDraw()) {
+ scaler->pause();
+ ExitBtnDraw(screen);
+ scaler->resume();
+ }
+#endif
+
return TRUE;
}