Help with a simple C++ wrapper. [solved. I'm stupid.]
Posted: Mon Apr 30, 2007 8:28 am
Hello everyone!
I was making a kind of C++ wrapper so that I won't have to write code for device, scene manager etc every new time. The idea was also to bring some OOP style to my games.
Basically, I have three classes, each handling the initialization of device, scene and driver.
Here's the header file that defines all these classes, GameManager.h
and now its code file GameManager.Cpp
and here's the main source file, main.cpp
Please pardon me if I have pasted too much or ugly code, I'm not any expert yet.
I'm using Dev-Cpp on Windows XP. When I run the application, it crashes without drawing anything. From mu debugger I could find that there was memory access violation.
More than likely, there is a programming error, as I have coded some C style demos for Irrlicht before, that worked fine.
So please, it's an humble request please do take a look at the code and tell me what's wrong...I'm going nuts here.
I was making a kind of C++ wrapper so that I won't have to write code for device, scene manager etc every new time. The idea was also to bring some OOP style to my games.
Basically, I have three classes, each handling the initialization of device, scene and driver.
Here's the header file that defines all these classes, GameManager.h
Code: Select all
//GameManager.h
#ifndef _GAMEMANAGER_H
#define _GAMEMANAGER_H
#include <Irrlicht.h>
using namespace irr;
using namespace core;
using namespace io;
using namespace video;
using namespace scene;
using namespace gui;
/************** the Device class ***********************/
class Device
{
private:
IrrlichtDevice *device;
public:
Device()
{
device = NULL;
}
int setup(dimension2d<s32> resolution, bool fullscreen);
bool run(); //if device is running
IrrlichtDevice* get() { return device; }
};
/****************** the SceneManager class *****************/
class SceneManager
{
private:
ISceneManager *smgr;
public:
SceneManager()
{
smgr = NULL;
}
int setup(ISceneManager *sm)
{
smgr = sm;
if(smgr == NULL)
return -1;
return 0;
}
ISceneManager* get() { return smgr; }
};
/********************* the Driver class ********************/
class Driver
{
private:
IVideoDriver* driver;
public:
Driver()
{
driver = NULL;
}
//int setup(Device* dev);
int setup(IVideoDriver *vd)
{
driver = vd;
if(driver == NULL)
return -1;
return 0;
}
IVideoDriver* get() { return driver; }
};
#endif
Code: Select all
//code file for GameManager
#include "GameManager.h"
/*****************Definitions for Device class ********************/
int Device::setup(dimension2d<s32> resolution, bool fullscreen)
{
device = createDevice(EDT_OPENGL, resolution, 32, false, false, fullscreen, 0);
if(device == NULL)
return -1;
return 0;
}
bool Device::run()
{
if(device->run())
return true;
return false;
}
Code: Select all
//my first cool game
#include <iostream>
#include <irrlicht.h>
#include "GameManager.h"
using namespace std;
using namespace irr;
using namespace core;
using namespace io;
using namespace video;
using namespace scene;
using namespace gui;
int main(int argc, char *argv[])
{
Device *device;
if(device->setup(dimension2d<s32>(600, 400), false) == -1)
return -1;
Driver *driver;
if(driver->setup(device->get()->getVideoDriver()) == -1)
return -1;
SceneManager *sceneManager;
if(sceneManager->setup(device->get()->getSceneManager()) == -1)
return -1;
while(device->run())
{
driver->get()->beginScene(true, true, SColor(255, 100, 20, 50));
sceneManager->get()->drawAll();
driver->get()->endScene();
}
device->get()->drop();
return 0;
}
I'm using Dev-Cpp on Windows XP. When I run the application, it crashes without drawing anything. From mu debugger I could find that there was memory access violation.
More than likely, there is a programming error, as I have coded some C style demos for Irrlicht before, that worked fine.
So please, it's an humble request please do take a look at the code and tell me what's wrong...I'm going nuts here.