7badc935 |
1 | /* |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2011,2012 |
3 | * |
4 | * This work is licensed under the terms of any of these licenses |
5 | * (at your option): |
6 | * - GNU GPL, version 2 or later. |
7 | * - GNU LGPL, version 2.1 or later. |
8 | * See the COPYING file in the top-level directory. |
9 | */ |
10 | |
11 | #include <stdio.h> |
12 | #include <SDL.h> |
cc56203b |
13 | |
14 | #include "libpicofe/input.h" |
15 | #include "libpicofe/in_sdl.h" |
16 | #include "libpicofe/menu.h" |
c9099d02 |
17 | #include "libpicofe/fonts.h" |
5b9aa749 |
18 | #include "libpicofe/plat_sdl.h" |
19 | #include "libpicofe/gl.h" |
c9099d02 |
20 | #include "../plugins/gpulib/cspace.h" |
b07c18e8 |
21 | #include "plugin_lib.h" |
22 | #include "main.h" |
7badc935 |
23 | #include "plat.h" |
24 | #include "revision.h" |
25 | |
b07c18e8 |
26 | static const struct in_default_bind in_sdl_defbinds[] = { |
2e6189bc |
27 | { SDLK_UP, IN_BINDTYPE_PLAYER12, DKEY_UP }, |
28 | { SDLK_DOWN, IN_BINDTYPE_PLAYER12, DKEY_DOWN }, |
29 | { SDLK_LEFT, IN_BINDTYPE_PLAYER12, DKEY_LEFT }, |
30 | { SDLK_RIGHT, IN_BINDTYPE_PLAYER12, DKEY_RIGHT }, |
31 | { SDLK_d, IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE }, |
32 | { SDLK_z, IN_BINDTYPE_PLAYER12, DKEY_CROSS }, |
33 | { SDLK_x, IN_BINDTYPE_PLAYER12, DKEY_CIRCLE }, |
34 | { SDLK_s, IN_BINDTYPE_PLAYER12, DKEY_SQUARE }, |
35 | { SDLK_v, IN_BINDTYPE_PLAYER12, DKEY_START }, |
36 | { SDLK_c, IN_BINDTYPE_PLAYER12, DKEY_SELECT }, |
37 | { SDLK_w, IN_BINDTYPE_PLAYER12, DKEY_L1 }, |
38 | { SDLK_r, IN_BINDTYPE_PLAYER12, DKEY_R1 }, |
39 | { SDLK_e, IN_BINDTYPE_PLAYER12, DKEY_L2 }, |
40 | { SDLK_t, IN_BINDTYPE_PLAYER12, DKEY_R2 }, |
41 | { SDLK_ESCAPE, IN_BINDTYPE_EMU, SACTION_ENTER_MENU }, |
456f1b86 |
42 | { SDLK_F1, IN_BINDTYPE_EMU, SACTION_SAVE_STATE }, |
43 | { SDLK_F2, IN_BINDTYPE_EMU, SACTION_LOAD_STATE }, |
44 | { SDLK_F3, IN_BINDTYPE_EMU, SACTION_PREV_SSLOT }, |
45 | { SDLK_F4, IN_BINDTYPE_EMU, SACTION_NEXT_SSLOT }, |
46 | { SDLK_F5, IN_BINDTYPE_EMU, SACTION_TOGGLE_FSKIP }, |
47 | { SDLK_F6, IN_BINDTYPE_EMU, SACTION_SCREENSHOT }, |
dde7da71 |
48 | { SDLK_F7, IN_BINDTYPE_EMU, SACTION_TOGGLE_FPS }, |
456f1b86 |
49 | { SDLK_F8, IN_BINDTYPE_EMU, SACTION_SWITCH_DISPMODE }, |
a8376201 |
50 | { SDLK_F11, IN_BINDTYPE_EMU, SACTION_TOGGLE_FULLSCREEN }, |
dde7da71 |
51 | { SDLK_BACKSPACE, IN_BINDTYPE_EMU, SACTION_FAST_FORWARD }, |
2e6189bc |
52 | { 0, 0, 0 } |
7badc935 |
53 | }; |
54 | |
a8376201 |
55 | static int psx_w, psx_h; |
5b9aa749 |
56 | static void *shadow_fb, *menubg_img; |
57 | static int in_menu; |
7badc935 |
58 | |
5b9aa749 |
59 | static int change_video_mode(void) |
7badc935 |
60 | { |
5b9aa749 |
61 | int w, h; |
a8376201 |
62 | |
5b9aa749 |
63 | if (in_menu) { |
64 | w = g_menuscreen_w; |
65 | h = g_menuscreen_h; |
a8376201 |
66 | } |
5b9aa749 |
67 | else { |
68 | w = psx_w; |
69 | h = psx_h; |
a8376201 |
70 | } |
71 | |
2c616080 |
72 | return plat_sdl_change_video_mode(w, h, 0); |
a8376201 |
73 | } |
74 | |
7badc935 |
75 | void plat_init(void) |
76 | { |
5b9aa749 |
77 | int ret; |
7badc935 |
78 | |
5b9aa749 |
79 | ret = plat_sdl_init(); |
80 | if (ret != 0) |
7badc935 |
81 | exit(1); |
a8376201 |
82 | |
83 | in_menu = 1; |
7badc935 |
84 | SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL); |
85 | |
5b9aa749 |
86 | shadow_fb = malloc(640 * 512 * 2); |
2e6189bc |
87 | menubg_img = malloc(640 * 512 * 2); |
5b9aa749 |
88 | if (shadow_fb == NULL || menubg_img == NULL) { |
89 | fprintf(stderr, "OOM\n"); |
90 | exit(1); |
91 | } |
2e6189bc |
92 | |
5b9aa749 |
93 | in_sdl_init(in_sdl_defbinds, plat_sdl_event_handler); |
7badc935 |
94 | in_probe(); |
4ea7de6a |
95 | pl_rearmed_cbs.only_16bpp = 1; |
c9099d02 |
96 | |
97 | bgr_to_uyvy_init(); |
7badc935 |
98 | } |
99 | |
100 | void plat_finish(void) |
101 | { |
5b9aa749 |
102 | free(shadow_fb); |
103 | shadow_fb = NULL; |
2e6189bc |
104 | free(menubg_img); |
105 | menubg_img = NULL; |
5b9aa749 |
106 | plat_sdl_finish(); |
7badc935 |
107 | } |
108 | |
ab423939 |
109 | void plat_gvideo_open(int is_pal) |
7badc935 |
110 | { |
111 | } |
112 | |
c9099d02 |
113 | static void uyvy_to_rgb565(void *d, const void *s, int pixels) |
7badc935 |
114 | { |
c9099d02 |
115 | unsigned short *dst = d; |
116 | const unsigned int *src = s; |
117 | int v; |
118 | |
119 | // no colors, for now |
120 | for (; pixels > 0; src++, dst += 2, pixels -= 2) { |
121 | v = (*src >> 8) & 0xff; |
122 | v = (v - 16) * 255 / 219 / 8; |
123 | dst[0] = (v << 11) | (v << 6) | v; |
124 | |
125 | v = (*src >> 24) & 0xff; |
126 | v = (v - 16) * 255 / 219 / 8; |
127 | dst[1] = (v << 11) | (v << 6) | v; |
128 | } |
7badc935 |
129 | } |
130 | |
c9099d02 |
131 | static void overlay_blit(int doffs, const void *src_, int w, int h, |
132 | int sstride, int bgr24) |
133 | { |
134 | const unsigned short *src = src_; |
135 | unsigned short *dst; |
5b9aa749 |
136 | int dstride = plat_sdl_overlay->w; |
c9099d02 |
137 | |
5b9aa749 |
138 | SDL_LockYUVOverlay(plat_sdl_overlay); |
139 | dst = (void *)plat_sdl_overlay->pixels[0]; |
c9099d02 |
140 | |
141 | dst += doffs; |
142 | if (bgr24) { |
143 | for (; h > 0; dst += dstride, src += sstride, h--) |
144 | bgr888_to_uyvy(dst, src, w); |
145 | } |
146 | else { |
147 | for (; h > 0; dst += dstride, src += sstride, h--) |
148 | bgr555_to_uyvy(dst, src, w); |
149 | } |
150 | |
5b9aa749 |
151 | SDL_UnlockYUVOverlay(plat_sdl_overlay); |
c9099d02 |
152 | } |
153 | |
154 | static void overlay_hud_print(int x, int y, const char *str, int bpp) |
155 | { |
5b9aa749 |
156 | SDL_LockYUVOverlay(plat_sdl_overlay); |
157 | basic_text_out_uyvy_nf(plat_sdl_overlay->pixels[0], plat_sdl_overlay->w, x, y, str); |
158 | SDL_UnlockYUVOverlay(plat_sdl_overlay); |
c9099d02 |
159 | } |
160 | |
161 | void *plat_gvideo_set_mode(int *w, int *h, int *bpp) |
162 | { |
5b9aa749 |
163 | psx_w = *w; |
164 | psx_h = *h; |
165 | change_video_mode(); |
166 | if (plat_sdl_overlay != NULL) { |
167 | pl_plat_clear = plat_sdl_overlay_clear; |
c9099d02 |
168 | pl_plat_blit = overlay_blit; |
169 | pl_plat_hud_print = overlay_hud_print; |
170 | return NULL; |
171 | } |
172 | else { |
173 | pl_plat_clear = NULL; |
174 | pl_plat_blit = NULL; |
175 | pl_plat_hud_print = NULL; |
5b9aa749 |
176 | if (plat_sdl_gl_active) |
177 | return shadow_fb; |
178 | else |
179 | return plat_sdl_screen->pixels; |
a8376201 |
180 | } |
181 | } |
182 | |
7badc935 |
183 | void *plat_gvideo_flip(void) |
184 | { |
5b9aa749 |
185 | if (plat_sdl_overlay != NULL) { |
186 | SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h }; |
187 | SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect); |
c9099d02 |
188 | return NULL; |
a8376201 |
189 | } |
5b9aa749 |
190 | else if (plat_sdl_gl_active) { |
191 | gl_flip(shadow_fb, psx_w, psx_h); |
192 | return shadow_fb; |
193 | } |
c9099d02 |
194 | else { |
5b9aa749 |
195 | // XXX: no locking, but should be fine with SDL_SWSURFACE? |
196 | SDL_Flip(plat_sdl_screen); |
197 | return plat_sdl_screen->pixels; |
a8376201 |
198 | } |
7badc935 |
199 | } |
200 | |
201 | void plat_gvideo_close(void) |
202 | { |
203 | } |
204 | |
205 | void plat_video_menu_enter(int is_rom_loaded) |
206 | { |
a8376201 |
207 | in_menu = 1; |
208 | |
2e6189bc |
209 | /* surface will be lost, must adjust pl_vout_buf for menu bg */ |
5b9aa749 |
210 | if (plat_sdl_overlay != NULL) |
211 | uyvy_to_rgb565(menubg_img, plat_sdl_overlay->pixels[0], psx_w * psx_h); |
212 | else if (plat_sdl_gl_active) |
213 | memcpy(menubg_img, shadow_fb, psx_w * psx_h * 2); |
c9099d02 |
214 | else |
5b9aa749 |
215 | memcpy(menubg_img, plat_sdl_screen->pixels, psx_w * psx_h * 2); |
2e6189bc |
216 | pl_vout_buf = menubg_img; |
217 | |
5b9aa749 |
218 | change_video_mode(); |
7badc935 |
219 | } |
220 | |
221 | void plat_video_menu_begin(void) |
222 | { |
5b9aa749 |
223 | if (plat_sdl_overlay != NULL || plat_sdl_gl_active) { |
224 | g_menuscreen_ptr = shadow_fb; |
225 | } |
226 | else { |
227 | SDL_LockSurface(plat_sdl_screen); |
228 | g_menuscreen_ptr = plat_sdl_screen->pixels; |
229 | } |
7badc935 |
230 | } |
231 | |
232 | void plat_video_menu_end(void) |
233 | { |
5b9aa749 |
234 | if (plat_sdl_overlay != NULL) { |
235 | SDL_Rect dstrect = { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h }; |
236 | |
237 | SDL_LockYUVOverlay(plat_sdl_overlay); |
238 | rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb, |
239 | g_menuscreen_w * g_menuscreen_h); |
240 | SDL_UnlockYUVOverlay(plat_sdl_overlay); |
241 | |
242 | SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect); |
243 | } |
244 | else if (plat_sdl_gl_active) { |
245 | gl_flip(g_menuscreen_ptr, g_menuscreen_w, g_menuscreen_h); |
246 | } |
247 | else { |
248 | SDL_UnlockSurface(plat_sdl_screen); |
249 | SDL_Flip(plat_sdl_screen); |
250 | } |
7badc935 |
251 | g_menuscreen_ptr = NULL; |
252 | } |
253 | |
254 | void plat_video_menu_leave(void) |
255 | { |
a8376201 |
256 | in_menu = 0; |
7badc935 |
257 | } |
258 | |
259 | /* unused stuff */ |
260 | void *plat_prepare_screenshot(int *w, int *h, int *bpp) |
261 | { |
262 | return 0; |
263 | } |
264 | |
7badc935 |
265 | void plat_trigger_vibrate(int is_strong) |
266 | { |
267 | } |
268 | |
269 | void plat_minimize(void) |
270 | { |
271 | } |
272 | |
273 | // vim:shiftwidth=2:expandtab |