Skip to content

Commit

Permalink
Bugfix, joystick reconnection support
Browse files Browse the repository at this point in the history
  • Loading branch information
zaps166 committed Mar 16, 2015
1 parent b7f0bf3 commit 66b0796
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ Need For Speed II SE - Linux port with 3D acceleration and TCP protocol!
* spanish,
* swedish.
* Run the game. Go to Options->Graphics and set all to maximum.
* On Linux the game saves files in "~/.nfs2se". At the first run, "nfs2se.conf" will be copied into "~/.nfs2se".
* If you want to configure the game, go to "~/.nfs2se" directory, edit "nfs2se.conf" and modify what you want.
* On Linux the game saves files in "~/.nfs2se". At the firs run, "nfs2se.conf" will be copied into "~/.nfs2se".
* The game crashes without original game data.

## What works:

* Game controllers (reconnected game controllers should be the same),
* Force Feedback (tested on Linux),
* TCP and UDP connection,
* Serial port connection,
* Game controllers,
* Brightness,
* Sound.

## What does not work:

* Modem connection (it will never work again, this feature was removed from assembly code),
* Force Feedback on Window$ may not work.
* Force Feedback on Window$ may not work due to bugs in SDL2.

## Patches:

Expand Down
8 changes: 5 additions & 3 deletions compile_nfs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/bin/bash

CPU_FLAGS='-mno-sse2 -mno-sse -mno-mmx'
DEBUG=0

echo -n "Building"
if [[ $DEBUG == 1 ]]; then
if [[ $1 == "debug" ]]; then
shift
echo -n " debug"
WIN_SUBSYSTEM='console'
DEBUG_ASM='-g dwarf2'
C_FLAGS='-g'
else
echo -n " release"
WIN_SUBSYSTEM='windows'
C_FLAGS='-O2'
STRIP='-s'
fi
Expand All @@ -24,7 +26,7 @@ if [[ $1 == "win32" ]]; then
i686-w64-mingw32-gcc -Wall $C_FLAGS $CPU_FLAGS -c *.c &&
yasm -f win32 NFS2SE.asm -o NFS2SE.o $DEBUG_ASM --prefix=_ &&
yasm -f win32 EAcsnd.asm -o EAcsnd.asm.o $DEBUG_ASM --prefix=_ &&
i686-w64-mingw32-ld --enable-stdcall-fixup -o "../Need For Speed II SE/nfs2se.exe" *.o --stack=0x7D00,0x7D00 --heap=0x2000,0x1000 -lws2_32 -lwinmm -lkernel32 -lmsvcrt -lopengl32 -lSDL2 -subsystem=windows $STRIP -e _start &&
i686-w64-mingw32-ld --enable-stdcall-fixup -o "../Need For Speed II SE/nfs2se.exe" *.o --stack=0x7D00,0x7D00 --heap=0x2000,0x1000 -lws2_32 -lwinmm -lkernel32 -lmsvcrt -lopengl32 -lSDL2 -subsystem=$WIN_SUBSYSTEM $STRIP -e _start &&
rm -f *.o &&
echo "OK!"
else
Expand Down
67 changes: 48 additions & 19 deletions src/DInput.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ static STDCALL uint32_t Release( void **this )
free( dinputDev->effects );

