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;
31 /** Inside the surface, where are we drawing */
32 static SDL_Rect renderArea;
35 static void setDoubling(bool enable)
38 SDL_VERSION(&wminfo.version);
39 if ( SDL_GetWMInfo(&wminfo) ) {
40 Display *dpy = wminfo.info.x11.display;
41 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
47 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
49 result.x = areaW / 2 - w / 2;
51 result.y = areaH / 2 - h / 2;
55 static void calculateScreenSize()
58 SDL_VERSION(&wminfo.version);
60 if ( SDL_GetWMInfo(&wminfo) ) {
61 Display *dpy = wminfo.info.x11.display;
64 XWindowAttributes xwa;
66 if (Config.fullscreen) {
67 w = wminfo.info.x11.fswindow;
71 w = wminfo.info.x11.wmwindow;
76 XGetWindowAttributes(dpy, w, &xwa);
84 void S9xSetTitle(const char *title)
87 SDL_VERSION(&info.version);
88 if ( SDL_GetWMInfo(&info) ) {
89 Display *dpy = info.info.x11.display;
92 win = info.info.x11.fswindow;
93 if (win) XStoreName(dpy, win, title);
94 win = info.info.x11.wmwindow;
95 if (win) XStoreName(dpy, win, title);
100 static void freeVideoSurface()
102 screen = 0; // There's no need to free the screen surface.
105 free(GFX.SubScreen); GFX.SubScreen = 0;
106 free(GFX.ZBuffer); GFX.ZBuffer = 0;
107 free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
110 static void setupVideoSurface()
112 // Real surface area.
113 const unsigned gameWidth = IMAGE_WIDTH;
114 const unsigned gameHeight = IMAGE_HEIGHT;
117 if ((Config.fullscreen && !gotScreenSize) ||
118 (!Config.fullscreen && !gotWindowSize)) {
119 // Do a first try, in order to get window/screen size
120 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
121 SDL_SWSURFACE | SDL_RESIZABLE |
122 (Config.fullscreen ? SDL_FULLSCREEN : 0));
123 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
124 calculateScreenSize();
126 if (Config.fullscreen) {
127 GUI.Width = screenSize.w;
128 GUI.Height = screenSize.h;
130 GUI.Width = windowSize.w;
131 GUI.Height = windowSize.h;
134 GUI.Width = gameWidth;
135 GUI.Height = gameHeight;
138 // So, can we enable Xsp?
139 if (gameWidth * 2 < GUI.Width && gameHeight * 2 < GUI.Height) {
143 setDoubling(false); // Before switching video modes; avoids flicker.
150 if (gameHeight > GUI.Height || gameWidth > GUI.Width)
151 DIE("Video is larger than window size!");
153 screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
154 Settings.SixteenBit ? 16 : 8,
156 (Config.fullscreen ? SDL_FULLSCREEN : 0));
158 DIE("SDL_SetVideoMode: %s", SDL_GetError());
160 SDL_ShowCursor(SDL_DISABLE);
162 // We get pitch surface values from SDL
163 GFX.RealPitch = GFX.Pitch = screen->pitch;
164 GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
165 GFX.PixSize = screen->format->BitsPerPixel / 8;
167 // Ok, calculate renderArea
171 centerRectangle(renderArea, GUI.Width, GUI.Height,
172 gameWidth * 2, gameHeight * 2);
174 centerRectangle(renderArea, GUI.Width, GUI.Height,
175 gameWidth, gameHeight);
178 centerRectangle(renderArea, GUI.Width, GUI.Height, gameWidth, gameHeight);
181 GFX.Screen = ((uint8*) screen->pixels)
182 + (renderArea.x * GFX.PixSize)
183 + (renderArea.y * GFX.Pitch);
184 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
185 GFX.ZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
186 GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
188 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
189 GFX.PPL = GFX.Pitch >> 1;
190 GFX.PPLx2 = GFX.Pitch;
192 GUI.RenderX = renderArea.x;
193 GUI.RenderY = renderArea.y;
194 GUI.RenderW = renderArea.w;
195 GUI.RenderH = renderArea.h;
199 // Do not update 2x the area.
205 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s %s\n",
206 gameWidth, gameHeight,
207 screen->w, screen->h, screen->format->BitsPerPixel,
208 Config.fullscreen ? "fullscreen" : "windowed",
209 Config.xsp ? "with pixel doubling" : "");
212 void S9xInitDisplay(int argc, const char ** argv)
214 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
215 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
220 void S9xDeinitDisplay()
223 SDL_QuitSubSystem(SDL_INIT_VIDEO);
226 void S9xVideoToggleFullscreen()
228 Config.fullscreen = !Config.fullscreen;
233 void S9xVideoOutputFocus(bool hasFocus)
237 setDoubling(hasFocus);
242 // This is here for completeness, but palette mode is useless on N8x0
243 void S9xSetPalette ()
245 if (Settings.SixteenBit) return;
247 SDL_Color colors[256];
248 int brightness = IPPU.MaxBrightness *138;
249 for (int i = 0; i < 256; i++)
251 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
252 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
253 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
256 SDL_SetColors(screen, colors, 0, 256);
259 bool8_32 S9xInitUpdate ()
261 if(SDL_MUSTLOCK(screen))
263 if(SDL_LockSurface(screen) < 0) {
264 DIE("Failed to lock SDL surface: %s", SDL_GetError());
271 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
273 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
275 SDL_UpdateRects(screen, 1, &renderArea);