How to integrate SDL and Direct3D
This is how I integrated SDL and Direct3D. By "integrated" I mean being able to use a SDL event loop, window management, etc in a window you can do D3D stuff on. This is *not* a Direct3D backend for SDL, OpenGL functions are *not* mapped, or anything like that.
1) Initialize the window. There are two things to take into account : first, force the use of the DIB video driver. Using the DirectDraw driver causes problems since it tries to initialize a different version of DirectX. Second, make the window always windowed, even if you want to set a fullscreen mode.
Example code (no error checking at all) :
{{{ SetEnvironmentVariable("SDL_VIDEODRIVER", "windib");
- SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
SDL_SetVideoMode(nWidth, nHeight, 16, 0); // Fullscreen = 0,
always. Not sure if the BPP matter here, though}}}
{{{ [Edited by Sander Bisschops, working for the OpenFRAG project.]
- Using SDL version 1.2.8 I have succesfully used the Ogre3d graphical engine to enable SDL input in a DirectX-drawn window. In my version there was an SDL_VIDEODRIVER called "directx" which worked fine. BPP does not seem to matter for DirectX. }}}
2) Initialize Direct3D. This is as usual, you just need to get the window handle from SDL.
Example code :
{{{ [This returns a different (incorrect) HWND if pInfo is a non-static local. Making it static or global works fine. I guess it has something to do with threading but it's not really clear]
- static SDL_SysWMinfo pInfo;
SDL_VERSION(&pInfo.version); SDL_GetWMInfo(&pInfo); IDirect3D* pD3D = Direct3DCreate8(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.hDeviceWindow = pInfo.window; d3dpp.Windowed = FALSE; // You can use TRUE here no matter what
you passed to SDL_SetVideoMode()
- ..}}}
The rest is as plain Direct3D code. You can now use Direct3D for rendering and SDL for event handling, for example.
For using textures as images, make sure you convert them to the format Direct3D expects - SDL_DisplayFormat and SDL_DisplayFormatAlpha won't necesarily give the results you want.
Note that I've tested all this with SDL 1.2.6 only.
