using SDL for timekeeping
[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 void setDoubling(bool enable)
24 {
25         SDL_SysWMinfo wminfo;
26         SDL_VERSION(&wminfo.version);
27         if ( SDL_GetWMInfo(&wminfo) ) {
28                 XSPSetPixelDoubling(wminfo.info.x11.display, 0, enable ? 1 : 0);
29         }
30 }
31
32 void S9xSetTitle(const char *title)
33 {
34         SDL_SysWMinfo info;
35         SDL_VERSION(&info.version);
36         if ( SDL_GetWMInfo(&info) ) {
37                 Display *dpy = info.info.x11.display;
38                 Window win;
39                 if (dpy) {
40                         win = info.info.x11.fswindow;
41                         if (win) XStoreName(dpy, win, title);
42                         win = info.info.x11.wmwindow;
43                         if (win) XStoreName(dpy, win, title);
44                 }
45         }
46 }
47
48 static void freeVideoSurface()
49 {
50         screen = 0; // There's no need to free the screen surface.
51         GFX.Screen = 0;
52         
53         free(GFX.SubScreen); GFX.SubScreen = 0;
54         free(GFX.ZBuffer); GFX.ZBuffer = 0;
55         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
56 }
57
58 static void setupVideoSurface()
59 {
60         int w = IMAGE_WIDTH;
61         int h = IMAGE_HEIGHT;
62         
63         // By now, just assume xsp == fullscreen. This has to change.
64         Config.xsp = Config.fullscreen;
65         if (Config.xsp) {
66                 w *= 2;
67                 h *= 2;
68         } else {
69                 setDoubling(false); // Before switching video modes
70         }
71
72         screen = SDL_SetVideoMode(w, h,
73                                                                 Settings.SixteenBit ? 16 : 8,
74                                                                 SDL_SWSURFACE |
75                                                                 (Config.fullscreen ? SDL_FULLSCREEN : 0));
76
77         if (!screen)
78                 DIE("SDL_SetVideoMode: %s", SDL_GetError());
79         
80         SDL_ShowCursor(SDL_DISABLE);
81
82         if (Config.xsp) setDoubling(true);
83         
84         GFX.RealPitch = GFX.Pitch = screen->pitch;
85         
86         GFX.Screen = (uint8*) screen->pixels;
87         GFX.SubScreen = (uint8 *) malloc(GFX.RealPitch * IMAGE_HEIGHT);
88         GFX.ZBuffer =  (uint8 *) malloc(GFX.RealPitch * IMAGE_HEIGHT);
89         GFX.SubZBuffer = (uint8 *) malloc(GFX.RealPitch * IMAGE_HEIGHT);
90         
91         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
92         GFX.PPL = GFX.Pitch >> 1;
93         GFX.PPLx2 = GFX.Pitch;
94         GFX.ZPitch = GFX.Pitch >> 1;
95         
96         printf("Video: %dx%d, %hu bits per pixel, %s %s\n", screen->w, screen->h,
97                 screen->format->BitsPerPixel,
98                 Config.fullscreen ? "fullscreen" : "windowed",
99                 Config.xsp ? "with pixel doubling" : "");
100 }
101
102 void S9xInitDisplay(int argc, const char ** argv)
103 {       
104         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
105                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
106         
107         setupVideoSurface();
108 }
109
110 void S9xDeinitDisplay()
111 {
112         freeVideoSurface();     
113         SDL_QuitSubSystem(SDL_INIT_VIDEO);
114 }
115
116 void S9xVideoToggleFullscreen()
117 {
118         Config.fullscreen = !Config.fullscreen;
119         freeVideoSurface();
120         setupVideoSurface();
121 }
122
123 void S9xVideoOutputFocus(bool hasFocus)
124 {
125         if (Config.xsp) {
126                 setDoubling(hasFocus);
127         } 
128 }
129
130 // This is here for completeness, but palette mode is useless on N8x0
131 void S9xSetPalette ()
132 {
133         if (Settings.SixteenBit) return;
134         
135         SDL_Color colors[256];
136         int brightness = IPPU.MaxBrightness *138;
137         for (int i = 0; i < 256; i++)
138         {
139                 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
140                 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
141                 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
142         }
143         
144         SDL_SetColors(screen, colors, 0, 256);
145 }
146
147 bool8_32 S9xInitUpdate ()
148 {
149         if(SDL_MUSTLOCK(screen)) 
150         {
151                 if(SDL_LockSurface(screen) < 0) {
152                         DIE("Failed to lock SDL surface: %s", SDL_GetError());
153                 }
154         }
155
156         return TRUE;
157 }
158
159 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
160 {
161         if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
162
163         if (Config.xsp) {       
164                 width *= 2;
165                 height *= 2;
166         }
167
168         SDL_UpdateRect(screen, 0, 0, width, height);
169         
170         return TRUE;
171 }
172