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