1 #include "SDL.h"
2 #include "SDL_image.h"
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "menu.h"
9 #include "sysconfig.h"
10 #include "sysdeps.h"
11 #include "uae.h"
12 #include "options.h"
13 #include "gui.h"
14 #include "zfile.h"
15 #include "button_mappings.h"
16
17 #define SDL_UI_DEBUG 1
18
19 #ifdef USE_GL
20 #define NO_SDL_GLEXT
21 # include <SDL_opengl.h>
22 /* These are not defined in the current version of SDL_opengl.h. */
23 # ifndef GL_TEXTURE_STORAGE_HINT_APPLE
24 # define GL_TEXTURE_STORAGE_HINT_APPLE 0x85BC
25 # endif
26 # ifndef GL_STORAGE_SHARED_APPLE
27 # define GL_STORAGE_SHARED_APPLE 0x85BF
28 # endif
29 #endif /* USE_GL */
30
31 #define VIDEO_FLAGS SDL_HWSURFACE
32 SDL_Surface* tmpSDLScreen = NULL;
33
34 int selected_item = 0;
35 char yol[256];
36 char msg[50];
37 char msg_status[50];
38
39 char launchDir[256];
40
41 extern int dirz(int parametre);
42 //extern int tweakz(int parametre);
43 extern int prefz(int parametre);
44 int soundVolume = 100;
45 extern int flashLED;
46
47 // --- internal prototypes ---
48 void cocoa_gui_early_setup (void);
49
50 //
gui_init(void)51 int gui_init (void) {
52 #if 0
53 if (display == NULL) {
54 SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
55 display = SDL_SetVideoMode(320,240,16,VIDEO_FLAGS);
56 #if SDL_UI_DEBUG > 0
57 write_log ("SDLUI: SDL_Init display init\n");
58 #endif
59 } else {
60 #if SDL_UI_DEBUG > 0
61 write_log ("SDLUI: SDL_Init display ready\n");
62 #endif
63 }
64 #endif
65 SDL_JoystickEventState(SDL_ENABLE);
66 SDL_JoystickOpen(0);
67 SDL_ShowCursor(SDL_DISABLE);
68 TTF_Init();
69
70 amiga_font = TTF_OpenFont("guidep/fonts/amiga4ever_pro2.ttf", 16);
71 if (!amiga_font) {
72 printf("SDLUI: TTF_OpenFont failed: %s\n", TTF_GetError());
73 abort();
74 }
75 text_color.r = 50;
76 text_color.g = 50;
77 text_color.b = 50;
78
79 pMenu_Surface = SDL_LoadBMP("guidep/images/menu.bmp");
80 if (pMenu_Surface == NULL) {
81 write_log ("SDLUI: Failed to load menu image\n");
82 abort();
83 }
84 pMouse_Pointer = SDL_LoadBMP("guidep/images/mousep.bmp");
85 if (pMouse_Pointer == NULL) {
86 write_log ("SDLUI: Failed to load mouse pointer image\n");
87 abort();
88 }
89 SDL_SetColorKey(pMouse_Pointer, SDL_SRCCOLORKEY, SDL_MapRGB(pMouse_Pointer->format, 75, 155, 135));
90
91 icon_expansion = SDL_LoadBMP("guidep/images/icon-expansion.bmp");
92 if (icon_expansion == NULL) {
93 write_log ("SDLUI: Failed to load icon expansion\n");
94 abort();
95 }
96 icon_preferences = SDL_LoadBMP("guidep/images/icon-preferences.bmp");
97 if (icon_preferences == NULL) {
98 write_log ("SDLUI: Failed to load icon preferences\n");
99 abort();
100 }
101 icon_keymaps = SDL_LoadBMP("guidep/images/icon-keymaps.bmp");
102 if (icon_keymaps == NULL) {
103 write_log ("SDLUI: Failed to load icon keymaps\n");
104 abort();
105 }
106 icon_floppy = SDL_LoadBMP("guidep/images/icon-floppy.bmp");
107 if (icon_floppy == NULL) {
108 write_log ("SDLUI: Failed to load icon floppy\n");
109 abort();
110 }
111 icon_reset = SDL_LoadBMP("guidep/images/icon-reset.bmp");
112 if (icon_reset == NULL) {
113 write_log ("SDLUI: Failed to load icon reset\n");
114 abort();
115 }
116 icon_storage = SDL_LoadBMP("guidep/images/icon-storage.bmp");
117 if (icon_storage == NULL) {
118 write_log ("SDLUI: Failed to load icon storage\n");
119 abort();
120 }
121 icon_run = SDL_LoadBMP("guidep/images/icon-run.bmp");
122 if (icon_run == NULL) {
123 write_log ("SDLUI: Failed to load icon run\n");
124 abort();
125 }
126 icon_exit = SDL_LoadBMP("guidep/images/icon-exit.bmp");
127 if (icon_exit == NULL) {
128 write_log ("SDLUI: Failed to load icon exit\n");
129 abort();
130 }
131 // icon_tweaks = SDL_LoadBMP("guidep/images/icon-tweaks.bmp");
132
133 return 1;
134 }
135
gui_exit(void)136 void gui_exit (void){
137 #if 0
138 SDL_FreeSurface(tmpSDLScreen);
139
140 SDL_FreeSurface(pMenu_Surface);
141 SDL_FreeSurface(pMouse_Pointer);
142
143 SDL_FreeSurface(icon_expansion);
144 SDL_FreeSurface(icon_preferences);
145 SDL_FreeSurface(icon_keymaps);
146 SDL_FreeSurface(icon_floppy);
147 SDL_FreeSurface(icon_reset);
148 SDL_FreeSurface(icon_storage);
149 SDL_FreeSurface(icon_run);
150 SDL_FreeSurface(icon_exit);
151 // SDL_FreeSurface(icon_tweaks);
152 #endif
153 SDL_Quit;
154 }
155
gui_display(int shortcut)156 void gui_display (int shortcut){
157 if (tmpSDLScreen == NULL) {
158 tmpSDLScreen = SDL_CreateRGBSurface(display->flags, display->w, display->h, display->format->BitsPerPixel,
159 display->format->Rmask, display->format->Gmask, display->format->Bmask, display->format->Amask);
160 if (tmpSDLScreen == NULL) {
161 write_log ("SDLUI: Failed to create temp screen\n");
162 abort();
163 } else {
164 write_log ("SDLUI: Created temp screen %dx%dx%d\n", display->w, display->h, display->format->BitsPerPixel);
165 }
166 }
167 SDL_Event event;
168
169 int menu_exitcode = -1;
170 int mainloopdone = 0;
171 int mouse_x = 30;
172 int mouse_y = 40;
173 int kup = 0;
174 int kdown = 0;
175 int kleft = 0;
176 int kright = 0;
177 int ksel = 0;
178 int iconpos_x = 0;
179 int iconpos_y = 0;
180
181 if (getcwd (launchDir, 256)) {
182 strcpy (yol, launchDir);
183 write_log ("SDLUI: current dir: %s\n", launchDir);
184 } else {
185 write_log("getcwd failed with errno %d\n", errno);
186 return;
187 }
188
189 while (!mainloopdone) {
190 while (SDL_PollEvent(&event)) {
191 if (event.type == SDL_QUIT) {
192 mainloopdone = 1;
193 }
194 if (event.type == SDL_JOYBUTTONDOWN) {
195 switch (event.jbutton.button) {
196 case PLATFORM_BUTTON_R: break;
197 case PLATFORM_BUTTON_L: break;
198 case PLATFORM_BUTTON_UP: kup = 1; break;
199 case PLATFORM_BUTTON_DOWN: kdown = 1; break;
200 case PLATFORM_BUTTON_LEFT: kleft = 1; break;
201 case PLATFORM_BUTTON_RIGHT: kright = 1; break;
202 case PLATFORM_BUTTON_CLICK: ksel = 1; break;
203 case PLATFORM_BUTTON_B: ksel = 1; break;
204 case PLATFORM_BUTTON_Y: break;
205 case PLATFORM_BUTTON_START: mainloopdone = 1; break;
206 }
207 }
208 if (event.type == SDL_KEYDOWN) {
209 switch (event.key.keysym.sym) {
210 case SDLK_ESCAPE: mainloopdone = 1; break;
211 case SDLK_UP: kup = 1; break;
212 case SDLK_DOWN: kdown = 1; break;
213 case SDLK_LEFT: kleft = 1; break;
214 case SDLK_RIGHT: kright = 1; break;
215 case SDLK_b: ksel = 1; break;
216 default: break;
217 }
218 }
219 if (event.type == SDL_MOUSEMOTION) {
220 mouse_x += event.motion.xrel;
221 mouse_y += event.motion.yrel;
222 }
223 if (event.type == SDL_MOUSEBUTTONDOWN) {
224 if (selected_item == 0) {
225 if (mouse_x >= 0 && mouse_x <= 20) {
226 if (mouse_y >= 0 && mouse_y <= 20) {
227 mainloopdone = 1;
228 }
229 }
230 } else {
231 ksel = 1; break;
232 }
233 }
234 }
235 if (ksel == 1) {
236 if (selected_item == menu_sel_expansion) {
237 sprintf (msg, "%s", "Select KickStart ROM");
238 sprintf (msg_status, "%s", "EXIT: Back/ESC");
239 sprintf (yol, "%s/roms", launchDir);
240 dirz(1);
241 }
242 if (selected_item == menu_sel_floppy) {
243 sprintf (msg, "%s", "Select Disk Image");
244 sprintf (msg_status, "%s", "DF0: B DF1: A");
245 sprintf (yol, "%s/disks", launchDir);
246 dirz(0);
247 }
248 if (selected_item == menu_sel_prefs) {
249 sprintf (msg, "%s", "Emulation Configuration");
250 sprintf (msg_status, "%s", "EXIT: Back/ESC");
251 prefz(0);
252 }
253 if (selected_item == menu_sel_reset) {
254 //reset amiga
255 menu_exitcode = 2;
256 mainloopdone = 1;
257 }
258 if (selected_item == menu_sel_keymaps) {
259 }
260 /* if (selected_item == menu_sel_tweaks) {
261 sprintf(msg,"%s","Tweaks");
262 sprintf(msg_status,"%s","L/R = -/+ B: Apply");
263 tweakz(0);
264 }*/
265 if (selected_item == menu_sel_storage) {
266
267 }
268 if (selected_item == menu_sel_run) {
269 menu_exitcode = 1;
270 mainloopdone = 1;
271 }
272 if (selected_item == menu_sel_exit) {
273 SDL_Quit();
274 exit(0);
275 }
276 ksel = 0;
277 }
278 // background
279 SDL_BlitSurface (pMenu_Surface, NULL, tmpSDLScreen, NULL);
280
281 // icons
282 iconpos_x = 10;
283 iconpos_y = 33;
284
285 selected_hilite (iconpos_x, iconpos_y, mouse_x, mouse_y, icon_floppy, menu_sel_floppy);
286 blit_image (icon_floppy, iconpos_x, iconpos_y);
287
288 iconpos_x += iconsizex + bosluk;
289 selected_hilite (iconpos_x, iconpos_y, mouse_x, mouse_y, icon_preferences, menu_sel_prefs);
290 blit_image (icon_preferences, iconpos_x, iconpos_y);
291
292 // iconpos_x += iconsizex + bosluk;
293 // selected_hilite (iconpos_x, iconpos_y, mouse_x, mouse_y, icon_tweaks, menu_sel_tweaks);
294 // blit_image (icon_tweaks, iconpos_x, iconpos_y);
295
296 iconpos_x += iconsizex + bosluk;
297 selected_hilite (iconpos_x, iconpos_y, mouse_x, mouse_y, icon_keymaps, menu_sel_keymaps);
298 blit_image (icon_keymaps, iconpos_x, iconpos_y);
299
300 iconpos_x += iconsizex + bosluk;
301 selected_hilite (iconpos_x, iconpos_y, mouse_x, mouse_y, icon_expansion, menu_sel_expansion);
302 blit_image (icon_expansion, iconpos_x, iconpos_y);
303
304 iconpos_x = 10;
305 iconpos_y = iconpos_y + iconsizey + bosluk;
306
307 selected_hilite (iconpos_x,iconpos_y,mouse_x,mouse_y,icon_storage, menu_sel_storage);
308 blit_image (icon_storage, iconpos_x, iconpos_y);
309
310 iconpos_x += iconsizex + bosluk;
311 selected_hilite (iconpos_x,iconpos_y,mouse_x,mouse_y, icon_reset, menu_sel_reset);
312 blit_image (icon_reset, iconpos_x, iconpos_y);
313
314 iconpos_x += iconsizex + bosluk;
315 selected_hilite (iconpos_x,iconpos_y,mouse_x,mouse_y, icon_run, menu_sel_run);
316 blit_image (icon_run, iconpos_x, iconpos_y);
317
318 iconpos_x += iconsizex + bosluk;
319 selected_hilite (iconpos_x,iconpos_y,mouse_x,mouse_y, icon_exit, menu_sel_exit);
320 blit_image (icon_exit, iconpos_x, iconpos_y);
321 // texts
322 write_text (TITLE_X, TITLE_Y, "PUAE //GnoStiC");
323
324 // mouse pointer ------------------------------
325 if (kleft == 1) {
326 mouse_x -= (iconsizex + bosluk);
327 kleft = 0;
328 }
329 if (kright == 1) {
330 mouse_x += (iconsizex + bosluk);
331 kright = 0;
332 }
333 if (kup == 1) {
334 mouse_y -= (iconsizey + bosluk);
335 kup = 0;
336 }
337 if (kdown == 1) {
338 kdown = 0;
339 mouse_y += (iconsizey + bosluk);
340 }
341
342 #define _MENU_X 640
343 #define _MENU_Y 480
344
345 if (mouse_x < 1) { mouse_x = 1; }
346 if (mouse_y < 1) { mouse_y = 1; }
347 if (mouse_x > _MENU_X) { mouse_x = _MENU_X; }
348 if (mouse_y > _MENU_Y) { mouse_y = _MENU_Y; }
349 rect.x = mouse_x;
350 rect.y = mouse_y;
351 //rect.w = pMouse_Pointer->w;
352 //rect.h = pMouse_Pointer->h;
353 SDL_BlitSurface (pMouse_Pointer, NULL, tmpSDLScreen, &rect);
354 // mouse pointer-end
355
356 SDL_BlitSurface (tmpSDLScreen, NULL, display, NULL);
357 #ifdef USE_GL
358 flush_gl_buffer (&glbuffer, 0, display->h - 1);
359 render_gl_buffer (&glbuffer, 0, display->h - 1);
360 glFlush ();
361 SDL_GL_SwapBuffers ();
362 #else
363 SDL_Flip (display);
364 #endif
365 } //while done
366
367 // return menu_exitcode;
368 }
369
write_text(int x,int y,char * txt)370 void write_text (int x, int y, char* txt) {
371 SDL_Surface* pText_Surface = TTF_RenderText_Solid(amiga_font, txt, text_color);
372
373 rect.x = x;
374 rect.y = y;
375 rect.w = pText_Surface->w;
376 rect.h = pText_Surface->h;
377
378 SDL_BlitSurface (pText_Surface,NULL,tmpSDLScreen,&rect);
379 SDL_FreeSurface(pText_Surface);
380 }
381
blit_image(SDL_Surface * img,int x,int y)382 void blit_image (SDL_Surface* img, int x, int y) {
383 SDL_Rect dest;
384 dest.x = x;
385 dest.y = y;
386 SDL_BlitSurface(img, 0, tmpSDLScreen, &dest);
387 }
388
selected_hilite(int ix,int iy,int mx,int my,SDL_Surface * img,int hangi)389 void selected_hilite (int ix, int iy, int mx, int my, SDL_Surface* img, int hangi) {
390 int secili = 0;
391 if (mx >= ix && mx <= ix + iconsizex) {
392 if (my >= iy && my <= iy + iconsizey) {
393 secili = 1;
394 }
395 }
396 if (secili == 1) {
397 SDL_SetAlpha(img, SDL_SRCALPHA, 100);
398 selected_item = hangi;
399 } else {
400 SDL_SetAlpha(img, SDL_SRCALPHA, 255);
401 }
402 }
403 //
gui_fps(int fps,int idle,int color)404 void gui_fps (int fps, int idle, int color)
405 {
406 gui_data.fps = fps;
407 gui_data.idle = idle;
408 }
409
gui_flicker_led(int led,int unitnum,int status)410 void gui_flicker_led (int led, int unitnum, int status)
411 {
412 }
413
gui_led(int led,int on)414 void gui_led (int led, int on)
415 {
416 }
417
gui_filename(int num,const char * name)418 void gui_filename (int num, const char *name)
419 {
420 }
421
gui_handle_events(void)422 void gui_handle_events (void)
423 {
424 }
425
gui_update(void)426 int gui_update (void)
427 {
428 return 0;
429 }
430
gui_message(const char * format,...)431 void gui_message (const char *format,...)
432 {
433 char msg[2048];
434 va_list parms;
435
436 va_start (parms,format);
437 vsprintf ( msg, format, parms);
438 va_end (parms);
439
440 write_log (msg);
441 }
442
gui_disk_image_change(int unitnum,const TCHAR * name,bool writeprotected)443 void gui_disk_image_change (int unitnum, const TCHAR *name, bool writeprotected) {}
gui_lock(void)444 void gui_lock (void) {}
gui_unlock(void)445 void gui_unlock (void) {}
446
447 static int guijoybutton[MAX_JPORTS];
448 static int guijoyaxis[MAX_JPORTS][4];
449 static bool guijoychange;
450
gui_gameport_button_change(int port,int button,int onoff)451 void gui_gameport_button_change (int port, int button, int onoff)
452 {
453 //write_log ("%d %d %d\n", port, button, onoff);
454 #ifdef RETROPLATFORM
455 int mask = 0;
456 if (button == JOYBUTTON_CD32_PLAY)
457 mask = RP_JOYSTICK_BUTTON5;
458 if (button == JOYBUTTON_CD32_RWD)
459 mask = RP_JOYSTICK_BUTTON6;
460 if (button == JOYBUTTON_CD32_FFW)
461 mask = RP_JOYSTICK_BUTTON7;
462 if (button == JOYBUTTON_CD32_GREEN)
463 mask = RP_JOYSTICK_BUTTON4;
464 if (button == JOYBUTTON_3 || button == JOYBUTTON_CD32_YELLOW)
465 mask = RP_JOYSTICK_BUTTON3;
466 if (button == JOYBUTTON_1 || button == JOYBUTTON_CD32_RED)
467 mask = RP_JOYSTICK_BUTTON1;
468 if (button == JOYBUTTON_2 || button == JOYBUTTON_CD32_BLUE)
469 mask = RP_JOYSTICK_BUTTON2;
470 rp_update_gameport (port, mask, onoff);
471 #endif
472 if (onoff)
473 guijoybutton[port] |= 1 << button;
474 else
475 guijoybutton[port] &= ~(1 << button);
476 guijoychange = true;
477 }
478
gui_gameport_axis_change(int port,int axis,int state,int max)479 void gui_gameport_axis_change (int port, int axis, int state, int max)
480 {
481 int onoff = state ? 100 : 0;
482 if (axis < 0 || axis > 3)
483 return;
484 if (max < 0) {
485 if (guijoyaxis[port][axis] == 0)
486 return;
487 if (guijoyaxis[port][axis] > 0)
488 guijoyaxis[port][axis]--;
489 } else {
490 if (state > max)
491 state = max;
492 if (state < 0)
493 state = 0;
494 guijoyaxis[port][axis] = max ? state * 127 / max : onoff;
495 #ifdef RETROPLATFORM
496 if (axis == DIR_LEFT_BIT)
497 rp_update_gameport (port, RP_JOYSTICK_LEFT, onoff);
498 if (axis == DIR_RIGHT_BIT)
499 rp_update_gameport (port, DIR_RIGHT_BIT, onoff);
500 if (axis == DIR_UP_BIT)
501 rp_update_gameport (port, DIR_UP_BIT, onoff);
502 if (axis == DIR_DOWN_BIT)
503 rp_update_gameport (port, DIR_DOWN_BIT, onoff);
504 #endif
505 }
506 guijoychange = true;
507 }
508
cocoa_gui_early_setup(void)509 void cocoa_gui_early_setup (void) {
510 //it's easier to put this here than adding a ifdef SDL_UI in main.m
511 }
512