Irrlicht/(SFML?) problems on vista32

Started by
1 comment, last by Elspin 15 years, 2 months ago
I'm having a bit of trouble with irrlicht. I'm just using part of the GUI right now, to make a listbox/textbox/button for a simple chat interface. It worked fine until I transferred the code onto my new laptop, which is a 32bit vista. It compiles and runs fine, but when the text is sent it sends, and displays the message in the listbox, but then seconds later I get the popup that says "Chat.exe has stopped working". Here's the code, SFML is worked in for the networking half but I determined even if you comment out the SFML stuff it still happens.
#include <irrlicht.h>
#include "Network.h"

#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

struct SAppContext
{
	IrrlichtDevice *device;
	IGUIListBox*	listbox;
	IGUIEditBox*	textbox;
	IGUIEditBox*	namebox;
	IGUIWindow*		window;

	stringw			name;

	sf::SocketTCP	Socket;
};

// Define some values that we'll use to identify individual GUI controls.
enum
{
	GUI_ID_SEND_BUTTON = 101,
	GUI_ID_SETNAME_BUTTON = 102
};

void send_text( SAppContext &context );
void CreateNameSelector( SAppContext &context );

void send_text( SAppContext &context )
{
    //put message together
	stringw message = context.name.c_str();
	message += ": ";
	message += context.textbox->getText();

    //add message to listbox.
	context.listbox->addItem( message.c_str() );
	context.textbox->setText(L"");

    //convert to normal char, wchar_t* does not work in a console app iostream.
    char* conv_string;
	wchar_t* input = const_cast<wchar_t*> (message.c_str());
    wcstombs( conv_string, input, 800);
    
	// Send it to the server
	sf::Packet Packet;
	Packet << SEND_CHAT << conv_string;
	(context.Socket.Send(Packet) == sf::Socket::Done);
}

class MyEventReceiver : public IEventReceiver
{
public:
	MyEventReceiver(SAppContext & context) : Context(context) { }

	virtual bool OnEvent(const SEvent& event)
	{
		if (event.EventType == EET_GUI_EVENT)
		{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* env = Context.device->getGUIEnvironment();

			switch(event.GUIEvent.EventType)
			{

			case EGET_BUTTON_CLICKED:

				if (id == 101)
				{
					send_text( Context );
					return true;
				}

				if (id == 102)
				{
				    Context.name = Context.namebox->getText();
				    Context.window->remove();
                }

				break;
			default:
				break;
			}
		}
		else if (event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
		{
		    switch (event.KeyInput.Key)
		    {
            case irr::KEY_F2:
                CreateNameSelector( Context );
                return true;

			case irr::KEY_ESCAPE:
                Context.Socket.Close();
                Context.device->closeDevice();
				return true;

            case irr::KEY_RETURN:
                send_text( Context );
                return true;

		    default:
                return true;
		    }
		}

		return false;
	}

private:
	SAppContext& Context;
};

void CreateNameSelector( SAppContext& context )
{
    context.listbox->addItem(L"Setting Name");

	context.window = context.device->getGUIEnvironment()->addWindow(
        rect<s32>(100, 100, 300, 160),
		false,
		L"Display Name:");

	context.namebox = context.device->getGUIEnvironment()->addEditBox(L"", rect<s32>( 10, 30, 140, 50), true, context.window);
	context.device->getGUIEnvironment()->addButton(rect<s32>( 150, 30, 200 - 10, 50), context.window, GUI_ID_SETNAME_BUTTON, L"Set", L"Set's your name");

}

void SetupGraphics( SAppContext& context )
{

	context.device->setWindowCaption(L"Irrlicht Engine - User Interface Demo");
	
	IGUISkin* skin = context.device->getGUIEnvironment()->getSkin();
	IGUIFont* font = context.device->getGUIEnvironment()->getFont("media/fonthaettenschweiler.bmp");
	if (font)
		skin->setFont(font);

	skin->setFont(context.device->getGUIEnvironment()->getBuiltInFont(), EGDF_TOOLTIP);

	context.device->getGUIEnvironment()->addButton(rect<s32>(700 - 10, 600 - 10 - 20, 800 - 10, 600 - 10), 0, GUI_ID_SEND_BUTTON, L"Send", L"Send Message");
	IGUIListBox * listbox = context.device->getGUIEnvironment()->addListBox(rect<s32>(10, 10, 800 - 10, 600 - 20 - 20));

	context.textbox = context.device->getGUIEnvironment()->addEditBox(L"Editable Text", rect<s32>( 10, 600 - 10 - 20, 700 - 20, 600 - 10));

	context.listbox = listbox;

	context.name = L"Default";

	for (u32 i=0; i<EGDC_COUNT ; ++i)
	{
		SColor col = context.device->getGUIEnvironment()->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
		col.setAlpha(255);
		context.device->getGUIEnvironment()->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
	}
}

void SetupNetwork(unsigned short Port, SAppContext& context)
{
    sf::IPAddress ServerAddress = sf::IPAddress::GetLocalAddress();

    // Connect to the server
    if (!context.Socket.Connect(Port, ServerAddress))
    {
        context.listbox->addItem(L"Failed to connect!");
        return;
    }
    else
    {
        context.listbox->addItem(L"Connected Successfully");
    }
}

int main()
{
	// create device and exit if creation failed

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

	if (device == 0)
		return 1; // could not create selected driver.

	// Store the appropriate data in a context structure.
	SAppContext context;
	context.device = device;

	SetupGraphics( context );
	SetupNetwork( 1337, context );

	// Then create the event receiver, giving it that context structure.
	MyEventReceiver receiver(context);

	// And tell the device to use our custom event receiver.
	device->setEventReceiver(&receiver);

	while(device->run() && device->getVideoDriver())
	if (device->isWindowActive())
	{
		device->getVideoDriver()->beginScene(true, true, SColor(0,200,200,200));

		device->getGUIEnvironment()->drawAll();
	
		device->getVideoDriver()->endScene();
	}

	device->drop();

	return 0;
}
Network.h just has SFML/Network.hpp and the enum that has SEND_CHAT. Does anyone know what could be causing this or how I could fix it? EDIT:Text being entered with just a constant like this:
context.listbox->addItem(L"Text");
never causes any problems, so I imagine it would be a problem with the actual variable being entered? Maybe the conversion? I determined if I comment up to right before wcstombs it's fine, but it still doesn't make sense that that crashes it because it still executes past that for the rest of the function and sends it. It's also recieved properly in the other program (console), as the correct string... [Edited by - Elspin on January 15, 2009 1:02:34 PM]
Advertisement
You're writing into uninitialised memory with wcstombs.
Thanks, I was warned about that when I was still using it on xp, but never changed it because it crashed whenever I tried to initialize it. I was just doing it the wrong way though, so it works now.

I really appreciate it

This topic is closed to new replies.

Advertisement