9 # include <X11/extensions/Xsp.h>
18 #define DIE(format, ...) do { \
19 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
20 fprintf(stderr, format "\n", ## __VA_ARGS__); \
26 static SDL_Surface* screen;
28 static SDL_Rect windowSize, screenSize;
29 static bool gotWindowSize, gotScreenSize;
32 /** The current scaler object */
33 static Scaler* scaler;
35 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
37 result.x = areaW / 2 - w / 2;
39 result.y = areaH / 2 - h / 2;
47 virtual ~Scaler() { };
49 virtual const char * getName() const = 0;
51 virtual uint8* getDrawBuffer() const = 0;
52 virtual unsigned int getDrawBufferPitch() const = 0;
53 virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
54 unsigned short & w, unsigned short & h)
56 virtual int getRatio() const = 0;
57 virtual void prepare() = 0;
58 virtual void finish() = 0;
65 virtual ~ScalerFactory() { };
66 virtual const char * getName() const = 0;
67 virtual bool canEnable(int w, int h) const = 0;
68 virtual Scaler* instantiate(SDL_Surface* screen, int w, int h) const = 0;
71 class DummyScaler : public Scaler
73 SDL_Surface * m_screen;
76 DummyScaler(SDL_Surface* screen, int w, int h)
79 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
87 class Factory : public ScalerFactory
89 const char * getName() const
94 bool canEnable(int w, int h) const
99 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
101 return new DummyScaler(screen, w, h);
105 static const Factory factory;
107 const char * getName() const
112 uint8* getDrawBuffer() const
114 const int Bpp = screen->format->BitsPerPixel / 8;
115 const int pitch = screen->pitch;
116 return ((uint8*) screen->pixels)
118 + (m_area.y * pitch);
121 unsigned int getDrawBufferPitch() const
123 return screen->pitch;
126 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
127 unsigned short & w, unsigned short & h) const
129 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
141 SDL_UpdateRects(m_screen, 1, &m_area);
144 const DummyScaler::Factory DummyScaler::factory;
146 class SWScaler : public Scaler
148 SDL_Surface * m_screen;
151 const int m_w, m_h, m_Bpp;
153 SWScaler(SDL_Surface* screen, int w, int h)
154 : m_screen(screen), m_w(w), m_h(h),
155 m_Bpp(m_screen->format->BitsPerPixel / 8)
157 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
158 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
166 class Factory : public ScalerFactory
168 const char * getName() const
173 bool canEnable(int w, int h) const
175 return w * 2 < GUI.Width && h * 2 < GUI.Height;
178 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
180 return new SWScaler(screen, w, h);
184 static const Factory factory;
186 const char * getName() const
188 return "software 2x scaling";
191 uint8* getDrawBuffer() const
196 unsigned int getDrawBufferPitch() const
201 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
202 unsigned short & w, unsigned short & h) const
204 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
216 uint16 * src = reinterpret_cast<uint16*>(m_surface);
217 uint16 * dst = reinterpret_cast<uint16*>(
218 ((uint8*) m_screen->pixels)
220 + (m_area.y * m_screen->pitch));
221 const int src_pitch = m_w;
222 const int dst_pitch = m_screen->pitch / m_Bpp;
225 for (y = 0; y < m_h*2; y++) {
226 for (x = 0; x < m_w*2; x+=2) {
228 dst[x + 1] = src[x/2];
231 if (y&1) src += src_pitch;
234 SDL_UpdateRects(m_screen, 1, &m_area);
237 const SWScaler::Factory SWScaler::factory;
240 class XSPScaler : public Scaler
242 SDL_Surface* m_screen;
244 SDL_Rect m_real_area;
246 static void setDoubling(bool enable)
248 SDL_SysWMinfo wminfo;
249 SDL_VERSION(&wminfo.version);
250 if ( SDL_GetWMInfo(&wminfo) ) {
251 Display *dpy = wminfo.info.x11.display;
252 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
257 XSPScaler(SDL_Surface* screen, int w, int h)
260 centerRectangle(m_area, GUI.Width, GUI.Height,
264 m_real_area.x = m_area.x;
265 m_real_area.y = m_area.y;
266 m_real_area.w = m_area.w / 2;
267 m_real_area.h = m_area.h / 2;
275 class Factory : public ScalerFactory
277 const char * getName() const
282 bool canEnable(int w, int h) const
284 return w * 2 < GUI.Width && h * 2 < GUI.Height;
287 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
289 return new XSPScaler(screen, w, h);
293 static const Factory factory;
295 const char * getName() const
297 return "XSP pixel doubling";
300 uint8* getDrawBuffer() const
302 const int Bpp = screen->format->BitsPerPixel / 8;
303 const int pitch = screen->pitch;
304 return ((uint8*) screen->pixels)
306 + (m_area.y * pitch);
309 int getDrawBufferPitch() const
311 return screen->pitch;
314 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
315 unsigned short & w, unsigned short & h) const
317 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
329 SDL_UpdateRects(m_screen, 1, &m_real_area);
332 const XSPScaler::Factory XSPScaler::factory;
335 static const ScalerFactory* scalers[] = {
340 &DummyScaler::factory
343 static const ScalerFactory* searchForScaler(int w, int h)
345 const int n = sizeof(scalers) / sizeof(ScalerFactory*);
349 // We prefer a specific scaler
350 for (i = 0; i < n; i++) {
351 if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
352 if (!scalers[i]->canEnable(w, h)) {
353 DIE("Cannot use selected scaler");
358 DIE("Select scaler does not exist");
361 for (i = 0; i < n; i++) {
362 if (scalers[i]->canEnable(w, h)) {
366 DIE("Can't use any scaler");
370 static void calculateScreenSize()
372 SDL_SysWMinfo wminfo;
373 SDL_VERSION(&wminfo.version);
375 if ( SDL_GetWMInfo(&wminfo) ) {
376 Display *dpy = wminfo.info.x11.display;
379 XWindowAttributes xwa;
381 if (Config.fullscreen) {
382 w = wminfo.info.x11.fswindow;
384 gotScreenSize = true;
386 w = wminfo.info.x11.wmwindow;
388 gotWindowSize = true;
391 XGetWindowAttributes(dpy, w, &xwa);
395 size->h = xwa.height;
399 void S9xSetTitle(const char *title)
402 SDL_VERSION(&info.version);
403 if ( SDL_GetWMInfo(&info) ) {
404 Display *dpy = info.info.x11.display;
407 win = info.info.x11.fswindow;
408 if (win) XStoreName(dpy, win, title);
409 win = info.info.x11.wmwindow;
410 if (win) XStoreName(dpy, win, title);
415 static void freeVideoSurface()
417 screen = 0; // There's no need to free the screen surface.
420 free(GFX.SubScreen); GFX.SubScreen = 0;
421 free(GFX.ZBuffer); GFX.ZBuffer = 0;
422 free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
424 free(scaler); scaler = 0;
427 static void setupVideoSurface()
429 // Real surface area.
430 const unsigned gameWidth = IMAGE_WIDTH;
431 const unsigned gameHeight = IMAGE_HEIGHT;
439 if ((Config.fullscreen && !gotScreenSize) ||
440 (!Config.fullscreen && !gotWindowSize)) {
441 // Do a first try, in order to get window/screen size
442 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
443 SDL_SWSURFACE | SDL_RESIZABLE |
444 (Config.fullscreen ? SDL_FULLSCREEN : 0));
445 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
446 calculateScreenSize();
448 if (Config.fullscreen) {
449 GUI.Width = screenSize.w;
450 GUI.Height = screenSize.h;
452 GUI.Width = windowSize.w;
453 GUI.Height = windowSize.h;
456 GUI.Width = gameWidth;
457 GUI.Height = gameHeight;
461 if (gameHeight > GUI.Height || gameWidth > GUI.Width)
462 DIE("Video is larger than window size!");
464 const ScalerFactory* sFactory = searchForScaler(gameWidth, gameHeight);
466 screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
467 Settings.SixteenBit ? 16 : 8,
469 (Config.fullscreen ? SDL_FULLSCREEN : 0));
471 DIE("SDL_SetVideoMode: %s", SDL_GetError());
473 SDL_ShowCursor(SDL_DISABLE);
475 scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
477 // We get pitch surface values from SDL
478 GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
479 GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
480 GFX.PixSize = screen->format->BitsPerPixel / 8;
482 GFX.Screen = scaler->getDrawBuffer();
483 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
484 GFX.ZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
485 GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
487 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
488 GFX.PPL = GFX.Pitch >> 1;
489 GFX.PPLx2 = GFX.Pitch;
491 scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
492 GUI.Scale = scaler->getRatio();
494 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
495 gameWidth, gameHeight,
496 screen->w, screen->h, screen->format->BitsPerPixel,
497 Config.fullscreen ? "fullscreen" : "windowed",
501 void S9xInitDisplay(int argc, const char ** argv)
503 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
504 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
509 void S9xDeinitDisplay()
512 SDL_QuitSubSystem(SDL_INIT_VIDEO);
515 void S9xVideoToggleFullscreen()
517 Config.fullscreen = !Config.fullscreen;
522 void S9xVideoOutputFocus(bool hasFocus)
526 setDoubling(hasFocus);
531 // This is here for completeness, but palette mode is useless on N8x0
532 void S9xSetPalette ()
534 if (Settings.SixteenBit) return;
536 SDL_Color colors[256];
537 int brightness = IPPU.MaxBrightness *138;
538 for (int i = 0; i < 256; i++)
540 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
541 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
542 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
545 SDL_SetColors(screen, colors, 0, 256);
548 bool8_32 S9xInitUpdate ()
555 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)