# Easy way to draw images/textures in 2d with DirectX?

This topic is 4730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using the DirectX9 April SDK using the CDUXT classes to make a game. Everything is going well so far, networking, graphics and gameplay. I'm designing the UI using the CDXUT::Dialog classes and others and would like to draw an image from a file on the screen. There is a CDXUTDialog::DrawSprite which takes a CDUXTElement, but the CDXUTElement::SetTexture() function looks for rectangles - is there some way I'm unware of to load textures/images into RECT objects? Thanks ahead of time!

##### Share on other sites
I've mainly been reading the C# version of those classes, but it should work the same.

They load textures using the CDXUTDialogResourceManager class:

This loads the texture image into a list and returns an index which is basically a handle.

When you want to add a texture to an object (like a button), they then use:
SetTexture(textureIndex, location);

- the textureIndex is same as above.
- the location is a rectangle that represents a sub-region in the texture to use. This is so they can have multiple gui elements all stored in one image. To see an example, look at 'dxutcontrols.dds' in the SDK's Samples\Media\UI directory.

So to answer your question, no you cant load images into rects - you load the image before that and use the rect to say which part of the image is which control.

- Wyzfen

##### Share on other sites
Thanks for the comments, that all makes sense coneptually to me. I tried the following code and it didn't draw anything, do you have any suggestions?

int tIndex = g_DialogResourceManager.AddTexture(L"frigate_picture.jpg");CDXUTElement ele;RECT r;SetRect(&r, 0, 0, 200, 200);ele.SetTexture(tIndex, &r);myRandomDialog.DrawSprite(&ele,&r);

##### Share on other sites
Have you called all the draw-initialisation code first ?
In the library, theres a CDXUTDialog::OnRender() method that sets up the render state, enables sprite drawing and THEN starts getting each element to draw itself.

Without seeing more of your code, thats my best guess; that you've not set up the drawing.

In general, the method to use the library (from what i can see) is to make an instance of an object (Button etc) and add it to the dialog. The Dialog acts as a container for all your objects.
The dialog then calls the render method.

Have you had a look at the CustomUI sample ? (Samples\C++\Direct3D\CustomUI)
It shows setting up a dialog (in InitApp) and the render routine, which just calls OnRender on your Dialog.

- Wyzfen

##### Share on other sites
Yes, I actually have a fully functional UI using the CDXUT methods up and running. The UI has multiple dialogs, each one of which I've added statics and buttons to. I'm correctly handling the buttons to navigate around the UI, etc etc. I'm calling .OnRender() on all of my dialogs which is rendering them.

I'm only having trouble using the DrawSprite because I'm not exactly sure how/what the CDXUTElements are. So I've got the UI working but there arent any examples in the DX Samples that I've seen that actually show any of the DrawSprites (or even DrawRect for that matter).

In an InitGraphics function, I'm building my CDXUTDialog class which will hold my sprite to draw. I'm also then using the following code to add a Sprite to my dialog:

int tIndex = g_DialogResourceManager.AddTexture(L"mypicture.jpg");CDXUTElement ele;RECT r;SetRect(&r, 0, 0, 200, 200);ele.SetTexture(tIndex, &r);myRandomDialog.DrawSprite(&ele,&r);

Then in my OnFrameRender function, I'm calling myRandomDialog.OnRender( fElapsedTime ). I know the call to the FrameRender is working correctly because other elements of myRandomDialog, like buttons or statics, are drawing correctly.

Perhaps DrawSprite needs to be called consantly, such that I should have another function called DrawSprites that is called by OnFrameRender, which each time goes through and calls myRandomDialog.DrawSprite(blah blah)? I tried this initially but it did not seem to work either.

• 9
• 9
• 13
• 41
• 15