16 #define DIE(format, ...) do { \
17 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
18 fprintf(stderr, format "\n", ## __VA_ARGS__); \
26 static SDL_Rect windowSize, screenSize;
27 static bool gotWindowSize, gotScreenSize;
29 /** The current scaler object */
30 static Scaler* scaler;
32 static void calculateScreenSize()
35 SDL_VERSION(&wminfo.version);
37 if ( SDL_GetWMInfo(&wminfo) ) {
38 Display *dpy = wminfo.info.x11.display;
41 XWindowAttributes xwa;
43 if (Config.fullscreen) {
44 w = wminfo.info.x11.fswindow;
48 w = wminfo.info.x11.wmwindow;
53 XGetWindowAttributes(dpy, w, &xwa);
61 void S9xSetTitle(const char *title)
64 SDL_VERSION(&info.version);
65 if ( SDL_GetWMInfo(&info) ) {
66 Display *dpy = info.info.x11.display;
69 win = info.info.x11.fswindow;
70 if (win) XStoreName(dpy, win, title);
71 win = info.info.x11.wmwindow;
72 if (win) XStoreName(dpy, win, title);
77 static void freeVideoSurface()
79 screen = 0; // There's no need to free the screen surface.
82 free(GFX.SubScreen); GFX.SubScreen = 0;
83 free(GFX.ZBuffer); GFX.ZBuffer = 0;
84 free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
86 delete scaler; scaler = 0;
89 static void setupVideoSurface()
92 const unsigned gameWidth = IMAGE_WIDTH;
93 const unsigned gameHeight = IMAGE_HEIGHT;
96 if ((Config.fullscreen && !gotScreenSize) ||
97 (!Config.fullscreen && !gotWindowSize)) {
98 // Do a first try, in order to get window/screen size
99 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
100 SDL_SWSURFACE | SDL_RESIZABLE |
101 (Config.fullscreen ? SDL_FULLSCREEN : 0));
102 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
103 calculateScreenSize();
105 if (Config.fullscreen) {
106 GUI.Width = screenSize.w;
107 GUI.Height = screenSize.h;
109 GUI.Width = windowSize.w;
110 GUI.Height = windowSize.h;
113 GUI.Width = gameWidth;
114 GUI.Height = gameHeight;
118 if (gameHeight > GUI.Height || gameWidth > GUI.Width)
119 DIE("Video is larger than window size!");
121 const ScalerFactory* sFactory =
122 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
124 screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
125 Settings.SixteenBit ? 16 : 8,
127 (Config.fullscreen ? SDL_FULLSCREEN : 0));
129 DIE("SDL_SetVideoMode: %s", SDL_GetError());
131 SDL_ShowCursor(SDL_DISABLE);
133 scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
135 // We get pitch surface values from SDL
136 GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
137 GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
138 GFX.PixSize = screen->format->BitsPerPixel / 8;
140 GFX.Screen = scaler->getDrawBuffer();
141 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
142 GFX.ZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
143 GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
145 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
146 GFX.PPL = GFX.Pitch >> 1;
147 GFX.PPLx2 = GFX.Pitch;
149 scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
150 GUI.Scale = scaler->getRatio();
152 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
153 gameWidth, gameHeight,
154 screen->w, screen->h, screen->format->BitsPerPixel,
155 Config.fullscreen ? "fullscreen" : "windowed",
159 static void drawOnscreenControls()
161 if (Config.touchscreenInput) {
162 S9xInputScreenChanged();
163 if (Config.touchscreenShow) {
165 SDL_FillRect(screen, NULL, 0);
166 S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
167 screen->pixels, screen->pitch);
174 void S9xInitDisplay(int argc, const char ** argv)
176 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
177 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
180 drawOnscreenControls();
183 void S9xDeinitDisplay()
186 SDL_QuitSubSystem(SDL_INIT_VIDEO);
189 void S9xVideoToggleFullscreen()
192 Config.fullscreen = !Config.fullscreen;
194 drawOnscreenControls();
197 void S9xVideoOutputFocus(bool hasFocus)
210 // This is here for completeness, but palette mode is useless on N8x0
211 void S9xSetPalette ()
213 if (Settings.SixteenBit) return;
215 SDL_Color colors[256];
216 int brightness = IPPU.MaxBrightness *138;
217 for (int i = 0; i < 256; i++)
219 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
220 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
221 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
224 SDL_SetColors(screen, colors, 0, 256);
227 bool8_32 S9xInitUpdate ()
234 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)