UInt32 Joystick::NumJoysticks() { UInt32 numPossibleJoysticks = joyGetNumDevs(); JOYINFOEX info; UInt32 numActualJoysticks = 0; for (UInt32 i = JOYSTICKID1; i < numPossibleJoysticks; ++i) { info.dwSize = sizeof(info); info.dwFlags = JOY_RETURNALL; if (joyGetPosEx(i, &info) == JOYERR_NOERROR) { ++numActualJoysticks; } else { break; } } return numActualJoysticks; }
/* Function to scan the system for joysticks. * This function should set SDL_numjoysticks to the number of available * joysticks. Joystick 0 should be the system default joystick. * It should return 0, or -1 on an unrecoverable fatal error. */ int SDL_SYS_JoystickInit(void) { int i; int maxdevs; int numdevs; JOYINFOEX joyinfo; JOYCAPS joycaps; MMRESULT result; /* Reset the joystick ID & name mapping tables */ for (i = 0; i < MAX_JOYSTICKS; ++i) { SYS_JoystickID[i] = 0; SYS_JoystickName[i] = NULL; } /* Loop over all potential joystick devices */ numdevs = 0; maxdevs = joyGetNumDevs(); for (i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i) { joyinfo.dwSize = sizeof(joyinfo); joyinfo.dwFlags = JOY_RETURNALL; result = joyGetPosEx(i, &joyinfo); if (result == JOYERR_NOERROR) { result = joyGetDevCaps(i, &joycaps, sizeof(joycaps)); if (result == JOYERR_NOERROR) { SYS_JoystickID[numdevs] = i; SYS_Joystick[numdevs] = joycaps; SYS_JoystickName[numdevs] = GetJoystickName(i, joycaps.szRegKey); numdevs++; } } } return (numdevs); }
VOID IK1_AnimRender( VOID ) { INT i; if (!IK1_IsInit) return; if ((i = joyGetNumDevs()) > 1) { JOYCAPS jc; if (joyGetDevCaps(JOYSTICKID1, &jc, sizeof(jc)) == JOYERR_NOERROR) { JOYINFOEX ji; ji.dwSize = sizeof(ji); ji.dwFlags = JOY_RETURNALL; if (joyGetPosEx(JOYSTICKID1, &ji) == JOYERR_NOERROR) { /* кнопки */ memcpy(IK1_Anim.JButOld, IK1_Anim.JBut, 32); for (i = 0; i < 32; i++) IK1_Anim.JBut[i] = (ji.dwButtons >> i) & 1; /* оси переводим в диапазон -1..1 */ IK1_Anim.Jx = 2.0 * (ji.dwXpos - jc.wXmin) / (jc.wXmax - jc.wXmin) - 1; IK1_Anim.Jy = 2.0 * (ji.dwYpos - jc.wYmin) / (jc.wYmax - jc.wYmin) - 1; IK1_Anim.Jz = 2.0 * (ji.dwZpos - jc.wZmin) / (jc.wZmax - jc.wZmin) - 1; IK1_Anim.Jr = 2.0 * (ji.dwRpos - jc.wRmin) / (jc.wRmax - jc.wRmin) - 1; if (ji.dwPOV == 0xFFFF) IK1_Anim.Jpov = 0; else IK1_Anim.Jpov = ji.dwPOV / 4500 + 1; }
ubyte joy_read_raw_buttons() { JOYINFOEX joy; int i; if (!joy_present) return 0; memset(&joy, 0, sizeof(joy)); joy.dwSize = sizeof(joy); joy.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNPOVCTS | JOY_USEDEADZONE; if (joyGetPosEx(joystick.joyid, &joy)!=JOYERR_NOERROR) return 0; for (i = 0; i < MAX_BUTTONS; i++) { joystick.buttons[i].last_state = joystick.buttons[i].state; joystick.buttons[i].state = (joy.dwButtons >> i) & 0x1; if (!joystick.buttons[i].last_state && joystick.buttons[i].state) { joystick.buttons[i].timedown = timer_get_fixed_seconds(); joystick.buttons[i].downcount++; } } /* Hat stuff */ if (joy.dwPOV != JOY_POVCENTERED) { joystick.buttons[19].state = (joy.dwPOV < JOY_POVRIGHT || joy.dwPOV > JOY_POVLEFT); joystick.buttons[15].state = (joy.dwPOV < JOY_POVBACKWARD && joy.dwPOV > JOY_POVFORWARD); joystick.buttons[11].state = (joy.dwPOV < JOY_POVLEFT && joy.dwPOV > JOY_POVRIGHT); joystick.buttons[7].state = (joy.dwPOV > JOY_POVBACKWARD); } return (ubyte)joy.dwButtons; }
void getJoystick(uintf index) // Retrieve the information (axis/rudder/button data) for joystick number 'index'. This can take a lot of time, so only probe the Joystick when you need to. { if (joystick[index].Connected) { if (index>=maxJoysticks) return; sJoystick *j = &joystickData[index]; memfill(&joystickinfo,0,sizeof(JOYINFOEX)); joystickinfo.dwSize = sizeof(JOYINFOEX); joystickinfo.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNPOVCTS | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_USEDEADZONE;//JOY_RETURNALL; joystickresult = joyGetPosEx(jsIDlookup[index],&joystickinfo); if (joystickresult==JOYERR_NOERROR) { j->Connected = true; j->X = getjoypos(joystickinfo.dwXpos); j->Y = getjoypos(joystickinfo.dwYpos); j->Z = getjoypos(joystickinfo.dwZpos); j->Rudder = getjoypos(joystickinfo.dwRpos); j->U = getjoypos(joystickinfo.dwUpos); j->V = getjoypos(joystickinfo.dwVpos); j->Buttons = joystickinfo.dwButtons; if (joystickinfo.dwPOV<=36000) j->POV = joystickinfo.dwPOV/10; else j->POV = -1; } else j->Connected = false; } }
glzInputData::glzInputData() { LMdown = false; MMdown = false; RMdown = false; Mpos_x = 0; Mpos_y = 0; Mweel = 0; Mactive = false; pulsar1 = false; pulsar2 = false; pulsar4 = false; pulsar8 = false; pulsar16 = false; pulsartimer = 0.0f; pulsarcounter = 0; JoyPresent = false; JOYINFOEX joyInfoEx; joyInfoEx.dwSize = sizeof(joyInfoEx); joyGetDevCaps(JOYSTICKID1, &joyCaps, sizeof(joyCaps)); JoyPresent = (joyGetPosEx(JOYSTICKID1, &joyInfoEx) == JOYERR_NOERROR); }
/* some joystick example code. Operating systems other than windows will need other options for the joy stick also */ GLvoid PollJoyStick(){ JOYINFOEX stuff; ZeroMemory(&stuff, sizeof(stuff)); stuff.dwSize = sizeof(stuff); stuff.dwFlags |= JOY_RETURNALL; joyGetPosEx(JOYSTICKID1, &stuff); /* axis forcing them between 0 and 1 you need to check for dead zones */ float axisY = (float)(stuff.dwYpos - 32767.0f) / 32768; float axisX = (float)(stuff.dwXpos - 32767.0f) / 32768; float axisZ = (float)(stuff.dwZpos - 32767.0f) / 32768; float axisR = (float)(stuff.dwRpos - 32767.0f) / 32768; /* * the buttons are set up on powers of 2 * i.e. button 1 = 1 or (2 ^ (button - 1)) or (2 ^ 0) * button 2 = 2 or (2 ^ (2 - 1)) or (2 ^ 1) * button 3 = 4 or (2 ^ (3 - 1)) or (2 ^ 2) * etc... * You can use flags to test them * e.g. (buttons & 0x00000001) or (buttons & 1) would give me true if 1 is pressed and false other wise or * (buttons & 0x00000004) or (buttons & 4) would give me true if button 3 is pressed * This allows the same int to pack multiple button pressed into one unsigned int * so buttons would be 3 if buttons 1 and 2 were pressed. You can do (buttons & 1) would give true * and (buttons & 2) would give true but (buttons & 8) would give false; */ unsigned int buttons = stuff.dwButtons; /*std::cout << "My joystick outputs: " << std::endl << "Axis Data: " << std::endl << "\tAxisY: " << axisY << std::endl << "\tAxisX: " << axisX << std::endl << "\tAxisZ: " << axisZ << std::endl << "\tAxisR: " << axisR << std::endl << std::endl << "Button Data:" << std::endl << "\tMy Button Data is: " << buttons << std::endl;*/ }
double joystick_axis(int id, int axisnum) { JOYINFOEX joyinfo; JOYCAPS joycaps; joyGetDevCaps(JOYSTICKID1 + id, &joycaps, sizeof(joycaps)); switch (axisnum) { case 1: joyinfo.dwFlags = JOY_RETURNX | JOY_RETURNCENTERED; joyGetPosEx(JOYSTICKID1 + id, &joyinfo); return (joyinfo.dwXpos - (joycaps.wXmax + joycaps.wXmin) / 2.f) / ((joycaps.wXmax - joycaps.wXmin)/ 2.f); break; case 2: joyinfo.dwFlags = JOY_RETURNY | JOY_RETURNCENTERED; joyGetPosEx(JOYSTICKID1 + id, &joyinfo); return (joyinfo.dwYpos - (joycaps.wYmax + joycaps.wYmin) / 2.f) / ((joycaps.wYmax - joycaps.wYmin)/ 2.f); break; case 3: joyinfo.dwFlags = JOY_RETURNZ | JOY_RETURNCENTERED; joyGetPosEx(JOYSTICKID1 + id, &joyinfo); return (joyinfo.dwZpos - (joycaps.wZmax + joycaps.wZmin) / 2.f) / ((joycaps.wZmax - joycaps.wZmin)/ 2.f); break; case 4: joyinfo.dwFlags = JOY_RETURNR | JOY_RETURNCENTERED; joyGetPosEx(JOYSTICKID1 + id, &joyinfo); return (joyinfo.dwRpos - (joycaps.wRmax + joycaps.wRmin) / 2.f) / ((joycaps.wRmax - joycaps.wRmin)/ 2.f); break; case 5: joyinfo.dwFlags = JOY_RETURNU | JOY_RETURNCENTERED; joyGetPosEx(JOYSTICKID1 + id, &joyinfo); return (joyinfo.dwUpos - (joycaps.wUmax + joycaps.wUmin) / 2.f) / ((joycaps.wUmax - joycaps.wUmin)/ 2.f); break; case 6: joyinfo.dwFlags = JOY_RETURNV | JOY_RETURNCENTERED; joyGetPosEx(JOYSTICKID1 + id, &joyinfo); return (joyinfo.dwVpos - (joycaps.wVmax + joycaps.wVmin) / 2.f) / ((joycaps.wVmax - joycaps.wVmin)/ 2.f); break; default: return 0; } }
/* =============== IN_StartupJoystick =============== */ void IN_StartupJoystick (void) { int numdevs; JOYCAPS jc; MMRESULT mmr; cvar_t *cv; // assume no joystick joy_avail = false; // abort startup if user requests no joystick cv = Cvar_Get ("in_initjoy", "1", CVAR_NOSET); if ( !cv->value ) return; // verify joystick driver is present if ((numdevs = joyGetNumDevs ()) == 0) { // Com_Printf ("\njoystick not found -- driver not present\n\n"); return; } // cycle through the joystick ids for the first valid one for (joy_id=0 ; joy_id<numdevs ; joy_id++) { memset (&ji, 0, sizeof(ji)); ji.dwSize = sizeof(ji); ji.dwFlags = JOY_RETURNCENTERED; if ((mmr = joyGetPosEx (joy_id, &ji)) == JOYERR_NOERROR) break; } // abort startup if we didn't find a valid joystick if (mmr != JOYERR_NOERROR) { Com_Printf ("\njoystick not found -- no valid joysticks (%x)\n\n", mmr); return; } // get the capabilities of the selected joystick // abort startup if command fails memset (&jc, 0, sizeof(jc)); if ((mmr = joyGetDevCaps (joy_id, &jc, sizeof(jc))) != JOYERR_NOERROR) { Com_Printf ("\njoystick not found -- invalid joystick capabilities (%x)\n\n", mmr); return; } // save the joystick's number of buttons and POV status joy_numbuttons = jc.wNumButtons; joy_haspov = jc.wCaps & JOYCAPS_HASPOV; // old button and POV states default to no buttons pressed joy_oldbuttonstate = joy_oldpovstate = 0; // mark the joystick as available and advanced initialization not completed // this is needed as cvars are not available during initialization joy_avail = true; joy_advancedinit = false; Com_Printf ("\njoystick detected\n\n"); }
/* * Read the raw joystick data */ static void fghJoystickRawRead( SFG_Joystick* joy, int* buttons, float* axes ) { #ifdef WIN32 MMRESULT status; #else int status; #endif int i; if( joy->error ) { if( buttons ) *buttons = 0; if( axes ) for( i=0; i<joy->num_axes; i++ ) axes[ i ] = 1500.0f; return; } #ifdef WIN32 status = joyGetPosEx( joy->js_id, &joy->js ); if( status != JOYERR_NOERROR ) { joy->error = GL_TRUE; return; } if( buttons ) *buttons = joy->js.dwButtons; if( axes ) { /* * WARNING - Fall through case clauses!! */ switch( joy->num_axes ) { case 6: axes[5] = (float) joy->js.dwVpos; case 5: axes[4] = (float) joy->js.dwUpos; case 4: axes[3] = (float) joy->js.dwRpos; case 3: axes[2] = (float) joy->js.dwZpos; case 2: axes[1] = (float) joy->js.dwYpos; case 1: axes[0] = (float) joy->js.dwXpos; } } #else # ifdef JS_NEW while( 1 ) { status = read( joy->fd, &joy->js, sizeof(struct js_event) ); if( status != sizeof( struct js_event ) ) { if( errno == EAGAIN ) { /* * Use the old values */ if( buttons ) *buttons = joy->tmp_buttons; if( axes ) memcpy( axes, joy->tmp_axes, sizeof( float ) * joy->num_axes ); return; } fgWarning( "%s", joy->fname ); joy->error = GL_TRUE; return; } switch( joy->js.type & ~JS_EVENT_INIT ) { case JS_EVENT_BUTTON: if( joy->js.value == 0 ) /* clear the flag */ joy->tmp_buttons &= ~( 1 << joy->js.number ); else joy->tmp_buttons |= ( 1 << joy->js.number ); break; case JS_EVENT_AXIS: joy->tmp_axes[ joy->js.number ] = ( float )joy->js.value; if( axes ) memcpy( axes, joy->tmp_axes, sizeof(float) * joy->num_axes ); break; } if( buttons ) *buttons = joy->tmp_buttons; } # else status = read( joy->fd, &joy->js, JS_RETURN ); if( status != JS_RETURN ) { fgWarning( "%s", joy->fname ); joy->error = GL_TRUE; return; } if( buttons ) # if defined( __FreeBSD__ ) || defined( __NetBSD__ ) *buttons = ( joy->js.b1 ? 1 : 0 ) | ( joy->js.b2 ? 2 : 0 ); # else *buttons = joy->js.buttons; # endif if( axes ) { axes[ 0 ] = (float) joy->js.x; axes[ 1 ] = (float) joy->js.y; } # endif #endif }
static int __glutProcessDeviceEvents(XEvent * event) { #if !defined(_WIN32) GLUTwindow *window; /* XXX Ugly code fan out. */ /* Can't use switch/case since X Input event types are dynamic. */ if (__glutDeviceMotionNotify && event->type == __glutDeviceMotionNotify) { XDeviceMotionEvent *devmot = (XDeviceMotionEvent *) event; window = __glutGetWindow(devmot->window); if (window) { if (__glutTablet && devmot->deviceid == __glutTablet->device_id && window->tabletMotion) { tabletPosChange(window, devmot->first_axis, devmot->axes_count, devmot->axis_data); } else if (__glutDials && devmot->deviceid == __glutDials->device_id && window->dials) { int i, first = devmot->first_axis, count = devmot->axes_count; for (i = first; i < first + count; i++) window->dials(i + 1, normalizeDialAngle(i, devmot->axis_data[i - first])); } else if (__glutSpaceball && devmot->deviceid == __glutSpaceball->device_id) { /* XXX Assume that space ball motion events come in as all the first 6 axes. Assume first 3 axes are XYZ translations; second 3 axes are XYZ rotations. */ if (devmot->first_axis == 0 && devmot->axes_count == 6) { if (window->spaceMotion) window->spaceMotion( normalizeSpaceballDelta(0, devmot->axis_data[0]), normalizeSpaceballDelta(1, devmot->axis_data[1]), normalizeSpaceballDelta(2, devmot->axis_data[2])); if (window->spaceRotate) window->spaceRotate( normalizeSpaceballAngle(3, devmot->axis_data[3]), normalizeSpaceballAngle(4, devmot->axis_data[4]), normalizeSpaceballAngle(5, devmot->axis_data[5])); } } return 1; } } else if (__glutDeviceButtonPress && event->type == __glutDeviceButtonPress) { XDeviceButtonEvent *devbtn = (XDeviceButtonEvent *) event; window = __glutGetWindow(devbtn->window); if (window) { if (__glutTablet && devbtn->deviceid == __glutTablet->device_id && window->tabletButton && devbtn->first_axis == 0 && devbtn->axes_count == 2) { tabletPosChange(window, devbtn->first_axis, devbtn->axes_count, devbtn->axis_data); window->tabletButton(devbtn->button, GLUT_DOWN, window->tabletPos[0], window->tabletPos[1]); } else if (__glutDials && devbtn->deviceid == __glutDials->device_id && window->buttonBox) { window->buttonBox(devbtn->button, GLUT_DOWN); } else if (__glutSpaceball && devbtn->deviceid == __glutSpaceball->device_id && window->spaceButton) { window->spaceButton(devbtn->button, GLUT_DOWN); } return 1; } } else if (__glutDeviceButtonRelease && event->type == __glutDeviceButtonRelease) { XDeviceButtonEvent *devbtn = (XDeviceButtonEvent *) event; window = __glutGetWindow(devbtn->window); if (window) { if (__glutTablet && devbtn->deviceid == __glutTablet->device_id && window->tabletButton && devbtn->first_axis == 0 && devbtn->axes_count == 2) { tabletPosChange(window, devbtn->first_axis, devbtn->axes_count, devbtn->axis_data); window->tabletButton(devbtn->button, GLUT_UP, window->tabletPos[0], window->tabletPos[1]); } else if (__glutDials && devbtn->deviceid == __glutDials->device_id && window->buttonBox) { window->buttonBox(devbtn->button, GLUT_UP); } else if (__glutSpaceball && devbtn->deviceid == __glutSpaceball->device_id && window->spaceButton) { window->spaceButton(devbtn->button, GLUT_UP); } return 1; } } #else { JOYINFOEX info; JOYCAPS joyCaps; memset(&info, 0, sizeof(JOYINFOEX)); info.dwSize = sizeof(JOYINFOEX); info.dwFlags = JOY_RETURNALL; if (joyGetPosEx(JOYSTICKID1,&info) != JOYERR_NOERROR) { __glutHasJoystick = 1; joyGetDevCaps(JOYSTICKID1, &joyCaps, sizeof(joyCaps)); __glutNumJoystickButtons = joyCaps.wNumButtons; __glutNumJoystickAxes = joyCaps.wNumAxes; } else { __glutHasJoystick = 0; __glutNumJoystickButtons = 0; __glutNumJoystickAxes = 0; } } #endif /* !_WIN32 */ return 0; }
VOID BG3_AnimRender( VOID ) { char buffer[1000]; POINT cursorPoint; DBL timeSec; LARGE_INTEGER time, timeFreq; INT i; if (!BG3_IsInit) return; /* Update timer */ QueryPerformanceFrequency(&timeFreq); QueryPerformanceCounter(&time); timeSec = (DBL)time.QuadPart / timeFreq.QuadPart; BG3_ANIM.timer.deltaGlobalTime = timeSec - BG3_ANIM.timer.animStart - BG3_ANIM.timer.globalTime; BG3_ANIM.timer.deltaActiveTime = BG3_ANIM.isPaused ? 0 : BG3_ANIM.timer.deltaGlobalTime; BG3_ANIM.timer.activeTime += BG3_ANIM.timer.deltaActiveTime; BG3_ANIM.timer.globalTime = timeSec - BG3_ANIM.timer.animStart; /* FPS Counter update */ BG3_ANIM.timer.fpsTime += BG3_ANIM.timer.deltaGlobalTime; if (BG3_ANIM.timer.fpsTime > 1) { BG3_ANIM.timer.fpsTime = 0; BG3_ANIM.lastFPS = BG3_ANIM.fpsCounter; BG3_ANIM.fpsCounter = 0; } /* Update input state */ // Keyboard memcpy(BG3_ANIM.keyState.old, BG3_ANIM.keyState.actual, sizeof(BG3_ANIM.keyState.actual)); memset(BG3_ANIM.keyState.actual, 0, sizeof(BG3_ANIM.keyState.actual)); GetKeyboardState(BG3_ANIM.keyState.actual); for (i = 0; i < 256; i++) BG3_ANIM.keyState.actual[i] >>= 7; // Joy if (joyGetNumDevs() > 1) { // Joy exists JOYCAPS jc; if (joyGetDevCaps(JOYSTICKID1, &jc, sizeof(jc)) == JOYERR_NOERROR) { JOYINFOEX ji; ji.dwSize = sizeof(ji); ji.dwFlags = JOY_RETURNALL; if (joyGetPosEx(JOYSTICKID1, &ji) == JOYERR_NOERROR) { // Buttons memcpy(BG3_ANIM.joy.buttonsOld, BG3_ANIM.joy.buttons, sizeof(BG3_ANIM.joy.buttons)); for (i = 0; i < 32; i++) { BG3_ANIM.joy.buttons[i] = (ji.dwButtons >> i) & 1; } // Axis BG3_ANIM.joy.x = 2.0 * (ji.dwXpos - jc.wXmin) / (jc.wXmax - jc.wXmin) - 1; BG3_ANIM.joy.y = 2.0 * (ji.dwYpos - jc.wYmin) / (jc.wYmax - jc.wYmin) - 1; BG3_ANIM.joy.z = 2.0 * (ji.dwZpos - jc.wZmin) / (jc.wZmax - jc.wZmin) - 1; BG3_ANIM.joy.r = 2.0 * (ji.dwRpos - jc.wRmin) / (jc.wRmax - jc.wRmin) - 1; // PoV if (ji.dwPOV == 0xFFFF) BG3_ANIM.joy.pov = 0; else BG3_ANIM.joy.pov = ji.dwPOV / 4500 + 1; }
/* Function to update the state of a joystick - called as a device poll. * This function shouldn't update the joystick structure directly, * but instead should call SDL_PrivateJoystick*() to deliver events * and update joystick device state. */ void SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) { MMRESULT result; int i; DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, JOY_RETURNR, JOY_RETURNU, JOY_RETURNV }; DWORD pos[MAX_AXES]; struct _transaxis *transaxis; int value, change; JOYINFOEX joyinfo; joyinfo.dwSize = sizeof(joyinfo); joyinfo.dwFlags = JOY_RETURNALL | JOY_RETURNPOVCTS; if (!joystick->hats) { joyinfo.dwFlags &= ~(JOY_RETURNPOV | JOY_RETURNPOVCTS); } result = joyGetPosEx(joystick->hwdata->id, &joyinfo); if (result != JOYERR_NOERROR) { SetMMerror("joyGetPosEx", result); return; } /* joystick motion events */ pos[0] = joyinfo.dwXpos; pos[1] = joyinfo.dwYpos; pos[2] = joyinfo.dwZpos; pos[3] = joyinfo.dwRpos; pos[4] = joyinfo.dwUpos; pos[5] = joyinfo.dwVpos; transaxis = joystick->hwdata->transaxis; for (i = 0; i < joystick->naxes; i++) { if (joyinfo.dwFlags & flags[i]) { value = (int) (((float) pos[i] + transaxis[i].offset) * transaxis[i].scale); change = (value - joystick->axes[i]); if ((change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD)) { SDL_PrivateJoystickAxis(joystick, (Uint8) i, (Sint16) value); } } } /* joystick button events */ if (joyinfo.dwFlags & JOY_RETURNBUTTONS) { for (i = 0; i < joystick->nbuttons; ++i) { if (joyinfo.dwButtons & JOY_BUTTON_FLAG(i)) { if (!joystick->buttons[i]) { SDL_PrivateJoystickButton(joystick, (Uint8) i, SDL_PRESSED); } } else { if (joystick->buttons[i]) { SDL_PrivateJoystickButton(joystick, (Uint8) i, SDL_RELEASED); } } } } /* joystick hat events */ if (joyinfo.dwFlags & JOY_RETURNPOV) { Uint8 pos; pos = TranslatePOV(joyinfo.dwPOV); if (pos != joystick->hats[0]) { SDL_PrivateJoystickHat(joystick, 0, pos); } } }
// -------------------------------------------------------------- // joy_process() // // Runs as a separate thread, and updates the state of the joystick // DWORD joy_process(DWORD lparam) { MMRESULT rs; JOYINFOEX ji; int i,state; joy_button_info *bi; EXECUTION_STATE last_exectution_state = 0; uint last_ssav_time = 0; // power management stuff, we need to handle this manually for joysticks { // give notification that we need both display and system (for multi) resources to remain available // NOTE: we'll have to restore the previous execution state before exiting this thread last_exectution_state = SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED); // turn of screen saver, but save the current timeout so that we can restore it later SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &last_ssav_time, 0); SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0); } for ( i = 0; i < JOY_TOTAL_BUTTONS; i++) { bi = &joy_buttons[i]; bi->actual_state = 0; // Don't set in flush code! bi->state = 0; bi->down_count = 0; bi->up_count = 0; bi->down_time = 0; bi->last_down_check = timer_get_milliseconds(); } while (1) { // Wait for the thread to be signaled to end or 1/18th of a second to pass... if ( WaitForSingleObject( Joy_tell_thread_to_end_event, joy_pollrate )==WAIT_OBJECT_0) { break; } memset(&ji, 0, sizeof(ji)); ji.dwSize = sizeof(ji); // ji.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNRAWDATA; ji.dwFlags = JOY_RETURNALL; EnterCriticalSection(&joy_lock); uint joy_state = 0; if (Cur_joystick >= 0) { rs = joyGetPosEx(Cur_joystick, &ji); // If there's an error, assume all buttons down. if (rs == JOYERR_NOERROR) { joy_state = ji.dwButtons; } } // Process ji.dwButtons for (i=0; i<JOY_TOTAL_BUTTONS; i++) { state = 0; if (i < JOY_NUM_BUTTONS) { state = joy_state & (1<<i); } else { // check for hat presses, which act like buttons switch (i) { case JOY_HATBACK: if (ji.dwPOV == JOY_POVBACKWARD) state = 1; break; case JOY_HATFORWARD: if (ji.dwPOV == JOY_POVFORWARD) state = 1; break; case JOY_HATLEFT: if (ji.dwPOV == JOY_POVLEFT) state = 1; break; case JOY_HATRIGHT: if (ji.dwPOV == JOY_POVRIGHT) state = 1; break; default: Int3(); // should never happen break; } // end switch } // end if if (state != joy_buttons[i].actual_state) { // Button position physically changed. joy_buttons[i].actual_state = state; if ( state ) { // went from up to down joy_buttons[i].down_count++; joy_buttons[i].down_time = 0; joy_buttons[i].state = 1; //////////////////////////// /// SOMETHING TERRIBLE IS ABOUT TO HAPPEN. I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE /// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK ALL THE JOYSTICK BUTTONS. /// PLEASE SEE ALAN FOR MORE INFORMATION. //////////////////////////// #ifdef FS2_DEMO { extern void demo_reset_trailer_timer(); demo_reset_trailer_timer(); } #endif //////////////////////////// /// IT'S OVER. SEE, IT WASN'T SO BAD RIGHT? IT'S IS VERY UGLY LOOKING, I KNOW. //////////////////////////// } else { // went from down to up if ( joy_buttons[i].state ) { joy_buttons[i].up_count++; } joy_buttons[i].state = 0; } } else { // Didn't move... increment time down if down. if (joy_buttons[i].state) { joy_buttons[i].down_time += joy_pollrate; } } } // end for LeaveCriticalSection(&joy_lock); } // power management stuff, we need to handle this manually for joysticks { // restore the original execution state last_exectution_state = SetThreadExecutionState(last_exectution_state); // restore the original screensaver timeout value SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, last_ssav_time, NULL, 0); } SetEvent(Joy_thread_says_its_done_event); return 0; }
int GamePad::read_event() { #if defined(WIN32) infoex.dwFlags = JOY_RETURNALL; joyGetPosEx(gamepad_fd, &infoex) ; axe[0] = (double)((1. * infoex.dwXpos - 32767.) / 32767.); axe[1] = (double)((1. * infoex.dwYpos - 32767.) / 32767.); axe[2] = (double)((1. * infoex.dwRpos - 32767.) / 32767.); axe[3] = (double)((1. * infoex.dwZpos - 32767.) / 32767.); if(infoex.dwPOV < 38000.) { double alpha = 3.14159 / 18000. * infoex.dwPOV; axe[4] = 1.001 * sin(alpha); axe[5] = -1.001 * cos(alpha); } else{ axe[4] = axe[5] = 0.; } for (int i = 0; i < 6; i++) if(fabs(axe[i]) < .01) axe[i] = 0.; bool event = false; int event_num = 0; bool event_value = false; for (int i = 0; i < buttons; i++){ int bin = pow(2.0, i); if(button[i] != (bool)(infoex.dwButtons & bin)) { event = true; event_num = i; event_value = (bool)(infoex.dwButtons & bin); } } if(event){ if(button[event_num] == 0 && event_value){ toggle_status[event_num] = true; } button[event_num] = event_value; } /* std::cout<< "--------------------" << std::endl; std::cout<< infoex.dwXpos << std::endl; std::cout<< infoex.dwYpos << std::endl; std::cout<< infoex.dwZpos << std::endl; std::cout<< infoex.dwRpos << std::endl; std::cout<< infoex.dwUpos << std::endl; std::cout<< infoex.dwVpos << std::endl; std::cout<< infoex.dwButtons << std::endl; std::cout<< infoex.dwButtonNumber << std::endl; std::cout<< infoex.dwPOV << std::endl; */ #elif defined(HAVE_LINUX_JOYSTICK) int result = read(gamepad_fd, &event, sizeof(event)); if (result > 0){ switch (event.type){ case JS_EVENT_INIT: break; case JS_EVENT_INIT | JS_EVENT_AXIS: break; case JS_EVENT_INIT | JS_EVENT_BUTTON: break; case JS_EVENT_AXIS: axe[(int)event.number] = (double)event.value / 32767.; break; case JS_EVENT_BUTTON: if(button[(int)event.number] == 0. && (bool)event.value) { toggle_status[(int)event.number] = true; } button[(int)event.number] = (bool)event.value ; break; default: break; } } #endif return 1; }
void Gamepad_detectDevices() { unsigned int numPadsSupported; unsigned int deviceIndex, deviceIndex2; JOYINFOEX info; JOYCAPS caps; bool duplicate; struct Gamepad_device * deviceRecord; struct Gamepad_devicePrivate * deviceRecordPrivate; UINT joystickID; int axisIndex; if (!inited) { return; } numPadsSupported = joyGetNumDevs(); for (deviceIndex = 0; deviceIndex < numPadsSupported; deviceIndex++) { info.dwSize = sizeof(info); info.dwFlags = JOY_RETURNALL; joystickID = JOYSTICKID1 + deviceIndex; if (joyGetPosEx(joystickID, &info) == JOYERR_NOERROR && joyGetDevCaps(joystickID, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) { duplicate = false; for (deviceIndex2 = 0; deviceIndex2 < numDevices; deviceIndex2++) { if (((struct Gamepad_devicePrivate *) devices[deviceIndex2]->privateData)->joystickID == joystickID) { duplicate = true; break; } } if (duplicate) { continue; } deviceRecord = malloc(sizeof(struct Gamepad_device)); deviceRecord->deviceID = nextDeviceID++; deviceRecord->description = getDeviceDescription(joystickID, caps); deviceRecord->vendorID = caps.wMid; deviceRecord->productID = caps.wPid; deviceRecord->numAxes = caps.wNumAxes + ((caps.wCaps & JOYCAPS_HASPOV) ? 2 : 0); deviceRecord->numButtons = caps.wNumButtons; deviceRecord->axisStates = calloc(sizeof(float), deviceRecord->numAxes); deviceRecord->buttonStates = calloc(sizeof(bool), deviceRecord->numButtons); deviceRecord->eventDispatcher = EventDispatcher_create(deviceRecord); devices = realloc(devices, sizeof(struct Gamepad_device *) * (numDevices + 1)); devices[numDevices++] = deviceRecord; deviceRecordPrivate = malloc(sizeof(struct Gamepad_devicePrivate)); deviceRecordPrivate->joystickID = joystickID; deviceRecordPrivate->lastState = info; deviceRecordPrivate->xAxisIndex = 0; deviceRecordPrivate->yAxisIndex = 1; axisIndex = 2; deviceRecordPrivate->zAxisIndex = (caps.wCaps & JOYCAPS_HASZ) ? axisIndex++ : -1; deviceRecordPrivate->rAxisIndex = (caps.wCaps & JOYCAPS_HASR) ? axisIndex++ : -1; deviceRecordPrivate->uAxisIndex = (caps.wCaps & JOYCAPS_HASU) ? axisIndex++ : -1; deviceRecordPrivate->vAxisIndex = (caps.wCaps & JOYCAPS_HASV) ? axisIndex++ : -1; deviceRecordPrivate->axisRanges = malloc(sizeof(UINT[2]) * axisIndex); deviceRecordPrivate->axisRanges[0][0] = caps.wXmin; deviceRecordPrivate->axisRanges[0][1] = caps.wXmax; deviceRecordPrivate->axisRanges[1][0] = caps.wYmin; deviceRecordPrivate->axisRanges[1][1] = caps.wYmax; if (deviceRecordPrivate->zAxisIndex != -1) { deviceRecordPrivate->axisRanges[deviceRecordPrivate->zAxisIndex][0] = caps.wZmin; deviceRecordPrivate->axisRanges[deviceRecordPrivate->zAxisIndex][1] = caps.wZmax; } if (deviceRecordPrivate->rAxisIndex != -1) { deviceRecordPrivate->axisRanges[deviceRecordPrivate->rAxisIndex][0] = caps.wRmin; deviceRecordPrivate->axisRanges[deviceRecordPrivate->rAxisIndex][1] = caps.wRmax; } if (deviceRecordPrivate->uAxisIndex != -1) { deviceRecordPrivate->axisRanges[deviceRecordPrivate->uAxisIndex][0] = caps.wUmin; deviceRecordPrivate->axisRanges[deviceRecordPrivate->uAxisIndex][1] = caps.wUmax; } if (deviceRecordPrivate->vAxisIndex != -1) { deviceRecordPrivate->axisRanges[deviceRecordPrivate->vAxisIndex][0] = caps.wVmin; deviceRecordPrivate->axisRanges[deviceRecordPrivate->vAxisIndex][1] = caps.wVmax; } deviceRecordPrivate->povXAxisIndex = (caps.wCaps & JOYCAPS_HASPOV) ? axisIndex++ : -1; deviceRecordPrivate->povYAxisIndex = (caps.wCaps & JOYCAPS_HASPOV) ? axisIndex++ : -1; deviceRecord->privateData = deviceRecordPrivate; Gamepad_eventDispatcher()->dispatchEvent(Gamepad_eventDispatcher(), Atom_fromString(GAMEPAD_EVENT_DEVICE_ATTACHED), deviceRecord); } } }
VOID IK3_AnimRender( VOID ) { int i; POINT pt; LARGE_INTEGER t; IK3_FrameCounter++; QueryPerformanceCounter(&t); IK3_Anim.GlobalTime =(DBL)(t.QuadPart - IK3_StartTime) / IK3_TimePerSec; IK3_Anim.GlobalDeltaTime =(DBL)(t.QuadPart - IK3_OldTime) / IK3_TimePerSec; if (IK3_Anim.IsPause) { IK3_Anim.DeltaTime = 0; IK3_PauseTime += t.QuadPart - IK3_OldTime; } else { IK3_Anim.DeltaTime = IK3_Anim.GlobalDeltaTime; IK3_Anim.Time = (DBL)(t.QuadPart - IK3_PauseTime - IK3_OldTime) / IK3_TimePerSec; } if (t.QuadPart - IK3_OldTimeFPS > IK3_TimePerSec); { CHAR str[100]; IK3_Anim.FPS = IK3_FrameCounter * IK3_TimePerSec / (DBL)(t.QuadPart - IK3_OldTimeFPS); IK3_OldTimeFPS = t.QuadPart; sprintf(str, "FPS: %.5f", IK3_Anim.FPS); SetWindowText(IK3_Anim.hWnd, str); IK3_FrameCounter = 0; } GetKeyboardState(IK3_Anim.Keys); for (i = 0; i < 256; i++) { IK3_Anim.Keys[i] >>= 7; if (!IK3_Anim.OldKeys[i] && IK3_Anim.Keys[i]) IK3_Anim.KeysClick[i] = TRUE; else IK3_Anim.KeysClick[i] = FALSE; } memcpy(IK3_Anim.OldKeys, IK3_Anim.Keys, 256); GetCursorPos(&pt); ScreenToClient(IK3_Anim.hWnd, &pt); IK3_Anim.Mdx = pt.x - IK3_Anim.Mx; IK3_Anim.Mdy = pt.y - IK3_Anim.My; IK3_Anim.Mdx = pt.x; IK3_Anim.Mdy = pt.y; /* Joystick */ if (joyGetNumDevs() > 0) { JOYCAPS jc; /* Get joystick info */ if (joyGetDevCaps(JOYSTICKID1, &jc, sizeof(jc)) == JOYERR_NOERROR) { JOYINFOEX ji; ji.dwSize = sizeof(JOYINFOEX); ji.dwFlags = JOY_RETURNALL; if (joyGetPosEx(JOYSTICKID1, &ji) == JOYERR_NOERROR) { /* Buttons */ for (i = 0; i < 32; i++) IK3_Anim.JBut[i] = (ji.dwButtons >> i) & 1; /* Axes */ IK3_Anim.JX = IK3_GET_JOYSTIC_AXIS(X); IK3_Anim.JY = IK3_GET_JOYSTIC_AXIS(Y); IK3_Anim.JZ = IK3_GET_JOYSTIC_AXIS(Z); IK3_Anim.JR = IK3_GET_JOYSTIC_AXIS(R); /* Point of view */ IK3_Anim.JPov = ji.dwPOV == 0xFFFF ? 0 : ji.dwPOV / 4500 + 1; } }
int __glutProcessDeviceEvents(XEvent * event) { #if !defined(_WIN32) GLUTwindow *window; /* XXX Ugly code fan out. */ /* Can't use switch/case since X Input event types are dynamic. */ if (__glutDeviceMotionNotify && event->type == __glutDeviceMotionNotify) { XDeviceMotionEvent *devmot = (XDeviceMotionEvent *) event; window = __glutGetWindow(devmot->window); if (window) { if (__glutTablet && devmot->deviceid == __glutTablet->device_id && window->tabletMotion) { tabletPosChange(window, devmot->first_axis, devmot->axes_count, devmot->axis_data); } else if (__glutDials && devmot->deviceid == __glutDials->device_id && window->dials) { int i, first = devmot->first_axis, count = devmot->axes_count; for (i = first; i < first + count; i++) window->dials(i + 1, normalizeDialAngle(i, devmot->axis_data[i - first])); } else if (__glutSpaceball && devmot->deviceid == __glutSpaceball->device_id) { /* XXX Assume that space ball motion events come in as all the first 6 axes. Assume first 3 axes are XYZ translations; second 3 axes are XYZ rotations. */ if (devmot->first_axis == 0 && devmot->axes_count == 6) { if (window->spaceMotion) window->spaceMotion( normalizeSpaceballDelta(0, devmot->axis_data[0]), normalizeSpaceballDelta(1, devmot->axis_data[1]), normalizeSpaceballDelta(2, devmot->axis_data[2])); if (window->spaceRotate) window->spaceRotate( normalizeSpaceballAngle(3, devmot->axis_data[3]), normalizeSpaceballAngle(4, devmot->axis_data[4]), normalizeSpaceballAngle(5, devmot->axis_data[5])); } } return 1; } } else if (__glutDeviceButtonPress && event->type == __glutDeviceButtonPress) { XDeviceButtonEvent *devbtn = (XDeviceButtonEvent *) event; window = __glutGetWindow(devbtn->window); if (window) { if (__glutTablet && devbtn->deviceid == __glutTablet->device_id && window->tabletButton && devbtn->first_axis == 0 && devbtn->axes_count == 2) { tabletPosChange(window, devbtn->first_axis, devbtn->axes_count, devbtn->axis_data); window->tabletButton(devbtn->button, GLUT_DOWN, window->tabletPos[0], window->tabletPos[1]); } else if (__glutDials && devbtn->deviceid == __glutDials->device_id && window->buttonBox) { window->buttonBox(devbtn->button, GLUT_DOWN); } else if (__glutSpaceball && devbtn->deviceid == __glutSpaceball->device_id && window->spaceButton) { window->spaceButton(devbtn->button, GLUT_DOWN); } return 1; } } else if (__glutDeviceButtonRelease && event->type == __glutDeviceButtonRelease) { XDeviceButtonEvent *devbtn = (XDeviceButtonEvent *) event; window = __glutGetWindow(devbtn->window); if (window) { if (__glutTablet && devbtn->deviceid == __glutTablet->device_id && window->tabletButton && devbtn->first_axis == 0 && devbtn->axes_count == 2) { tabletPosChange(window, devbtn->first_axis, devbtn->axes_count, devbtn->axis_data); window->tabletButton(devbtn->button, GLUT_UP, window->tabletPos[0], window->tabletPos[1]); } else if (__glutDials && devbtn->deviceid == __glutDials->device_id && window->buttonBox) { window->buttonBox(devbtn->button, GLUT_UP); } else if (__glutSpaceball && devbtn->deviceid == __glutSpaceball->device_id && window->spaceButton) { window->spaceButton(devbtn->button, GLUT_UP); } return 1; } } #else { JOYINFOEX info; JOYCAPS joyCaps; if (joyGetPosEx(JOYSTICKID1,&info) != JOYERR_NOERROR) { __glutHasJoystick = 1; joyGetDevCaps(JOYSTICKID1, &joyCaps, sizeof(joyCaps)); __glutNumJoystickButtons = joyCaps.wNumButtons; __glutNumJoystickAxes = joyCaps.wNumAxes; } else { __glutHasJoystick = 0; __glutNumJoystickButtons = 0; __glutNumJoystickAxes = 0; } #if 0 JOYINFOEX info; int njoyId = 0; int nConnected = 0; MMRESULT result; /* Loop through all possible joystick IDs until we get the error JOYERR_PARMS. Count the number of times we get JOYERR_NOERROR indicating an installed joystick driver with a joystick currently attached to the port. */ while ((result = joyGetPosEx(njoyId++,&info)) != JOYERR_PARMS) { if (result == JOYERR_NOERROR) { ++nConnected; /* The count of connected joysticks. */ } } #endif } #endif /* !_WIN32 */ return 0; }
bool joystick_button(int id, int buttonnum) { JOYINFOEX joyinfo; joyinfo.dwFlags = JOY_RETURNBUTTONS; joyGetPosEx(JOYSTICKID1 + id, &joyinfo); return (joyinfo.dwButtons & (JOY_BUTTON1 << buttonnum)); }
//----------------------------------------------------------------------------- // Function: IN_StartupJoystick // Parameters: Nothing // Returns: Nothing // Notes: Pause CD audio //----------------------------------------------------------------------------- void IN_StartupJoystick( void ) { int numdevs; JOYCAPS jc; MMRESULT mmr; cvar_t *cv; // joystick variables in_joystick = Cvar_Get( "in_joystick", "0", CVAR_ARCHIVE); joy_name = Cvar_Get( "joy_name", "joystick", 0 ); joy_advanced = Cvar_Get( "joy_advanced", "0", 0 ); joy_advaxisx = Cvar_Get( "joy_advaxisx", "0", 0 ); joy_advaxisy = Cvar_Get( "joy_advaxisy", "0", 0 ); joy_advaxisz = Cvar_Get( "joy_advaxisz", "0", 0 ); joy_advaxisr = Cvar_Get( "joy_advaxisr", "0", 0 ); joy_advaxisu = Cvar_Get( "joy_advaxisu", "0", 0 ); joy_advaxisv = Cvar_Get( "joy_advaxisv", "0", 0 ); joy_forwardthreshold = Cvar_Get( "joy_forwardthreshold", "0.15", 0 ); joy_sidethreshold = Cvar_Get( "joy_sidethreshold", "0.15", 0 ); joy_upthreshold = Cvar_Get( "joy_upthreshold", "0.15", 0 ); joy_pitchthreshold = Cvar_Get( "joy_pitchthreshold", "0.15", 0 ); joy_yawthreshold = Cvar_Get( "joy_yawthreshold", "0.15", 0 ); joy_forwardsensitivity = Cvar_Get( "joy_forwardsensitivity", "-1", 0 ); joy_sidesensitivity = Cvar_Get( "joy_sidesensitivity", "-1", 0 ); joy_upsensitivity = Cvar_Get( "joy_upsensitivity", "-1", 0 ); joy_pitchsensitivity = Cvar_Get( "joy_pitchsensitivity", "1", 0 ); joy_yawsensitivity = Cvar_Get( "joy_yawsensitivity", "-1", 0 ); Cmd_AddCommand( "joy_advancedupdate", Joy_AdvancedUpdate_f ); // assume no joystick joy_avail = false; // abort startup if user requests no joystick cv = Cvar_Get( "in_initjoy", "1", CVAR_ROM ); if( ! cv->value ) return; // verify joystick driver is present if( ( numdevs = joyGetNumDevs() ) == 0 ) { return; } // cycle through the joystick ids for the first valid one for( joy_id = 0; joy_id < numdevs; ++joy_id ) { memset( &ji, 0, sizeof( ji ) ); ji.dwSize = sizeof( ji ); ji.dwFlags = JOY_RETURNCENTERED; if( ( mmr = joyGetPosEx( joy_id, &ji ) ) == JOYERR_NOERROR ) break; } // abort startup if we didn't find a valid joystick if( mmr != JOYERR_NOERROR ) { Com_Printf( "\njoystick not found -- no valid joysticks (%x)\n\n", mmr ); return; } // get the capabilities of the selected joystick // abort startup if command fails memset( &jc, 0, sizeof( jc ) ); if( ( mmr = joyGetDevCaps( joy_id, &jc, sizeof( jc ) ) ) != JOYERR_NOERROR ) { Com_Printf( "\njoystick not found -- invalid joystick capabilities (%x)\n\n", mmr ); return; } // save the joystick's number of buttons and POV status joy_numbuttons = jc.wNumButtons; joy_haspov = jc.wCaps & JOYCAPS_HASPOV; // old button and POV states default to no buttons pressed joy_oldbuttonstate = joy_oldpovstate = 0; // mark the joystick as available and advanced initialization not completed // this is needed as cvars are not available during initialization joy_avail = true; joy_advancedinit = false; Com_Printf( "\njoystick detected\n\n" ); }
/* Функция построения кадра анимации. * АРГУМЕНТЫ: Нет. * ВОЗВРАЩАЕМОЕ ЗНАЧЕНИЕ: Нет. */ VOID OK2_AnimRender( VOID ) { INT i; LARGE_INTEGER li; POINT pt; /* Обновление ввода */ GetKeyboardState(OK2_Anim.Keys); for (i = 0; i < 256; i++) OK2_Anim.KeyClick[i] = OK2_Anim.Keys[i] && !OK2_Anim.KeysOld[i]; memcpy(OK2_Anim.KeysOld, OK2_Anim.Keys, sizeof(OK2_Anim.Keys)); /* Обновление таймера */ OK2_Anim.Time = (DBL)clock() / CLOCKS_PER_SEC; /* Обновление кадра */ QueryPerformanceCounter(&li); /* глобальное время */ OK2_Anim.GlobalTime = (DBL)(li.QuadPart - TimeStart) / TimeFreq; OK2_Anim.GlobalDeltaTime = (DBL)(li.QuadPart - TimeOld) / TimeFreq; /* локальное время */ if (OK2_Anim.IsPause) { TimePause += li.QuadPart - TimeOld; OK2_Anim.DeltaTime = 0; } else OK2_Anim.DeltaTime = OK2_Anim.GlobalDeltaTime; OK2_Anim.Time = (DBL)(li.QuadPart - TimeStart - TimePause) / TimeFreq; /* вычисляем FPS */ if (li.QuadPart - TimeFPS > TimeFreq) { OK2_Anim.FPS = FrameCounter / ((DBL)(li.QuadPart - TimeFPS) / TimeFreq); TimeFPS = li.QuadPart; FrameCounter = 0; } /* время "прошлого" кадра */ TimeOld = li.QuadPart; /* очистка фона */ SelectObject(OK2_Anim.hDC, GetStockObject(DC_BRUSH)); SelectObject(OK2_Anim.hDC, GetStockObject(NULL_PEN)); SetDCBrushColor(OK2_Anim.hDC, RGB(0, 110, 0)); Rectangle(OK2_Anim.hDC, 0, 0, OK2_Anim.W, OK2_Anim.H); /* опрос на изменение состояний объектов */ for (i = 0; i < OK2_Anim.NumOfUnits; i++) OK2_Anim.Units[i]->Response(OK2_Anim.Units[i], &OK2_Anim); /* рисование объектов */ for (i = 0; i < OK2_Anim.NumOfUnits; i++) { SelectObject(OK2_Anim.hDC, GetStockObject(DC_BRUSH)); SelectObject(OK2_Anim.hDC, GetStockObject(DC_PEN)); SetDCBrushColor(OK2_Anim.hDC, RGB(0, 0, 0)); SetDCPenColor(OK2_Anim.hDC, RGB(55, 155, 255)); OK2_Anim.Units[i]->Render(OK2_Anim.Units[i], &OK2_Anim); } /* Мышь */ /* колесо */ OK2_Anim.MsWheel = OK2_MouseGlobalWheel; OK2_MouseGlobalWheel = 0; /* абсолютная позиция */ pt.x = OK2_MouseGlobalX; pt.y = OK2_MouseGlobalY; ScreenToClient(OK2_Anim.hWnd, &pt); OK2_Anim.MsX = pt.x; OK2_Anim.MsY = pt.y; /* относительное перемещение */ OK2_Anim.MsDeltaX = OK2_MouseGlobalX - OK2_MouseXOld; OK2_Anim.MsDeltaY = OK2_MouseGlobalY - OK2_MouseYOld; OK2_MouseXOld = OK2_MouseGlobalX; OK2_MouseYOld = OK2_MouseGlobalY; /* Джойстик */ if ((i = joyGetNumDevs()) > 0) { JOYCAPS jc; /* получение общей информации о джостике */ if (joyGetDevCaps(JOYSTICKID1, &jc, sizeof(jc)) == JOYERR_NOERROR) { JOYINFOEX ji; /* получение текущего состояния */ ji.dwSize = sizeof(JOYCAPS); ji.dwFlags = JOY_RETURNALL; if (joyGetPosEx(JOYSTICKID1, &ji) == JOYERR_NOERROR) { /* Кнопки */ memcpy(OK2_Anim.JButsOld, OK2_Anim.JButs, sizeof(OK2_Anim.JButs)); for (i = 0; i < 32; i++) OK2_Anim.JButs[i] = (ji.dwButtons >> i) & 1; for (i = 0; i < 32; i++) OK2_Anim.JButsClick[i] = OK2_Anim.JButs[i] && !OK2_Anim.JButsOld[i]; /* Оси */ OK2_Anim.JX = 2.0 * (ji.dwXpos - jc.wXmin) / (jc.wXmax - jc.wXmin - 1) - 1; OK2_Anim.JY = 2.0 * (ji.dwYpos - jc.wYmin) / (jc.wYmax - jc.wYmin - 1) - 1; if (jc.wCaps & JOYCAPS_HASZ) OK2_Anim.JZ = 2.0 * (ji.dwZpos - jc.wZmin) / (jc.wZmax - jc.wZmin - 1) - 1; if (jc.wCaps & JOYCAPS_HASR) OK2_Anim.JR = 2.0 * (ji.dwRpos - jc.wRmin) / (jc.wRmax - jc.wRmin - 1) - 1; if (jc.wCaps & JOYCAPS_HASU) OK2_Anim.JU = 2.0 * (ji.dwUpos - jc.wUmin) / (jc.wUmax - jc.wUmin - 1) - 1; /* Point-Of-View */ if (jc.wCaps & JOYCAPS_HASPOV) { if (ji.dwPOV == 0xFFFF) OK2_Anim.JPOV = 0; else OK2_Anim.JPOV = ji.dwPOV / 4500 + 1; } }
GamePad::GamePad() : active(false), frequency(.01) { #if defined(WIN32) gamepad_fd = 0; for (int i = JOYSTICKID1 ; i < JOYSTICKID2 ; i++) { if(JOYERR_NOERROR == joyGetDevCaps(i, &caps, sizeof(JOYCAPS)) ) { /* std::cout << "Joystick/Gamepad detected: "<< name ; std::cout << " ( " << (int)caps.wNumAxes <<" axes /" ; std::cout << (int)caps.wNumButtons <<" buttons)" << std::endl; std::cout << " Driver name ....... " << caps.szPname << std::endl; std::cout << " Manufacturer ID ... " << caps.wMid << std::endl; std::cout << " Product ID ........ " << caps.wPid << std::endl; std::cout << " RegKey ............ " << caps.szRegKey << std::endl; std::cout << " OEMVxD ............ " << caps.szOEMVxD << std::endl; std::cout << " wXmin ............. " << caps.wXmin << std::endl; std::cout << " wXmax ............. " << caps.wXmax << std::endl; std::cout << " wYmin ............. " << caps.wYmin << std::endl; std::cout << " wYmax ............. " << caps.wYmax << std::endl; std::cout << " wZmin ............. " << caps.wZmin << std::endl; std::cout << " wZmax ............. " << caps.wZmax << std::endl; std::cout << " wPeriodMin ........ " << caps.wPeriodMin << std::endl; std::cout << " wPeriodMax ........ " << caps.wPeriodMax << std::endl; std::cout << " wRmin ............. " << caps.wRmin << std::endl; std::cout << " wRmax ............. " << caps.wRmax << std::endl; std::cout << " wUmin ............. " << caps.wUmin << std::endl; std::cout << " wUmax ............. " << caps.wUmax << std::endl; std::cout << " wVmin ............. " << caps.wVmin << std::endl; std::cout << " wVmax ............. " << caps.wVmax << std::endl; */ if((1./frequency < (double)caps.wPeriodMin) || (1./frequency > (double)caps.wPeriodMax)){ frequency=.1/(double)caps.wPeriodMin; } gamepad_fd = i; axes = std::min(GP_AXES,(int)caps.wNumAxes + 2); buttons = std::min(GP_BUTTONS,(int)caps.wNumButtons); for (int i = 0; i < buttons; i++) button[i] = false; for (int i = 0; i < axes; i++) axe[i] = 0.; active = true; } } for(int i = 0; i < buttons; i++) button[i] = 0; for(int i = 0; i < axes; i++) axe[i] = 0; joyGetPosEx(gamepad_fd, &infoex); infoex.dwFlags = JOY_RETURNALL; #elif defined(HAVE_LINUX_JOYSTICK) gamepad_fd = open(GAMEPAD_DEV, O_RDONLY | O_NONBLOCK); if (gamepad_fd > 0) { ioctl(gamepad_fd, JSIOCGNAME(256), name); ioctl(gamepad_fd, JSIOCGVERSION, &version); ioctl(gamepad_fd, JSIOCGAXES, &axes); ioctl(gamepad_fd, JSIOCGBUTTONS, &buttons); std::cout << "Joystick/Gamepad detected: " << name ; std::cout << " (version " << version << " / " ; std::cout << (int)axes <<" axes /" ; std::cout << (int)buttons <<" buttons)" << std::endl; active = true; } #endif if(active){ for (int i = 0; i < std::min(9, (int)buttons); i++) { button[i] = false;toggle_status[i] = false; } for (int i = 0; i < std::min(7, (int)axes); i++) axe[i] = 0.; for (int i = 0; i < std::min(9, (int)buttons); i++) button_map[i] = i; for (int i = 0; i < std::min(7, (int)axes); i++) axe_map[i] = i; axe_map[6]=1; // another recognized map "Thrustmaster Run'N' Drive Wireless PS3" // warning :: on Windows we dont have the human-friendly Model Name of the Gamepad if(strcmp(name, "Thrustmaster Run'N' Drive Wireless PS3") == 0){ button_map[0] = 1; button_map[1] = 0; button_map[5] = 6; button_map[6] = 5; } } }
/* joystick_win32_init: * Initialises the Win32 joystick driver. */ static int joystick_win32_init(void) { JOYCAPS caps; JOYINFOEX js; int n_joyat, n_joy, n_axis; win32_joy_num = joyGetNumDevs(); if (win32_joy_num > MAX_JOYSTICKS) _TRACE(PREFIX_W "The system supports more than %d joysticks\n", MAX_JOYSTICKS); /* retrieve joystick infos */ n_joy = 0; for (n_joyat = 0; n_joyat < win32_joy_num; n_joyat++) { if (n_joy == MAX_JOYSTICKS) break; if (joyGetDevCaps(n_joyat, &caps, sizeof(caps)) == JOYERR_NOERROR) { /* is the joystick physically attached? */ js.dwSize = sizeof(js); js.dwFlags = JOY_RETURNALL; if (joyGetPosEx(n_joyat, &js) == JOYERR_UNPLUGGED) continue; memset(&win32_joystick[n_joy], 0, sizeof(struct WIN32_JOYSTICK_INFO)); /* set global properties */ win32_joystick[n_joy].device = n_joyat; win32_joystick[n_joy].caps = caps.wCaps; win32_joystick[n_joy].num_buttons = MIN(caps.wNumButtons, MAX_JOYSTICK_BUTTONS); win32_joystick[n_joy].num_axes = MIN(caps.wNumAxes, WINDOWS_MAX_AXES); /* fill in ranges of axes */ win32_joystick[n_joy].axis_min[0] = caps.wXmin; win32_joystick[n_joy].axis_max[0] = caps.wXmax; win32_joystick[n_joy].axis_min[1] = caps.wYmin; win32_joystick[n_joy].axis_max[1] = caps.wYmax; n_axis = 2; if (caps.wCaps & JOYCAPS_HASZ) { win32_joystick[n_joy].axis_min[2] = caps.wZmin; win32_joystick[n_joy].axis_max[2] = caps.wZmax; n_axis++; } if (caps.wCaps & JOYCAPS_HASR) { win32_joystick[n_joy].axis_min[n_axis] = caps.wRmin; win32_joystick[n_joy].axis_max[n_axis] = caps.wRmax; n_axis++; } if (caps.wCaps & JOYCAPS_HASU) { win32_joystick[n_joy].axis_min[n_axis] = caps.wUmin; win32_joystick[n_joy].axis_max[n_axis] = caps.wUmax; n_axis++; } if (caps.wCaps & JOYCAPS_HASV) { win32_joystick[n_joy].axis_min[n_axis] = caps.wVmin; win32_joystick[n_joy].axis_max[n_axis] = caps.wVmax; n_axis++; } /* register this joystick */ if (win_add_joystick((WINDOWS_JOYSTICK_INFO *)&win32_joystick[n_joy]) != 0) break; n_joy++; } } win32_joy_num = n_joy; return (win32_joy_num == 0); }
// -------------------------------------------------------------------------- // main(Number of arguments, Value of arguments) // This is the main function. // Return value Success:0 Error:-1 // -------------------------------------------------------------------------- int main(int argc, char **argv) { // AR.Drone class ARDrone ardrone; // Initialize if (!ardrone.open()) { printf("Failed to initialize.\n"); return -1; } // Main loop while (!GetAsyncKeyState(VK_ESCAPE)) { // Update your AR.Drone if (!ardrone.update()) break; // Get an image IplImage *image = ardrone.getImage(); // Battery printf("ardrone.battery = %d [��] (�c���%d��)\n", battery, 12*battery/100); // Take off / Landing if (KEY_PUSH(VK_SPACE)) { if (ardrone.onGround()) ardrone.takeoff(); else ardrone.landing(); } // Emergency stop if (KEY_PUSH(VK_RETURN)) ardrone.emergency(); // AR.Drone is flying if (!ardrone.onGround()) { double x = 0.0, y = 0.0, z = 0.0, r = 0.0; // Keyboard if (KEY_DOWN(VK_UP)) x = 0.5; if (KEY_DOWN(VK_DOWN)) x = -0.5; if (KEY_DOWN(VK_LEFT)) r = 0.5; if (KEY_DOWN(VK_RIGHT)) r = -0.5; if (KEY_DOWN('Q')) z = 0.5; if (KEY_DOWN('A')) z = -0.5; // Joypad JOYINFOEX JoyInfoEx; JoyInfoEx.dwSize = sizeof(JOYINFOEX); JoyInfoEx.dwFlags = JOY_RETURNALL; // Get joypad infomations if (joyGetPosEx(0, &JoyInfoEx) == JOYERR_NOERROR) { int y_pad = -((int)JoyInfoEx.dwXpos - 0x7FFF) / 32512.0*100.0; int x_pad = -((int)JoyInfoEx.dwYpos - 0x7FFF) / 32512.0*100.0; int r_pad = -((int)JoyInfoEx.dwZpos - 0x7FFF) / 32512.0*100.0; int z_pad = ((int)JoyInfoEx.dwRpos - 0x7FFF) / 32512.0*100.0; printf("X = %d ", x_pad); printf("Y = %d ", y_pad); printf("Z = %d ", z_pad); printf("R = %d\n", r_pad); x = 0.5 * x_pad / 100; y = 0.5 * y_pad / 100; z = 0.5 * z_pad / 100; r = 0.5 * r_pad / 100; if (JoyInfoEx.dwButtons & JOY_BUTTON1) ardrone.takeoff(); if (JoyInfoEx.dwButtons & JOY_BUTTON2) ardrone.landing(); } // Move ardrone.move3D(x, y, z, r); } // Display the image cvShowImage("camera", image); cvWaitKey(1); } // See you ardrone.close(); return 0; }
/* joystick_win32_poll: * Polls the Win32 joystick devices. */ static int joystick_win32_poll(void) { int n_joy, n_axis, n_but, p, range; JOYINFOEX js; for (n_joy = 0; n_joy < win32_joy_num; n_joy++) { js.dwSize = sizeof(js); js.dwFlags = JOY_RETURNALL; if (joyGetPosEx(win32_joystick[n_joy].device, &js) == JOYERR_NOERROR) { /* axes */ win32_joystick[n_joy].axis[0] = js.dwXpos; win32_joystick[n_joy].axis[1] = js.dwYpos; n_axis = 2; if (win32_joystick[n_joy].caps & JOYCAPS_HASZ) { win32_joystick[n_joy].axis[n_axis] = js.dwZpos; n_axis++; } if (win32_joystick[n_joy].caps & JOYCAPS_HASR) { win32_joystick[n_joy].axis[n_axis] = js.dwRpos; n_axis++; } if (win32_joystick[n_joy].caps & JOYCAPS_HASU) { win32_joystick[n_joy].axis[n_axis] = js.dwUpos; n_axis++; } if (win32_joystick[n_joy].caps & JOYCAPS_HASV) { win32_joystick[n_joy].axis[n_axis] = js.dwVpos; n_axis++; } /* map Windows axis range to 0-256 Allegro range */ for (n_axis = 0; n_axis < win32_joystick[n_joy].num_axes; n_axis++) { p = win32_joystick[n_joy].axis[n_axis] - win32_joystick[n_joy].axis_min[n_axis]; range = win32_joystick[n_joy].axis_max[n_axis] - win32_joystick[n_joy].axis_min[n_axis]; if (range > 0) win32_joystick[n_joy].axis[n_axis] = p * 256 / range; else win32_joystick[n_joy].axis[n_axis] = 0; } /* hat */ if (win32_joystick[n_joy].caps & JOYCAPS_HASPOV) win32_joystick[n_joy].hat = js.dwPOV; /* buttons */ for (n_but = 0; n_but < win32_joystick[n_joy].num_buttons; n_but++) win32_joystick[n_joy].button[n_but] = ((js.dwButtons & (1 << n_but)) != 0); } else { for(n_axis = 0; n_axis<win32_joystick[n_joy].num_axes; n_axis++) win32_joystick[n_joy].axis[n_axis] = 0; if (win32_joystick[n_joy].caps & JOYCAPS_HASPOV) win32_joystick[n_joy].hat = 0; for (n_but = 0; n_but < win32_joystick[n_joy].num_buttons; n_but++) win32_joystick[n_joy].button[n_but] = FALSE; } win_update_joystick_status(n_joy, (WINDOWS_JOYSTICK_INFO *)&win32_joystick[n_joy]); } return 0; }
void ArJoyHandler::getData(void) { int x, y, z, r, u, v; if (!myFirstData && myLastDataGathered.mSecSince() < 5) return; myLastDataGathered.setToNow(); JOYINFOEX joyInfoEx; joyInfoEx.dwFlags = JOY_RETURNALL; joyInfoEx.dwSize = sizeof(joyInfoEx); MMRESULT joyResult = joyGetPosEx(myJoyID,&joyInfoEx); std::string axes; /* if (myFirstData) { if (joyInfoEx.dwFlags & JOY_RETURNR) axes += " r "; if (joyInfoEx.dwFlags & JOY_RETURNU) axes += " u "; if (joyInfoEx.dwFlags & JOY_RETURNV) axes += " v "; if (joyInfoEx.dwFlags & JOY_RETURNX) axes += " x "; if (joyInfoEx.dwFlags & JOY_RETURNY) axes += " y "; if (joyInfoEx.dwFlags & JOY_RETURNZ) axes += " z "; ArLog::log(ArLog::Normal, "Axes %s\n", axes.c_str()); } */ myFirstData = false; if (joyResult == JOYERR_NOERROR) { // KMC: I don't understand this logic... The spec says that // getAxis returns a number between -1 and 1; the getAxis method // multiplies the contents of myAxes by 128. The logic below // however seems to double everything... /**/ x = (int)(joyInfoEx.dwXpos*256.0/myPhysMax)-128; y = (int)-((joyInfoEx.dwYpos*256.0/myPhysMax)-128); z = (int)-((joyInfoEx.dwZpos*256.0/myPhysMax)-128); r = (int)-((joyInfoEx.dwRpos*256.0/myPhysMax)-128); u = (int)-((joyInfoEx.dwUpos*256.0/myPhysMax)-128); v = (int)-((joyInfoEx.dwVpos*256.0/myPhysMax)-128); /***/ /*** x = (int) 128 * ((2.0 * (double) myJoyInfo.wXpos / (double) myPhysMax) - 1); y = (int)-128 * ((2.0 * (double) myJoyInfo.wYpos / (double) myPhysMax) - 1); z = (int)-128 * ((2.0 * (double) myJoyInfo.wZpos / (double) myPhysMax) - 1); **/ if (myLastZ != z) myHaveZ = true; if (x > myMaxX) myMaxX = x; if (x < myMinX) myMinX = x; if (y > myMaxY) myMaxY = y; if (y < myMinY) myMinY = y; myAxes[1] = x; myAxes[2] = y; myAxes[3] = z; myAxes[4] = r; myAxes[5] = u; myAxes[6] = v; myLastZ = z; /* This seems off by one on the dual controller myButtons[1] = (joyInfoEx.dwButtons & 0x1); myButtons[2] = (joyInfoEx.dwButtons & 0x2); myButtons[3] = (joyInfoEx.dwButtons & 0x4); myButtons[4] = (joyInfoEx.dwButtons & 0x8); myButtons[5] = (joyInfoEx.dwButtons & 0xf); myButtons[6] = (joyInfoEx.dwButtons & 0x10); myButtons[7] = (joyInfoEx.dwButtons & 0x20); myButtons[8] = (joyInfoEx.dwButtons & 0x40); myButtons[9] = (joyInfoEx.dwButtons & 0x80); myButtons[10] = (joyInfoEx.dwButtons & 0xf0); myButtons[11] = (joyInfoEx.dwButtons & 0x100); myButtons[12] = (joyInfoEx.dwButtons & 0x200); */ myButtons[1] = (joyInfoEx.dwButtons & JOY_BUTTON1); myButtons[2] = (joyInfoEx.dwButtons & JOY_BUTTON2); myButtons[3] = (joyInfoEx.dwButtons & JOY_BUTTON3); myButtons[4] = (joyInfoEx.dwButtons & JOY_BUTTON4); myButtons[5] = (joyInfoEx.dwButtons & JOY_BUTTON5); myButtons[6] = (joyInfoEx.dwButtons & JOY_BUTTON6); myButtons[7] = (joyInfoEx.dwButtons & JOY_BUTTON7); myButtons[8] = (joyInfoEx.dwButtons & JOY_BUTTON8); myButtons[9] = (joyInfoEx.dwButtons & JOY_BUTTON9); myButtons[10] = (joyInfoEx.dwButtons & JOY_BUTTON10); myButtons[11] = (joyInfoEx.dwButtons & JOY_BUTTON11); myButtons[12] = (joyInfoEx.dwButtons & JOY_BUTTON12); } else //(joyResult == JOYERR_UNPLUGGED) { myAxes[1] = 0; myAxes[2] = 0; myAxes[3] = 0; myAxes[4] = 0; myAxes[5] = 0; myAxes[6] = 0; myButtons[1] = 0; myButtons[2] = 0; myButtons[3] = 0; myButtons[4] = 0; myButtons[5] = 0; myButtons[6] = 0; myButtons[7] = 0; myButtons[8] = 0; myButtons[9] = 0; myButtons[10] = 0; myButtons[11] = 0; myButtons[12] = 0; // Reset the initialized flag so that the joystick button in the GUI // will be disabled. myInitialized = false; } }
/* Функция построения кадра анимации. * АРГУМЕНТЫ: Нет. * ВОЗВРАЩАЕМОЕ ЗНАЧЕНИЕ: Нет. */ VOID VG4_AnimRender( VOID ) { INT i; LARGE_INTEGER li; POINT pt; /*** Обновление ввода ***/ /* Клавиатура */ GetKeyboardState(VG4_Anim.Keys); for (i = 0; i < 256; i++) VG4_Anim.Keys[i] >>= 7; for (i = 0; i < 256; i++) VG4_Anim.KeysClick[i] = VG4_Anim.Keys[i] && !VG4_Anim.KeysOld[i]; memcpy(VG4_Anim.KeysOld, VG4_Anim.Keys, 256); /* Мышь */ /* колесо */ VG4_Anim.MsWheel = VG4_MouseGlobalWheel; VG4_MouseGlobalWheel = 0; /* абсолютная позиция */ pt.x = VG4_MouseGlobalX; pt.y = VG4_MouseGlobalY; ScreenToClient(VG4_Anim.hWnd, &pt); VG4_Anim.MsX = pt.x; VG4_Anim.MsY = pt.y; /* относительное перемещение */ VG4_Anim.MsDeltaX = VG4_MouseGlobalX - VG4_MouseXOld; VG4_Anim.MsDeltaY = VG4_MouseGlobalY - VG4_MouseYOld; VG4_MouseXOld = VG4_MouseGlobalX; VG4_MouseYOld = VG4_MouseGlobalY; /* Джойстик */ if ((i = joyGetNumDevs()) > 0) { JOYCAPS jc; /* получение общей информации о джостике */ if (joyGetDevCaps(JOYSTICKID1, &jc, sizeof(jc)) == JOYERR_NOERROR) { JOYINFOEX ji; /* получение текущего состояния */ ji.dwSize = sizeof(JOYCAPS); ji.dwFlags = JOY_RETURNALL; if (joyGetPosEx(JOYSTICKID1, &ji) == JOYERR_NOERROR) { /* Кнопки */ memcpy(VG4_Anim.JButsOld, VG4_Anim.JButs, sizeof(VG4_Anim.JButs)); for (i = 0; i < 32; i++) VG4_Anim.JButs[i] = (ji.dwButtons >> i) & 1; for (i = 0; i < 32; i++) VG4_Anim.JButsClick[i] = VG4_Anim.JButs[i] && !VG4_Anim.JButsOld[i]; /* Оси */ VG4_Anim.JX = VG4_GET_AXIS_VALUE(X); VG4_Anim.JY = VG4_GET_AXIS_VALUE(Y); if (jc.wCaps & JOYCAPS_HASZ) VG4_Anim.JZ = VG4_GET_AXIS_VALUE(Z); if (jc.wCaps & JOYCAPS_HASR) VG4_Anim.JR = VG4_GET_AXIS_VALUE(R); if (jc.wCaps & JOYCAPS_HASU) VG4_Anim.JU = 2.0 * (ji.dwUpos - jc.wUmin) / (jc.wUmax - jc.wUmin - 1) - 1; /* Point-Of-View */ if (jc.wCaps & JOYCAPS_HASPOV) { if (ji.dwPOV == 0xFFFF) VG4_Anim.JPOV = 0; else VG4_Anim.JPOV = ji.dwPOV / 4500 + 1; } }
bool MWindow::onEvents(void) { JOYCAPS caps; MWindow * window = MWindow::getInstance(); // joystick 1 if(joyGetDevCaps(JOYSTICKID1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) { JOYINFOEX joyinfo; MJoystick * joystick = window->getJoystick1(); joyinfo.dwSize = sizeof(JOYINFOEX); memset(&(joyinfo.dwFlags), 0, sizeof(JOYINFOEX) - sizeof(DWORD)); joyinfo.dwFlags = JOY_RETURNALL; joyGetPosEx(JOYSTICKID1, &joyinfo); updateJoystick(joystick, &caps, &joyinfo); MWinEvent events; events.type = MWIN_EVENT_JOYSTICK1_UPDATE; window->sendEvents(&events); joystick->flush(); } // joystick 2 if(joyGetDevCaps(JOYSTICKID2, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) { JOYINFOEX joyinfo; MJoystick * joystick = window->getJoystick2(); joyinfo.dwSize = sizeof(JOYINFOEX); memset(&(joyinfo.dwFlags), 0, sizeof(JOYINFOEX) - sizeof(DWORD)); joyinfo.dwFlags = JOY_RETURNALL; joyGetPosEx(JOYSTICKID2, &joyinfo); updateJoystick(joystick, &caps, &joyinfo); MWinEvent events; events.type = MWIN_EVENT_JOYSTICK2_UPDATE; window->sendEvents(&events); joystick->flush(); } // mouse move event { POINT p; MWinEvent events; MMouse * mouse = MMouse::getInstance(); GetCursorPos(&p); p.x -= window->getXPosition(); p.y -= window->getYPosition(); int dx = p.x - mouse->getXPosition(); int dy = p.y - mouse->getYPosition(); if((dx != 0) || (dy != 0)) { mouse->setPosition(p.x, p.y); events.type = MWIN_EVENT_MOUSE_MOVE; events.data[0] = p.x; events.data[1] = p.y; window->sendEvents(&events); } } MSG msg; if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); return false; } return true; }
/* Функция построения кадра. * АРГУМЕНТЫ: Нет. * ВОЗВРАЩАЕМОЕ ЗНАЧЕНИЕ: Нет. */ VOID VG4_AnimRender( VOID ) { INT i; LARGE_INTEGER li; POINT pt; if (!VG4_IsInit) return; /*** Опрос таймера ***/ /* обновляем информацию: */ QueryPerformanceCounter(&li); /* глобальное время */ VG4_Anim.GlobalTime = (DBL)(li.QuadPart - VG4_TimeStart) / VG4_TimeFreq; VG4_Anim.GlobalDeltaTime = (DBL)(li.QuadPart - VG4_TimeOld) / VG4_TimeFreq; /* локальное время */ if (VG4_Anim.IsPause) VG4_Anim.DeltaTime = 0, VG4_TimePause += li.QuadPart - VG4_TimeOld; else VG4_Anim.DeltaTime = VG4_Anim.GlobalDeltaTime; VG4_Anim.Time = (DBL)(li.QuadPart - VG4_TimeStart - VG4_TimePause - VG4_TimeShift) / VG4_TimeFreq; /* вычисляем производительность */ if (li.QuadPart - VG4_TimeFPS > VG4_TimeFreq * 3.0) { VG4_Anim.FPS = VG4_FrameCounter / ((DBL)(li.QuadPart - VG4_TimeFPS) / VG4_TimeFreq); VG4_TimeFPS = li.QuadPart; VG4_FrameCounter = 0; } /* обновляем время "старого" кадра */ VG4_TimeOld = li.QuadPart; /*** Опрос устройств ввода ***/ /* клавиатура */ /* копируем старое состояние */ memcpy(VG4_Anim.KeysOld, VG4_Anim.Keys, 256); /* Считываем новое */ GetKeyboardState(VG4_Anim.Keys); /* конвертироем в Да,Нет */ for (i = 0; i < 256; i++) VG4_Anim.Keys[i] >>= 7; /* мышь */ GetCursorPos(&pt); ScreenToClient(VG4_Anim.hWnd, &pt); VG4_Anim.MouseX = pt.x; VG4_Anim.MouseY = pt.y; VG4_Anim.MouseBut[0] = VG4_Anim.Keys[VK_LBUTTON]; VG4_Anim.MouseBut[1] = VG4_Anim.Keys[VK_RBUTTON]; VG4_Anim.MouseBut[2] = VG4_Anim.Keys[VK_MBUTTON]; VG4_Anim.MouseDeltaX = pt.x - VG4_MousePosOld.x; VG4_Anim.MouseDeltaY = pt.y - VG4_MousePosOld.y; VG4_Anim.MouseWheel = VG4_MouseWheel; VG4_MouseWheel = 0; VG4_MousePosOld = pt; /* джойстик */ if ((i = joyGetNumDevs()) > 1) { JOYCAPS jc; if (joyGetDevCaps(JOYSTICKID1, &jc, sizeof(jc)) == JOYERR_NOERROR) { JOYINFOEX ji; ji.dwSize = sizeof(ji); ji.dwFlags = JOY_RETURNALL; if (joyGetPosEx(JOYSTICKID1, &ji) == JOYERR_NOERROR) { /* кнопки */ memcpy(VG4_Anim.JButOld, VG4_Anim.JBut, 32); for (i = 0; i < 32; i++) VG4_Anim.JBut[i] = (ji.dwButtons >> i) & 1; /* оси переводим в диапазон -1..1 */ VG4_Anim.Jx = 2.0 * (ji.dwXpos - jc.wXmin) / (jc.wXmax - jc.wXmin) - 1; VG4_Anim.Jy = 2.0 * (ji.dwYpos - jc.wYmin) / (jc.wYmax - jc.wYmin) - 1; VG4_Anim.Jz = 2.0 * (ji.dwZpos - jc.wZmin) / (jc.wZmax - jc.wZmin) - 1; VG4_Anim.Jr = 2.0 * (ji.dwRpos - jc.wRmin) / (jc.wRmax - jc.wRmin) - 1; if (ji.dwPOV == 0xFFFF) VG4_Anim.JPov = 0; else VG4_Anim.JPov = ji.dwPOV / 4500 + 1; }
// -------------------------------------------------------------- // joy_process() // // Runs as a separate thread, and updates the state of the joystick // DWORD joy_process(DWORD lparam) { MMRESULT rs; JOYINFOEX ji; int i,state; joy_button_info *bi; for ( i = 0; i < JOY_TOTAL_BUTTONS; i++) { bi = &joy_buttons[i]; bi->actual_state = 0; // Don't set in flush code! bi->state = 0; bi->down_count = 0; bi->up_count = 0; bi->down_time = 0; bi->last_down_check = timer_get_milliseconds(); } while (1) { // Wait for the thread to be signaled to end or 1/18th of a second to pass... if ( WaitForSingleObject( Joy_tell_thread_to_end_event, joy_pollrate )==WAIT_OBJECT_0) { break; } memset(&ji, 0, sizeof(ji)); ji.dwSize = sizeof(ji); // ji.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNRAWDATA; ji.dwFlags = JOY_RETURNALL; EnterCriticalSection(&joy_lock); uint joy_state = 0; if (Cur_joystick >= 0) { rs = joyGetPosEx(Cur_joystick, &ji); // If there's an error, assume all buttons down. if (rs == JOYERR_NOERROR) { joy_state = ji.dwButtons; } } // Process ji.dwButtons for (i=0; i<JOY_TOTAL_BUTTONS; i++) { state = 0; if (i < JOY_NUM_BUTTONS) { state = joy_state & (1<<i); } else { // check for hat presses, which act like buttons switch (i) { case JOY_HATBACK: if (ji.dwPOV == JOY_POVBACKWARD) state = 1; break; case JOY_HATFORWARD: if (ji.dwPOV == JOY_POVFORWARD) state = 1; break; case JOY_HATLEFT: if (ji.dwPOV == JOY_POVLEFT) state = 1; break; case JOY_HATRIGHT: if (ji.dwPOV == JOY_POVRIGHT) state = 1; break; default: Int3(); // should never happen break; } // end switch } // end if if (state != joy_buttons[i].actual_state) { // Button position physically changed. joy_buttons[i].actual_state = state; if ( state ) { // went from up to down joy_buttons[i].down_count++; joy_buttons[i].down_time = 0; joy_buttons[i].state = 1; //////////////////////////// /// SOMETHING TERRIBLE IS ABOUT TO HAPPEN. I FEEL THIS IS NECESSARY FOR THE DEMO, SINCE /// I DON'T WANT TO CALL CRITICAL SECTION CODE EACH FRAME TO CHECK ALL THE JOYSTICK BUTTONS. /// PLEASE SEE ALAN FOR MORE INFORMATION. //////////////////////////// #ifdef FS2_DEMO { extern void demo_reset_trailer_timer(); demo_reset_trailer_timer(); } #endif //////////////////////////// /// IT'S OVER. SEE, IT WASN'T SO BAD RIGHT? IT'S IS VERY UGLY LOOKING, I KNOW. //////////////////////////// } else { // went from down to up if ( joy_buttons[i].state ) { joy_buttons[i].up_count++; } joy_buttons[i].state = 0; } } else { // Didn't move... increment time down if down. if (joy_buttons[i].state) { joy_buttons[i].down_time += joy_pollrate; } } } // end for LeaveCriticalSection(&joy_lock); } SetEvent(Joy_thread_says_its_done_event); return 0; }