I need to display text in an OpenGL window. I have tried FTGL but I got GP fault immediately in font->FaceSize(size). I am using Debian and wxWidgets.
- the OpenGL window is a part of a frame window (a “control”). This should work. I have written more such OpenGL apps and they work.
- the crashing code:
int argc = 0;
fnt = new FTGLPixmapFont("/usr/share/fonts/truetype/ttf-liberation/LiberationSans-Regular");
bb = fnt->FaceSize(72); // crash
- InitGL() is called from the OnPaint handler of the OpenGL window. The code above is the first touch to OpenGL and FTGL in the app. It is known that in wxWidgets, the first OpenGL call must be made from this handler. You get a GP fault otherwise.
- OpenGL Device Context is set.
- Creating the font passes, the debugger shows a non-NULL pointer. In fact, the font may be created in the MyCanvas ctor.
- GLUT: I have noted that FTGL examples search for GLUT so I tried glutInit() but it solves nothing.
- fnt->FaceSize() causes GP fault. The debugger states that the fault is in FTSize::CharSize() of FTGL. The end.
If I comment out the crashing statement, I get a GP fault in fnt->Render(). Something is wrong with my coding, I was able to compile and run FTGL examples on my comp - but they are GLUT apps which use GLUT window and GLUT controlling of the app, I cannot use this.
What might be wrong?
If you don’t get an answer here you could try the FTGL mailing list. https://lists.sourceforge.net/lists/listinfo/ftgl-devel they are very helpful.
Thanks, but I’ve already made FTGL work. The problem was caused by my too quick reading examples and code snippets on the net and by too relying on non-NULL pointers. The .ttf cannot be omitted in the font name. The examples add .ttf later, which I overlooked.
The ctor returned a non-NULL pointer but the font object was “empty”. Perhaps, the internal logic of the font object isn’t perfect: instead of “doing nothing” or returning failures, the empty font object crashes. Only a few accesses to the object work somehow, for example fnt->Error(), which returns 0x01 - “cannot allocate resource”. The rest was easy.
Therefore, don’t be so quick with reading and check error status :doh: GLUT is not necessary, FTGL works with wxWidgets nicely.
FTGL only works for legacy OpenGL, doesn’t it?
Are you sure that is what you want?
Not your source code, but a constructor that can fail is usually a sign of bad design. The constructor should always succeed. That means the constructor is usually used to initialize the object into an empty state, and then you define some kind of Init-function that does the real work.
The default ctor is protected. The user can create only an initialized object. With this design, the ctor should throw or all methods of the object should take some default action (return failure, zero, NULL, etc.) when the object was initialized badly. But the object shouldn’t crash - this leads to searching errors where they aren’t, loss of nerves, and posting stupid questions on forums