[Opentnl-cvs] tnl/zap winJoystick.cpp,1.8,1.9
Brought to you by:
mark_frohnmayer,
s_alanet
From: Mark F. <mar...@us...> - 2004-05-12 01:56:52
|
Update of /cvsroot/opentnl/tnl/zap In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31749/zap Modified Files: winJoystick.cpp Log Message: Added joystick move journaling Updated macros for when journaling is compiled out Index: winJoystick.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/zap/winJoystick.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** winJoystick.cpp 9 May 2004 22:17:28 -0000 1.8 --- winJoystick.cpp 12 May 2004 01:56:42 -0000 1.9 *************** *** 33,36 **** --- 33,37 ---- #include "point.h" #include "UI.h" + #include "tnlJournal.h" namespace Zap *************** *** 117,208 **** } ! void JoystickUpdateMove( Move *theMove ) { ! DIJOYSTATE2 js; // DInput joystick state ! if(!gJoystick) ! return; ! if(FAILED(gJoystick->Poll() ) ) ! { ! HRESULT hr; ! hr = gJoystick->Acquire(); ! ! while( hr == DIERR_INPUTLOST ) ! hr = gJoystick->Acquire(); ! return; ! } ! // Get the input's device state ! if(FAILED(gJoystick->GetDeviceState( sizeof(DIJOYSTATE2), &js ) ) ) ! return; // The device should have been acquired during the Poll() ! S32 x = js.lX; ! F32 deadZone = 8192.0f; ! F32 controls[4]; ! controls[0] = F32( js.lX ) - 32768.0f; ! controls[1] = F32( js.lY ) - 32768.0f; ! if(gJoystickType == 0) ! { controls[2] = F32( js.lRz ) - 32768.0f; controls[3] = F32( js.rglSlider[0] ) - 32768.0f; ! } ! else if(gJoystickType == 1) ! { controls[3] = F32( js.lZ ) - 32768.0f; controls[2] = F32( js.lRz ) - 32768.0f; ! } ! for(U32 i = 0; i < 4; i++) ! { ! if(controls[i] < -deadZone) ! controls[i] = -(-controls[i] - deadZone) / F32(32768.0f - deadZone); ! else if(controls[i] > deadZone) ! controls[i] = (controls[i] - deadZone) / F32(32768.0f - deadZone); ! else ! controls[i] = 0; ! } ! if(controls[0] < 0) ! { ! theMove->left = -controls[0]; ! theMove->right = 0; ! } ! else ! { ! theMove->left = 0; ! theMove->right = controls[0]; ! } ! if(controls[1] < 0) ! { ! theMove->up = -controls[1]; ! theMove->down = 0; ! } ! else ! { ! theMove->down = controls[1]; ! theMove->up = 0; ! } ! Point p(controls[2], controls[3]); ! if(p.len() > 0.1) ! { ! theMove->angle = atan2(p.x, p.y); ! theMove->fire = true; ! } ! else ! theMove->fire = false; - static bool buttonDown[12] = { 0, }; // check the state of the buttons: for( U32 i = 0; i < 12; i++ ) { ! bool buttonPressed = (js.rgbButtons[i] & 0x80) != 0; ! if(buttonPressed && !buttonDown[i]) UserInterface::current->onControllerButtonDown(i); ! else if(!buttonPressed && buttonDown[i]) UserInterface::current->onControllerButtonUp(i); ! buttonDown[i] = buttonPressed; } } --- 118,252 ---- } ! ! static bool updateMoveInternal( Move *theMove, bool buttonPressed[12] ) { ! DIJOYSTATE2 js; // DInput joystick state ! if(!gJoystick) ! return false; ! if(FAILED(gJoystick->Poll() ) ) ! { ! HRESULT hr; ! hr = gJoystick->Acquire(); ! ! while( hr == DIERR_INPUTLOST ) ! hr = gJoystick->Acquire(); ! return false; ! } ! // Get the input's device state ! if(FAILED(gJoystick->GetDeviceState( sizeof(DIJOYSTATE2), &js ) ) ) ! return false; // The device should have been acquired during the Poll() ! S32 x = js.lX; ! F32 deadZone = 8192.0f; ! F32 controls[4]; ! controls[0] = F32( js.lX ) - 32768.0f; ! controls[1] = F32( js.lY ) - 32768.0f; ! if(gJoystickType == 0) ! { controls[2] = F32( js.lRz ) - 32768.0f; controls[3] = F32( js.rglSlider[0] ) - 32768.0f; ! } ! else if(gJoystickType == 1) ! { controls[3] = F32( js.lZ ) - 32768.0f; controls[2] = F32( js.lRz ) - 32768.0f; ! } ! for(U32 i = 0; i < 4; i++) ! { ! if(controls[i] < -deadZone) ! controls[i] = -(-controls[i] - deadZone) / F32(32768.0f - deadZone); ! else if(controls[i] > deadZone) ! controls[i] = (controls[i] - deadZone) / F32(32768.0f - deadZone); ! else ! controls[i] = 0; ! } ! if(controls[0] < 0) ! { ! theMove->left = -controls[0]; ! theMove->right = 0; ! } ! else ! { ! theMove->left = 0; ! theMove->right = controls[0]; ! } ! if(controls[1] < 0) ! { ! theMove->up = -controls[1]; ! theMove->down = 0; ! } ! else ! { ! theMove->down = controls[1]; ! theMove->up = 0; ! } ! Point p(controls[2], controls[3]); ! if(p.len() > 0.1) ! { ! theMove->angle = atan2(p.x, p.y); ! theMove->fire = true; ! } ! else ! theMove->fire = false; // check the state of the buttons: for( U32 i = 0; i < 12; i++ ) + buttonPressed[i] = (js.rgbButtons[i] & 0x80) != 0; + + return true; + } + + static bool updateMoveInternalJournaled( Move *theMove, bool buttonPressed[12] ) + { + TNL_JOURNAL_READ_BLOCK(3000, + BitStream *readStream = Journal::getReadStream(); + if(!readStream->readFlag()) + return false; + + Move aMove; + aMove.unpack(readStream); + *theMove = aMove; + for(U32 i = 0; i < 12; i++) + buttonPressed[i] = readStream->readFlag(); + return true; + ) + + bool ret = updateMoveInternal(theMove, buttonPressed); + + TNL_JOURNAL_WRITE_BLOCK(3000, + BitStream *writeStream = Journal::getWriteStream(); + if(writeStream->writeFlag(ret)) + { + Move dummy; + theMove->pack(writeStream, &dummy); + for(U32 i = 0;i < 12; i++) + writeStream->writeFlag(buttonPressed[i]); + } + ) + return ret; + } + + void JoystickUpdateMove( Move *theMove ) + { + static bool buttonDown[12] = { 0, }; + bool buttonPressed[12]; + + if(!updateMoveInternalJournaled( theMove, buttonPressed )) + return; + + for(U32 i = 0; i < 12; i++) { ! if(buttonPressed[i] && !buttonDown[i]) UserInterface::current->onControllerButtonDown(i); ! else if(!buttonPressed[i] && buttonDown[i]) UserInterface::current->onControllerButtonUp(i); ! buttonDown[i] = buttonPressed[i]; } } |