Drawing in 2D with DirectX 9 - Sprites?

Started by
3 comments, last by beefsteak 11 years, 6 months ago
I would like to develop a 2D game client in c++ and windows, with DirectX 9.

Most of the graphics are stored in png format, hence I found textures to be very useful (easy to load png files with "D3DXCreateTextureFromFile").

In order to be able to draw the textures on the screen in 2D, I used one sprite (LPD3DXSPRITE). This seemed like a simple solution and works perfectly fine for textures.

Now I would like to additionally draw some colored rectangles in between the drawing of images. Surprisingly for me, sprites in DirectX don't support any kind of primitive drawing. There is only one "Draw" method that will render a texture to the display device.


So far I've been pondering about the following solutions:

1. Create textures that contain a certain area of solid color. Then draw any kind of rectangle by aligning the small single-colored textures next to each other. I guess it will work but seems really cumbersome, as I will need to write lots of code to manage the color textures and do the drawing.

2. Find a better way to draw my images to the display device, so that i can mix the image-drawing with primitive drawing operations. So far I couldn't figure out how to do it. I suspect I will need some kind of intermediate "surface" object to draw my texture on.

3. Mix sprite drawing operations with device drawing operations. This would require to split up my drawing code to use many different sprites for different purposes. I will need to call sprite.Begin/End very often. On the whole I don't like this idea because it seems "messy".


Thank you for any kind of feedback that will point me in the right direction! smile.png
Advertisement
Are you strictly limiting yourself to 2d?
I ended up just drawing everything 2d as primitives in my engine I did and subverted LPD3DXSPRITE all together.

Using a custom vertex format like:

//Custom vertex format
const DWORD D3DFVF_TLVERTEX = D3DFVF_XYZRHW| D3DFVF_DIFFUSE | D3DFVF_TEX1;
//Custom vertex
struct TLVERTEX
{
float x;
float y;
float z;
float rhw;
D3DCOLOR colour;
float u;
float v;
};
ID3DXSprite::Draw has a D3DCOLOR parameter and the default texture stage states are set for modulation, so you could use a 1x1 white texture and set this color appropriately for untextured drawing.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Are you strictly limiting yourself to 2d?
I ended up just drawing everything 2d as primitives in my engine I did and subverted LPD3DXSPRITE all together.


The graphics of my game are 2D, which means everything is drawn flat and there is no depth. I wouldn't mind using 3D functions for programming however, if they are suitable for my needs.

The problem I currenty have with 3D functions is that I couldn't find a way to render images. How did you handle that in your engine? Image I have an image of a red dragon, stored in a png file on disk - how can I print that to the screen?

ID3DXSprite::Draw has a D3DCOLOR parameter and the default texture stage states are set for modulation, so you could use a 1x1 white texture and set this color appropriately for untextured drawing.


Thank you for this hint - I didn't realize I can do that.
Starting with a white texture is really a cool trick.

Now I'm using a white "blueprint" texture for primitive drawing, then add color via the "sprite.Draw" method. This makes life a lot easier indeed, because I don't need to create and manage multiple textures for the purpose of primitive drawing.

If anyone else has other / better ideas, I would be glad to read about it.

This topic is closed to new replies.

Advertisement