636d5f25 |
1 | /* |
2 | * PicoDrive |
3 | * (C) notaz, 2013 |
4 | * |
5 | * This work is licensed under the terms of MAME license. |
6 | * See COPYING file in the top-level directory. |
7 | */ |
8 | |
9 | #include <stdio.h> |
10 | |
11 | #include "../libpicofe/input.h" |
12 | #include "../libpicofe/plat_sdl.h" |
13 | #include "../libpicofe/in_sdl.h" |
14 | #include "../libpicofe/gl.h" |
15 | #include "emu.h" |
16 | #include "menu_pico.h" |
17 | #include "input_pico.h" |
18 | #include "version.h" |
19 | |
20 | // FIXME: these 2 shouldn't be here |
21 | static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)]; |
22 | unsigned char *PicoDraw2FB = PicoDraw2FB_; |
23 | |
24 | static void *shadow_fb; |
25 | |
26 | static const struct in_default_bind in_sdl_defbinds[] = { |
27 | { SDLK_UP, IN_BINDTYPE_PLAYER12, GBTN_UP }, |
28 | { SDLK_DOWN, IN_BINDTYPE_PLAYER12, GBTN_DOWN }, |
29 | { SDLK_LEFT, IN_BINDTYPE_PLAYER12, GBTN_LEFT }, |
30 | { SDLK_RIGHT, IN_BINDTYPE_PLAYER12, GBTN_RIGHT }, |
31 | { SDLK_z, IN_BINDTYPE_PLAYER12, GBTN_A }, |
32 | { SDLK_x, IN_BINDTYPE_PLAYER12, GBTN_B }, |
33 | { SDLK_c, IN_BINDTYPE_PLAYER12, GBTN_C }, |
34 | { SDLK_a, IN_BINDTYPE_PLAYER12, GBTN_X }, |
35 | { SDLK_s, IN_BINDTYPE_PLAYER12, GBTN_Y }, |
36 | { SDLK_d, IN_BINDTYPE_PLAYER12, GBTN_Z }, |
37 | { SDLK_RETURN, IN_BINDTYPE_PLAYER12, GBTN_START }, |
38 | { SDLK_f, IN_BINDTYPE_PLAYER12, GBTN_MODE }, |
39 | { SDLK_ESCAPE, IN_BINDTYPE_EMU, PEVB_MENU }, |
40 | { SDLK_F1, IN_BINDTYPE_EMU, PEVB_STATE_SAVE }, |
41 | { SDLK_F2, IN_BINDTYPE_EMU, PEVB_STATE_LOAD }, |
42 | { SDLK_F3, IN_BINDTYPE_EMU, PEVB_SSLOT_PREV }, |
43 | { SDLK_F4, IN_BINDTYPE_EMU, PEVB_SSLOT_NEXT }, |
44 | { SDLK_F5, IN_BINDTYPE_EMU, PEVB_SWITCH_RND }, |
45 | { SDLK_F6, IN_BINDTYPE_EMU, PEVB_PICO_PPREV }, |
46 | { SDLK_F7, IN_BINDTYPE_EMU, PEVB_PICO_PNEXT }, |
47 | { SDLK_F8, IN_BINDTYPE_EMU, PEVB_PICO_SWINP }, |
48 | { SDLK_BACKSPACE, IN_BINDTYPE_EMU, PEVB_FF }, |
49 | { 0, 0, 0 } |
50 | }; |
51 | |
52 | /* YUV stuff */ |
53 | static int yuv_ry[32], yuv_gy[32], yuv_by[32]; |
54 | static unsigned char yuv_u[32 * 2], yuv_v[32 * 2]; |
55 | |
56 | void bgr_to_uyvy_init(void) |
57 | { |
58 | int i, v; |
59 | |
60 | /* init yuv converter: |
61 | y0 = (int)((0.299f * r0) + (0.587f * g0) + (0.114f * b0)); |
62 | y1 = (int)((0.299f * r1) + (0.587f * g1) + (0.114f * b1)); |
63 | u = (int)(8 * 0.565f * (b0 - y0)) + 128; |
64 | v = (int)(8 * 0.713f * (r0 - y0)) + 128; |
65 | */ |
66 | for (i = 0; i < 32; i++) { |
67 | yuv_ry[i] = (int)(0.299f * i * 65536.0f + 0.5f); |
68 | yuv_gy[i] = (int)(0.587f * i * 65536.0f + 0.5f); |
69 | yuv_by[i] = (int)(0.114f * i * 65536.0f + 0.5f); |
70 | } |
71 | for (i = -32; i < 32; i++) { |
72 | v = (int)(8 * 0.565f * i) + 128; |
73 | if (v < 0) |
74 | v = 0; |
75 | if (v > 255) |
76 | v = 255; |
77 | yuv_u[i + 32] = v; |
78 | v = (int)(8 * 0.713f * i) + 128; |
79 | if (v < 0) |
80 | v = 0; |
81 | if (v > 255) |
82 | v = 255; |
83 | yuv_v[i + 32] = v; |
84 | } |
85 | } |
86 | |
87 | void rgb565_to_uyvy(void *d, const void *s, int pixels) |
88 | { |
89 | unsigned int *dst = d; |
90 | const unsigned short *src = s; |
91 | const unsigned char *yu = yuv_u + 32; |
92 | const unsigned char *yv = yuv_v + 32; |
93 | int r0, g0, b0, r1, g1, b1; |
94 | int y0, y1, u, v; |
95 | |
96 | for (; pixels > 0; src += 2, dst++, pixels -= 2) |
97 | { |
98 | r0 = (src[0] >> 11) & 0x1f; |
99 | g0 = (src[0] >> 6) & 0x1f; |
100 | b0 = src[0] & 0x1f; |
101 | r1 = (src[1] >> 11) & 0x1f; |
102 | g1 = (src[1] >> 6) & 0x1f; |
103 | b1 = src[1] & 0x1f; |
104 | y0 = (yuv_ry[r0] + yuv_gy[g0] + yuv_by[b0]) >> 16; |
105 | y1 = (yuv_ry[r1] + yuv_gy[g1] + yuv_by[b1]) >> 16; |
106 | u = yu[b0 - y0]; |
107 | v = yv[r0 - y0]; |
108 | // valid Y range seems to be 16..235 |
109 | y0 = 16 + 219 * y0 / 31; |
110 | y1 = 16 + 219 * y1 / 31; |
111 | |
112 | *dst = (y1 << 24) | (v << 16) | (y0 << 8) | u; |
113 | } |
114 | } |
115 | |
116 | void plat_video_flip(void) |
117 | { |
118 | if (plat_sdl_overlay != NULL) { |
119 | SDL_Rect dstrect = |
120 | { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h }; |
121 | |
122 | SDL_LockYUVOverlay(plat_sdl_overlay); |
123 | rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb, |
124 | g_screen_width * g_screen_height); |
125 | SDL_UnlockYUVOverlay(plat_sdl_overlay); |
126 | SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect); |
127 | } |
128 | else if (plat_sdl_gl_active) { |
129 | gl_flip(shadow_fb, g_screen_width, g_screen_height); |
130 | } |
131 | else { |
132 | // XXX: no locking, but should be fine with SDL_SWSURFACE? |
133 | SDL_Flip(plat_sdl_screen); |
134 | g_screen_ptr = plat_sdl_screen->pixels; |
135 | } |
136 | } |
137 | |
138 | void plat_video_wait_vsync(void) |
139 | { |
140 | } |
141 | |
142 | void plat_video_menu_enter(int is_rom_loaded) |
143 | { |
144 | plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 0); |
d438a8dd |
145 | g_screen_ptr = shadow_fb; |
636d5f25 |
146 | } |
147 | |
148 | void plat_video_menu_begin(void) |
149 | { |
150 | if (plat_sdl_overlay != NULL || plat_sdl_gl_active) { |
151 | g_menuscreen_ptr = shadow_fb; |
152 | } |
153 | else { |
154 | SDL_LockSurface(plat_sdl_screen); |
155 | g_menuscreen_ptr = plat_sdl_screen->pixels; |
156 | } |
157 | } |
158 | |
159 | void plat_video_menu_end(void) |
160 | { |
161 | if (plat_sdl_overlay != NULL) { |
162 | SDL_Rect dstrect = |
163 | { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h }; |
164 | |
165 | SDL_LockYUVOverlay(plat_sdl_overlay); |
166 | rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb, |
167 | g_menuscreen_w * g_menuscreen_h); |
168 | SDL_UnlockYUVOverlay(plat_sdl_overlay); |
169 | |
170 | SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect); |
171 | } |
172 | else if (plat_sdl_gl_active) { |
173 | gl_flip(g_menuscreen_ptr, g_menuscreen_w, g_menuscreen_h); |
174 | } |
175 | else { |
176 | SDL_UnlockSurface(plat_sdl_screen); |
177 | SDL_Flip(plat_sdl_screen); |
178 | } |
179 | g_menuscreen_ptr = NULL; |
180 | |
181 | } |
182 | |
183 | void plat_video_menu_leave(void) |
184 | { |
185 | } |
186 | |
187 | void plat_video_loop_prepare(void) |
188 | { |
189 | plat_sdl_change_video_mode(g_screen_width, g_screen_height, 0); |
190 | |
191 | if (plat_sdl_overlay != NULL || plat_sdl_gl_active) { |
192 | g_screen_ptr = shadow_fb; |
193 | } |
194 | else { |
195 | SDL_LockSurface(plat_sdl_screen); |
196 | g_screen_ptr = plat_sdl_screen->pixels; |
197 | } |
198 | } |
199 | |
200 | void plat_early_init(void) |
201 | { |
202 | } |
203 | |
bec84f92 |
204 | static void plat_sdl_quit(void) |
205 | { |
206 | // for now.. |
207 | exit(1); |
208 | } |
209 | |
636d5f25 |
210 | void plat_init(void) |
211 | { |
212 | int shadow_size; |
213 | int ret; |
214 | |
215 | ret = plat_sdl_init(); |
216 | if (ret != 0) |
217 | exit(1); |
218 | |
bec84f92 |
219 | plat_sdl_quit_cb = plat_sdl_quit; |
220 | |
fc11dd05 |
221 | SDL_WM_SetCaption("PicoDrive " VERSION, NULL); |
636d5f25 |
222 | |
223 | g_menuscreen_w = plat_sdl_screen->w; |
224 | g_menuscreen_h = plat_sdl_screen->h; |
225 | g_menuscreen_ptr = NULL; |
226 | |
227 | shadow_size = g_menuscreen_w * g_menuscreen_h * 2; |
228 | if (shadow_size < 320 * 480 * 2) |
229 | shadow_size = 320 * 480 * 2; |
230 | |
231 | shadow_fb = malloc(shadow_size); |
232 | g_menubg_ptr = malloc(shadow_size); |
233 | if (shadow_fb == NULL || g_menubg_ptr == NULL) { |
234 | fprintf(stderr, "OOM\n"); |
235 | exit(1); |
236 | } |
237 | |
238 | g_screen_width = 320; |
239 | g_screen_height = 240; |
240 | g_screen_ptr = shadow_fb; |
241 | |
242 | in_sdl_init(in_sdl_defbinds, plat_sdl_event_handler); |
243 | in_probe(); |
244 | |
245 | bgr_to_uyvy_init(); |
246 | } |
247 | |
248 | void plat_finish(void) |
249 | { |
250 | free(shadow_fb); |
251 | shadow_fb = NULL; |
252 | free(g_menubg_ptr); |
253 | g_menubg_ptr = NULL; |
254 | plat_sdl_finish(); |
255 | } |