[A5+OpenGL] How to prevent al_draw_text from resetting the viewport?

I only need OpenGL.
I need to set the viewport before drawing anything.For image, I can use the low level vertex array rendering to bypass any al_draw_bitmap family functions.
However, I use al_draw_text to render the text. al_draw_text calls al_draw_bitmap underlying, and al_draw_bitmap will "use_transform", when OpenGL implementation updates the transform, it always reset the viewport, which is I don't expect. (for details, see function ogl_update_transformation in ogl_draw.c in Allegro source code).

So, how can I render text without reset the viewport?



Which version are you using?


The latest 5.2.2 from the download page.


I see: https://github.com/liballeg/allegro5/blob/master/src/opengl/ogl_draw.c#L531

I think that only gets called when you change the target bitmap. al_draw_text has to change the target bitmap when it draws into the texture with the glyph pictures. When it switches back to the original target, it will reset the viewport to 0/0/w/h and not what it was before. You can completely avoid the target bitmap switch by pre-rendering your glyphs. You can try something like this (with a display created and active on the current thread):

font = al_load_font(...);
int ranges[100 * 2];
int n = al_get_font_ranges(font, 100, ranges);
for (int i = 0; i < n; i++) {
    for (int j = ranges[i * 2 + 0]; j <= ranges[i * 2 + 1]; j++) {
        int s = al_utf8_encode(text, j);
        text[s] = 0;

Alternatively, a patch to preserve the viewport probably could be applied to Allegro. We do have some functions to support mixing Allegro with OpenGL, and this sounds like something that would make sense.


Can't you just reset the viewport to what you expect after the text is drawn? Or is the issue that you expect the text drawing to actually use your viewport setting?


Seems rendering to a memory texture is a good workaround for now. Though I think Allegro should support customized viewport. I will raise feature request later.
BTW, I think the viewport will be reset on each call of al_use_transform, no matter the target changes or not.

I want the text be drawn within the viewport. What I want to implement is a camera system using viewport.

Thread #616766. Printed from Allegro.cc