createDevice() crashing program!

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
pszlachetka
Posts: 23
Joined: Mon Aug 30, 2004 12:30 am

createDevice() crashing program!

Post by pszlachetka »

Segmenation fault at the createDevice() function. Apparently my event receiver isn't good enough for it ;)

CGame.h

Code: Select all

#ifndef CGame_H_
#define CGame_H_

//Include Irrlicht header file and use its namespaces
#include <irrlicht.h>

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

//Include title screen header, which includes the CGameState header.
#include "CTitleScreen.h"

//forward declaration of the event receiver, so the CGame class knows what
//that the event receiver class exists
class CMyEventRec;

class CGame
{
    public:
        
    CGame();
    ~CGame();//drops the irrlicht device
    
    /*This will initialize both the CGame class, and the event receiver class.
    It will dynamically create a new event receiver class to the pointer in the private
    section of the CGame class. e.g. CMyEventRec* myEventRec; a "this" pointer will be
    passed into its constructor. It will then initialize all the usual Irrlicht pointers*/
    void CGame_Init();
    
    //Will call on the current gamestate's Render function
    void Render();
    
    //Will call on the current gamestate's Update function
    bool Update();
    
    //usual Irrlicht engine pointers
    IrrlichtDevice*    device;
    ISceneManager*     sceneMgr;
    IVideoDriver*      driver;
    IGUIEnvironment*   guiEnv;
    
    //The pointer to the current gamestate
    CGameState*         currentGameState;
    
    //The pointer which the event receiver class is allocated to
    CMyEventRec*        eventRec;
    
    bool OnEvent( SEvent event );
    
    /////////////////////////////////////////////////
    /////////////GAME SPECIFIC FUNCTIONS/////////////
    /////////////////////////////////////////////////
    
    //Nothing as of yet.
     
};
////////////////////////
//EVENT RECEIVER CLASS//
////////////////////////

class CMyEventRec : public IEventReceiver
{   
    //Pointer to the CGame class
    CGame*        ptrCGame;
    
    //Declare the CGame class a friend of this class
    //friend class CGame;
    public:
        
    //Constructor takes a pointer to the CGame class as a parameter, and assigns
    //it to CGame* ptrCGame;
    CMyEventRec( CGame* gam ){ ptrCGame = gam; }
    
    //Destructor does nothing so far
    ~CMyEventRec();
    
    //The virtual OnEvent function
    virtual bool OnEvent( SEvent event );
    
    //Engine pointers inside the event receiver class
    IrrlichtDevice*    ptrDevice;
    ISceneManager*     ptrMgr;
    IVideoDriver*      ptrDriver;
    IGUIEnvironment*   ptrGUI;  
    
    
    ///////////////////////////////////////////////////////
    //////////////////GAME SPECIFIC STUFF//////////////////
    ///////////////////////////////////////////////////////

    //Nothing as of yet
    
    private:
    
}; 
    
#endif
CGame.cpp

Code: Select all

#include "CGame.h"

//Does nothing.
CGame::CGame()
{
}

//The destructor drops the irrlicht device
CGame::~CGame()
{
    device->drop();
}

//Initialization function
void CGame::CGame_Init()
{
    //allocate a new event receiver class
    eventRec = new CMyEventRec( this );
    
    //Create device
    device = createDevice( EDT_DIRECTX8, 
                           dimension2d<s32>( 640, 480 ), 
                           32, 
                           false, 
                           false, 
                           eventRec );
                           
    //Set a window caption
    device->setWindowCaption(L"POLISH PONG - Peter Szlachetka");
    
    //Initialize the engine pointers                                             
    sceneMgr = device->getSceneManager();
    driver = device->getVideoDriver();
    guiEnv = device->getGUIEnvironment();
    
    //Create the title screen upon execution
    currentGameState = new CTitleScreen( device, sceneMgr, driver, guiEnv );
}

