15 #define DIE(format, ...) do { \
16 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
17 fprintf(stderr, format "\n", ## __VA_ARGS__); \
25 static SDL_Rect windowSize, screenSize;
26 static bool gotWindowSize, gotScreenSize;
28 /** The current scaler object */
31 static void calculateScreenSize()
34 SDL_VERSION(&wminfo.version);
36 if ( SDL_GetWMInfo(&wminfo) ) {
37 Display *dpy = wminfo.info.x11.display;
40 XWindowAttributes xwa;
42 if (Config.fullscreen) {
43 w = wminfo.info.x11.fswindow;
47 w = wminfo.info.x11.wmwindow;
52 XGetWindowAttributes(dpy, w, &xwa);
60 void S9xSetTitle(const char *title)
63 SDL_VERSION(&info.version);
64 if ( SDL_GetWMInfo(&info) ) {
65 Display *dpy = info.info.x11.display;
68 win = info.info.x11.fswindow;
69 if (win) XStoreName(dpy, win, title);
70 win = info.info.x11.wmwindow;
71 if (win) XStoreName(dpy, win, title);
76 static void freeVideoSurface()
78 screen = 0; // There's no need to free the screen surface.
81 free(GFX.SubScreen); GFX.SubScreen = 0;
82 free(GFX.ZBuffer); GFX.ZBuffer = 0;
83 free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
85 delete scaler; scaler = 0;
88 static void setupVideoSurface()
91 const unsigned gameWidth = IMAGE_WIDTH;
92 const unsigned gameHeight = IMAGE_HEIGHT;
95 if ((Config.fullscreen && !gotScreenSize) ||
96 (!Config.fullscreen && !gotWindowSize)) {
97 // Do a first try, in order to get window/screen size
98 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
99 SDL_SWSURFACE | SDL_RESIZABLE |
100 (Config.fullscreen ? SDL_FULLSCREEN : 0));
101 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
102 calculateScreenSize();
104 if (Config.fullscreen) {
105 GUI.Width = screenSize.w;
106 GUI.Height = screenSize.h;
108 GUI.Width = windowSize.w;
109 GUI.Height = windowSize.h;
112 GUI.Width = gameWidth;
113 GUI.Height = gameHeight;
120 if (gameHeight > GUI.Height || gameWidth > GUI.Width)
121 DIE("Video is larger than window size!");
123 const ScalerFactory* sFactory =
124 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
126 screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
127 Settings.SixteenBit ? 16 : 8,
129 (Config.fullscreen ? SDL_FULLSCREEN : 0));
131 DIE("SDL_SetVideoMode: %s", SDL_GetError());
133 SDL_ShowCursor(SDL_DISABLE);
135 scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
137 // We get pitch surface values from SDL
138 GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
139 GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
140 GFX.PixSize = screen->format->BitsPerPixel / 8;
142 GFX.Screen = scaler->getDrawBuffer();
143 GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
144 GFX.ZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
145 GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
147 GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
148 GFX.PPL = GFX.Pitch >> 1;
149 GFX.PPLx2 = GFX.Pitch;
151 scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
152 scaler->getRatio(GUI.ScaleX, GUI.ScaleY);
154 printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
155 gameWidth, gameHeight,
156 screen->w, screen->h, screen->format->BitsPerPixel,
157 Config.fullscreen ? "fullscreen" : "windowed",
161 static void drawOnscreenControls()
163 if (Config.touchscreenInput) {
164 S9xInputScreenChanged();
167 if (Config.touchscreenShow) {
169 SDL_FillRect(screen, NULL, 0);
170 S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
171 screen->pixels, screen->pitch);
177 void S9xInitDisplay(int argc, const char ** argv)
179 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
180 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
183 drawOnscreenControls();
186 void S9xDeinitDisplay()
189 SDL_QuitSubSystem(SDL_INIT_VIDEO);
192 void S9xVideoToggleFullscreen()
195 Config.fullscreen = !Config.fullscreen;
197 drawOnscreenControls();
200 void processVideoEvent(const SDL_Event& event)
203 scaler->filter(event);
206 // This is here for completeness, but palette mode is useless on N8x0
207 void S9xSetPalette ()
209 if (Settings.SixteenBit) return;
211 SDL_Color colors[256];
212 int brightness = IPPU.MaxBrightness *138;
213 for (int i = 0; i < 256; i++)
215 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
216 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
217 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
220 SDL_SetColors(screen, colors, 0, 256);
223 bool8_32 S9xInitUpdate ()
230 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
235 if (ExitBtnRequiresDraw()) {