Program crashes at driver->getTexture()

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

Program crashes at driver->getTexture()

Post by pszlachetka »

I've got the BMP file in the correct place, and the GS_driver pointer IS initialized correctly before the function call because I checked, so what could be causing the crash? Oh, and the program also increases in memory usage like 3kb every 2 seconds for some reason. How can I solve this problem? Thanks!

-Peter

Code: Select all

#ifndef CTitleScreen_H_
#define CTitleScreen_H_

//Include the CGameState class, since CTitleScreen is being derived from it
#include "CGameState.h"

//This class will control the title screen. It will contain all the resources needed for
//the title screen.

//It inherites the engine pointers from the CGameState class, as well as the Update,
//Render, and OnEvent functions, and redefines them to suit its own needs.
class CTitleScreen : public CGameState
{
    public:
    
    //Constuctor sets engine pointers in the ->CGameState<- class
    CTitleScreen( IrrlichtDevice* dev, ISceneManager* mgr, IVideoDriver* driv, IGUIEnvironment* guienv );
    
    //Will clean up after title screen is destroyed.
    ~CTitleScreen();
    
    //Will return true if the device is running, and the driver points to something
    bool Update();
    
    //Draws the stuff
    void Render();
    
    //Handles the events for the title screen
    bool OnEvent( SEvent event );
   
    private:
        
    static ITexture*    titleBMP;
 
};

#endif 

Code: Select all

#include "CTitleScreen.h"

ITexture* CTitleScreen::titleBMP = 0;

//Constructor passes the engine pointers to the CGameState class!
CTitleScreen::CTitleScreen( IrrlichtDevice* dev, ISceneManager* mgr, IVideoDriver* driv, IGUIEnvironment* guienv ) : CGameState( dev, mgr, driv, guienv )
{
    titleBMP = GS_driver->getTexture( "titleBMP.bmp" );
}

//Cleans up its inherited engine pointers.
CTitleScreen::~CTitleScreen()
{
    GS_device = 0;
    GS_sceneMgr = 0;
    GS_driver = 0;
    GS_guiEnv = 0;
}

//Its own update function, if the device is running, and the driver points to
//something, return true, otherwise return false.
bool CTitleScreen::Update()
{
    if( GS_device->run() && GS_driver )
    {
        return true;
    }
    else
    {
        return false;
    }
}    

//Its own render function, draws the stuff.
void CTitleScreen::Render()
{
    //Looking for quality, not speed.
    GS_driver->setTextureCreationFlag( ETCF_OPTIMIZED_FOR_QUALITY, true );
    
    ////////////////BEGIN RENDERING////////////////
    GS_driver->beginScene(true, true, SColor(0,100,100,100));
    
    //Displays background.
    GS_driver->draw2DImage( titleBMP, position2d<s32>(0,0) );
    
    GS_guiEnv->addButton( rect<s32>( 300, 300, 350, 325 ), 0, 101, L"Quit" );
    
    GS_guiEnv->addButton( rect<s32>( 275, 250, 325, 275 ), 0, 103, L"2 Player" );
    
    GS_guiEnv->drawAll();
    
    GS_sceneMgr->drawAll();
    
    GS_driver->endScene();
}    

//Its own OnEvent function, handles the events that occur during the title screen gamestate
bool CTitleScreen::OnEvent( SEvent event )
{
    if( event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown )
    {
        switch( event.KeyInput.Key )
        {
            case KEY_ESCAPE: 
                 { 
                     GS_device->closeDevice(); 
                     break; 
                 }
        }
    }
    
    if (event.EventType == EET_GUI_EVENT)
    {
        s32 id = event.GUIEvent.Caller->getID();
        
        switch(event.GUIEvent.EventType)
        {
            case EGET_BUTTON_CLICKED:
            {
                if (id == 101)
                {
                    GS_device->closeDevice();
                    return true;
                }
                
                if( id == 103)
                {
                    //CGame.setToTwoPlayer();
                    GS_device->closeDevice();
                }    
                
            }
        }
    }    
    return false;
}           
saigumi
Posts: 921
Joined: Fri Aug 22, 2003 11:31 am
Location: St. Louis, MO USA
Contact:

Post by saigumi »

That should work.. but I saw another problem. You are adding the buttons during every render frame. Within about 3 seconds you going to have 3 seconds * you Frames-Per-Second in buttons.


These only need to be called once:

GS_guiEnv->addButton( rect<s32>( 300, 300, 350, 325 ), 0, 101, L"Quit" );

GS_guiEnv->addButton( rect<s32>( 275, 250, 325, 275 ), 0, 103, L"2 Player" );
Crud, how do I do this again?
pszlachetka
Posts: 23
Joined: Mon Aug 30, 2004 12:30 am

Post by pszlachetka »

Ahh, your button suggestion fixed the increase in memory usage problem. But I still don't understand why I can't load the texture, I want to load the textures once, so I don't have to do this GS_driver->draw2DImage( GS_driver->getTexture( blah ), blah ); which works perfectly fine.

What could possibly be causing this crash? Thanks!

-Peter
Post Reply