DirectX8, Fmod and CFL

DX8 graphics

Open source as .cpp as .html

Open stdverts.h as .h as .html

First of all, there's a new include file - stdverts.h. The old VERTEX, LVERTEX, and TLVERTEX objects do not exist in dx8 anymore, most probably to disencourage people from using vertex formats that contain information they don't need. I took the most logical lazy route and rewrote those objects, and they can be found in stdverts.h. There's two new objects in there as well for your amusement.

Let's go through the changes, top-down, again:

LPDIRECT3DTEXTURE8      mytexture;  // The only texture in this example
LPDIRECT3DVERTEXBUFFER8 vbb;        // The only vertex buffer in this example

Texture and vertex buffer objects. D3dx8 makes the texture creation rather simple, as we'll see later on. Vertex buffers have changed a lot. In dx7, changing between vertex buffers was extremely slow; now it's fast, and in fact recommended. Time to recode that vector engine..

Next there's a new function, 'setup()'. This function is called from winmain. Most of it should be familiar to you if you've used directx before. Main change is that enumerations have somewhat shorter names these days - D3DRENDERSTATE_ became D3DRS_, etc.

// Create a vertex buffer (die if failed)
if (FAILED(d3dd->CreateVertexBuffer(
    sizeof(D3DTLVERTEX)*129,
    D3DUSAGE_WRITEONLY,
    D3DFVF_TLVERTEX,
    D3DPOOL_MANAGED,&vbb)))
    exit(1);

Confusing bit about directx8 part two: D3DPOOL. Be sure to read the dx8 help file on this topic through several times. Vertex buffers that are 'managed' cannot be 'dynamic'. textures that are not 'managed' cannot be locked.

//  Set up vbb as the vertex stream source.
d3dd->SetStreamSource(0,vbb,sizeof(D3DTLVERTEX));
d3dd->SetVertexShader(D3DFVF_TLVERTEX);

As we're using the fixed rendering pipeline, we just set vertex shader to our vertex flags, and say that our vertex buffer is the vertex stream source.

The texture loading couldn't be much simpler than this:

// Load the texture. (If this fails, you'll see a white rectangle instead,
// which is not so devastating in this example)
D3DXCreateTextureFromFileA(d3dd,"flare64.jpg",&mytexture);

I couldn't find anything about supported file formats in the documentation, but I've successfully loaded bmps, jpgs and png files with that function. Sometimes, when loading a lot of textures in row, the function fails for no apparent reason. The png alpha layer works, by the way.

Next up we have the rendering function, which I promised to fill in the last chapter. There I create two triangle fan 'flowers' by locking the vertex buffer, filling it with one vertex in the center per 'flower', and the rest as a circles around the centers, with varying distances from the center using sin and cosine. Finally I render the two 'flowers' on top of each other using additive blending.

You should notice that the DrawPrimitive call now only takes three parameters; it no longer needs the vertex buffer as that's defined with SetStreamSource. The third parameter is (somewhat confusingly) the number of primitives, instead of vertices - and it is rather easy to bluescreen your machine if you set this to a too high value. At least with nvidia's drivers.

The final change is the call to 'setup()', after creating the debug font.

If you run the application you should see two 'flowers' on top of each other, rotating in different directions.

Next we'll play some music..