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