180b795028d9feca9f1d4adde078c7209040d28c
[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
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 struct gui GUI;
22
23 SDL_Surface* screen;
24
25 static SDL_Rect windowSize, screenSize;
26 static bool gotWindowSize, gotScreenSize;
27
28 /** The current scaler object */
29 Scaler* scaler;
30
31 static void calculateScreenSize()
32 {
33         SDL_SysWMinfo wminfo;
34         SDL_VERSION(&wminfo.version);
35
36         if ( SDL_GetWMInfo(&wminfo) ) {
37                 Display *dpy = wminfo.info.x11.display;
38                 Window w;
39                 SDL_Rect* size;
40                 XWindowAttributes xwa;
41
42                 if (Config.fullscreen) {
43                         w =  wminfo.info.x11.fswindow;
44                         size = &screenSize;
45                         gotScreenSize = true;
46                 } else {
47                         w =  wminfo.info.x11.wmwindow;
48                         size = &windowSize;
49                         gotWindowSize = true;
50                 }
51
52                 XGetWindowAttributes(dpy, w, &xwa);
53                 size->x = xwa.x;
54                 size->y = xwa.y;
55                 size->w = xwa.width;
56                 size->h = xwa.height;
57         }
58 }
59
60 void S9xSetTitle(const char *title)
61 {
62         SDL_SysWMinfo info;
63         SDL_VERSION(&info.version);
64         if ( SDL_GetWMInfo(&info) ) {
65                 Display *dpy = info.info.x11.display;
66                 Window win;
67                 if (dpy) {
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);
72                 }
73         }
74 }
75
76 static void freeVideoSurface()
77 {
78         screen = 0; // There's no need to free the screen surface.
79         GFX.Screen = 0;
80
81         free(GFX.SubScreen); GFX.SubScreen = 0;
82         free(GFX.ZBuffer); GFX.ZBuffer = 0;
83         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
84
85         delete scaler; scaler = 0;
86 }
87
88 static void setupVideoSurface()
89 {
90         // Real surface area.
91         const unsigned gameWidth = IMAGE_WIDTH;
92         const unsigned gameHeight = IMAGE_HEIGHT;
93
94 #ifdef MAEMO
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();
103         }
104         if (Config.fullscreen) {
105                 GUI.Width = screenSize.w;
106                 GUI.Height = screenSize.h;
107         } else {
108                 GUI.Width = windowSize.w;
109                 GUI.Height = windowSize.h;
110         }
111 #else
112         GUI.Width = gameWidth;
113         GUI.Height = gameHeight;
114 #endif
115 #if CONF_EXIT_BUTTON
116         ExitBtnReset();
117 #endif
118
119         // Safeguard
120         if (gameHeight > GUI.Height || gameWidth > GUI.Width)
121                 DIE("Video is larger than window size!");
122
123         const ScalerFactory* sFactory =
124                 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
125
126         screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
127                                                                 Settings.SixteenBit ? 16 : 8,
128                                                                 SDL_SWSURFACE |
129                                                                 (Config.fullscreen ? SDL_FULLSCREEN : 0));
130         if (!screen)
131                 DIE("SDL_SetVideoMode: %s", SDL_GetError());
132         
133         SDL_ShowCursor(SDL_DISABLE);
134
135         scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
136
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;
141         
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);
146
147         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
148         GFX.PPL = GFX.Pitch >> 1;
149         GFX.PPLx2 = GFX.Pitch;
150
151         scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
152         scaler->getRatio(GUI.ScaleX, GUI.ScaleY);
153
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",
158                 scaler->getName());
159 }
160
161 static void drawOnscreenControls()
162 {
163         if (Config.touchscreenInput) {
164                 S9xInputScreenChanged();
165         }
166
167         if (Config.touchscreenShow) {
168                 scaler->pause();
169                 SDL_FillRect(screen, NULL, 0);
170                 S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
171                                                         screen->pixels, screen->pitch);
172                 SDL_Flip(screen);
173                 scaler->resume();
174         }
175 }
176
177 void S9xInitDisplay(int argc, const char ** argv)
178 {       
179         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
180                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
181
182         setupVideoSurface();
183         drawOnscreenControls();
184 }
185
186 void S9xDeinitDisplay()
187 {
188         freeVideoSurface();     
189         SDL_QuitSubSystem(SDL_INIT_VIDEO);
190 }
191
192 void S9xVideoToggleFullscreen()
193 {
194         freeVideoSurface();
195         Config.fullscreen = !Config.fullscreen;
196         setupVideoSurface();
197         drawOnscreenControls();
198 }
199
200 void processVideoEvent(const SDL_Event& event)
201 {
202         if (scaler)
203                 scaler->filter(event);
204 }
205
206 // This is here for completeness, but palette mode is useless on N8x0
207 void S9xSetPalette ()
208 {
209         if (Settings.SixteenBit) return;
210         
211         SDL_Color colors[256];
212         int brightness = IPPU.MaxBrightness *138;
213         for (int i = 0; i < 256; i++)
214         {
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;
218         }
219         
220         SDL_SetColors(screen, colors, 0, 256);
221 }
222
223 bool8_32 S9xInitUpdate ()
224 {
225         scaler->prepare();
226
227         return TRUE;
228 }
229
230 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
231 {
232         scaler->finish();
233
234 #if CONF_EXIT_BUTTON
235         if (ExitBtnRequiresDraw()) {
236                 scaler->pause();
237                 ExitBtnDraw(screen);
238                 scaler->resume();
239         }
240 #endif
241
242         return TRUE;
243 }
244