5 #include <X11/extensions/Xsp.h>
15 #define DIE(format, ...) do { \
16 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
17 fprintf(stderr, format "\n", ## __VA_ARGS__); \
23 static SDL_Surface *screen;
25 static SDL_Rect windowSize, screenSize;
26 static bool gotWindowSize, gotScreenSize;
28 /** Inside the surface, where are we drawing */
29 static SDL_Rect renderArea;
32 static void setDoubling(bool enable)
35 SDL_VERSION(&wminfo.version);
36 if ( SDL_GetWMInfo(&wminfo) ) {
37 Display *dpy = wminfo.info.x11.display;
38 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
44 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
46 result.x = areaW / 2 - w / 2;
48 result.y = areaH / 2 - h / 2;
52 static void calculateScreenSize()
55 SDL_VERSION(&wminfo.version);
57 if ( SDL_GetWMInfo(&wminfo) ) {
58 Display *dpy = wminfo.info.x11.display;
61 XWindowAttributes xwa;
63 if (Config.fullscreen) {
64 w = wminfo.info.x11.fswindow;
68 w = wminfo.info.x11.wmwindow;
73 XGetWindowAttributes(dpy, w, &xwa);
81 void S9xSetTitle(const char *title)
84 SDL_VERSION(&info.version);
85 if ( SDL_GetWMInfo(&info) ) {
86 Display *dpy = info.info.x11.display;
89 win = info.info.x11.fswindow;
90 if (win) XStoreName(dpy, win, title);
91 win = info.info.x11.wmwindow;
92 if (win) XStoreName(dpy, win, title);
97 static void freeVideoSurface()
99 screen = 0; // There's no need to free the screen surface.
102 free(GFX.SubScreen); GFX.SubScreen = 0;
103 free(GFX.ZBuffer); GFX.ZBuffer = 0;
104 free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
107 static void setupVideoSurface()
109 // Real surface area.
110 const unsigned gameWidth = IMAGE_WIDTH;
111 const unsigned gameHeight = IMAGE_HEIGHT;
114 if ((Config.fullscreen && !gotScreenSize) ||
115 (!Config.fullscreen && !gotWindowSize)) {
116 // Do a first try, in order to get window/screen size
117 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
118 SDL_SWSURFACE | SDL_RESIZABLE |
119 (Config.fullscreen ? SDL_FULLSCREEN : 0));
120 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
121 calculateScreenSize();
123 if (Config.fullscreen) {
124 GUI.Width = screenSize.w;
125 GUI.Height = screenSize.h;
127 GUI.Width = windowSize.w;
128 GUI.Height = windowSize.h;
131 // So, can we enable Xsp?
132 if (gameWidth * 2 < GUI.Width && gameHeight * 2 < GUI.Height) {
136 setDoubling(false); // Before switching video modes; avoids flicker.
139 GUI.Width = gameWidth;
140 GUI.Height = gameHeight;
144 if (gameHeight > GUI.Height || gameWidth > GUI.Width)
145 DIE("Video is larger than window size!");
147 screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
148 Settings.SixteenBit ? 16 : 8,
150 (Config.fullscreen ? SDL_FULLSCREEN : 0));
152 DIE("SDL_SetVideoMode: %s", SDL_GetError());
154 SDL_ShowCursor(SDL_DISABLE);
156 // We get pitch surface values from SDL
157 GFX.RealPitch = GFX.Pitch = screen->pitch;
158 GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
159 GFX.PixSize = screen->format->BitsPerPixel / 8;
161 // Ok, calculate renderArea
165 centerRectangle(renderArea, GUI.Width, GUI.Height,
166 gameWidth * 2, gameHeight * 2);
168 centerRectangle(renderArea, GUI.Width, GUI.Height,
169 gameWidth, gameHeight);
172 centerRectangle(renderArea, GUI.Width, GUI.Height, gameWidth, gameHeight);
175 GFX.Screen = ((uint8*) screen->pixels)
176 + (renderArea.x * GFX.PixSize)
177 + (renderArea.y * GFX.Pitch);
178 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
179 GFX.ZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
180 GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
182 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
183 GFX.PPL = GFX.Pitch >> 1;
184 GFX.PPLx2 = GFX.Pitch;
186 GUI.RenderX = renderArea.x;
187 GUI.RenderY = renderArea.y;
188 GUI.RenderW = renderArea.w;
189 GUI.RenderH = renderArea.h;
193 // Do not update 2x the area.
199 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s %s\n",
200 gameWidth, gameHeight,
201 screen->w, screen->h, screen->format->BitsPerPixel,
202 Config.fullscreen ? "fullscreen" : "windowed",
203 Config.xsp ? "with pixel doubling" : "");
206 void S9xInitDisplay(int argc, const char ** argv)
208 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
209 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
214 void S9xDeinitDisplay()
217 SDL_QuitSubSystem(SDL_INIT_VIDEO);
220 void S9xVideoToggleFullscreen()
222 Config.fullscreen = !Config.fullscreen;
227 void S9xVideoOutputFocus(bool hasFocus)
230 setDoubling(hasFocus);
234 // This is here for completeness, but palette mode is useless on N8x0
235 void S9xSetPalette ()
237 if (Settings.SixteenBit) return;
239 SDL_Color colors[256];
240 int brightness = IPPU.MaxBrightness *138;
241 for (int i = 0; i < 256; i++)
243 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
244 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
245 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
248 SDL_SetColors(screen, colors, 0, 256);
251 bool8_32 S9xInitUpdate ()
253 if(SDL_MUSTLOCK(screen))
255 if(SDL_LockSurface(screen) < 0) {
256 DIE("Failed to lock SDL surface: %s", SDL_GetError());
263 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
265 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
267 SDL_UpdateRects(screen, 1, &renderArea);