src/video/windx5/SDL_dx5video.c
branchSDL-1.3
changeset 1661 281d3f4870e5
parent 1659 14717b52abc0
child 1662 782fd950bd46
     1.1 --- a/src/video/windx5/SDL_dx5video.c	Sat May 20 04:35:58 2006 +0000
     1.2 +++ b/src/video/windx5/SDL_dx5video.c	Sun May 21 17:27:13 2006 +0000
     1.3 @@ -436,102 +436,98 @@
     1.4  extern int DIB_GetGammaRamp(_THIS, Uint16 *ramp);
     1.5  extern void DIB_QuitGamma(_THIS);
     1.6  
     1.7 +/* Functions for loading the DirectX functions dynamically */
     1.8 +static int DX5_loaded = 0;
     1.9 +static HINSTANCE DDrawDLL = NULL;
    1.10 +static HINSTANCE DInputDLL = NULL;
    1.11 +
    1.12 +void DX5_Unload(void)
    1.13 +{
    1.14 +	if ( --DX5_loaded == 0 ) {
    1.15 +		if ( DDrawDLL != NULL ) {
    1.16 +			FreeLibrary(DDrawDLL);
    1.17 +			DDrawCreate = NULL;
    1.18 +			DDrawDLL = NULL;
    1.19 +		}
    1.20 +		if ( DInputDLL != NULL ) {
    1.21 +			FreeLibrary(DInputDLL);
    1.22 +			DInputCreate = NULL;
    1.23 +			DInputDLL = NULL;
    1.24 +		}
    1.25 +	}
    1.26 +}
    1.27 +int DX5_Load(void)
    1.28 +{
    1.29 +	int status = 0;
    1.30 +
    1.31 +	if ( ++DX5_loaded == 1 ) {
    1.32 +		DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
    1.33 +		if ( DDrawDLL != NULL ) {
    1.34 +			DDrawCreate = (void *)GetProcAddress(DDrawDLL,
    1.35 +						TEXT("DirectDrawCreate"));
    1.36 +		}
    1.37 +		DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
    1.38 +		if ( DInputDLL != NULL ) {
    1.39 +			DInputCreate = (void *)GetProcAddress(DInputDLL,
    1.40 +						TEXT("DirectInputCreateA"));
    1.41 +		}
    1.42 +		if ( DDrawDLL && DDrawCreate && DInputDLL && DInputCreate ) {
    1.43 +			status = 0;
    1.44 +		} else {
    1.45 +			DX5_Unload();
    1.46 +			status = -1;
    1.47 +		}
    1.48 +	}
    1.49 +	return status;
    1.50 +}
    1.51 +
    1.52  /* DX5 driver bootstrap functions */
    1.53  
    1.54  static int DX5_Available(void)
    1.55  {
    1.56 -	HINSTANCE DInputDLL;
    1.57 -	HINSTANCE DDrawDLL;
    1.58 -	int dinput_ok;
    1.59 -	int ddraw_ok;
    1.60 +	int ddraw_ok = 0;
    1.61 +	HRESULT (WINAPI *DDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
    1.62 +	LPDIRECTDRAW DDraw;
    1.63  
    1.64  	/* Version check DINPUT.DLL and DDRAW.DLL (Is DirectX okay?) */
    1.65 -	dinput_ok = 0;
    1.66 -	DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
    1.67 -	if ( DInputDLL != NULL ) {
    1.68 -		dinput_ok = 1;
    1.69 -	  	FreeLibrary(DInputDLL);
    1.70 +	if ( DX5_Load() < 0 ) {
    1.71 +		return -1;
    1.72  	}
    1.73 -	ddraw_ok = 0;
    1.74 -	DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
    1.75 -	if ( DDrawDLL != NULL ) {
    1.76 -	  HRESULT (WINAPI *DDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
    1.77 -	  LPDIRECTDRAW DDraw;
    1.78  
    1.79 -	  /* Try to create a valid DirectDraw object */
    1.80 -	  DDrawCreate = (void *)GetProcAddress(DDrawDLL, TEXT("DirectDrawCreate"));
    1.81 -	  if ( (DDrawCreate != NULL)
    1.82 +	/* Try to create a valid DirectDraw object */
    1.83 +	DDrawCreate = (void *)GetProcAddress(DDrawDLL, TEXT("DirectDrawCreate"));
    1.84 +	if ( (DDrawCreate != NULL)
    1.85  			&& !FAILED(DDrawCreate(NULL, &DDraw, NULL)) ) {
    1.86 -	    if ( !FAILED(IDirectDraw_SetCooperativeLevel(DDraw,
    1.87 +	  if ( !FAILED(IDirectDraw_SetCooperativeLevel(DDraw,
    1.88  							NULL, DDSCL_NORMAL)) ) {
    1.89 -	      DDSURFACEDESC desc;
    1.90 -	      LPDIRECTDRAWSURFACE  DDrawSurf;
    1.91 -	      LPDIRECTDRAWSURFACE3 DDrawSurf3;
    1.92 -
    1.93 -	      /* Try to create a DirectDrawSurface3 object */
    1.94 -	      SDL_memset(&desc, 0, sizeof(desc));
    1.95 -	      desc.dwSize = sizeof(desc);
    1.96 -	      desc.dwFlags = DDSD_CAPS;
    1.97 -	      desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_VIDEOMEMORY;
    1.98 -	      if ( !FAILED(IDirectDraw_CreateSurface(DDraw, &desc,
    1.99 -							&DDrawSurf, NULL)) ) {
   1.100 -	        if ( !FAILED(IDirectDrawSurface_QueryInterface(DDrawSurf,
   1.101 -			&IID_IDirectDrawSurface3, (LPVOID *)&DDrawSurf3)) ) {
   1.102 -	          /* Yay! */
   1.103 -		  ddraw_ok = 1;
   1.104 +	    DDSURFACEDESC desc;
   1.105 +	    LPDIRECTDRAWSURFACE  DDrawSurf;
   1.106 +	    LPDIRECTDRAWSURFACE3 DDrawSurf3;
   1.107  
   1.108 -	          /* Clean up.. */
   1.109 -	          IDirectDrawSurface3_Release(DDrawSurf3);
   1.110 -	        }
   1.111 -	        IDirectDrawSurface_Release(DDrawSurf);
   1.112 -	      }
   1.113 -	    }
   1.114 -	    IDirectDraw_Release(DDraw);
   1.115 -	  }
   1.116 -	  FreeLibrary(DDrawDLL);
   1.117 -	}
   1.118 -	return(dinput_ok && ddraw_ok);
   1.119 -}
   1.120 -
   1.121 -/* Functions for loading the DirectX functions dynamically */
   1.122 -static HINSTANCE DDrawDLL = NULL;
   1.123 -static HINSTANCE DInputDLL = NULL;
   1.124 +	    /* Try to create a DirectDrawSurface3 object */
   1.125 +	    SDL_memset(&desc, 0, sizeof(desc));
   1.126 +	    desc.dwSize = sizeof(desc);
   1.127 +	    desc.dwFlags = DDSD_CAPS;
   1.128 +	    desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_VIDEOMEMORY;
   1.129 +	    if ( !FAILED(IDirectDraw_CreateSurface(DDraw, &desc,
   1.130 +							&DDrawSurf, NULL)) ) {
   1.131 +	      if ( !FAILED(IDirectDrawSurface_QueryInterface(DDrawSurf,
   1.132 +			&IID_IDirectDrawSurface3, (LPVOID *)&DDrawSurf3)) ) {
   1.133 +	        /* Yay! */
   1.134 +		ddraw_ok = 1;
   1.135  
   1.136 -static void DX5_Unload(void)
   1.137 -{
   1.138 -	if ( DDrawDLL != NULL ) {
   1.139 -		FreeLibrary(DDrawDLL);
   1.140 -		DDrawCreate = NULL;
   1.141 -		DDrawDLL = NULL;
   1.142 +	        /* Clean up.. */
   1.143 +	        IDirectDrawSurface3_Release(DDrawSurf3);
   1.144 +	      }
   1.145 +	      IDirectDrawSurface_Release(DDrawSurf);
   1.146 +	    }
   1.147 +	  }
   1.148 +	  IDirectDraw_Release(DDraw);
   1.149  	}
   1.150 -	if ( DInputDLL != NULL ) {
   1.151 -		FreeLibrary(DInputDLL);
   1.152 -		DInputCreate = NULL;
   1.153 -		DInputDLL = NULL;
   1.154 -	}
   1.155 -}
   1.156 -static int DX5_Load(void)
   1.157 -{
   1.158 -	int status;
   1.159  
   1.160  	DX5_Unload();
   1.161 -	DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
   1.162 -	if ( DDrawDLL != NULL ) {
   1.163 -		DDrawCreate = (void *)GetProcAddress(DDrawDLL,
   1.164 -					TEXT("DirectDrawCreate"));
   1.165 -	}
   1.166 -	DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
   1.167 -	if ( DInputDLL != NULL ) {
   1.168 -		DInputCreate = (void *)GetProcAddress(DInputDLL,
   1.169 -					TEXT("DirectInputCreateA"));
   1.170 -	}
   1.171 -	if ( DDrawDLL && DDrawCreate && DInputDLL && DInputCreate ) {
   1.172 -		status = 0;
   1.173 -	} else {
   1.174 -		DX5_Unload();
   1.175 -		status = -1;
   1.176 -	}
   1.177 -	return status;
   1.178 +
   1.179 +	return ddraw_ok;
   1.180  }
   1.181  
   1.182  static void DX5_DeleteDevice(SDL_VideoDevice *this)
   1.183 @@ -541,6 +537,7 @@
   1.184  		IDirectDraw2_Release(ddraw2);
   1.185  	}
   1.186  	DX5_Unload();
   1.187 +
   1.188  	if ( this ) {
   1.189  		if ( this->hidden ) {
   1.190  			SDL_free(this->hidden);