[DirectX9 C++] - First attempt at 2D

Started by
8 comments, last by Endurion 13 years, 11 months ago
Good day, In my 3D application I must add something in 2D. This 2D object must be dynamic and contain some text and color. Think of it like a map legend, only this changes dynamically if something is changed in the 3D space. Now I've never drawn anything in 2D before and hope it's simple. After some reading I found out the easiest way is to use ID3DXSprite. Its function seems straightforward. However when calling ID3DXSprite::Draw() a texture is required. So whatever i need to draw i must draw in a texture. My 2D object must be dynamic, so the texture should be dynamic too. Now of course i will not load a texture from a file as the texture will be different each time. So the real question is: How can i create a texture containing text and colors from scratch? Also if anyone thinks there is a better approach to the problem, please let me know. Thanx in advance, stakon
Advertisement
From what you described I think you can.

For Direct3D 9 you can use GDI by obtaining a HDC for normal GDI drawing IDirect3DSurface9::GetDC(), and you can use Windows dialogs to implement user interfaces IDirect3DDevice9::SetDialogBoxMode(). check the DirectX help for details.

For Direct3D 10.1 or above, you can use DXGI GDI Interop, or Direct3D and Dirext2D combined programming.

http://msdn.microsoft.com/en-us/library/ee913554(VS.85).aspx#interoperability_between_direct3d_9ex_and_dxgi_based_apis

I'll spend some time this weekend to start my own web site in gamedev and post some simple tutorials.

[Edited by - standby01 on April 28, 2010 5:06:45 PM]
Jimmy Chen
The short answer is, you can create a texture in memory and draw to it dynamically.

As far as helping you with a "better approach to the problem" it might be easier if you explained some of what it is you are drawing. Are you actually making a 2D mini map? or is it something else?
So i'll explain this a little bit more:

What i need to create is a rectangle which contains a color scale. This will appear always on the screen and will act as a "legend" The bottom of the scale is close to Red the top close to Blue and the mid close to Green.

Next to the color scale there must be some numeric values which represent what a color means.
Both (color scale and numbers) should be dynamic according to user input.


You say i can create such a texture in memory. How is this possible if in the texture i have to write text in addition to the color scale?

Thanx in advance,
stakon.



To draw the text you probably want to look into using ID3DXFont.

Drawing a colour scale is fairly straightforward too. You just want to draw a rectangle, and set up the vertex colours at each end. D3D will automatically generate the colours in between for you by linear interpolation. To go from red to blue to green you'll need more than one of those rectangles.

Unfortunately ID3DXSprite isn't flexible enough to let you do that. The next simplest API is probably combining SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); with DrawPrimitiveUP(D3DPT_TRIANGLELIST, ...).

You may find this useful.
Hi Adam,
i am already aware of the solutions you propose.

However , as i originally posted the solution i am looking for is 2D not 3D.
That's why i am looking for a way to create a texture during run time which contains text and colors.

btw i have already started to implement a 3D version of what i want because i can't find a way to make it in 2D.

look up in the DirectX SDK help and find texture creating function then create it, lock it then draw in it.
the SDK is you friend.

Creates an empty texture, adjusting the calling parameters as needed.

HRESULT D3DXCreateTexture(
LPDIRECT3DDEVICE9 pDevice,
UINT Width,
UINT Height,
UINT MipLevels,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
LPDIRECT3DTEXTURE9 * ppTexture
);
Quote:Original post by stakon
Hi Adam,
i am already aware of the solutions you propose.

However , as i originally posted the solution i am looking for is 2D not 3D.

But it in fact was 2D. If you specify D3DFVF_XYZRHW in your vertex structure, you're working with 2D coordinates, that means coordinates directly on your screen.

Hi ankhd,
as far as i know the only way to draw in a texture is by using IDirect3DDevice9::SetRenderTarget(0,pSurface) after creating a surface from a texture.

Is this what you are suggesting?


@TOM
You're, right i mistook the D3DFVF_XYZRHW flag for anither 3d variant of D3DFVF_XYZ.
You can also Lock a texture/surface and access the pixel data directly. This may not be the fastest approach and also should probably not done every frame.

You also need to take in account the stored pixel format and pitch. And don't forget to call Unlock.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement