I am creating a quadtree for the first time with some drawing and classifying of objects. I am getting an access violation error with the driver variable.
I am new to this so the class is not complete, but what am I missing ?
Code: Select all
#include <irrlicht.h>
#include "driverChoice.h"
#include "EventLogger.h"
using namespace irr;
#define LOG_TESTCLASS LOG_COLOR_RED | LOG_BOLD
#define LOG_TESTFN LOG_COLOR_DK_BLUE | LOG_ITALICS | LOG_UNDERLINE
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
enum
{
MAZE_X1 = 40,
MAZE_Y1 = 20,
XBlocks = 20,
YBlocks = 20,
BlockWidth = (480-MAZE_Y1)/XBlocks,
//BlockWidth = 100,
Depth = 20,
};
class CHuman
{
public:
CHuman(void);
int iX, iY;
float xVel,yVel;
float fPosX, fPosY;
bool bHasTarget;
int xTarget, yTarget;
int xDir, yDir;
void Draw(video::IVideoDriver* driver,irr::video::SColor color = irr::video::SColor(255,255,255,255));
void Update(SEvent& event);
};
CHuman::CHuman(void)
{
iX = 0;
iY = 0;
xVel = 0.0;
yVel = 0.0;
bHasTarget=false;
xTarget=0;
yTarget=0;
}
void CHuman::Draw(video::IVideoDriver* driver,irr::video::SColor color)
{
driver->draw2DRectangle(color,irr::core::rect<irr::s32>(fPosX+2,fPosY+2,
fPosX+BlockWidth-2,fPosY+BlockWidth-2));
}
void CHuman::Update(SEvent& event)
{
}
class CAlien
{
public:
CAlien(void);
int iX, iY;
void Update(int iHumanX,int iHumanY);
void Draw(video::IVideoDriver* driver,irr::video::SColor color = irr::video::SColor(255,255,255,255));
};
CAlien::CAlien(void)
{
iX = 0;
iY = 0;
}
void CAlien::Update(int iHumanX,int iHumanY)
{
}
void CAlien::Draw(video::IVideoDriver* driver,irr::video::SColor color)
{
driver->draw2DRectangle(color,irr::core::rect<irr::s32>(MAZE_X1+iX*BlockWidth+2,MAZE_Y1+iY*BlockWidth+2,
MAZE_X1+(iX+1)*BlockWidth-2,MAZE_Y1+(iY+1)*BlockWidth-2));
}
CHuman Human; // global player
CAlien Alien; // global alien
class MyEventReceiver : public IEventReceiver
{
public:
irr::EKEY_CODE MyKey;
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event)
{
//MyKey = KEY_KEY_0;
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
//MyKey = event.KeyInput.Key; // get this key
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
KeyIsUp[event.KeyInput.Key] = event.KeyInput.PressedDown;
}
return false;
}
// This is used to check whether a key is being held down
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
virtual bool IsKeyUp(EKEY_CODE keyCode) const
{
return KeyIsUp[keyCode];
}
MyEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
{
KeyIsDown[i] = false;
KeyIsUp[i] = false;
}
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
bool KeyIsUp[KEY_KEY_CODES_COUNT];
};
class CQuadTree
{
public:
CQuadTree(void);
void Draw(video::IVideoDriver* driver);
void CreateChildren(void);
int X1,Y1,X2,Y2; // bounds
int iSize;
CQuadTree* parent, *NW,*NE,*SW,*SE;
};
CQuadTree::CQuadTree(void)
{
}
void CQuadTree::Draw(video::IVideoDriver* driver)
{
driver->draw2DRectangleOutline(irr::core::recti(X1,Y1,X2,Y2),irr::video::SColor(255,0,255,0));
//if (NW)
// NW->Draw(driver);
}
void CQuadTree::CreateChildren(void)
{
NW = new CQuadTree();
NW ->iSize = iSize/2;
NW ->X1 = X1;
NW ->Y1 = Y1;
NW -> X2 = X1+NW->iSize;
NW -> Y2 = Y1+NW->iSize;
NE = new CQuadTree();
NE ->iSize = iSize/2;
NE ->X1 = X1+NW->iSize;
NE ->Y1 = Y1;
NE -> X2 = NE->X1+NE->iSize;
NE -> Y2 = NE->Y1+NE->iSize;
SW = new CQuadTree();
SW ->iSize = iSize/2;
SW ->X1 = X1;
SW ->Y1 = Y1+SW->iSize;
SW -> X2 = X1+SW->iSize;
SW -> Y2 = SW->Y1+SW->iSize;
SE = new CQuadTree();
SE ->iSize = iSize/2;
SE ->X1 = X1+SE->iSize;
SE ->Y1 = Y1+SE->iSize;
SE -> X2 = SE->X1+SE->iSize;
SE -> Y2 = SE->Y1+SE->iSize;
}
int main()
{
// ask user for driver
video::E_DRIVER_TYPE driverType=driverChoiceConsole();
if (driverType==video::EDT_COUNT)
return 1;
MyEventReceiver receiver;
IrrlichtDevice* device = createDevice(driverType,
core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
if (device == 0)
return 1; // could not create selected driver.
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* env = device->getGUIEnvironment();
Human.fPosX = MAZE_X1+Human.iX*BlockWidth;
Human.fPosY = MAZE_Y1+Human.iY*BlockWidth;
int iNewX,iNewY;
video::ITexture* cheese = driver->getTexture("../../media/cheese.png");
driver->makeColorKeyTexture(cheese, core::position2d<s32>(0,0));
video::ITexture* mouse = driver->getTexture("../../media/mouse.png");
driver->makeColorKeyTexture(mouse, core::position2d<s32>(0,0));
core::rect<s32> cheese_rect(0,0,20,20);
core::rect<s32> mouse_rect(0,0,20,20);
gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
irr::core::stringw str;
CQuadTree QuadTree;
CQuadTree* Current;
QuadTree.iSize = 400;
QuadTree.X1 = 200;
QuadTree.Y1 = 50;
QuadTree.X2 = QuadTree.X1+QuadTree.iSize;
QuadTree.Y2 = QuadTree.Y1+QuadTree.iSize;
//QuadTree.CreateChildren();
Current = &QuadTree;
for (int i=0;i<4;i++)
{
Current->CreateChildren();
Current = Current->NW;
}
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, irr::video::SColor(255,255,255,255) );
while (Current!=0)
{
Current->Draw(driver);
Current = Current->NW;
}
driver->endScene();
}
device->drop();
return 0;
}