font getDimensions

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
Quantum1982
Posts: 45
Joined: Mon Apr 23, 2012 9:31 am

font getDimensions

Post by Quantum1982 »

Hi

I am testing the font->getDimensions function, but the program crashes. Here is the code :

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,
                        Empty = 0,
                        Wall = 1,
                        Movable = 2
    };
/*
#define Empty = 0;
#define Wall = 1;
#define Movable = 2;
*/
 
class CTextReader
{
public:
        irr::core::stringw str;
        wchar_t *str2;
        int X1,Y1,X2,Y2;
        irr::u32 Width, Height;
        int iSpeed;
        irr::video::SColor FontColor, BackGroundColor;
        gui::IGUIFont* font;
 
        void SetPos(int iX1,int iY1);
        void Update(void);
        void Draw(video::IVideoDriver* driver,gui::IGUIEnvironment* env);
};
 
void CTextReader::SetPos(int iX1,int iY1)
{
        X1 = iX1;
        Y1 = iY1;
        X2 = X1+Width;
        Y2 = Y1+Height;
}
void CTextReader::Update(void)
{
        
}
void CTextReader::Draw(video::IVideoDriver* driver,gui::IGUIEnvironment* env)
{
        driver->draw2DRectangle(BackGroundColor,irr::core::rect<irr::s32>(X1,Y1,X2,Y2));
        font->draw(str,irr::core::rect<irr::s32>(X1,Y1,X2,Y2),FontColor);
}
 
int main()
{
 
        // ask user for driver
        video::E_DRIVER_TYPE driverType=driverChoiceConsole();
        if (driverType==video::EDT_COUNT)
                return 1;
 
        
 
        IrrlichtDevice* device = createDevice(driverType,
                        core::dimension2d<u32>(640,480), 16, false, false, false);
                        
 
        
 
        if (device == 0)
                return 1; // could not create selected driver.
 
        
        /*
        First, we add standard stuff to the scene: A nice irrlicht engine
        logo, a small help text, a user controlled camera, and we disable
        the mouse cursor.
        */
 
        video::IVideoDriver* driver = device->getVideoDriver();
        scene::ISceneManager* smgr = device->getSceneManager();
        gui::IGUIEnvironment* env = device->getGUIEnvironment();
        
 
        CTextReader TextReader;
        
        TextReader.str = "Text reader test";
        TextReader.str2 = (wchar_t *)"Text reader test";
        TextReader.Width = TextReader.font->getDimension(TextReader.str2).Width;
        TextReader.Height = TextReader.font->getDimension(TextReader.str2).Height;
 
        TextReader.SetPos(50,50);
        TextReader.font = device->getGUIEnvironment()->getFont("../../media/bigfont.png");
        TextReader.FontColor = irr::video::SColor(255,0,255,255);
        TextReader.BackGroundColor = irr::video::SColor(255,0,255,0);
        
 
        //LOG_INIT("TestLog.html");
 
 
        while(device->run())
        if (device->isWindowActive())
        {
                
 
                driver->beginScene(true, true, 0 );
 
                TextReader.Draw(driver,env);
 
                driver->endScene();
        }
        //LOG_TERM();
        device->drop();
        
        return 0;
}
 
 
 
It crashes at this line :

Code: Select all

 
TextReader.Width = TextReader.font->getDimension(TextReader.str2).Width;
 
What am I missing ?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: font getDimensions

Post by CuteAlien »

Because you try to use it before you initialize it.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply