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;
59 virtual void pause() = 0;
60 virtual void resume() = 0;
67 virtual ~ScalerFactory() { };
68 virtual const char * getName() const = 0;
69 virtual bool canEnable(int bpp, int w, int h) const = 0;
70 virtual Scaler* instantiate(SDL_Surface* screen, int w, int h) const = 0;
73 class DummyScaler : public Scaler
75 SDL_Surface * m_screen;
78 DummyScaler(SDL_Surface* screen, int w, int h)
81 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
89 class Factory : public ScalerFactory
91 const char * getName() const
96 bool canEnable(int bpp, int w, int h) const
101 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
103 return new DummyScaler(screen, w, h);
107 static const Factory factory;
109 const char * getName() const
114 uint8* getDrawBuffer() const
116 const int Bpp = screen->format->BitsPerPixel / 8;
117 const int pitch = screen->pitch;
118 return ((uint8*) screen->pixels)
120 + (m_area.y * pitch);
123 unsigned int getDrawBufferPitch() const
125 return screen->pitch;
128 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
129 unsigned short & w, unsigned short & h) const
131 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
143 SDL_UpdateRects(m_screen, 1, &m_area);
149 const DummyScaler::Factory DummyScaler::factory;
152 class ARMScaler : public Scaler
154 SDL_Surface * m_screen;
157 const int m_w, m_h, m_Bpp;
159 ARMScaler(SDL_Surface* screen, int w, int h)
160 : m_screen(screen), m_w(w), m_h(h),
161 m_Bpp(m_screen->format->BitsPerPixel / 8)
163 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
164 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
172 class Factory : public ScalerFactory
174 const char * getName() const
179 bool canEnable(int bpp, int w, int h) const
181 return bpp == 16 && w * 2 < GUI.Width && h * 2 < GUI.Height &&
182 w % 16 == 0 /* asm assumes w div by 16 */;
185 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
187 return new ARMScaler(screen, w, h);
191 static const Factory factory;
193 const char * getName() const
195 return "software ARM 2x scaling";
198 uint8* getDrawBuffer() const
203 unsigned int getDrawBufferPitch() const
208 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
209 unsigned short & w, unsigned short & h) const
211 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
223 uint16 * src = reinterpret_cast<uint16*>(m_surface);
224 uint16 * dst = reinterpret_cast<uint16*>(
225 ((uint8*) m_screen->pixels)
227 + (m_area.y * m_screen->pitch));
228 const int src_pitch = m_w;
229 const int dst_pitch = m_screen->pitch / m_Bpp;
232 for (y = 0; y < m_h*2; y++) {
235 "mov r0, %0; mov r1, %1; mov r2, %2;"
236 "stmdb r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
237 "1: ldmia r1!,{r3,r4,r5,r6,r7,r8,r9,r10};"
238 "mov r14,r5,lsr #16;"
239 "mov r12,r5,lsl #16;"
240 "orr r14,r14,r14,lsl #16;"
241 "orr r12,r12,r12,lsr #16;"
242 "mov r11,r4,lsr #16;"
244 "orr r11,r11,r11,lsl #16;"
245 "orr r5,r5,r5,lsr #16;"
248 "orr r4,r4,r4,lsl #16;"
249 "orr r3,r3,r3,lsr #16;"
250 "stmia r0!,{r3,r4,r5,r11,r12,r14};"
253 "orr r3,r3,r3,lsr #16;"
254 "orr r4,r4,r4,lsl #16;"
257 "orr r5,r5,r5,lsr #16;"
258 "orr r6,r6,r6,lsl #16;"
261 "orr r7,r7,r7,lsr #16;"
262 "orr r8,r8,r8,lsl #16;"
263 "mov r12,r10,lsr #16;"
264 "mov r11,r10,lsl #16;"
265 "orr r12,r12,r12,lsl #16;"
266 "orr r11,r11,r11,lsr #16;"
267 "mov r10,r9,lsr #16;"
269 "orr r10,r10,r10,lsl #16;"
270 "orr r9,r9,r9,lsr #16;"
271 "stmia r0!,{r3,r4,r5,r6,r7,r8,r9,r10,r11,r12};"
274 "ldmia r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
276 : "r" (dst), "r" (src), "r" (m_w)
277 : "r0", "r1", "r2", "r3"
280 if (y&1) src += src_pitch;
283 SDL_UpdateRects(m_screen, 1, &m_area);
289 const ARMScaler::Factory ARMScaler::factory;
292 class SWScaler : public Scaler
294 SDL_Surface * m_screen;
297 const int m_w, m_h, m_Bpp;
299 SWScaler(SDL_Surface* screen, int w, int h)
300 : m_screen(screen), m_w(w), m_h(h),
301 m_Bpp(m_screen->format->BitsPerPixel / 8)
303 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
304 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
312 class Factory : public ScalerFactory
314 const char * getName() const
319 bool canEnable(int bpp, int w, int h) const
321 return w * 2 < GUI.Width && h * 2 < GUI.Height;
324 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
326 return new SWScaler(screen, w, h);
330 static const Factory factory;
332 const char * getName() const
334 return "software 2x scaling";
337 uint8* getDrawBuffer() const
342 unsigned int getDrawBufferPitch() const
347 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
348 unsigned short & w, unsigned short & h) const
350 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
362 uint16 * src = reinterpret_cast<uint16*>(m_surface);
363 uint16 * dst = reinterpret_cast<uint16*>(
364 ((uint8*) m_screen->pixels)
366 + (m_area.y * m_screen->pitch));
367 const int src_pitch = m_w;
368 const int dst_pitch = m_screen->pitch / m_Bpp;
371 for (y = 0; y < m_h*2; y++) {
372 for (x = 0; x < m_w*2; x+=2) {
374 dst[x + 1] = src[x/2];
377 if (y&1) src += src_pitch;
380 SDL_UpdateRects(m_screen, 1, &m_area);
386 const SWScaler::Factory SWScaler::factory;
389 class XSPScaler : public Scaler
391 SDL_Surface* m_screen;
393 SDL_Rect m_real_area;
394 bool m_should_enable, m_enabled; // Try to avoid flicker.
396 static void setDoubling(bool enable)
398 SDL_SysWMinfo wminfo;
399 SDL_VERSION(&wminfo.version);
400 if ( SDL_GetWMInfo(&wminfo) ) {
401 Display *dpy = wminfo.info.x11.display;
402 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
407 XSPScaler(SDL_Surface* screen, int w, int h)
408 : m_screen(screen), m_should_enable(true), m_enabled(false)
410 centerRectangle(m_area, GUI.Width, GUI.Height,
413 m_real_area.x = m_area.x;
414 m_real_area.y = m_area.y;
415 m_real_area.w = m_area.w / 2;
416 m_real_area.h = m_area.h / 2;
421 if (m_enabled) setDoubling(false);
424 class Factory : public ScalerFactory
426 const char * getName() const
431 bool canEnable(int bpp, int w, int h) const
433 return w * 2 < GUI.Width && h * 2 < GUI.Height;
436 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
438 return new XSPScaler(screen, w, h);
442 static const Factory factory;
444 const char * getName() const
446 return "XSP pixel doubling";
449 uint8* getDrawBuffer() const
451 const int Bpp = screen->format->BitsPerPixel / 8;
452 const int pitch = screen->pitch;
453 return ((uint8*) screen->pixels)
455 + (m_area.y * pitch);
458 unsigned int getDrawBufferPitch() const
460 return screen->pitch;
463 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
464 unsigned short & w, unsigned short & h) const
466 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
476 if (m_should_enable && !m_enabled) {
484 SDL_UpdateRects(m_screen, 1, &m_real_area);
489 m_should_enable = false;
498 m_should_enable = true; // Will enable later
501 const XSPScaler::Factory XSPScaler::factory;
504 static const ScalerFactory* scalers[] = {
512 &DummyScaler::factory
515 static const ScalerFactory* searchForScaler(int bpp, int w, int h)
517 const int n = sizeof(scalers) / sizeof(ScalerFactory*);
520 if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
522 printf("Scalers list:\n");
523 for (i = 0; i < n; i++) {
524 printf(" %s\n", scalers[i]->getName());
526 DIE("End of scalers list");
527 } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
528 // We prefer a specific scaler
529 for (i = 0; i < n; i++) {
530 if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
531 if (scalers[i]->canEnable(bpp, w, h)) {
532 // Found the scaler selected by the user, and we can use it.
536 "Selected scaler '%s' cannot be enabled in this mode\n",
538 break; // Fallback to another scaler.
543 fprintf(stderr, "Selected scaler '%s' does not exist\n",
548 // Just try them all now, in a buildtime set priority.
549 for (i = 0; i < n; i++) {
550 if (scalers[i]->canEnable(bpp, w, h)) {
555 DIE("Can't use any scaler; this shouldn't happen.");
558 static void calculateScreenSize()
560 SDL_SysWMinfo wminfo;
561 SDL_VERSION(&wminfo.version);
563 if ( SDL_GetWMInfo(&wminfo) ) {
564 Display *dpy = wminfo.info.x11.display;
567 XWindowAttributes xwa;
569 if (Config.fullscreen) {
570 w = wminfo.info.x11.fswindow;
572 gotScreenSize = true;
574 w = wminfo.info.x11.wmwindow;
576 gotWindowSize = true;
579 XGetWindowAttributes(dpy, w, &xwa);
583 size->h = xwa.height;
587 void S9xSetTitle(const char *title)
590 SDL_VERSION(&info.version);
591 if ( SDL_GetWMInfo(&info) ) {
592 Display *dpy = info.info.x11.display;
595 win = info.info.x11.fswindow;
596 if (win) XStoreName(dpy, win, title);
597 win = info.info.x11.wmwindow;
598 if (win) XStoreName(dpy, win, title);
603 static void freeVideoSurface()
605 screen = 0; // There's no need to free the screen surface.
608 free(GFX.SubScreen); GFX.SubScreen = 0;
609 free(GFX.ZBuffer); GFX.ZBuffer = 0;
610 free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
612 delete scaler; scaler = 0;
615 static void setupVideoSurface()
617 // Real surface area.
618 const unsigned gameWidth = IMAGE_WIDTH;
619 const unsigned gameHeight = IMAGE_HEIGHT;
622 if ((Config.fullscreen && !gotScreenSize) ||
623 (!Config.fullscreen && !gotWindowSize)) {
624 // Do a first try, in order to get window/screen size
625 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
626 SDL_SWSURFACE | SDL_RESIZABLE |
627 (Config.fullscreen ? SDL_FULLSCREEN : 0));
628 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
629 calculateScreenSize();
631 if (Config.fullscreen) {
632 GUI.Width = screenSize.w;
633 GUI.Height = screenSize.h;
635 GUI.Width = windowSize.w;
636 GUI.Height = windowSize.h;
639 GUI.Width = gameWidth;
640 GUI.Height = gameHeight;
644 if (gameHeight > GUI.Height || gameWidth > GUI.Width)
645 DIE("Video is larger than window size!");
647 const ScalerFactory* sFactory =
648 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
650 screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
651 Settings.SixteenBit ? 16 : 8,
653 (Config.fullscreen ? SDL_FULLSCREEN : 0));
655 DIE("SDL_SetVideoMode: %s", SDL_GetError());
657 SDL_ShowCursor(SDL_DISABLE);
659 scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
661 // We get pitch surface values from SDL
662 GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
663 GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
664 GFX.PixSize = screen->format->BitsPerPixel / 8;
666 GFX.Screen = scaler->getDrawBuffer();
667 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
668 GFX.ZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
669 GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
671 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
672 GFX.PPL = GFX.Pitch >> 1;
673 GFX.PPLx2 = GFX.Pitch;
675 scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
676 GUI.Scale = scaler->getRatio();
678 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
679 gameWidth, gameHeight,
680 screen->w, screen->h, screen->format->BitsPerPixel,
681 Config.fullscreen ? "fullscreen" : "windowed",
685 static void drawOnscreenControls()
687 if (Config.touchscreenInput) {
688 S9xInputScreenChanged();
689 if (Config.touchscreenShow) {
691 SDL_FillRect(screen, NULL, 0);
692 S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
693 screen->pixels, screen->pitch);
700 void S9xInitDisplay(int argc, const char ** argv)
702 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
703 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
706 drawOnscreenControls();
709 void S9xDeinitDisplay()
712 SDL_QuitSubSystem(SDL_INIT_VIDEO);
715 void S9xVideoToggleFullscreen()
717 Config.fullscreen = !Config.fullscreen;
720 drawOnscreenControls();
723 void S9xVideoOutputFocus(bool hasFocus)
736 // This is here for completeness, but palette mode is useless on N8x0
737 void S9xSetPalette ()
739 if (Settings.SixteenBit) return;
741 SDL_Color colors[256];
742 int brightness = IPPU.MaxBrightness *138;
743 for (int i = 0; i < 256; i++)
745 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
746 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
747 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
750 SDL_SetColors(screen, colors, 0, 256);
753 bool8_32 S9xInitUpdate ()
760 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)