irrlicht's string class template [Solved]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
JuicyLobster
Posts: 25
Joined: Sat Jun 07, 2008 8:55 pm

irrlicht's string class template [Solved]

Post by JuicyLobster »

Im new to c++, I usually use C# and java.

What I am trying to do is place mouse x and y position values on the screen. The problem is in c/c++ u cant add strings togeather, and for the font draw function it requires a wide character.

Heres what I tried:

Code: Select all

		string* mxy;
		mxy->append(input->mouseX());
		mxy->append(", ");
		mxy->append(input->mouseY());
		fWhite->draw(mxy->c_str(),rect<s32>(50,100,0,0),SColor(255,255,0,0));
After I tried to compile I realized its a template class and that im probably using append() wrong too because it has no overloads for what im trying to do, but its just an example to let you know what im trying to do here.

Can someone point me in the right direction or toss me an example of how to use irrlichts string class? I'm hoping it will be shorter than using itoa, stringf etc...

edit: to make it clear im trying to concatonate the x and y values seporated by a comma, and convert using .c_str() to wchar_t
Last edited by JuicyLobster on Thu Jun 18, 2009 8:10 pm, edited 1 time in total.
--
Juicy, hot, and full of butter!
scgtrp
Posts: 16
Joined: Sun Jun 15, 2008 1:25 am

Post by scgtrp »

Code: Select all

string<wchar_t> s;
s += 123;
s += L", ";
s += 456;
std::wcout << s.c_str() << std::endl;
Note that you don't need to use a pointer, and if you did you'd want to initialize it to something (how did yours not crash?)
JuicyLobster
Posts: 25
Joined: Sat Jun 07, 2008 8:55 pm

Post by JuicyLobster »

oh so thats how you use templates? Makes sense to me now, thanks :D

Code: Select all

		string<wchar_t> mxy;
		mxy = input->mouseX() + L", " + input->mouseY();
		fWhite->draw(mxy.c_str(),rect<s32>(50,100,0,0),SColor(255,255,0,0));
btw, it didnt crash because it did not compile, I only said I tried to compile :wink:. It didnt compile because I didnt pass it a type to use with the template.

---

Alright its crashing because i need to cast the mousex into wchar_t somehow, i think

tried this too but it still crashes :/

Code: Select all

		string<wchar_t> mxy;
		mxy = string<wchar_t>(input->mouseX());
		mxy += L", ";
		mxy += string<wchar_t>(input->mouseY());
		fWhite->draw(mxy.c_str(),rect<s32>(50,100,0,0),SColor(255,255,0,0));
Last edited by JuicyLobster on Thu Jun 18, 2009 7:27 pm, edited 1 time in total.
--
Juicy, hot, and full of butter!
scgtrp
Posts: 16
Joined: Sun Jun 15, 2008 1:25 am

Post by scgtrp »

The += operator converts it for you. = is assuming you meant 123 as in whatever ASCII character 123 corresponds to ({?). You could, however, do

Code: Select all

string<wchar_t> s;
s += 123 + L", " + 456;
JuicyLobster
Posts: 25
Joined: Sat Jun 07, 2008 8:55 pm

Post by JuicyLobster »

Code: Select all

		string<wchar_t> mxy;
		mxy += 123 + L", " + 456;
		fWhite->draw(mxy.c_str(),rect<s32>(50,100,0,0),SColor(255,255,0,0));
Tried that instead of using the functions to return mouse position (think that was crashing it). However the text draw function is not drawing any of the string values.
--
Juicy, hot, and full of butter!
scgtrp
Posts: 16
Joined: Sun Jun 15, 2008 1:25 am

Post by scgtrp »

Try this:

Code: Select all

fWhite->draw(mxy.c_str(), rect<s32>(50, 100, 150, 130), SColor(255, 255, 0, 0));
JuicyLobster
Posts: 25
Joined: Sat Jun 07, 2008 8:55 pm

Post by JuicyLobster »

still not showing. I've always used the last 2 arguments as 0 because it seemed they werent being used. If I substitute the mxy.c_str() with L"Test123" it shows. Something wrong with the string somewhere

edit: could it be that using += 123 + L", " + 456 is just adding up the characters as a single char value which then my font has nothing to show it as?

This willdisplay the character a:

Code: Select all

		string<wchar_t> mxy;
		//mxy += 123 + L", " + 456;
		mxy = L"a";
		fWhite->draw(mxy.c_str(),rect<s32>(50,100,0,0),SColor(255,255,0,0));
so will:

Code: Select all

		string<wchar_t> mxy;
		//mxy += 123 + L", " + 456;
		mxy += L"a";
		fWhite->draw(mxy.c_str(),rect<s32>(50,100,0,0),SColor(255,255,0,0));
--
Juicy, hot, and full of butter!
scgtrp
Posts: 16
Joined: Sun Jun 15, 2008 1:25 am

Post by scgtrp »

Strange. I didn't test what I posted, just took a look at the API docs - I read it as the rectangle actually being used as a rectangle and not a point, i.e. the text would be clipped to fit inside that rectangle.

Try using += 3 times to test your theory. It could be true.
JuicyLobster
Posts: 25
Joined: Sat Jun 07, 2008 8:55 pm

Post by JuicyLobster »

yup it seems to be the case (finally :D)

Can only assign one array of chars per += operator. Else it will change the value of the text being concatonated;
--
Juicy, hot, and full of butter!
scgtrp
Posts: 16
Joined: Sun Jun 15, 2008 1:25 am

Post by scgtrp »

Yes, it seems you're right. I did come up with this funky syntax which works, but looks horrible unless you're entering the IOCCC:

Code: Select all

(((mxy += 123) += L", ") += 456);
JuicyLobster
Posts: 25
Joined: Sat Jun 07, 2008 8:55 pm

Post by JuicyLobster »

yup, thanks again, solved.
--
Juicy, hot, and full of butter!
FuzzYspo0N
Posts: 914
Joined: Fri Aug 03, 2007 12:43 pm
Location: South Africa
Contact:

Post by FuzzYspo0N »

something more irr friendly :


core::stringw mxy;

So try :

Code: Select all

mxy = core::stringw(core::stringw(123).append(", ")).append(456);

Or

Code: Select all

mxy = core::stringw(123) + core::stringw(", ") + core::stringw(456);
These arent tested, but should be fine
Post Reply