SDL_HapticClose( dinputDev->haptic );
#ifdef WIN32
SDL_JoystickClose( dinputDev->joy ); //SDL2 crashes on closing joystick on Linux... :D
#endif
SDL_JoystickClose( dinputDev->joy );
}
// printf( "Release: free 0x%p\n", *this );
free( dinputObj );
Expand Down Expand Up @@ -183,12 +181,8 @@ static STDCALL uint32_t GetCapabilities( DirectInputDevice **this, DIDEVCAPS *de
{
if ( (*this)->haptic )
devCaps->flags = 0x100; //DIDC_FORCEFEEDBACK
devCaps->buttons = SDL_JoystickNumButtons( (*this)->joy );
devCaps->axes = SDL_JoystickNumAxes( (*this)->joy );
if ( devCaps->buttons > 15 )
devCaps->buttons = 15;
if ( devCaps->axes > 4 )
devCaps->axes = 4;
devCaps->buttons = 15;
devCaps->axes = 4;
}
else //Mouse as joystick
{
Expand Down Expand Up @@ -219,19 +213,54 @@ static STDCALL uint32_t GetDeviceState( DirectInputDevice **this, uint32_t cbDat
/* Joystick only */
if ( data && cbData == sizeof( DIJOYSTATE ) && (*this)->guid.a == JOYSTICK )
{
const int32_t joyIdx = (*this)->guid.b - 1;
uint32_t i;

DIJOYSTATE *joyState = ( DIJOYSTATE * )data;
SDL_memset4( joyState->axes, 0x7FFF, 6 );
memset( joyState->buttons, 0, sizeof joyState->buttons );

SDL_Joystick *joy = (*this)->joy;
if ( joy )
if ( joyIdx > -1 ) //Real joystick
{
SDL_Joystick *joy = (*this)->joy;

SDL_JoystickUpdate();

/* Check if joystick is unplugged */
if ( !SDL_JoystickGetAttached( joy ) )
{
/* Close haptic */
if ( (*this)->haptic )
{
SDL_HapticClose( (*this)->haptic );
for ( i = 0 ; i < (*this)->num_effects ; ++i )
{
(*this)->effects[ i ]->haptic = NULL;
(*this)->effects[ i ]->effect_idx = -1;
}
(*this)->haptic = NULL;
}
/* Close joystick */
SDL_JoystickClose( joy );

/* Try to open a joystick */
if ( !( (*this)->joy = joy = SDL_JoystickOpen( joyIdx ) ) )
return 0; //Cannot open the joystick

/* Reopen haptic */
if ( (*this)->num_effects )
{
(*this)->haptic = SDL_HapticOpenFromJoystick( joy );
for ( i = 0 ; i < (*this)->num_effects ; ++i )
{
(*this)->effects[ i ]->haptic = (*this)->haptic;
(*this)->effects[ i ]->effect_idx = SDL_HapticNewEffect( (*this)->haptic, &(*this)->effects[ i ]->effect );
}
}
}

uint32_t numButtons = SDL_JoystickNumButtons( joy );
uint32_t numAxes = SDL_JoystickNumAxes( joy );
uint32_t joyIdx = (*this)->guid.b - 1;
uint32_t j;

if ( numButtons > 15 )
numButtons = 15;
Expand All @@ -242,7 +271,7 @@ static STDCALL uint32_t GetDeviceState( DirectInputDevice **this, uint32_t cbDat
joyState->buttons[ i ] = SDL_JoystickGetButton( joy, joystickButtons[ joyIdx ][ i ] ) << 7;
for ( i = 0 ; i < numAxes ; ++i )
{
j = i < 3 ? i : 5;
uint32_t j = i < 3 ? i : 5;
joyState->axes[ j ] = ( uint16_t )SDL_JoystickGetAxis( joy, joystickAxes[ joyIdx ][ i ] ) ^ 0x8000;
if ( joystickAxes[ joyIdx ][ i + 4 ] )
joyState->axes[ j ] = ( joyState->axes[ j ] >> 1 ) + 32768;
Expand Down Expand Up @@ -486,12 +515,12 @@ static STDCALL uint32_t EnumDevices( void **this, uint32_t devType, DIENUMDEVICE
{
if ( !mouseAsJoystick && !i )
continue;
#ifdef WIN32
// #ifdef WIN32
// printf( "%s\n", SDL_JoystickNameForIndex( i - 1 ) );
/* To prevent joysticks duplicates - discard any xinput devices */
if ( !strncasecmp( SDL_JoystickNameForIndex( i - 1 ), "xinput", 6 ) )
continue;
#endif
// /* To prevent joysticks duplicates - discard any xinput devices */
// if ( !strncasecmp( SDL_JoystickNameForIndex( i - 1 ), "xinput", 6 ) )
// continue;
// #endif
deviceInstance.guidInstance.a = JOYSTICK;
deviceInstance.guidInstance.b = i;
if ( !callback( &deviceInstance, ref ) )
Expand Down
4 changes: 4 additions & 0 deletions src/Wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ void WrapperInit( void )

SDL_Init( ( SDL_INIT_EVERYTHING | SDL_INIT_NOPARACHUTE ) & ~SDL_INIT_GAMECONTROLLER );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_JoystickEventState( SDL_IGNORE );
SDL_ShowCursor( false );

#ifndef WIN32
Expand Down Expand Up @@ -372,6 +373,9 @@ void WrapperInit( void )
perror( "sched_setaffinity" );
}
#endif

if ( mouseAsJoystick )
SDL_SetRelativeMouseMode( SDL_TRUE );
}

extern WindowProc wndProc;
Expand Down

0 comments on commit 66b0796

Please sign in to comment.