reorg gfx & tile code
[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 = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
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.ZPitch * IMAGE_HEIGHT);
175         GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
176
177         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
178         GFX.PPL = GFX.Pitch >> 1;
179         GFX.PPLx2 = GFX.Pitch;
180
181         printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s %s\n",
182                 realWidth, realHeight,
183                 screen->w, screen->h, screen->format->BitsPerPixel,
184                 Config.fullscreen ? "fullscreen" : "windowed",
185                 Config.xsp ? "with pixel doubling" : "");
186 }
187
188 void S9xInitDisplay(int argc, const char ** argv)
189 {       
190         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
191                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
192
193         setupVideoSurface();
194 }
195
196 void S9xDeinitDisplay()
197 {
198         freeVideoSurface();     
199         SDL_QuitSubSystem(SDL_INIT_VIDEO);
200 }
201
202 void S9xVideoToggleFullscreen()
203 {
204         Config.fullscreen = !Config.fullscreen;
205         freeVideoSurface();
206         setupVideoSurface();
207 }
208
209 void S9xVideoOutputFocus(bool hasFocus)
210 {
211         if (Config.xsp) {
212                 setDoubling(hasFocus);
213         } 
214 }
215
216 // This is here for completeness, but palette mode is useless on N8x0
217 void S9xSetPalette ()
218 {
219         if (Settings.SixteenBit) return;
220         
221         SDL_Color colors[256];
222         int brightness = IPPU.MaxBrightness *138;
223         for (int i = 0; i < 256; i++)
224         {
225                 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
226                 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
227                 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
228         }
229         
230         SDL_SetColors(screen, colors, 0, 256);
231 }
232
233 bool8_32 S9xInitUpdate ()
234 {
235         if(SDL_MUSTLOCK(screen)) 
236         {
237                 if(SDL_LockSurface(screen) < 0) {
238                         DIE("Failed to lock SDL surface: %s", SDL_GetError());
239                 }
240         }
241
242         return TRUE;
243 }
244
245 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
246 {
247         if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
248
249         if (Config.xsp) {       
250                 width *= 2;
251                 height *= 2;
252         }
253
254         SDL_UpdateRects(screen, 1, &renderArea);
255         
256         return TRUE;
257 }
258