Cant use CreateTextureFromFile() function

Started by
11 comments, last by tariq9112003 13 years, 8 months ago
Hey guys , small problem...i use visual 2010 ..i get an error in my CreateTextureFromFile() function call where you write the image file name ...this error is thrown



1 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"

this is my function call
D3DXCreateTextureFromFile(d3ddev, "dx5_logo.bmp", &Tex);

My Solution Explorer has no folder for images also.i am so lost
Advertisement
That error you're getting is because of the fact that VS 2010 uses Unicode. That is, it uses wide strings, while you are using regular ones.
You won't get any errors if you put an L in front of "dx5_logo.bmp". Like so:

D3DXCreateTextureFromFile(d3ddev, L"dx5_logo.bmp", &Tex);

Or, if you don't want to use Unicode you can disable it by going to your project properties and set the character set to No Character Set or Multibyte.

In addition, you might want to read this article, specifically the section on wchar_t*.

While L"dx5_logo.bmp" works in the above hard coded example, it will help to understand the difference between chars and wchars. What if you want to store your texture file names in a class? Well, you'll have something like

// In your header....private:    std::string m_TexFilename;...


// In your source.HRESULT foo(){    ...    // Your compiler will generate that same error or one that is extremely similar.    hr = D3DXCreateTextureFromFile(d3ddev, m_TexFilename.c_str(), &Tex);    ...}


This is when a more complete understanding of windows' characters will come in handy as you will have to convert between different types.

Or you could use

D3DXCreateTextureFromFileA

it's the same.

Have a great day!Richard Geslot, 3D game developermy 3D creation
Thanks guys..that helped..but when i compile the image never shows up i know i haven't included the image properly in the project..how do i add an image to the project(in xna i could just drag my image to the images folder of solution explorer here in d3d i cant)
Firstly, I advice you to write the full image path (just for testing):

D3DXCreateTextureFromFileA(d3ddev, "C:\\Folder1\\dx5_logo.bmp", &Tex);

If you see the texture in your game, that's because there is a problem of import
If you don't see the texture, it's because there is an other problem

it's important that you take the HRESULT :

HRESULT res = D3DXCreateTextureFromFileA(d3ddev, "C:\\Folder1\\dx5_logo.bmp", &Tex);

res contains a lot of informations : http://msdn.microsoft.com/en-us/library/bb401631.aspx
Have a great day!Richard Geslot, 3D game developermy 3D creation
Hey still in vain..i wrote the exact path,still the image doesn't show up...i shall show you my code
http://pastebin.com/zAwCQHqz
You're creating the texture every frame and never releasing it, you're probably running out of memory, causing D3DXCreateTextureFromFile() to fail.

Either Release() the texture at the end of your render loop, or - preferably - create it once at startup, and Release() it at cleanup time.
Hey tried still doesn't work :(...this sucks :(...i dont wanna be stuck with this
Take one thing at a time. "still doesn't work" is too little to help you with.

After you create your device, create a texture from file. Get that working before you do anything else to your code.
#include "dxerr.h"#pragma comment(lib,"dxerr.lib")//... after device created...HRESULT hr;hr = D3DXCreateTextureFromFile(d3ddev, "c:\\folder1\\dx5_logo.bmp", &Tex); // whatever your full path to the file isif( FAILED(hr) ){   MessageBox(NULL,DXGetErrorDescription(hr),"Failed to load texture",MB_OK);}else{   MessageBox(NULL,"Texture loaded successfully","Load Success!",MB_OK);}

At least get some information to work with. When you get that working, we'll go from there.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement