πŸ™‚ How to use: D3DXCreateTextureFromResource

Posts 1–5 of 5 Β· Page 1 of 1
πŸ™‚ How to use: D3DXCreateTextureFromResource
Hello people!

After struggling a lot and spotting out each mistake, I finally managed to overcome this problem: Rendering picture from a resource.

Without much talk, let's get straight to the code:

1) Navigate to Solution Explorer and right click on Project and choose Add -> Resource.
Click "Import" and select an image file (.png, .jpg, .dds, etc.. .gif won't work!). Hit OK.

For this project, I selected .JPG

2) Now Visual Studio should have added 3 new things in your project:
resource.h
(projectname).rc
(image).jpg

The (image).jpg is just a reference, others are newly created.

3) Right-click on (projectname).rc and click "View Code"

*If a dialog appears, click "OK".

Scroll down the editor and you should see a code similar to this:
Code:
IDB_BITMAP1             JPG                     "C:\\Users\\X\\Desktop\\x.JPG"
It can be slightly different.

Now, after "IDB_BITMAP1", you see "JPG". Rename this to "RCDATA"

Code:
It should look like this:
IDB_BITMAP1             RCDATA                    "C:\\Users\\X\\Desktop\\x.JPG"
* This step is necessary or DirectX will give "invalid data" error.
* Now, save all.

4) Goto your script.
And add these as Global Variables:

Code:
LPD3DXSPRITE Sprite;
LPDIRECT3DTEXTURE9 Texture;
bool TexturesLoaded = false;
5) Add this to where you want the image to be loaded:

(Assuming that your global LPDIRECT3DDEVICE9 is declared as iDevice)
Code:
if(!TexturesLoaded)
{
	HRESULT DTEX = D3DXCreateTextureFromResourceA(iDevice, GetModuleHandleA("(yourmodulename).dll"), MAKEINTRESOURCEA( IDB_BITMAP1 ), &Texture);
	HRESULT DSPR = D3DXCreateSprite(iDevice, &Sprite);
	if(DTEX == D3D_OK && DSPR == S_OK) TexturesLoaded = true;
}
6) Add this to BeginScene or EndScene:

Code:
if(TexturesLoaded)
{
	D3DXVECTOR3 POS;
	POS.x = 10;
	POS.y = 10;
	POS.z = 0;

	Sprite->Begin(0); //For transparent images, set the flag value to 16.
	Sprite->Draw(Texture, NULL, NULL, &POS, 0xFFFFFFFF);
	Sprite->End();
}
That's it, now Build this project and inject the DLL to see if it works. If not, check if you made any mistakes in:
- Importing.
- Renaming.
- Initiating resources before drawing.
- Drawing on wrong callbacks.
- Module name.

Tip:
Make sure you resize the images to a square with pixel sizes which is a power of 2.
Like 16x16, 32x32, 64x64, 128x128 and so on.

Demonstration:
Check attachment.
I think I saw this tutorial before, if you C&P it, give credits! Anyway, thanks
Quote Originally Posted by ~Syphox View Post
I think I saw this tutorial before, if you C&P it, give credits! Anyway, thanks
This isn't a copy at all... The codes for creating a texture for D3D may seem similar.
@~Syphox, @WhiteHat PH: My pleasure.
Posts 1–5 of 5 Β· Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us