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 realWidth = IMAGE_WIDTH;
109 unsigned realHeight = IMAGE_HEIGHT;
110 // SDL Window/Surface size (bigger, so we can get mouse events there).
111 unsigned srfWidth, srfHeight;
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(realWidth, realHeight, 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 srfWidth = screenSize.w;
125 srfHeight = screenSize.h;
127 srfWidth = windowSize.w;
128 srfHeight = windowSize.h;
131 // By now, just assume xsp == fullscreen. This has to change.
132 Config.xsp = Config.fullscreen;
134 setDoubling(false); // Before switching video modes; avoids flicker.
137 srfWidth = realWidth;
138 srfHeight = realHeight;
141 screen = SDL_SetVideoMode(srfWidth, srfHeight,
142 Settings.SixteenBit ? 16 : 8,
144 (Config.fullscreen ? SDL_FULLSCREEN : 0));
146 DIE("SDL_SetVideoMode: %s", SDL_GetError());
148 SDL_ShowCursor(SDL_DISABLE);
150 // We get pitch surface values from SDL
151 GFX.RealPitch = GFX.Pitch = screen->pitch;
152 GFX.ZPitch = realWidth; // The ZBuffer is independent of SDL surface size.
153 GFX.PixSize = screen->format->BitsPerPixel / 8;
155 // Ok, calculate renderArea
159 centerRectangle(renderArea, srfWidth, srfHeight,
160 realWidth * 2, realHeight * 2);
164 centerRectangle(renderArea, srfWidth, srfHeight, realWidth, realHeight);
167 centerRectangle(renderArea, srfWidth, srfHeight, realWidth, realHeight);
170 GFX.Screen = ((uint8*) screen->pixels)
171 + (renderArea.x * GFX.PixSize)
172 + (renderArea.y * GFX.Pitch);
173 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
174 GFX.ZBuffer = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
175 GFX.SubZBuffer = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
177 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
178 GFX.PPL = GFX.Pitch >> 1;
179 GFX.PPLx2 = GFX.Pitch;
180 GFX.ZPitch = GFX.Pitch >> 1; // TODO
182 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s %s\n",
183 realWidth, realHeight,
184 screen->w, screen->h, screen->format->BitsPerPixel,
185 Config.fullscreen ? "fullscreen" : "windowed",
186 Config.xsp ? "with pixel doubling" : "");
189 void S9xInitDisplay(int argc, const char ** argv)
191 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
192 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
197 void S9xDeinitDisplay()
200 SDL_QuitSubSystem(SDL_INIT_VIDEO);
203 void S9xVideoToggleFullscreen()
205 Config.fullscreen = !Config.fullscreen;
210 void S9xVideoOutputFocus(bool hasFocus)
213 setDoubling(hasFocus);
217 // This is here for completeness, but palette mode is useless on N8x0
218 void S9xSetPalette ()
220 if (Settings.SixteenBit) return;
222 SDL_Color colors[256];
223 int brightness = IPPU.MaxBrightness *138;
224 for (int i = 0; i < 256; i++)
226 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
227 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
228 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
231 SDL_SetColors(screen, colors, 0, 256);
234 bool8_32 S9xInitUpdate ()
236 if(SDL_MUSTLOCK(screen))
238 if(SDL_LockSurface(screen) < 0) {
239 DIE("Failed to lock SDL surface: %s", SDL_GetError());
246 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
248 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
255 SDL_UpdateRects(screen, 1, &renderArea);