3f20ffe9a5f4814519a36bfeb9ab5d23a94414ce
[fceu.git] / drivers / sdl / sdl.c
1 #include <SDL.h>
2 #include "../common/platform.h"
3 #include "../common/args.h"
4 #include "../common/settings.h"
5 #include "../common/input.h"
6 #include "../common/main.h"
7 #include "../libpicofe/menu.h"
8 #include "../libpicofe/input.h"
9 #include "../libpicofe/in_sdl.h"
10 #include "../libpicofe/plat.h"
11 #include "../../video.h"
12
13 static const struct in_default_bind in_sdl_defbinds[] = {
14   { SDLK_UP,     IN_BINDTYPE_PLAYER12, NKEYB_UP },
15   { SDLK_DOWN,   IN_BINDTYPE_PLAYER12, NKEYB_DOWN },
16   { SDLK_LEFT,   IN_BINDTYPE_PLAYER12, NKEYB_LEFT },
17   { SDLK_RIGHT,  IN_BINDTYPE_PLAYER12, NKEYB_RIGHT },
18   { SDLK_z,      IN_BINDTYPE_PLAYER12, NKEYB_B },
19   { SDLK_x,      IN_BINDTYPE_PLAYER12, NKEYB_A },
20   { SDLK_a,      IN_BINDTYPE_PLAYER12, NKEYB_B_TURBO },
21   { SDLK_s,      IN_BINDTYPE_PLAYER12, NKEYB_A_TURBO },
22   { SDLK_c,      IN_BINDTYPE_PLAYER12, NKEYB_SELECT },
23   { SDLK_v,      IN_BINDTYPE_PLAYER12, NKEYB_START },
24   { SDLK_ESCAPE, IN_BINDTYPE_EMU, EACTB_ENTER_MENU },
25   { 0, 0, 0 }
26 };
27
28 static SDL_Surface *screen;
29 static char fps_str[32];
30 static SDL_Color psdl[256];
31
32 struct plat_target plat_target;
33
34 static int changemode(int bpp)
35 {
36         const SDL_VideoInfo *vinf;
37         int flags = 0;
38
39         vinf = SDL_GetVideoInfo();
40
41         if(vinf->hw_available)
42                 flags |= SDL_HWSURFACE;
43
44         if (bpp == 8)
45                 flags |= SDL_HWPALETTE;
46
47         screen = SDL_SetVideoMode(320, 240, bpp, flags);
48         if (!screen)
49         {
50                 puts(SDL_GetError());
51                 return -1;
52         }
53
54         SDL_WM_SetCaption("FCE Ultra", "FCE Ultra");
55         return 0;
56 }
57
58 void platform_init(void)
59 {
60         memset(&Settings, 0, sizeof(Settings));
61         Settings.frameskip = -1; // auto
62         Settings.sound_rate = 44100;
63         Settings.turbo_rate_add = (8*2 << 24) / 60 + 1; // 8Hz turbofire
64         Settings.gamma = 100;
65         Settings.sstate_confirm = 1;
66
67         if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_VIDEO))
68         {
69                 printf("Could not initialize SDL: %s.\n", SDL_GetError());
70                 exit(1);
71         }
72
73         g_menuscreen_w = 320;
74         g_menuscreen_h = 240;
75         if (changemode(8) != 0)
76                 exit(1);
77 }
78
79 void platform_late_init(void)
80 {
81         in_sdl_init(in_sdl_defbinds);
82         in_probe();
83 }
84
85 // video
86 static void flip(void)
87 {
88         SDL_Flip(screen);
89         XBuf = screen->pixels;
90 }
91
92 void CleanSurface(void)
93 {
94         memset(screen->pixels, 0, 320 * 240 * screen->format->BytesPerPixel);
95         flip();
96 }
97
98 void KillVideo(void)
99 {
100 }
101
102 int InitVideo(void)
103 {
104         fps_str[0] = 0;
105
106         CleanSurface();
107
108         srendline = 0;
109         erendline = 239;
110
111         return 1;
112 }
113
114 void FCEUD_SetPalette(uint8 index, uint8 r, uint8 g, uint8 b)
115 {
116         psdl[index].r = r;
117         psdl[index].g = g;
118         psdl[index].b = b;
119
120         SDL_SetPalette(screen, SDL_PHYSPAL, &psdl[index], index, 1);
121 }
122
123 void FCEUD_GetPalette(uint8 index, uint8 * r, uint8 * g, uint8 * b)
124 {
125         // dummy, will break snapshots
126         *r = *g = *b = 0;
127 }
128
129 void BlitPrepare(int skip)
130 {
131         if (skip) {
132                 return;
133         }
134
135         if (eoptions & EO_CLIPSIDES)
136         {
137                 int i, *p = (int *) ((char *)screen->pixels + 32);
138                 for (i = 240; i; i--, p += 320/4)
139                 {
140                         p[0] = p[1] = p[62] = p[63] = 0;
141                 }
142         }
143
144         if (Settings.accurate_mode && Settings.scaling < 2)
145         {
146                 int i, *p = (int *)screen->pixels + 32/4;
147                 if (srendline > 0)
148                         for (i = srendline; i > 0; i--, p += 320/4)
149                                 memset(p, 0, 256);
150                 if (erendline < 239)
151                 {
152                         int *p = (int *)screen->pixels + erendline*320/4 + 32/4;
153                         for (i = 239-srendline; i > 0; i--, p += 320/4)
154                                 memset(p, 0, 256);
155                 }
156         }
157 }
158
159 void BlitScreen(int skip)
160 {
161         if (!skip)
162                 flip();
163 }
164
165 void platform_apply_config(void)
166 {
167 }
168
169 void platform_set_volume(int val)
170 {
171 }
172
173 void plat_video_menu_enter(int is_rom_loaded)
174 {
175         changemode(16);
176 }
177
178 void plat_video_menu_begin(void)
179 {
180         g_menuscreen_ptr = screen->pixels;
181 }
182
183 void plat_video_menu_end(void)
184 {
185         SDL_Flip(screen);
186 }
187
188 void plat_video_menu_leave(void)
189 {
190         changemode(8);
191         SDL_SetPalette(screen, SDL_PHYSPAL, psdl, 0, 256);
192 }
193
194 char *DriverUsage="";
195
196 ARGPSTRUCT DriverArgs[]={
197          {0,0,0,0}
198 };
199
200 void DoDriverArgs(void)
201 {
202 }
203
204 void GetBaseDirectory(char *BaseDirectory)
205 {
206         strcpy(BaseDirectory, "fceultra");
207 }
208
209 void platform_finish(void)
210 {
211         SDL_Quit();
212 }