Page 1 of 1

error while adding item to a listbox [solved]

Posted: Sun Jun 24, 2007 10:58 pm
by SkaCahToa
I'm trying to add a GUI console... I am trying to use this code

Code: Select all

bool engine::OnEvent(SEvent event)
{

	if (event.EventType == irr::EET_LOG_TEXT_EVENT) 
     { 
		consoleListBox->addItem(event.LogEvent.Text);
		return true; 
     }; 
	return false;
};
But i get this error while building

Code: Select all

c:\programming\fpstest\main.cpp(188) : error C2664: 'irr::s32 irr::gui::IGUIListBox::addItem(const wchar_t *)' : cannot convert parameter 1 from 'const irr::c8 *' to 'const wchar_t *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
I'm pretty sure the 2 values are different types or something, but i don't know how to programm... so I have no clue.

Posted: Sun Jun 24, 2007 11:26 pm
by omar shaaban
well event.LogEvent.Text is an irr:c8 it must be const wchar_t *
so all u have to do is
const wchar_t * item;
item+=event.LogEvent.Text;//(or add the item if it is a text or int bec i am not sure if this will work)
consoleListBox->addItem(item);

Posted: Sun Jun 24, 2007 11:47 pm
by SkaCahToa
thanks for the quick reply.

i'm still getting similar errors

Code: Select all

c:\programming\fpstest\main.cpp(190) : error C2297: '+=' : illegal, right operand has type 'const irr::c8 *'
c:\programming\fpstest\main.cpp(190) : error C2114: '+=' : pointer on left; needs integral value on right
item+=event.LogEvent.Text; << that is line 190

Posted: Mon Jun 25, 2007 2:20 am
by vitek

Code: Select all

const core::stringw msg( event.LogEvent.Text );
consoleListBox->addItem(msg.c_str());

Posted: Mon Jun 25, 2007 2:36 am
by SkaCahToa
I ended up using

Code: Select all

core::stringw item;
item = event.LogEvent.Text;
consoleListBox->addItem(item.c_str());
which i think is basically the same code... The code compiles with out errors or warnings. When the app is ran tho there is an error when it runs... I'm not sure if the code is at fault or the compiler or bad memory or what.

Code: Select all

The instructions at 0x00413da2 referenced memory at 0xccccccc. The memory could not be read.

Edit: I set my app to write the event.LogEvent.Text to a text file before it adds the item to the listbox. I know that the program errors on that step. I'm not sure if that helps anything.

Posted: Mon Jun 25, 2007 7:49 am
by hybrid
Your code is correct and almost the same as vitek's, but his is a tweak better because it uses assignment initialisation, but you use an additional assignment to the empty string. For strings it does not really matter much, but for complex classes an assigment (and maybe also an additional constructor call at that point) can be costly.
So I guess it's the file handling code which has problems, show us that part.

@omar: This code will hopefully not compile (pointer arithmetics with chars is no good), but if it would it would fail gloriously. You should never use an uninitialized pointer for anything else than initializing it. However, if you remove the '*' it would work, but the latter versions are better.

Posted: Mon Jun 25, 2007 8:11 am
by greenya
There is enigma for me:
if we have:

Code: Select all

core::stringw w;
core::stringc c = "test";
this works:

Code: Select all

w = c.c_str();
but this, doesn't:

Code: Select all

w = c;
as far as i know, all what is necessary -- is to create one more copy-constructor for core::stringw -- so it will be able to take core::stringc as parameter. why this is still not implemented -- is enigma for me. (that is why we have such kind of question every month :) ).

am i wrong?

Posted: Mon Jun 25, 2007 11:35 am
by hybrid
This whole conversion stuff is a half-hearted hack. The copy constructor would be a string<t>(string<u>) copy construcotr. How would you make this one working for all incarnations of <t> and <u>? What would be a real solution is a proper conversion library such as iconv.

Posted: Mon Jun 25, 2007 3:36 pm
by SkaCahToa
hmm.... file handling?... I'm not sure what you mean by file handling.

