main page | gallery | rationale | contact

a particle simulation using DirectDraw and DirectInput

Written:Spring 2001
Last Touched:Spring 2001


I wrote this particle simulation as a technology demonstration for part of a larger game that never was finished, or even coordinated enough to really get started. Cleaning up some of my old DirectDraw and DirectInput code, I hacked together this neat demo in a couple hours. Five minutes of entertainment, guaranteed.

What Was Difficult?

Not much. Refactoring code was fairly fun, but I may have got a bit carried away as I tried to combine the best aspects of two of my old graphics libraries. At this point, I didn't have enough of an idea about object oriented design to come up with a cohesive DirectDraw/DirectInput wrapper, so there are plenty of naked COM calls in the code.

How nasty is COM to work with? Here's a code snippet to set the video mode.

Fvideo.c (excerpt)

bool FVideo::SetVideoMode(int iw, int ih, int ibpp, int ihz)
	HRESULT ddret;

	// initialize directdraw
	if(!InitializeDDraw()) goto Failed;
	// attempt to set the cooperative level for fullscreen
	ddret = lpDD->SetCooperativeLevel(g_MainWindow, DDSCL_FULLSCREEN |
	if(FAILED(ddret)) goto Failed;
	// attempt to set the video mode to what was specified
	ddret = lpDD->SetDisplayMode(iw,ih,ibpp,ihz,0);
// ...

Lots of bitwise flags, lots of headache-inducing preprocessor macros that declare namespaces, lots of merely adequate online documentation.

Thankfully, I didn't have to figure out a lot of this on my own; I had both an article from Dr. Dobb's (fine) Programming Journal detailing DirectDraw work as well as a hefty tome of wisdom by famed game programmer Andre LaMothe. Even with these two references, it was still a bit of hit-and-miss with DirectDraw. After several unresponsive blank screens, I memorized the keystrokes to switch to Visual Studio, open the Debug menu and stop the program, saving me a reboot and sometimes letting me track down the exception that I had been unable to see in full-screen mode.

What you need to compile & run this

For Windows, I'd recommend Microsoft Visual C++ 6.0 or greater, since that's exactly the tool I used to develop this project. You'll also need the DirectX 7.0a or newer SDK -- thanks to COM, they're backwards compatible. This is currently available from:

For Linux, you're out of luck. Unless you feel like messing with WINE, but neither I or the WINE programmers will guarantee you any degree of success. But hey -- it never hurts to try. Let me know if you manage to get it working, and I'll give you something cool for your efforts.

Controls (also listed during simulation)

Spawn Particles:Left Mouse Button
Increase Initial X Velocity:Right Arrow
Decrease Initial X Velocity:Left Arrow
Increase Initial Y Velocity:Up Arrow
Decrease Initial Y Velocity:Down Arrow
Increase Gravity:Page Up
Decrease Gravity:Page Down
Increase Wind (X-axis Gravity):Insert
Decrease Wind (X-axis Gravity):Home
Decrease Red:Z
Increase Red:A
Decrease Green:X
Increase Green:S
Decrease Blue:C
Increase Blue:D
Increase Particle Time To Live:F
Decrease Particle Time To Live:B
Increase Number of Initial Particles:- (Main Keyboard)
Decrease Number of Initial Particles:= (Main Keyboard)
Quit:Escape, Alt+F4 (310 K) (312 K)

Last updated: 2003.03.03 by