Moved D3D_LoadDLL and SDL_Direct3D9GetAdapterIndex to SDL_windowswindow.c at Jorgen's insistence. That file is wrapped in a more appropriate define check so it will work if somebody builds a binary without D3D support.
authorSam Lantinga <slouken@libsdl.org>
Sat, 28 Sep 2013 14:06:20 -0700
changeset 7756f45660a4974c
parent 7755 f06772125578
child 7757 572546fa1c9b
Moved D3D_LoadDLL and SDL_Direct3D9GetAdapterIndex to SDL_windowswindow.c at Jorgen's insistence. That file is wrapped in a more appropriate define check so it will work if somebody builds a binary without D3D support.

Added a reference to SDL_Direct3D9GetAdapterIndex to SDL_test_common.c so SDL will fail to compile if the new symbol isn't included properly.

CR: Jorgen
src/render/direct3d/SDL_render_d3d.c
src/test/SDL_test_common.c
src/video/windows/SDL_windowswindow.c
src/video/windows/SDL_windowswindow.h
     1.1 --- a/src/render/direct3d/SDL_render_d3d.c	Sat Sep 28 15:48:32 2013 -0300
     1.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Sat Sep 28 14:06:20 2013 -0700
     1.3 @@ -28,10 +28,12 @@
     1.4  #include "SDL_hints.h"
     1.5  #include "SDL_loadso.h"
     1.6  #include "SDL_syswm.h"
     1.7 +#include "SDL_system.h"
     1.8  #include "../SDL_sysrender.h"
     1.9 +#include <stdio.h>
    1.10 +
    1.11  #include "../../video/SDL_sysvideo.h"
    1.12 -#include "../../video/windows/SDL_windowsmodes.h"
    1.13 -#include <stdio.h>
    1.14 +#include "../../video/windows/SDL_windowswindow.h"
    1.15  
    1.16  #if SDL_VIDEO_RENDER_D3D
    1.17  #define D3D_DEBUG_INFO
    1.18 @@ -533,72 +535,6 @@
    1.19      return 0;
    1.20  }
    1.21  
    1.22 -SDL_bool 
    1.23 -D3D_LoadDLL( void **pD3DDLL, IDirect3D9 **pDirect3D9Interface )
    1.24 -{
    1.25 -	*pD3DDLL = SDL_LoadObject("D3D9.DLL");
    1.26 -	if (*pD3DDLL) {
    1.27 -		IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
    1.28 -
    1.29 -		D3DCreate =
    1.30 -			(IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(*pD3DDLL,
    1.31 -			"Direct3DCreate9");
    1.32 -		if (D3DCreate) {
    1.33 -			*pDirect3D9Interface = D3DCreate(D3D_SDK_VERSION);
    1.34 -		}
    1.35 -		if (!*pDirect3D9Interface) {
    1.36 -			SDL_UnloadObject(*pD3DDLL);
    1.37 -			*pD3DDLL = NULL;
    1.38 -			return SDL_FALSE;
    1.39 -		}
    1.40 -
    1.41 -		return SDL_TRUE;
    1.42 -	} else {
    1.43 -		*pDirect3D9Interface = NULL;
    1.44 -		return SDL_FALSE;
    1.45 -	}
    1.46 -}
    1.47 -
    1.48 -
    1.49 -int 
    1.50 -SDL_Direct3D9GetAdapterIndex( int displayIndex )
    1.51 -{
    1.52 -	void *pD3DDLL;
    1.53 -	IDirect3D9 *pD3D;
    1.54 -	if (!D3D_LoadDLL( &pD3DDLL, &pD3D)) {
    1.55 -		SDL_SetError("Unable to create Direct3D interface");
    1.56 -		return D3DADAPTER_DEFAULT;
    1.57 -	} else {
    1.58 -		SDL_DisplayData *pData = (SDL_DisplayData *)SDL_GetDisplayDriverData( displayIndex );
    1.59 -		int adapterIndex = D3DADAPTER_DEFAULT;
    1.60 -
    1.61 -		if (!pData) {
    1.62 -			SDL_SetError( "Invalid display index" );
    1.63 -		} else {
    1.64 -			char *displayName = WIN_StringToUTF8( pData->DeviceName );
    1.65 -			unsigned int count = IDirect3D9_GetAdapterCount( pD3D );
    1.66 -			unsigned int i;
    1.67 -			for (i=0; i<count; i++) {
    1.68 -				D3DADAPTER_IDENTIFIER9 id;
    1.69 -				IDirect3D9_GetAdapterIdentifier(pD3D, i, 0, &id);
    1.70 -
    1.71 -				if (SDL_strcmp(id.DeviceName, displayName) == 0) {
    1.72 -					adapterIndex = i;
    1.73 -					break;
    1.74 -				}
    1.75 -			}
    1.76 -			SDL_free( displayName );
    1.77 -		}
    1.78 -
    1.79 -		/* free up the D3D stuff we inited */
    1.80 -		IDirect3D9_Release(pD3D);
    1.81 -		SDL_UnloadObject(pD3DDLL);
    1.82 -
    1.83 -		return adapterIndex;
    1.84 -	}
    1.85 -}
    1.86 -
    1.87 -
    1.88  SDL_Renderer *
    1.89  D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
    1.90  {
     2.1 --- a/src/test/SDL_test_common.c	Sat Sep 28 15:48:32 2013 -0300
     2.2 +++ b/src/test/SDL_test_common.c	Sat Sep 28 14:06:20 2013 -0700
     2.3 @@ -701,7 +701,9 @@
     2.4              SDL_DisplayMode mode;
     2.5              int bpp;
     2.6              Uint32 Rmask, Gmask, Bmask, Amask;
     2.7 -
     2.8 +#if SDL_VIDEO_DRIVER_WINDOWS
     2.9 +			int adapterIndex = 0;
    2.10 +#endif
    2.11              n = SDL_GetNumVideoDisplays();
    2.12              fprintf(stderr, "Number of displays: %d\n", n);
    2.13              for (i = 0; i < n; ++i) {
    2.14 @@ -754,6 +756,12 @@
    2.15                          }
    2.16                      }
    2.17                  }
    2.18 +
    2.19 +#if SDL_VIDEO_DRIVER_WINDOWS
    2.20 +				/* Print the adapter index */
    2.21 +				adapterIndex = SDL_Direct3D9GetAdapterIndex( i );
    2.22 +				fprintf( stderr, "Adapter Index: %d", adapterIndex );
    2.23 +#endif
    2.24              }
    2.25          }
    2.26  
     3.1 --- a/src/video/windows/SDL_windowswindow.c	Sat Sep 28 15:48:32 2013 -0300
     3.2 +++ b/src/video/windows/SDL_windowswindow.c	Sat Sep 28 14:06:20 2013 -0700
     3.3 @@ -22,10 +22,15 @@
     3.4  
     3.5  #if SDL_VIDEO_DRIVER_WINDOWS
     3.6  
     3.7 +#include "../../core/windows/SDL_windows.h"
     3.8 +
     3.9  #include "SDL_assert.h"
    3.10 +#include "SDL_system.h"
    3.11  #include "../SDL_sysvideo.h"
    3.12  #include "../SDL_pixels_c.h"
    3.13  #include "../../events/SDL_keyboard_c.h"
    3.14 +#include "../../video/SDL_sysvideo.h"
    3.15 +#include "../../video/windows/SDL_windowsmodes.h"
    3.16  
    3.17  #include "SDL_windowsvideo.h"
    3.18  #include "SDL_windowswindow.h"
    3.19 @@ -36,6 +41,9 @@
    3.20  /* This is included after SDL_windowsvideo.h, which includes windows.h */
    3.21  #include "SDL_syswm.h"
    3.22  
    3.23 +#define D3D_DEBUG_INFO
    3.24 +#include <d3d9.h>
    3.25 +
    3.26  /* Windows CE compatibility */
    3.27  #ifndef SWP_NOCOPYBITS
    3.28  #define SWP_NOCOPYBITS 0
    3.29 @@ -683,6 +691,74 @@
    3.30  #endif /* WM_MOUSELEAVE */
    3.31  }
    3.32  
    3.33 +SDL_bool 
    3.34 +D3D_LoadDLL( void **pD3DDLL, IDirect3D9 **pDirect3D9Interface )
    3.35 +{
    3.36 +	*pD3DDLL = SDL_LoadObject("D3D9.DLL");
    3.37 +	if (*pD3DDLL) {
    3.38 +		IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
    3.39 +
    3.40 +		D3DCreate =
    3.41 +			(IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(*pD3DDLL,
    3.42 +			"Direct3DCreate9");
    3.43 +		if (D3DCreate) {
    3.44 +			*pDirect3D9Interface = D3DCreate(D3D_SDK_VERSION);
    3.45 +		}
    3.46 +		if (!*pDirect3D9Interface) {
    3.47 +			SDL_UnloadObject(*pD3DDLL);
    3.48 +			*pD3DDLL = NULL;
    3.49 +			return SDL_FALSE;
    3.50 +		}
    3.51 +
    3.52 +		return SDL_TRUE;
    3.53 +	} else {
    3.54 +		*pDirect3D9Interface = NULL;
    3.55 +		return SDL_FALSE;
    3.56 +	}
    3.57 +}
    3.58 +
    3.59 +
    3.60 +int 
    3.61 +SDL_Direct3D9GetAdapterIndex( int displayIndex )
    3.62 +{
    3.63 +	void *pD3DDLL;
    3.64 +	IDirect3D9 *pD3D;
    3.65 +	if (!D3D_LoadDLL(&pD3DDLL, &pD3D)) {
    3.66 +		SDL_SetError("Unable to create Direct3D interface");
    3.67 +		return D3DADAPTER_DEFAULT;
    3.68 +	} else {
    3.69 +		SDL_DisplayData *pData = (SDL_DisplayData *)SDL_GetDisplayDriverData(displayIndex);
    3.70 +		int adapterIndex = D3DADAPTER_DEFAULT;
    3.71 +
    3.72 +		if (!pData) {
    3.73 +			SDL_SetError("Invalid display index");
    3.74 +			adapterIndex = -1; /* make sure we return something invalid */
    3.75 +		} else {
    3.76 +			char *displayName = WIN_StringToUTF8(pData->DeviceName);
    3.77 +			unsigned int count = IDirect3D9_GetAdapterCount(pD3D);
    3.78 +			unsigned int i;
    3.79 +			for (i=0; i<count; i++) {
    3.80 +				D3DADAPTER_IDENTIFIER9 id;
    3.81 +				IDirect3D9_GetAdapterIdentifier(pD3D, i, 0, &id);
    3.82 +
    3.83 +				if (SDL_strcmp(id.DeviceName, displayName) == 0) {
    3.84 +					adapterIndex = i;
    3.85 +					break;
    3.86 +				}
    3.87 +			}
    3.88 +			SDL_free(displayName);
    3.89 +		}
    3.90 +
    3.91 +		/* free up the D3D stuff we inited */
    3.92 +		IDirect3D9_Release(pD3D);
    3.93 +		SDL_UnloadObject(pD3DDLL);
    3.94 +
    3.95 +		return adapterIndex;
    3.96 +	}
    3.97 +}
    3.98 +
    3.99 +
   3.100 +
   3.101  #endif /* SDL_VIDEO_DRIVER_WINDOWS */
   3.102  
   3.103  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/video/windows/SDL_windowswindow.h	Sat Sep 28 15:48:32 2013 -0300
     4.2 +++ b/src/video/windows/SDL_windowswindow.h	Sat Sep 28 14:06:20 2013 -0700
     4.3 @@ -36,6 +36,8 @@
     4.4      struct SDL_VideoData *videodata;
     4.5  } SDL_WindowData;
     4.6  
     4.7 +typedef struct IDirect3D9 IDirect3D9;
     4.8 +
     4.9  extern int WIN_CreateWindow(_THIS, SDL_Window * window);
    4.10  extern int WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data);
    4.11  extern void WIN_SetWindowTitle(_THIS, SDL_Window * window);
    4.12 @@ -57,6 +59,7 @@
    4.13  extern SDL_bool WIN_GetWindowWMInfo(_THIS, SDL_Window * window,
    4.14                                      struct SDL_SysWMinfo *info);
    4.15  extern void WIN_OnWindowEnter(_THIS, SDL_Window * window);
    4.16 +extern SDL_bool D3D_LoadDLL( void **pD3DDLL, IDirect3D9 **pDirect3D9Interface );
    4.17  
    4.18  #endif /* _SDL_windowswindow_h */
    4.19