[Solved] Cegui and States (Switching state gives exception)

Problems building or running the engine, queries about how to use features etc.
Post Reply
pxL
Gremlin
Posts: 158
Joined: Fri Oct 07, 2005 11:48 am
Location: Hilversum, The Netherlands
Contact:

[Solved] Cegui and States (Switching state gives exception)

Post by pxL »

Hey all, I'm having a problem with (I think) with Cegui and GameStates. I've looked at numerous examples and I just can't find out what's wrong :?.

I've got a menu loaded and I'm trying to get the 'quit' button working. At the quit buttonhandler I'm trying to change states (from MenuState to CreditState). But when I run the program and click on the quit button I get a 'Unhandled exception at 0x0079ce96 in...'. When I comment away the changeState() method and try to do something else (like change the background colour or shutdown) it works just fine. So I think Cegui has been setup just fine.

So I'm thinking its the creditstate, but when I call the changeState() method when I press the spacebar the program works just fine.
I've looked at the Ogre.log and Cegui.log and I don't see any error's. So what is wrong ?? :?.

Enviroment:
OgreSDK 1.0.5 (Azatoth)
Visual.Net 2003
WinXP

MenuState.cpp:

Code: Select all

#include <Ogre.h>
#include <OgreKeyEvent.h>

//mem probs without this next one
#include <OgreNoMemoryMacros.h>
#include <CEGUI/CEGUIImageset.h>
#include <CEGUI/CEGUISystem.h>
#include <CEGUI/CEGUILogger.h>
#include <CEGUI/CEGUISchemeManager.h>
#include <CEGUI/CEGUIWindowManager.h>
#include <CEGUI/CEGUIWindow.h>
#include <CEGUI/elements/CEGUIPushButton.h>
#include "OgreCEGUIRenderer.h"
#include "OgreCEGUIResourceProvider.h"
//regular mem handler
#include <OgreMemoryMacros.h> 

#include "MenuState.h"
#include "PlayState.h"
#include "CreditState.h"

using namespace Ogre;

CEGUI::MouseButton convertOgreButtonToCegui(int iButtonID) {
	switch(iButtonID) {
		case MouseEvent::BUTTON0_MASK:
			return CEGUI::LeftButton;
		case MouseEvent::BUTTON1_MASK:
			return CEGUI::RightButton;
		case MouseEvent::BUTTON2_MASK:
			return CEGUI::MiddleButton;
		case MouseEvent::BUTTON3_MASK:
			return CEGUI::X1Button;
		default:
			return CEGUI::LeftButton;
	}
};

MenuState MenuState::mMenuState;

void MenuState::enterState(void) {
	mRoot			= Root::getSingletonPtr();
	mInputDevice	= InputManager::getSingletonPtr()->getInputDevice();
	mSceneMgr		= mRoot->getSceneManager(ST_GENERIC);

	mCamera			= mSceneMgr->createCamera("MenuCamera");
	mViewPort		= mRoot->getAutoCreatedWindow()->addViewport(mCamera);

	mRenderWindow	= mRoot->getAutoCreatedWindow();

	// Setup GUI system
	mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mRenderWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
	mGUISystem = new CEGUI::System(mGUIRenderer);
	CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
	CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");

	// Set Font and Mouse Cursor
	mGUISystem->setDefaultFont((CEGUI::utf8*)"Tahoma-12");
	mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");

	// Create and Set Main Sheet
	mGUISheet = CEGUI::WindowManager::getSingleton().loadWindowLayout((CEGUI::utf8*)"Main Menu.xml");
	mGUISystem->setGUISheet(mGUISheet);

//	CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();
//	wmgr.getWindow((CEGUI::utf8*)"cmdQuit")->subscribeEvent(
//		CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MenuState::handleQuit, this));

	CEGUI::WindowManager::getSingletonPtr()->getWindow((CEGUI::utf8*)"cmdQuit")->subscribeEvent(
		CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MenuState::handleQuit, this));
}

void MenuState::exitState(void) {
	CEGUI::WindowManager::getSingleton().destroyWindow(mGUISheet);
	delete mGUISystem;
	mGUISystem = 0;
	delete mGUIRenderer;
	mGUIRenderer = 0;

	mSceneMgr->clearScene();
        //!! Note: This is supposed to be mSceneMgr->destroyAllCameras(); for CVS head
	mSceneMgr->removeAllCameras();
	mRoot->getAutoCreatedWindow()->removeAllViewports();
}

void MenuState::pauseState(void) {
}

void MenuState::resumeState(void) {
}

void MenuState::mouseMoved(MouseEvent* meEvent) {
	CEGUI::System::getSingleton().injectMouseMove(
		meEvent->getRelX() * mGUIRenderer->getWidth(), 
		meEvent->getRelY() * mGUIRenderer->getHeight());
	meEvent->consume();
}

void MenuState::mouseDragged(MouseEvent* meEvent) {
	this->mouseMoved(meEvent);
}

void MenuState::mousePressed(MouseEvent* meEvent) {
	CEGUI::System::getSingleton().injectMouseButtonDown(
		convertOgreButtonToCegui(meEvent->getButtonID()));
	meEvent->consume();
}

void MenuState::mouseReleased(MouseEvent* meEvent) {
	CEGUI::System::getSingleton().injectMouseButtonUp(
		convertOgreButtonToCegui(meEvent->getButtonID()));
	meEvent->consume();
}

void MenuState::mouseClicked(MouseEvent* meEvent) {
}

void MenuState::mouseEntered(MouseEvent* meEvent) {
}

void MenuState::mouseExited(MouseEvent* meEvent) {
}

void MenuState::keyClicked(KeyEvent* keEvent) {
}

