1

I'm using the code outlined in the following post:

Draw text in OpenGL ES

I thought I could use this technique in order to dynamically display text (say an FPS counter). I realised that calls to resources to get the drawable slows down this process quite a lot, and I didn't need a bitmap background, so I removed it.

The technique works, but after a while (~2000 frames) the whole phone locks up. I suspect there's some memory which is not being freed in this code but I don't know where. I tried offloading the Canvas, Paint and Bitmap object creations which worked (so they aren't created every single frame) but the same problem still occurs.

I suspect therefore, that the generated GL texture is to blame, but I'm unsure how to remove it, or if this is even the case.

Any help would be appreciated.

EDIT: As an alternative, can someone please point out an easy way to render text to the screen dynamically (e.g. should be able to render the # of frames since starting for example, continually being updated and increasing). All the ways I can think of are either extremely tedious (make individual quads for each digit, store the textures for 0-9 in memory, parse the number and render each digit onto each quad), cannot be updated in good time (overlay Views) or can't get the required positioning in the glSurfaceView.

Community
  • 1
  • 1
Dororo
  • 3,420
  • 2
  • 30
  • 46
  • code code and more code please. – Jason Rogers Jun 23 '11 at 23:30
  • The code is the same as in the example but without Drawable background and with the Paint/Bitmap instantiations made outside the function. – Dororo Jun 23 '11 at 23:42
  • What do you mean by 'lock up'? Does the frame rate drop consistently after 2000 frames? Or do you get a stall every now and then? – Aert Jun 24 '11 at 01:29
  • According to DDMS other processes start getting killed after 2000 frames. This is only usually done when the active activity requires more memory. The frame rate must go down to 1-2 FPS and stays there consistently. Inputs from touch/buttons taken 10-30 seconds to respond under these conditions. – Dororo Jun 24 '11 at 01:38

3 Answers3

3

CBFG - http://www.codehead.co.uk/cbfg

This really is exactly what I've been wanting. You build a bitmap file from a font file using CBFG which can then be loaded and displayed with only a few lines of code (after importing his packages). It's literally as easy as fnt.PrintAt(gl,"Hello world!", 50, 160); in onDraw and more importantly, it handles dynamic text really well. I strongly advise anyone who is the same situation to try this.

Dororo
  • 3,420
  • 2
  • 30
  • 46
1

I just wrote an entire tutorial on creating exactly what you are looking for.

The idea is basically to use font files and then generate a font bitmap (or atlas) at run-time instead of using a tool like CBFG to generate it offline. The benefit of this is that you can ship a small font file instead of multiple large bitmaps with your app, and never have to sacrifice font quality by using scaling.

The tutorial includes full working source (that can be dropped into any project). If you are interested go have a look here.

Bart
  • 19,692
  • 7
  • 68
  • 77
free3dom
  • 18,729
  • 7
  • 52
  • 51
1

two things I can guess you'll want to try:

1) dont' recreate the number of your frs every frame, generate number 1 to 60 and always reuse those.

2) there is an issue I found when generating text for my textures is that the font loader code of android never frees the memory space so avoid loading the font all the time, do it once and store a reference to it

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
  • So basically, pre-generate the textures and deal with it? If I wanted to display larger numbers, I couldn't viably pre-generate all the textures, so this system wouldn't work? If so, is there an 'easy' way to use font maps and glyphs for the same thing? – Dororo Jun 23 '11 at 23:40