• Advertisement
Sign in to follow this  

Cant use CreateTextureFromFile() function

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 is
if( 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.

Share this post


Link to post
Share on other sites
Hey I did what you said...it throws a message saying Texture loaded succesfully ...i dont get it...why wont the image show up...it shows no error on the intellisense and even after compilation

Share this post


Link to post
Share on other sites
One thing at a time.

Okay, the texture's loaded.

First: move the D3DXCreateTextureFromFile call and the SamplerState calls to init3d(), following the CreateDevice call, either before or after init_graphics(). Make sure you delete the call from the render_frame function. You only want to create the texture once, and the sampler state only needs to be set once.

Then, I noticed you have to fix the vertex buffer in init_graphics()

memcpy(pVoid, VB, sizeof(CUSTOMVERTEX));

You're copying from VB, not the v[] array, and you only copy 1 vertex, not the entire array.

Change to:

memcpy(pVoid, v, 6*sizeof(CUSTOMVERTEX));


See if that helps.

EDIT: If you still have problems, it would probably be a good idea to repost your code after you've made the changes.

Share this post


Link to post
Share on other sites
Yes it works!!!!!!!!!!!!!!!!!!!!This is the change i made

memcpy(pVoid, v, sizeof(v));

Thanks a lot guys(esp.BuckEye)...I love this site :)...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement