Help with a simple C++ wrapper. [solved. I'm stupid.]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
gamemaker981
Posts: 12
Joined: Sat Jan 27, 2007 4:21 am

Help with a simple C++ wrapper. [solved. I'm stupid.]

Post by gamemaker981 »

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

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

and now its code file GameManager.Cpp

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;
}     

and here's the main source file, main.cpp

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;
}

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. :(
Last edited by gamemaker981 on Mon Apr 30, 2007 10:44 am, edited 1 time in total.
Why did I choose Irrlicht? Simple: It has the most helpful tutorials. :-)
zakhrim
Posts: 7
Joined: Mon Apr 30, 2007 7:03 am
Location: Italy

Post by zakhrim »

I'm not an expert of C++ programming (too many years with Java and C#... i forgot pointers! :oops: ) but it seems you have a problem of dangling pointers. Try to initialize your Device pointers with "new" instruction

Code: Select all

Device *device = new Device();
and so on for the other objects.
Try... i hope...
And also try to have a look to "smart pointers" (search with Google): it's an "easy" way to manage dangling pointers without getting mad!

Zak[/code]
gamemaker981
Posts: 12
Joined: Sat Jan 27, 2007 4:21 am

Post by gamemaker981 »

well, thanks for trying.

We don't need new to allocate pointer in main() function, I guess.
But what I did find out was that I never initialized pointers to IrrlichtDevice, ISceneManager etc!!

But here's the problem again, when I modify the setup() function like this:

Code: Select all

int setup(ISceneManager *sm) 
              { 
                smgr = new ISceneManager; //new line of code                 
                smgr = sm;
                if(smgr == NULL)
                  return -1;
                
                return 0;  
              }
my compiler reports an error, saying:

cannot allocate an object of type `irr::scene::ISceneManager'
because the following virtual functions are abstract:

virtual irr::scene::IAnimatedMesh* irr::scene::ISceneManager::getMesh(const irr::c8*)

virtual irr::scene::IMeshCache* irr::scene::ISceneManager::getMeshCache()


etc.

Please tell me what is going on!
Why did I choose Irrlicht? Simple: It has the most helpful tutorials. :-)
zakhrim
Posts: 7
Joined: Mon Apr 30, 2007 7:03 am
Location: Italy

Post by zakhrim »

I guess ISceneManager is an abstract class.
It means that this class exposes methods that are implemented by derived classes. And also it means that you cannot instanciate objects of this class.

I think there are methods like createDevice(...) -don't remember the signature of the method- that creates the object deriving from ISceneManager (it's a common pattern to abstract the program from the OS)

Zak
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

We don't need new to allocate pointer in main() function, I guess.
He??? :shock:
I don't know where you have seen that. I've not really look at your code, but it's clear your error is here, zakhrim is right. You need to initialize your device with a new. I think you should try what zakhrim said.
gamemaker981
Posts: 12
Joined: Sat Jan 27, 2007 4:21 am

Post by gamemaker981 »

Oh YES!!!
Works smooth now! :)

Thanks a lot guys!
Expect some more stupid questions from me in future. :D
Why did I choose Irrlicht? Simple: It has the most helpful tutorials. :-)
Post Reply