PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Custom Font in OpenGL

Hi,

I'm trying to draw text using a custom font in OpenGL mode and I can't get it working.
I'm using the MakeFont tool, standard ascii charset, arial font, size 12, color FFFFFF, max pixel per row 0, variable width font or not, 24bit or not, anti aliased or not, saving to PNG
with the following code
Code:

E2DSurfaceRGBA my_font;

ERESULT ClassMain::OnDisplayInit(ENATIVETYPE instance, ClassEDisplay *display)
{
if (display->CreateSurface(&my;_font, "test.png") != E_OK)
return(E_ERROR);
my_font.SetFontCharacters(32, 127);
my_font.BuildFont(true);
[...]

ERESULT ClassMain::OnNextFrame(ClassEDisplay *display, unsigned long timedelta)
{
display->DrawFont(0, 200, &my;_font, "youpi !");


I tried every combination of variable width or not, rgba surface or not, color key or not, etc...
All I get is a blackscreen.
In the DrawFont function, when I replace &my;_font by &display-;>fontinternal, it's working. so I tried to save the fontinternal as png/bmp with WritePNG/BMP function to look how the picture was made but it's not working.
so now I'm stuck, can you please help me get this working ? thanks !
Hi Orion,

When using fonts the following functions need to be called in the correct order:

- First load the image
- Then setup the font and call BuildFont
- Finally upload the surface to OpenGL

The reason why your font doesn't show is that it is uploaded too soon (when uploading the image the original data is cleared which is needed by the BuildFont function), or it isn't uploaded at all. You can also try to show the image using BltFast (to see if the image itself is loaded correctly).

The internal font can't be written to a file because it is already uploaded to OpenGL and the original image data is cleared. The font image itself is also delivered seperately with the SDK inside the media folder.
thanks, I totally forgot to upload the texture :)
I've another problem, I made a 24bits PNG font, variable width, antialiased using the MakeFont tool.
I've also limited the font width to 512 because of the OpenGL ES texture size limitation.
Everything works fine on PC using OpenGL, but on iPhone device I've a problem, the CreateSurface function result in an error when I try to load the PNG made by MakeFont. (malloc error)
So I resaved the PNG using photoshop and the PNG now loads correctly but doesn't work like on PC, the text won't display, instead I've a long bar of pixels.
I've attached the PNGs I'm using: font_24.png is the font made by MakeFont which doesn't load on iPhone, the font24.png is the font resaved using Photoshop that load on iPhone but displays wrong.

The code I use:
Code:

E2DSurfaceRGBA my_font;

ERESULT ClassMain::OnDisplayInit(ENATIVETYPE instance, ClassEDisplay *display)
{
if (display->CreateSurface(&my;_font, "font24.png", EST_READONLYPATH) != E_OK)
return (E_ERROR);
my_font.SetFontCharacters(32, 255);
my_font.SetFontSheetLayout(6);
my_font.BuildFont(true);
display->UploadTexture(&my;_font);

ERESULT ClassMain::OnNextFrame(ClassEDisplay *display, unsigned long timedelta)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
display->Perspective2D(SCREEN_WIDTH, SCREEN_HEIGHT, false);
display->SetShading(E3D_TEXTURE);

display->DrawFont((SCREEN_WIDTH - my_font.GetFontWidth("ñàöó héhèhêhë !"))/2, 10, &my;_font, "ñàöó héhèhêhë !");
Attachments:
font.zip (65 KB)
Hi Orion,

It could be that your PNG file has become unreadable after transferring it to the iPhone. When transferring PNG files to the iPhone, they are converted to a iPhone-specific format.

To solve this issue you can either rename the PNG file to use a different extension than .png (EDGELIB will still load it, but the iPhone doesn't convert it), or ship it in a package (.epk) file.
ok, thanks, it's working now :)