Sign in to follow this  
Lith

Converting Strings

Recommended Posts

I have been trying to solve this for about 3 days and i cant.

I want to have my program load a texture that a text file specifys. I have everything working but there's 1 problem. I cant convert from a C++ string to a "const wchar_t *".

I looked on MSDN and found a nice page that told me everything, apart from using C++ strings. So i thought that if i convert from my C++ string to a char, then follow MSDN and convert it to a wchar_t it would work. It didnt.

Here is the function call to load a texture:

Success = Load2DOpenGLTexture(&TEXTURE_MenuBack, <FILENAME>, &ErrorStr);



I replace <FILENAME> with the filename as a "const whcar_t *"

Here is the definition if it is any help. (the function itself works perfectly)

bool Load2DOpenGLTexture(GLuint* PointerToOpenGLTextureID, const wchar_t* ImageFileName, string* ErrorString)
{
//This is a complicated function that loads an OpenGL texture and return's 3 values

ILboolean Success;
ILuint DevILTextureID;
//GLuint TempOGLTextureID;

ilGenImages(1, &DevILTextureID);
ilBindImage(DevILTextureID);
Success = ilLoadImage(ImageFileName);
if(Success)
{
Success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
if(!Success)
{
//string TempString = "Could not convert image";
//ErrorString = &TempString;
*ErrorString = "Could not convert image";
return false;
}
glGenTextures(1, PointerToOpenGLTextureID);
glBindTexture(GL_TEXTURE_2D, *PointerToOpenGLTextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
ilDeleteImages(1, &DevILTextureID);
}
else
{
//string TempString = "Could not load image";
//ErrorString = &TempString;
*ErrorString = "Could not load image";
return false;
}
//PointerToOpenGLTextureID = &TempOGLTextureID;
return true;
}



I am using OpenGL and DevIL

So any idea on how i can convert from a C++ "string" to a "const whcar_t *" ?

Share this post


Link to post
Share on other sites
For unicode strings you should use std::wstring.
Then you call the c_str() member function to convert to const wchar_t.
If you need to convert from string to wstring you could do something like :


std::string src;
std::wstring dest(src.begin(),src.end());



I can't remember,but didn't DevIL had a loading function that takes ansi strings?

Share this post


Link to post
Share on other sites
Thanks for the reply, but i still cant do it.

So far i have this:

//first convert from C++ string into wstring
wstring WString(PointerToMainGame->Propertys.MainMenuBackgroundFilename.begin(),PointerToMainGame->Propertys.MainMenuBackgroundFilename.end());

//now convert from wstring to const wchar_t
wstring *WStringP = &WString;
const wchar_t *FinalString = (const wchar_t *)WStringP;
cout << "\n" << FinalString << "\n";
cout << "\n" << *FinalString << "\n";



It doesnt work, the couts print this:

002AF7BC

and

1


Please can you give me some more help, im clueless

Share this post


Link to post
Share on other sites
It looks like you're casting the address of the wstring object to a wchar_t* and then printing that out, which probably isn't what you want. Most likely, you instead need to be using the c_str() function, as mentioned previously.

Share this post


Link to post
Share on other sites
Thanks, now i have this:

wstring WString(PointerToMainGame->Propertys.MainMenuBackgroundFilename.begin(),PointerToMainGame->Propertys.MainMenuBackgroundFilename.end());

const wchar_t FinalString = WString.c_str();
cout << "\n" << FinalString << "\n";




The function needs a const wchar_t * so this is the function call i use:

Success = Load2DOpenGLTexture(&TEXTURE_MenuBack, &FinalString, &ErrorStr);




shouldnt this work? i get this error from VC++
cannot convert from 'const wchar_t *' to 'const wchar_t'


This really confuses me, does c_str() return a pointer?
I tryed
const wchar_t *FinalString = WString.c_str();
and removed the & from the function call and it compiled but the image still didnt load. Proberly because i gave it a bad file name.

Share this post


Link to post
Share on other sites
Quote:

I tryed
const wchar_t *FinalString = WString.c_str();
and removed the & from the function call and it compiled but the image still didnt load. Proberly because i gave it a bad file name.

std::basic_string<T>::c_str() returns a pointer. You are doing the right thing here. You must now see if DevIL has any functions which will inform you why the image loading failed.

One way to test it is to pass the absolute path to a good image, and see if that succeeds. Failing to load resources is frequently caused by relying on the working directory.

Share this post


Link to post
Share on other sites
I have allready tested it with an absolute image path and it worked fine. This is what i used:

Success = Load2DOpenGLTexture(&TEXTURE_MenuBack, L"C:/Users/*****/Documents/Visual Studio 2008/Projects/Tiny Bob/Release/data/textures/mm.png", &ErrorStr);





EDIT:

I also put the absolute path in the file that the program open's and got the same error.


EDIT 2:

Im trying to get a better way of error checking so i do this after the function call:

if((ErrorVal = ilGetError()) != IL_NO_ERROR)
{
cout << "An error occured\n";
wcout << *iluErrorString(ErrorVal);
cout << "Aftermath\n";
}



The 2 couts are there so i know what function is causing my program to crash. I have no idea why this is happening, i was following this:
http://openil.sourceforge.net/tuts/tut_3/index.htm

[Edited by - Lith on November 13, 2010 4:19:25 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this