"stashing" stuff in a stupid way for 1.2.5
[drnoksnes] / platform / sdlv.cpp
1 #include <stdio.h>
2
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5 #include <SDL.h>
6 #include <SDL_syswm.h>
7
8 #include "snes9x.h"
9 #include "platform.h"
10 #include "display.h"
11 #include "gfx.h"
12 #include "ppu.h"
13 #include "sdlv.h"
14 #include "scaler.h"
15
16 #define DIE(format, ...) do { \
17                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
18                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
19                 abort(); \
20         } while (0);
21
22 struct gui GUI;
23
24 SDL_Surface* screen;
25
26 static SDL_Rect windowSize, screenSize;
27 static bool gotWindowSize, gotScreenSize;
28
29 /** The current scaler object */
30 static Scaler* scaler;
31
32 static void calculateScreenSize()
33 {
34         SDL_SysWMinfo wminfo;
35         SDL_VERSION(&wminfo.version);
36
37         if ( SDL_GetWMInfo(&wminfo) ) {
38                 Display *dpy = wminfo.info.x11.display;
39                 Window w;
40                 SDL_Rect* size;
41                 XWindowAttributes xwa;
42
43                 if (Config.fullscreen) {
44                         w =  wminfo.info.x11.fswindow;
45                         size = &screenSize;
46                         gotScreenSize = true;
47                 } else {
48                         w =  wminfo.info.x11.wmwindow;
49                         size = &windowSize;
50                         gotWindowSize = true;
51                 }
52
53                 XGetWindowAttributes(dpy, w, &xwa);
54                 size->x = xwa.x;
55                 size->y = xwa.y;
56                 size->w = xwa.width;
57                 size->h = xwa.height;
58         }
59 }
60
61 void S9xSetTitle(const char *title)
62 {
63         SDL_SysWMinfo info;
64         SDL_VERSION(&info.version);
65         if ( SDL_GetWMInfo(&info) ) {
66                 Display *dpy = info.info.x11.display;
67                 Window win;
68                 if (dpy) {
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);
73                 }
74         }
75 }
76
77 static void freeVideoSurface()
78 {
79         screen = 0; // There's no need to free the screen surface.
80         GFX.Screen = 0;
81
82         free(GFX.SubScreen); GFX.SubScreen = 0;
83         free(GFX.ZBuffer); GFX.ZBuffer = 0;
84         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
85
86         delete scaler; scaler = 0;
87 }
88
89 static void setupVideoSurface()
90 {
91         // Real surface area.
92         const unsigned gameWidth = IMAGE_WIDTH;
93         const unsigned gameHeight = IMAGE_HEIGHT;
94
95 #ifdef MAEMO
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();
104         }
105         if (Config.fullscreen) {
106                 GUI.Width = screenSize.w;
107                 GUI.Height = screenSize.h;
108         } else {
109                 GUI.Width = windowSize.w;
110                 GUI.Height = windowSize.h;
111         }
112 #else
113         GUI.Width = gameWidth;
114         GUI.Height = gameHeight;
115 #endif
116 #if CONF_EXIT_BUTTON
117         exitReset();
118 #endif
119
120         // Safeguard
121         if (gameHeight > GUI.Height || gameWidth > GUI.Width)
122                 DIE("Video is larger than window size!");
123
124         const ScalerFactory* sFactory =
125                 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
126
127         screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
128                                                                 Settings.SixteenBit ? 16 : 8,
129                                                                 SDL_SWSURFACE |
130                                                                 (Config.fullscreen ? SDL_FULLSCREEN : 0));
131         if (!screen)
132                 DIE("SDL_SetVideoMode: %s", SDL_GetError());
133         
134         SDL_ShowCursor(SDL_DISABLE);
135
136 #if CONF_HD
137         hdSetupFullscreen(Config.fullscreen);
138 #endif
139
140         scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
141
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;
146         
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);
151
152         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
153         GFX.PPL = GFX.Pitch >> 1;
154         GFX.PPLx2 = GFX.Pitch;
155
156         scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
157         scaler->getRatio(GUI.ScaleX, GUI.ScaleY);
158
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",
163                 scaler->getName());
164 }
165
166 static void drawOnscreenControls()
167 {
168         if (Config.touchscreenInput) {
169                 S9xInputScreenChanged();
170         }
171
172         if (Config.touchscreenShow) {
173                 scaler->pause();
174                 SDL_FillRect(screen, NULL, 0);
175                 S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
176                                                         screen->pixels, screen->pitch);
177                 SDL_Flip(screen);
178                 scaler->resume();
179         }
180 }
181
182 void S9xInitDisplay(int argc, const char ** argv)
183 {       
184         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
185                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
186
187 #if CONF_HD
188         hdSetup();
189 #endif
190
191         setupVideoSurface();
192         drawOnscreenControls();
193 }
194
195 void S9xDeinitDisplay()
196 {
197         freeVideoSurface();     
198         SDL_QuitSubSystem(SDL_INIT_VIDEO);
199 }
200
201 void S9xVideoToggleFullscreen()
202 {
203         freeVideoSurface();
204         Config.fullscreen = !Config.fullscreen;
205         setupVideoSurface();
206         drawOnscreenControls();
207 }
208
209 void S9xVideoOutputFocus(bool hasFocus)
210 {
211 #if MAEMO
212         if (scaler) {
213                 if (hasFocus) {
214                         scaler->resume();
215                 } else {
216                         scaler->pause();
217                 }
218         }
219 #endif
220 }
221
222 // This is here for completeness, but palette mode is useless on N8x0
223 void S9xSetPalette ()
224 {
225         if (Settings.SixteenBit) return;
226         
227         SDL_Color colors[256];
228         int brightness = IPPU.MaxBrightness *138;
229         for (int i = 0; i < 256; i++)
230         {
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;
234         }
235         
236         SDL_SetColors(screen, colors, 0, 256);
237 }
238
239 bool8_32 S9xInitUpdate ()
240 {
241         scaler->prepare();
242
243         return TRUE;
244 }
245
246 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
247 {
248         scaler->finish();
249
250 #if CONF_EXIT_BUTTON
251         if (exitRequiresDraw()) {
252                 scaler->pause();
253                 exitDraw(screen);
254                 SDL_Flip(screen);
255                 scaler->resume();
256         }
257 #endif
258
259         return TRUE;
260 }
261