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");

always. Not sure if the BPP matter here, though}}}

{{{ [Edited by Sander Bisschops, working for the OpenFRAG project.]

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]

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.

FAQ_SDL_and_Direct3D (last edited 2008-04-17 08:18:14 by localhost)