I have this as my event reciever

Code: Select all

bool engine::OnEvent(SEvent event)
{

	if (event.EventType == irr::EET_LOG_TEXT_EVENT) 
     { 
		std::fstream LogFile("LogFile.log",std::ios::out|std::ios::app); 
		LogFile << (event.LogEvent.Text) << std::endl;
		LogFile.close(); 
		const core::stringw msg( event.LogEvent.Text ); 
		consoleListBox->addItem(msg.c_str());
		return true; 
     }; 
	return false;
};

I added the list box earlier using this

Code: Select all

	consoleListBox = guienv->addListBox(core::rect<s32>(0,0,800,250));
with this in the header file

Code: Select all

gui::IGUIListBox* consoleListBox;

Posted: Mon Jun 25, 2007 4:15 pm
by vitek
It would be very useful to see more of your source or the stack when the access violation occurs. I see nothing wrong with my code.

Travis

Posted: Mon Jun 25, 2007 4:34 pm
by SkaCahToa
Hmm... sorry about that.


main.h

Code: Select all

#include <irrlicht.h>
#include <irrKlang.h>
using namespace irr;

	IrrlichtDevice* device;
	audio::ISoundEngine* irrKlang;
	video::IVideoDriver* driver;

	scene::ISceneManager* smgr;
	gui::IGUIEnvironment* guienv;

class engine : public IEventReceiver
{
public:

	void runLevel();

	virtual bool OnEvent(SEvent event);


private:
	scene::ISceneNode* selectedSceneNode;
	scene::ISceneNode* lastSelectedSceneNode;
	scene::IAnimatedMesh* q3levelmesh;
	scene::ISceneNode* q3node;
	scene::ITriangleSelector* selector;
	scene::ISceneNode* skyboxNode;
	scene::ICameraSceneNode* camera;
	scene::ISceneNodeAnimator* anim;

	gui::IGUISkin* skin;
	gui::IGUIFont* font;
	gui::IGUIListBox* consoleListBox;
};


main.cpp

Code: Select all

#include <irrlicht.h>
#include <irrKlang.h>
#include "main.h"

using namespace irr;

#pragma comment(lib, "Irrlicht.lib")
#pragma comment(lib, "irrKlang.lib")
#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")

s32 cnt = 0;

void engine::runLevel()
{
// create sfx device

	irrKlang = audio::createIrrKlangDevice();

// create gfx device

	device =
	//	createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(800, 600), 32, false, true, true);
	//	createDevice(video::EDT_DIRECT3D8, core::dimension2d<s32>(800, 600), 32, false, true, true);
		createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600), 32, false, true, true);

	if (device == 0)
		return;

	driver = device->getVideoDriver();
	smgr = device->getSceneManager();
	guienv = device->getGUIEnvironment();

// init event reveiver
	
	engine receiver;
	device->setEventReceiver(&receiver);


// load data

	device->getFileSystem()->addZipFileArchive("data/pak0.pk3");

	skin = guienv->getSkin();
	font = guienv->getFont("data/fonthaettenschweiler.bmp");
	if (font)
		skin->setFont(font);

// load map

	q3levelmesh = smgr->getMesh("20kdm2.bsp");
	q3node = 0;
	
	if (q3levelmesh)
		q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));

	selector = 0;
	
	if (q3node)
	{		
		q3node->setPosition(core::vector3df(-1350,-130,-1400));

		selector = smgr->createOctTreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
		q3node->setTriangleSelector(selector);
		selector->drop();
	}

// create sky box
	driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
	skyboxNode = smgr->addSkyBoxSceneNode(
		driver->getTexture("irrlicht2_up.jpg"),
		driver->getTexture("irrlicht2_dn.jpg"),
		driver->getTexture("irrlicht2_lf.jpg"),
		driver->getTexture("irrlicht2_rt.jpg"),
		driver->getTexture("irrlicht2_ft.jpg"),
		driver->getTexture("irrlicht2_bk.jpg"));
	driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);


