window-size sdl surfaces instead of snes
[drnoksnes] / platform / sdlv.cpp
1 #include <stdio.h>
2
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5 #include <X11/extensions/Xsp.h>
6 #include <SDL.h>
7 #include <SDL_syswm.h>
8
9 #include "snes9x.h"
10 #include "platform.h"
11 #include "display.h"
12 #include "gfx.h"
13 #include "ppu.h"
14
15 #define DIE(format, ...) do { \
16                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
17                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
18                 abort(); \
19         } while (0);
20
21 static SDL_Surface *screen;
22
23 static SDL_Rect windowSize, screenSize;
24 static bool gotWindowSize, gotScreenSize;
25
26 /** Inside the surface, where are we drawing */
27 static SDL_Rect renderArea;
28
29 #ifdef MAEMO
30 static void setDoubling(bool enable)
31 {
32         SDL_SysWMinfo wminfo;
33         SDL_VERSION(&wminfo.version);
34         if ( SDL_GetWMInfo(&wminfo) ) {
35                 Display *dpy = wminfo.info.x11.display;
36                 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
37                 XFlush(dpy);
38         }
39 }
40 #endif
41
42 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
43 {
44         result.x = areaW / 2 - w / 2;
45         result.w = w;
46         result.y = areaH / 2 - h / 2;
47         result.h = h;
48 }
49
50 static void calculateScreenSize()
51 {
52         SDL_SysWMinfo wminfo;
53         SDL_VERSION(&wminfo.version);
54
55         if ( SDL_GetWMInfo(&wminfo) ) {
56                 Display *dpy = wminfo.info.x11.display;
57                 Window w;
58                 SDL_Rect* size;
59                 XWindowAttributes xwa;
60
61                 if (Config.fullscreen) {
62                         w =  wminfo.info.x11.fswindow;
63                         size = &screenSize;
64                         gotScreenSize = true;
65                 } else {
66                         w =  wminfo.info.x11.wmwindow;
67                         size = &windowSize;
68                         gotWindowSize = true;
69                 }
70
71                 XGetWindowAttributes(dpy, w, &xwa);
72                 size->x = xwa.x;
73                 size->y = xwa.y;
74                 size->w = xwa.width;
75                 size->h = xwa.height;
76         }
77 }
78
79 void S9xSetTitle(const char *title)
80 {
81         SDL_SysWMinfo info;
82         SDL_VERSION(&info.version);
83         if ( SDL_GetWMInfo(&info) ) {
84                 Display *dpy = info.info.x11.display;
85                 Window win;
86                 if (dpy) {
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);
91                 }
92         }
93 }
94
95 static void freeVideoSurface()
96 {
97         screen = 0; // There's no need to free the screen surface.
98         GFX.Screen = 0;
99         
100         free(GFX.SubScreen); GFX.SubScreen = 0;
101         free(GFX.ZBuffer); GFX.ZBuffer = 0;
102         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
103 }
104
105 static void setupVideoSurface()
106 {
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;
112
113 #ifdef MAEMO
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();
122         }
123         if (Config.fullscreen) {
124                 srfWidth = screenSize.w;
125                 srfHeight = screenSize.h;
126         } else {
127                 srfWidth = windowSize.w;
128                 srfHeight = windowSize.h;
129         }
130         
131         // By now, just assume xsp == fullscreen. This has to change.
132         Config.xsp = Config.fullscreen;
133         if (!Config.xsp) {
134                 setDoubling(false); // Before switching video modes; avoids flicker.
135         }
136 #else
137         srfWidth = realWidth;
138         srfHeight = realHeight;
139 #endif
140
141         screen = SDL_SetVideoMode(srfWidth, srfHeight,
142                                                                 Settings.SixteenBit ? 16 : 8,
143                                                                 SDL_SWSURFACE |
144                                                                 (Config.fullscreen ? SDL_FULLSCREEN : 0));
145         if (!screen)
146                 DIE("SDL_SetVideoMode: %s", SDL_GetError());
147         
148         SDL_ShowCursor(SDL_DISABLE);
149
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;
154
155         // Ok, calculate renderArea
156 #ifdef MAEMO
157         if (Config.xsp) {
158                 setDoubling(true);
159                 centerRectangle(renderArea, srfWidth, srfHeight,
160                         realWidth * 2, realHeight * 2);
161                 renderArea.w /= 2;
162                 renderArea.h /= 2;
163         } else {
164                 centerRectangle(renderArea, srfWidth, srfHeight, realWidth, realHeight);
165         }
166 #else
167         centerRectangle(renderArea, srfWidth, srfHeight, realWidth, realHeight);
168 #endif
169         
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);
176         
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
181
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" : "");
187 }
188
189 void S9xInitDisplay(int argc, const char ** argv)
190 {       
191         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
192                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
193
194         setupVideoSurface();
195 }
196
197 void S9xDeinitDisplay()
198 {
199         freeVideoSurface();     
200         SDL_QuitSubSystem(SDL_INIT_VIDEO);
201 }
202
203 void S9xVideoToggleFullscreen()
204 {
205         Config.fullscreen = !Config.fullscreen;
206         freeVideoSurface();
207         setupVideoSurface();
208 }
209
210 void S9xVideoOutputFocus(bool hasFocus)
211 {
212         if (Config.xsp) {
213                 setDoubling(hasFocus);
214         } 
215 }
216
217 // This is here for completeness, but palette mode is useless on N8x0
218 void S9xSetPalette ()
219 {
220         if (Settings.SixteenBit) return;
221         
222         SDL_Color colors[256];
223         int brightness = IPPU.MaxBrightness *138;
224         for (int i = 0; i < 256; i++)
225         {
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;
229         }
230         
231         SDL_SetColors(screen, colors, 0, 256);
232 }
233
234 bool8_32 S9xInitUpdate ()
235 {
236         if(SDL_MUSTLOCK(screen)) 
237         {
238                 if(SDL_LockSurface(screen) < 0) {
239                         DIE("Failed to lock SDL surface: %s", SDL_GetError());
240                 }
241         }
242
243         return TRUE;
244 }
245
246 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
247 {
248         if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
249
250         if (Config.xsp) {       
251                 width *= 2;
252                 height *= 2;
253         }
254
255         SDL_UpdateRects(screen, 1, &renderArea);
256         
257         return TRUE;
258 }
259