void MenuState::keyPressed(KeyEvent* keEvent) {
	if (keEvent->getKey() == KC_ESCAPE) {
		this->changeState(CreditState::getInstance());				// <- This works just fine
	}

	if (keEvent->getKey() == KC_SPACE)
	{
		this->changeState(PlayState::getInstance());
	}
}

void MenuState::keyReleased(KeyEvent* keEvent) {
}

bool MenuState::handleQuit(const CEGUI::EventArgs& e) {
//	mViewPort->setBackgroundColour(ColourValue(0.0, 1.0, 0.0));// <- This works just fine
//	this->requestShutdown();											  // <- This works just fine
	this->changeState(CreditState::getInstance());					// <- This does not work just fine... :(
	return true;
}
Part of CreditState.cpp:

Code: Select all

#include <Ogre.h>
#include <OgreKeyEvent.h>

#include "CreditState.h"

using namespace Ogre;

CreditState CreditState::mCreditState;

void CreditState::enterState(void) {
	mRoot			= Root::getSingletonPtr();
	mInputDevice	= InputManager::getSingletonPtr()->getInputDevice();
	mSceneMgr		= mRoot->getSceneManager(ST_GENERIC);

	mCamera			= mSceneMgr->createCamera("CreditCamera");
	mViewport		= mRoot->getAutoCreatedWindow()->addViewport(mCamera);

	// get OverlayManager
	OverlayManager *mOverlayManager = OverlayManager::getSingletonPtr();

	// create Overlay
	mCreditOverlay = mOverlayManager->create("Overlay/Credits");

	// create OverlayContainer
	mCreditOverlayContainer = static_cast<OverlayContainer*>(
		mOverlayManager->createOverlayElement("Panel", "OverlayContainer/Credits"));

	mCreditOverlayContainer->setDimensions(1.0, 1.0);
	mCreditOverlayContainer->setPosition(0,0);
	mCreditOverlayContainer->setMaterialName("Credits");
	mCreditOverlay->add2D(mCreditOverlayContainer);
	mCreditOverlay->show();
}

void CreditState::exitState(void) {
	mCreditOverlay->hide();

	mSceneMgr->clearScene();
        //!! Note: This is supposed to be mSceneMgr->destroyAllCameras(); for CVS head
	mSceneMgr->removeAllCameras();
	mRoot->getAutoCreatedWindow()->removeAllViewports();
}
Thanks in advance for any help.
Last edited by pxL on Fri Nov 25, 2005 2:13 pm, edited 2 times in total.
User avatar
Exsortis
Halfling
Posts: 41
Joined: Mon Feb 07, 2005 6:16 pm
Location: Palmdale
Contact:

Re: Cegui and Gamestates (Switching state gives exeption)

Post by Exsortis »

pxL wrote:But when I run the program and click on the quit button I get a 'Unhandled exception at 0x0079ce96 in...'. When I comment away the changeState() method and try to do something else (like change the background colour or shutdown) it works just fine.
What's the exception that's being generated? I suspect that's the source of the problem.

-E
pxL
Gremlin
Posts: 158
Joined: Fri Oct 07, 2005 11:48 am
Location: Hilversum, The Netherlands
Contact:

Post by pxL »

Exception:
Unhandled exception at 0x0079ce96 in Sheathed Sword.exe: 0xC0000005: Access violation reading location 0x07cb7f58.

When I press 'Break' I get a tab 'Dissembly' at Visual.net and it breaks at:
0079CE96 mov ecx,dword ptr [eax+4]

Now I'm not that familiar with Visual so what now? :)
Last edited by pxL on Mon Oct 31, 2005 7:35 pm, edited 1 time in total.
User avatar
tuan kuranes
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 2653
Joined: Wed Sep 24, 2003 8:07 am
Location: Haute Garonne, France
x 4
Contact:

Post by tuan kuranes »

go up on the call stack.
(view->window->call stack and double click on lines.)

Maybe Cegui has to be notified of scenemanager changes in a way, no ?
pxL
Gremlin
Posts: 158
Joined: Fri Oct 07, 2005 11:48 am
Location: Hilversum, The Netherlands
Contact:

Post by pxL »

You had me searching there for a while (it's actually debug->windows->call stack).
But thanks now I know what to do when I get another exception :D.

Back to the exception:
The stack points at the CEGUIBase_d.dll (3x)...
So I think you're right, I need to notify CEGUI (But that is just guessing atm).
Last edited by pxL on Mon Oct 31, 2005 7:35 pm, edited 1 time in total.
User avatar
tuan kuranes
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 2653
Joined: Wed Sep 24, 2003 8:07 am
Location: Haute Garonne, France
x 4
Contact:

Post by tuan kuranes »

put a breakpoints (rightmousebutton on line) on CEGUI::WindowManager::getSingleton().destroyWindow(mGUISheet);

and rerun debug.

You should really read and master Debugging basics :

http://msdn.microsoft.com/library/defau ... ing101.asp
pxL
Gremlin
Posts: 158
Joined: Fri Oct 07, 2005 11:48 am
Location: Hilversum, The Netherlands
Contact:

Post by pxL »

Yea I already put breakpoints here and there to check if I would reach those breakpoints.
And it reaches everything at MenuState::exitState(), so like I said I thought it might be the CreditState
but also at CreditState::enterState() it reaches every breakpoint just fine.

I'll take a look at those debugging basics :).
pxL
Gremlin
Posts: 158
Joined: Fri Oct 07, 2005 11:48 am
Location: Hilversum, The Netherlands
Contact:

Post by pxL »

Well I was browsing the forum once again and I found this topic:
http://www.ogre3d.org/phpBB2/viewtopic. ... ight=cegui

Which contains the solution for my problem. So this topic is now solved. :D
Post Reply