// init keymap

	SKeyMap keyMap[9];
			keyMap[0].Action = EKA_MOVE_FORWARD;
			keyMap[0].KeyCode = KEY_UP;
			keyMap[1].Action = EKA_MOVE_FORWARD;
			keyMap[1].KeyCode = KEY_KEY_W;

			keyMap[2].Action = EKA_MOVE_BACKWARD;
			keyMap[2].KeyCode = KEY_DOWN;
			keyMap[3].Action = EKA_MOVE_BACKWARD;
			keyMap[3].KeyCode = KEY_KEY_S;

			keyMap[4].Action = EKA_STRAFE_LEFT;
			keyMap[4].KeyCode = KEY_LEFT;
			keyMap[5].Action = EKA_STRAFE_LEFT;
			keyMap[5].KeyCode = KEY_KEY_A;

			keyMap[6].Action = EKA_STRAFE_RIGHT;
			keyMap[6].KeyCode = KEY_RIGHT;
			keyMap[7].Action = EKA_STRAFE_RIGHT;
			keyMap[7].KeyCode = KEY_KEY_D;

// init cam

	camera = 
		smgr->addCameraSceneNodeFPS(0, 100.0f, 300.0f, -1, keyMap, 9, true);
	irrKlang->setListenerPosition(camera->getAbsolutePosition(), camera->getTarget());
	camera->setPosition(core::vector3df(-100,50,-150));
	
	anim = smgr->createCollisionResponseAnimator(
		selector, camera, core::vector3df(30,50,30),
		core::vector3df(0,-3,0), 
		core::vector3df(0,50,0));
	camera->addAnimator(anim);
	anim->drop();


// disable mouse cursor

	device->getCursorControl()->setVisible(false);


	selectedSceneNode = 0;
	lastSelectedSceneNode = 0;

	int lastFPS = -1;

//initHUD

	consoleListBox = guienv->addListBox(core::rect<s32>(0,0,800,250));

// play music

	audio::ISound* snd = irrKlang->play2D("data/IrrlichtTheme.ogg", true, false, true);

	if (snd)
	{
		snd->setVolume(0.5f); // 50% volume
		snd->drop();
	}
// run game

	while(device->run())
	if (device->isWindowActive())
	{
		driver->beginScene(true, true, 0);

		smgr->drawAll();

		guienv->drawAll();

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
		  core::stringw str = L"[";
		  str += driver->getName();
		  str += "] FPS:";
		  str += fps;

		  device->setWindowCaption(str.c_str());
		  lastFPS = fps;
		}
	}

	device->drop();
	return;
}


bool engine::OnEvent(SEvent event)
{

	if (event.EventType == irr::EET_LOG_TEXT_EVENT) 
     { 
		std::fstream LogFile("LogFile.log",std::ios::out|std::ios::app); 
		LogFile << (event.LogEvent.Text) << std::endl;
		LogFile.close(); 
		const core::stringw msg( event.LogEvent.Text ); 
		consoleListBox->addItem(msg.c_str()); 
		return true; 
     }; 
	return false;
};



int main()
{
	engine eng;
	eng.runLevel();
	return 0;
}
EDIT: there is suppose to be "#include <fstream>" on the top line... I must have copied it wrong or something.

Posted: Mon Jun 25, 2007 8:59 pm
by SkaCahToa
yeah i figured out the problem... it was a stupid mistake... the program was trying to add the item to the listbox before it was made.

Posted: Sun Dec 30, 2007 9:51 pm
by dlangdev
ok, i'm stumped with listbox->additem.

here it is...

Code: Select all



unsigned char* data = "an item here";
const core::stringw item(data); 
listbox->addItem(item.c_str());


it doesn't work.

what should i do now?

Posted: Sun Dec 30, 2007 10:10 pm
by dlangdev
ok, finaly got it...

Code: Select all


RakNet::Packet* p;
...
printf("%s\n", p->data);
const core::stringw item(p->data, p->length); 
listbox->addItem(item.c_str());
...