--- Frodo4/Src/Display_SDL.h 2003/07/01 17:32:13 1.1 +++ Frodo4/Src/Display_SDL.h 2005/06/27 19:55:48 1.7 @@ -2,7 +2,7 @@ * Display_SDL.h - C64 graphics display, emulator window handling, * SDL specific stuff * - * Frodo (C) 1994-1997,2002 Christian Bauer + * Frodo (C) 1994-1997,2002-2005 Christian Bauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,6 +37,9 @@ static C64Display *c64_disp; static struct sigaction pulse_sa; static itimerval pulse_tv; +// SDL joysticks +static SDL_Joystick *joy[2] = {NULL, NULL}; + // Colors for speedometer/drive LEDs enum { black = 0, @@ -73,15 +76,11 @@ enum { int init_graphics(void) { // Init SDL - if (SDL_Init(SDL_INIT_VIDEO) < 0) { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError()); return 0; } - // Open window - SDL_WM_SetCaption(VERSION_STRING, "Frodo"); - screen = SDL_SetVideoMode(DISPLAY_X, DISPLAY_Y + 17, 8, SDL_DOUBLEBUF); - return 1; } @@ -95,6 +94,10 @@ C64Display::C64Display(C64 *the_c64) : T quit_requested = false; speedometer_string[0] = 0; + // Open window + SDL_WM_SetCaption(VERSION_STRING, "Frodo"); + screen = SDL_SetVideoMode(DISPLAY_X, DISPLAY_Y + 17, 8, SDL_DOUBLEBUF | (ThePrefs.DisplayType == DISPTYPE_SCREEN ? SDL_FULLSCREEN : 0)); + // LEDs off for (int i=0; i<4; i++) led_state[i] = old_led_state[i] = LED_OFF; @@ -102,7 +105,7 @@ C64Display::C64Display(C64 *the_c64) : T // Start timer for LED error blinking c64_disp = this; pulse_sa.sa_handler = (void (*)(int))pulse_handler; - pulse_sa.sa_flags = 0; + pulse_sa.sa_flags = SA_RESTART; sigemptyset(&pulse_sa.sa_mask); sigaction(SIGALRM, &pulse_sa, NULL); pulse_tv.it_interval.tv_sec = 0; @@ -491,6 +494,80 @@ bool C64Display::NumLock(void) } +/* + * Open/close joystick drivers given old and new state of + * joystick preferences + */ + +void C64::open_close_joystick(int port, int oldjoy, int newjoy) +{ + if (oldjoy != newjoy) { + joy_minx[port] = joy_miny[port] = 32767; // Reset calibration + joy_maxx[port] = joy_maxy[port] = -32768; + if (newjoy) { + joy[port] = SDL_JoystickOpen(newjoy - 1); + if (joy[port] == NULL) + fprintf(stderr, "Couldn't open joystick %d\n", port + 1); + } else { + if (joy[port]) { + SDL_JoystickClose(joy[port]); + joy[port] = NULL; + } + } + } +} + +void C64::open_close_joysticks(int oldjoy1, int oldjoy2, int newjoy1, int newjoy2) +{ + open_close_joystick(0, oldjoy1, newjoy1); + open_close_joystick(1, oldjoy2, newjoy2); +} + + +/* + * Poll joystick port, return CIA mask + */ + +uint8 C64::poll_joystick(int port) +{ + uint8 j = 0xff; + + if (port == 0 && (joy[0] || joy[1])) + SDL_JoystickUpdate(); + + if (joy[port]) { + int x = SDL_JoystickGetAxis(joy[port], 0), y = SDL_JoystickGetAxis(joy[port], 1); + + if (x > joy_maxx[port]) + joy_maxx[port] = x; + if (x < joy_minx[port]) + joy_minx[port] = x; + if (y > joy_maxy[port]) + joy_maxy[port] = y; + if (y < joy_miny[port]) + joy_miny[port] = y; + + if (joy_maxx[port] - joy_minx[port] < 100 || joy_maxy[port] - joy_miny[port] < 100) + return 0xff; + + if (x < (joy_minx[port] + (joy_maxx[port]-joy_minx[port])/3)) + j &= 0xfb; // Left + else if (x > (joy_minx[port] + 2*(joy_maxx[port]-joy_minx[port])/3)) + j &= 0xf7; // Right + + if (y < (joy_miny[port] + (joy_maxy[port]-joy_miny[port])/3)) + j &= 0xfe; // Up + else if (y > (joy_miny[port] + 2*(joy_maxy[port]-joy_miny[port])/3)) + j &= 0xfd; // Down + + if (SDL_JoystickGetButton(joy[port], 0)) + j &= 0xef; // Button + } + + return j; +} + + /* * Allocate C64 colors */