//Main render function. It calls on the current game state's render fnction
void CGame::Render()
{
    currentGameState->Render();
}

//Main update function. It calls on the current game state's update function
bool CGame::Update()
{
    currentGameState->Update();
}

//CGame OnEvent function calls on the current game state's OnEvent function
bool CGame::OnEvent( SEvent event )
{
    if( currentGameState )
    {
        currentGameState->OnEvent( event );
    }    
}

//////CMyEventRec class functions///////

//Main event function, calls the CGame's OnEvent function
bool CMyEventRec::OnEvent( SEvent event )
{
    return ptrCGame->OnEvent( event );
}

/////////////////////////////////////////////////////////////
////////////////////GAME SPECIFIC STUFF//////////////////////
/////////////////////////////////////////////////////////////

//nothing as of yet
main.cpp

Code: Select all

#include "CGame.h"

int main()
{
    
    CGame game;
    
    game.CGame_Init();
    
    while( game.Update() )
    {
        game.Render();
    }    
    
    return 0;
}
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

Change

eventRec );

to

&eventRec );

Hope this helps :).
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
pszlachetka
Posts: 23
Joined: Mon Aug 30, 2004 12:30 am

Post by pszlachetka »

When I do that, I get this compile-time error:

C:\Documents and Settings\Owner\Desktop\POLISH PONG\CGame.cpp In member function `void CGame::CGame_Init()':
26 C:\Documents and Settings\Owner\Desktop\POLISH PONG\CGame.cpp cannot convert `CMyEventRec**' to `irr::IEventReceiver*' for argument `6' to `irr::IrrlichtDevice*
C:\Documents and Settings\Owner\Desktop\POLISH PONG\Makefile.win [Build Error] [CGame.o] Error 1

:( Any other suggestions? I don't know why this is causing me problems. The program seems kinda straigh-forward so far to me. Thanks for the effort though :)

-Peter
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

Why don't you just to:

CMyEventRec eventRec;

And then &eventRec which should work.
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
DrBenito
Posts: 35
Joined: Mon Jul 19, 2004 8:50 pm
Location: Newcastle, UK
Contact:

Post by DrBenito »

have you tried:

device = createDevice( EDT_DIRECTX8,
dimension2d<s32>( 640, 480 ),
32,
false,
false,
& (*eventRec) );

? not sure off hand if that will work, havent got code to test on im afraid :)

HTH
Ben
pszlachetka
Posts: 23
Joined: Mon Aug 30, 2004 12:30 am

Post by pszlachetka »

Tried that DrBenito, didn't work. Crashes exactly the same as before. I want to dynamically allocate this class, and I've seen another person's code do the exact same method flawlessly. Thanks for the suggestions though. Any others? This is a bastard.

-Peter
DrBenito
Posts: 35
Joined: Mon Jul 19, 2004 8:50 pm
Location: Newcastle, UK
Contact:

Post by DrBenito »

Ah right...

well the way that i'm doing it...

Im declaring a CEventReceiver * eventReceiver

and rather than passing a reference direct to the constructor - createDevice()

I pass it later in device->setEventReceiver(eventReceiver)

and that has no problems at all... :)

Apologies if above makes no sense, I haven't had my morning coffee yet ;)

Cheers
Ben
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

DrBenito wrote:Im declaring a CEventReceiver * eventReceiver

and rather than passing a reference direct to the constructor - createDevice()

I pass it later in device->setEventReceiver(eventReceiver)

and that has no problems at all... :)
Indeed, this one works. Had the same problem some time ago, but forgot it and thx to your setEventReceiver I remember it again :).
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
pszlachetka
Posts: 23
Joined: Mon Aug 30, 2004 12:30 am

Post by pszlachetka »

Yes! It's about time this works! Thanks a lot for the help bal and DrBenito! What's wrong with the method I was doing anyways? I'm curious to find out why this had to frustrate me so much. :(

Thanks again!

-Peter
Post Reply