Problem with pointers
Posted: Thu Jan 03, 2008 8:52 pm
Hi everybody!
I have a small game project going on and first I have to say that this forum has been very helpfull and I thank you for it. However I guess the wall finaly came and I got stuck badly.
The problem came as I tried to organize my work, use classes and inheritance mainly. It seems that there is some problem getting the irrlicht device created in Core -class to SceneManager -class.
Idea is that Core keeps the irrlicht engine in memory and Game handles the logic of showing correct scenes and asks SceneManager to load right maps.
Code compiles but creates immediatly this error after I start debugging it:
Unhandled exception at 0x00412546 in GameAmentia.exe: 0xC0000005: Access violation reading location 0xccccccd0.
And the compiler points this part in Core -class:
//return a funtion to engine core fuctions here
IrrlichtDevice* CGameCore::getDevice()
{
return pDevice;
}
Main parts of code are here, they use some of the tutorials and code snippets posted here in forums. They work in the "spagetti" code.
You would save my day if you could help me a bit, I have a bad habit to forget something small wich causes code not to work.
Core.h
Core.cpp
Game.h
Game.cpp
SceneManager.h
SceneManager.cpp
I have a small game project going on and first I have to say that this forum has been very helpfull and I thank you for it. However I guess the wall finaly came and I got stuck badly.
The problem came as I tried to organize my work, use classes and inheritance mainly. It seems that there is some problem getting the irrlicht device created in Core -class to SceneManager -class.
Idea is that Core keeps the irrlicht engine in memory and Game handles the logic of showing correct scenes and asks SceneManager to load right maps.
Code compiles but creates immediatly this error after I start debugging it:
Unhandled exception at 0x00412546 in GameAmentia.exe: 0xC0000005: Access violation reading location 0xccccccd0.
And the compiler points this part in Core -class:
//return a funtion to engine core fuctions here
IrrlichtDevice* CGameCore::getDevice()
{
return pDevice;
}
Main parts of code are here, they use some of the tutorials and code snippets posted here in forums. They work in the "spagetti" code.
You would save my day if you could help me a bit, I have a bad habit to forget something small wich causes code not to work.
Core.h
Code: Select all
#ifndef _CORE_H_
#define _CORE_H_
#include <irrlicht.h>
//engine namespaces
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class CGameCore //here we define the class and create our poiners
{
public:
//functions providing acess to engine componants.
//GUI Environment
IGUIEnvironment* getGUIEnv();
//Scene Manager
ISceneManager* getSceneMngr();
//video driver
IVideoDriver* getVideo();
//Engine core device
IrrlichtDevice* getDevice();
//constructor
CGameCore();
//destructor
virtual ~CGameCore();
private:
//used to insanianate device
void InitalizeDevice();
//corisponding pointers to engine core functions
IrrlichtDevice* pDevice;
IVideoDriver* pDrv;
ISceneManager* pSceneMngr;
IGUIEnvironment* pGUIEnv;
};
#endif
Code: Select all
#include "Core.h"
#include <iostream>
CGameCore::CGameCore()
{
InitalizeDevice();
}
CGameCore::~CGameCore()
{
}
void CGameCore::InitalizeDevice()
{
pDevice = createDevice(EDT_DIRECT3D9, core::dimension2d<s32>(1024,768), 32, false, true, true);
//Init engine functions and store the pointers to them
pSceneMngr = pDevice->getSceneManager();
pDrv = pDevice->getVideoDriver();
pGUIEnv = pDevice->getGUIEnvironment();
}
//return a funtion to engine core fuctions here
IrrlichtDevice* CGameCore::getDevice()
{
return pDevice;
}
// GUI
IGUIEnvironment* CGameCore::getGUIEnv()
{
return pGUIEnv;
}
//Video
IVideoDriver* CGameCore::getVideo()
{
return pDrv;
}
//Scene Manager
ISceneManager* CGameCore::getSceneMngr()
{
return pSceneMngr;
}
Code: Select all
#ifndef _GAME_H_
#define _GAME_H_
#include <irrlicht.h>
#include "Core.h"
#include "SceneManager.h"
#include "CameraManager.h"
using namespace irr;
//! Main entry point to game
class CGame
{
public:
CGame();
~CGame();
bool run();
private:
CGameCore * pManager;
SceneManager *gameSceneManager;
CameraManager *gameCameraManager;
};
#endif
Code: Select all
#include "Game.h"
CGame::CGame()
{
CGameCore *pManager = new CGameCore;
gameSceneManager = new SceneManager(pManager);
gameCameraManager = new CameraManager;
}
CGame::~CGame()
{
delete gameSceneManager;
delete gameCameraManager;
delete pManager;
}
//! Main game loop
bool CGame::run()
{
// Name of game, displayed in window if windowed
pManager->getDevice()->setWindowCaption(L"some app name here");
int lastFPS = -1;
// Keep running game loop if device exists
gameSceneManager->SceneMansion();
gameCameraManager->FirstPersonCamera();
while(pManager->getDevice()->run())
{
pManager->getVideo()->beginScene(true,true, SColor(255, 100, 101, 140));
pManager->getSceneMngr()->drawAll();
pManager->getGUIEnv()->drawAll();
pManager->getVideo()->endScene();
}
{
pManager->getDevice()->drop();
}
return 0;
}
Code: Select all
#ifndef _SCENEMANAGER_H_
#define _SCENEMANAGER_H_
#include "Core.h"
#include "irrlicht.h"
class SceneManager
{
public:
SceneManager(CGameCore *gameCore);
~SceneManager();
void SceneMansion();
private:
IrrlichtDevice *gameDevice;
scene::ISceneManager* sm;
scene::IQ3LevelMesh* quakeLevelMesh;
scene::ISceneNode* quakeLevelNode;
scene::ITriangleSelector* mapSelector;
};
#endif
Code: Select all
#include "SceneManager.h"
SceneManager::SceneManager(CGameCore *gameCore){
gameDevice = gameCore->getDevice();
sm = gameDevice->getSceneManager();
gameDevice->getFileSystem()->addZipFileArchive("media/irrlicht.dat");
gameDevice->getFileSystem()->addZipFileArchive("media/map-20kdm2.pk3");
}
SceneManager::~SceneManager(){}
void SceneManager::SceneMansion()
{
quakeLevelMesh = (scene::IQ3LevelMesh*) sm->getMesh("maps/20kdm2.bsp");
if (quakeLevelMesh)
{
u32 i;
//move all quake level meshes (non-realtime)
core::matrix4 m;
m.setTranslation ( core::vector3df(-1300,-70,-1249) );
for ( i = 0; i!= scene::quake3::E_Q3_MESH_SIZE; ++i )
{
sm->getMeshManipulator()->transformMesh ( quakeLevelMesh->getMesh(i), m );
}
quakeLevelNode = sm->addOctTreeSceneNode(quakeLevelMesh->getMesh( scene::quake3::E_Q3_MESH_GEOMETRY),0,20);
if (quakeLevelNode)
{
//quakeLevelNode->setPosition(core::vector3df(-1300,-70,-1249));
quakeLevelNode->setVisible(true);
// create map triangle selector
mapSelector = sm->createOctTreeTriangleSelector(quakeLevelMesh->getMesh(0),
quakeLevelNode, 128);
// if not using shader and no gamma it's better to use more lighting, because
// quake3 level are dark
quakeLevelNode->setMaterialType ( video::EMT_LIGHTMAP_M4 );
// set additive blending if wanted
}
// the additional mesh can be quite huge and is unoptimized
scene::IMesh * additional_mesh = quakeLevelMesh->getMesh ( scene::quake3::E_Q3_MESH_ITEMS );
for ( i = 0; i!= additional_mesh->getMeshBufferCount (); ++i )
{
scene::IMeshBuffer *meshBuffer = additional_mesh->getMeshBuffer ( i );
const video::SMaterial &material = meshBuffer->getMaterial();
//! The ShaderIndex is stored in the material parameter
s32 shaderIndex = (s32) material.MaterialTypeParam2;
// the meshbuffer can be rendered without additional support, or it has no shader
const scene::quake3::SShader *shader = quakeLevelMesh->getShader ( shaderIndex );
if ( 0 == shader )
{
continue;
}
// Now add the MeshBuffer(s) with the current Shader to the Manager
sm->addQuake3SceneNode ( meshBuffer, shader );
}
// original mesh is not needed anymore
quakeLevelMesh->releaseMesh ( scene::quake3::E_Q3_MESH_ITEMS );
}
}