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;
121 if (gameHeight > GUI.Height || gameWidth > GUI.Width)
122 DIE("Video is larger than window size!");
124 const ScalerFactory* sFactory =
125 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
127 screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
128 Settings.SixteenBit ? 16 : 8,
130 (Config.fullscreen ? SDL_FULLSCREEN : 0));
132 DIE("SDL_SetVideoMode: %s", SDL_GetError());
134 SDL_ShowCursor(SDL_DISABLE);
137 hdSetupFullscreen(Config.fullscreen);
140 scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
142 // We get pitch surface values from SDL
143 GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
144 GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
145 GFX.PixSize = screen->format->BitsPerPixel / 8;
147 GFX.Screen = scaler->getDrawBuffer();
148 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
149 GFX.ZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
150 GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
152 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
153 GFX.PPL = GFX.Pitch >> 1;
154 GFX.PPLx2 = GFX.Pitch;
156 scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
157 scaler->getRatio(GUI.ScaleX, GUI.ScaleY);
159 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
160 gameWidth, gameHeight,
161 screen->w, screen->h, screen->format->BitsPerPixel,
162 Config.fullscreen ? "fullscreen" : "windowed",
166 static void drawOnscreenControls()
168 if (Config.touchscreenInput) {
169 S9xInputScreenChanged();
172 if (Config.touchscreenShow) {
174 SDL_FillRect(screen, NULL, 0);
175 S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
176 screen->pixels, screen->pitch);
182 void S9xInitDisplay(int argc, const char ** argv)
184 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
185 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
192 drawOnscreenControls();
195 void S9xDeinitDisplay()
198 SDL_QuitSubSystem(SDL_INIT_VIDEO);
201 void S9xVideoToggleFullscreen()
204 Config.fullscreen = !Config.fullscreen;
206 drawOnscreenControls();
209 void S9xVideoOutputFocus(bool hasFocus)
222 // This is here for completeness, but palette mode is useless on N8x0
223 void S9xSetPalette ()
225 if (Settings.SixteenBit) return;
227 SDL_Color colors[256];
228 int brightness = IPPU.MaxBrightness *138;
229 for (int i = 0; i < 256; i++)
231 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
232 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
233 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
236 SDL_SetColors(screen, colors, 0, 256);
239 bool8_32 S9xInitUpdate ()
246 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
251 if (exitRequiresDraw()) {