problem gui and wchar_t*

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.
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

problem gui and wchar_t*

Post by full_newbie »

Hey everyone.
Im getting a problem.
This code:

Code: Select all

locale::global(locale(""));
wifstream f("in.txt", std::wifstream::in);//in.txt - file encoding utf-8
wstring s1;
const wchar_t *load_string;
getline(f, s1);
load_string=s1.c_str();
load_string=(wchar_t*)load_string;
wofstream out("out.txt");
out<<load_string;//good work and encode utf-8
env->addEditBox(load_string, rect<s32>(350, 80, 550, 100));// bad work and encode ????
Why text is good in file , but in Gui element - bad encoding?
if writing this:

Code: Select all

load_string=L"text";
env->addEditBox(load_string, rect<s32>(350, 80, 550, 100));
This code is good work.
Please help me.
This result:

Image
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

Probably because built-in font doesn't support Cyrillic characters. You can check trying:

Code: Select all

load_string=L"Русский текст"; 
env->addEditBox(load_string, rect<s32>(350, 80, 550, 100));
You have to use own font with Cyrillic to display this.
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

No, my programm good support Cyrillic characters, font - Cyrillic.
See, I am writing:
if writing this:
Code:
load_string=L"text";// and load_string=L"Русский текст" - good
env->addEditBox(load_string, rect<s32>(350, 80, 550, 100));
This code is good work.
But, if I am get chars in file and past in EditBox, result bad.
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

I have just check it with Irrlicht 1.7.1, all works fine, here is working sample:

Code: Select all

#include "irrlicht.h"

#pragma comment(lib, "Irrlicht.lib")

using namespace irr;
using namespace core;

int main()
{
	IrrlichtDevice *irrDevice =
		createDevice(video::EDT_OPENGL, dimension2du(500, 400));

	video::IVideoDriver *irrDriver = irrDevice->getVideoDriver();
	gui::IGUIEnvironment *irrGUI = irrDevice->getGUIEnvironment();

	irrDevice->setWindowCaption(L"002. Кириллический текст");

	gui::IGUIFont* fnt = irrGUI->getFont("../media/myfont.xml");

	irrGUI->getSkin()->setFont(fnt);

	gui::IGUIStaticText *label =
		irrGUI->addStaticText(L"Это пример русского текста на метке",
		core::rect<int>(10, 10, 490, 60), true);
	label->setOverrideColor(video::SColor(255, 255, 0, 0));
	
	gui::IGUIButton *button =
		irrGUI->addButton(core::rect<int>(100, 200, 400, 225),
		0, -1, L"Название кнопки", L"Подсказка для кнопки");

	gui::IGUIEditBox *editBox =
		irrGUI->addEditBox(L"Русский едитбокс", recti(100, 150, 400, 180));

	while(irrDevice->run())
	{
		irrDriver->beginScene(true, true, video::SColor(255, 30, 60, 90));
		irrGUI->drawAll();
		irrDriver->endScene();
	}

	irrDevice->drop();
	return 0;
}
to run it you will need "myfont", i have uploaded it here -- http://filebeam.com/54e76256324bcec74740ac5386e577d3

screenshot:
Image
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

Yes, and me you code - good work. And my code - good work. But, I need load text out file. And array with loaded chars changed encode.
(greenya Мне не нужно в самом коде программы русский текст, он у меня и так работает, я все настроил, русский шрифт, всё нормально отображает, мне нужно подгружать русский текст из файла, но когда я его подгружаю и заношу в GUI, то отображается "белиберда", если этот подгруженый текст, обратно занести в файл, то тоже все нормально работает.)
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

ok, i see.

here is a code how to read 1 text line from a file (../media/textFile.txt); important: file must be saved in UNICODE.

Code: Select all

...
	// READ TEXT FROM FILE -----------------
	wchar_t* fileText = L"FAILED TO LOAD FROM FILE";
	FILE *f;
	errno_t e = _wfopen_s(&f, L"../media/textFile.txt", L"rt,ccs=UNICODE");
	if (!e && f)
	{
		wchar_t t[1000];
		fgetws(t, 1000, f);
		fclose(f);
		fileText = t;
	}
	// -------------------------------------

	gui::IGUIEditBox *editBox =
		irrGUI->addEditBox(fileText, recti(100, 120, 400, 180));
...
but result a bit strange: text is higher (i increased height of the editbox) and when i selecting part of text -- it draws at the center, look:
Image
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

errno_t e = _wfopen_s(&f, L"../media/textFile.txt", L"rt,ccs=UNICODE");
`errno_t' was not declared in this scope , what will include?

If I write this:

Code: Select all

wchar_t* fileText = L"FAILED TO LOAD FROM FILE";
   FILE *f;
   wchar_t t[1000];
   fgetws(t, 1000, f);
   fclose(f);
   fileText = t;
Run application, black screen and error.
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

This debuger message:
In ntdll!LdrEnumerateLoadedModules () (ntdll.dll)
In ntdll!LdrEnumerateLoadedModules () (ntdll.dll)
In ntdll!LdrEnumerateLoadedModules () (ntdll.dll)
Program received signal SIGSEGV, Segmentation fault.
In ntdll!EtwEventWriteTransfer () (ntdll.dll)
Program exited with code 030000000005.
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

In my VS2008, i include only stdio.h, and it works.

_wfopen_s() defined in stdio.h
errno_t defined in crtdefs.h (included by stdio.h), and it defined as:

Code: Select all

typedef int errno_t;
so you may use simply int.
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

I am use Code::Blocks and only Code::Blocks.
If i include stdio.h - not work
crtdefs.h - no file
If include stdio.h and define errno_t (typedef int errno_t;) - `_wfopen_s' was not declared in this scope.
This code work only VS, bu i use Code::Blocks (Но чего нет, того нет.)
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The replacement would be wfopen
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

Haha, i write:

Code: Select all

wchar_t* fileText = L"FAILED TO LOAD FROM FILE";
   FILE *f;
   f = _wfopen(L"test.dlg", L"rt,ccs=UNICODE");
   if (f)
   {
      wchar_t t[1000];
      fgetws(t, 1000, f);
      fclose(f);
      fileText = t;
   }
The result has not changed. Bad bad. Why? What wrong?
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

If you see "FAILED TO LOAD FROM FILE" than it really failed to open the file. Make sure you place it near your EXE file since you writing "test.dlg".

If you don't even see FAILED... message, than probably you forgot to specify fileText variable when creating EditBox:
gui::IGUIEditBox *editBox = irrGUI->addEditBox(fileText, recti(100, 120, 400, 180));
full_newbie
Posts: 27
Joined: Mon Apr 05, 2010 7:33 am

Post by full_newbie »

I am not stupid. I see text in other encode, no utf-8.
File: utf-8
If i use QT library and this code - all fine work.
If i use this code in console - all fine work.
If i use OGRE and this code - all fine work
If i use Irrlicht and this code - all incorect work.
I see:
Image
Sorry, my english is very bad. | I use minGW+CodeBlocks+Irrlicht 1.7.1(1.6,1.5)
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

As i said above, encoding of your text file must be UNICODE (if you use my code sample for reading text from it).

P.S.: UNICODE != UTF-8.
Post Reply