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__); \
21 static SDL_Surface *screen;
23 static SDL_Rect windowSize, screenSize;
24 static bool gotWindowSize, gotScreenSize;
26 /** Inside the surface, where are we drawing */
27 static SDL_Rect renderArea;
30 static void setDoubling(bool enable)
33 SDL_VERSION(&wminfo.version);
34 if ( SDL_GetWMInfo(&wminfo) ) {
35 Display *dpy = wminfo.info.x11.display;
36 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
42 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
44 result.x = areaW / 2 - w / 2;
46 result.y = areaH / 2 - h / 2;
50 static void calculateScreenSize()
53 SDL_VERSION(&wminfo.version);
55 if ( SDL_GetWMInfo(&wminfo) ) {
56 Display *dpy = wminfo.info.x11.display;
59 XWindowAttributes xwa;
61 if (Config.fullscreen) {
62 w = wminfo.info.x11.fswindow;
66 w = wminfo.info.x11.wmwindow;
71 XGetWindowAttributes(dpy, w, &xwa);
79 void S9xSetTitle(const char *title)
82 SDL_VERSION(&info.version);
83 if ( SDL_GetWMInfo(&info) ) {
84 Display *dpy = info.info.x11.display;
87 win = info.info.x11.fswindow;
88 if (win) XStoreName(dpy, win, title);
89 win = info.info.x11.wmwindow;
90 if (win) XStoreName(dpy, win, title);
95 static void freeVideoSurface()
97 screen = 0; // There's no need to free the screen surface.
100 free(GFX.SubScreen); GFX.SubScreen = 0;
101 free(GFX.ZBuffer); GFX.ZBuffer = 0;
102 free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
105 static void setupVideoSurface()
107 // Real surface area.
108 unsigned gameWidth = IMAGE_WIDTH;
109 unsigned gameHeight = IMAGE_HEIGHT;
110 // SDL Window/Surface size (bigger, so we can get mouse events there).
111 unsigned winWidth, winHeight;
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 winWidth = screenSize.w;
125 winHeight = screenSize.h;
127 winWidth = windowSize.w;
128 winHeight = windowSize.h;
131 // So, can we enable Xsp?
132 if (gameWidth * 2 < winWidth && gameHeight * 2 < winHeight) {
136 setDoubling(false); // Before switching video modes; avoids flicker.
139 winWidth = gameWidth;
140 winHeight = gameHeight;
144 if (gameHeight > winHeight || gameWidth > winWidth)
145 DIE("Video is larger than window size!");
147 screen = SDL_SetVideoMode(winWidth, winHeight,
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, winWidth, winHeight,
166 gameWidth * 2, gameHeight * 2);
170 centerRectangle(renderArea, winWidth, winHeight, gameWidth, gameHeight);
173 centerRectangle(renderArea, winWidth, winHeight, gameWidth, gameHeight);
176 GFX.Screen = ((uint8*) screen->pixels)
177 + (renderArea.x * GFX.PixSize)
178 + (renderArea.y * GFX.Pitch);
179 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
180 GFX.ZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
181 GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
183 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
184 GFX.PPL = GFX.Pitch >> 1;
185 GFX.PPLx2 = GFX.Pitch;
187 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s %s\n",
188 gameWidth, gameHeight,
189 screen->w, screen->h, screen->format->BitsPerPixel,
190 Config.fullscreen ? "fullscreen" : "windowed",
191 Config.xsp ? "with pixel doubling" : "");
194 void S9xInitDisplay(int argc, const char ** argv)
196 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
197 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
202 void S9xDeinitDisplay()
205 SDL_QuitSubSystem(SDL_INIT_VIDEO);
208 void S9xVideoToggleFullscreen()
210 Config.fullscreen = !Config.fullscreen;
215 void S9xVideoOutputFocus(bool hasFocus)
218 setDoubling(hasFocus);
222 void S9xVideoGetWindowSize(unsigned int* w, unsigned int* h)
224 if (Config.fullscreen) {
233 // This is here for completeness, but palette mode is useless on N8x0
234 void S9xSetPalette ()
236 if (Settings.SixteenBit) return;
238 SDL_Color colors[256];
239 int brightness = IPPU.MaxBrightness *138;
240 for (int i = 0; i < 256; i++)
242 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
243 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
244 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
247 SDL_SetColors(screen, colors, 0, 256);
250 bool8_32 S9xInitUpdate ()
252 if(SDL_MUSTLOCK(screen))
254 if(SDL_LockSurface(screen) < 0) {
255 DIE("Failed to lock SDL surface: %s", SDL_GetError());
262 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
264 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
266 SDL_UpdateRects(screen, 1, &renderArea);