Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5+OpenGL] How to prevent al_draw_text from resetting the viewport?

This thread is locked; no one can reply to it. rss feed Print
[A5+OpenGL] How to prevent al_draw_text from resetting the viewport?
wqking
Member #16,641
February 2017

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?

Thanks

Elias
Member #358
May 2000

Which version are you using?

--
"Either help out or stop whining" - Evert

wqking
Member #16,641
February 2017

The latest 5.2.2 from the download page.

Elias
Member #358
May 2000

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;
        al_draw_text(text);
    }
}

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.

--
"Either help out or stop whining" - Evert

SiegeLord
Member #7,827
October 2006
avatar

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?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

wqking
Member #16,641
February 2017

@Elias:
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.

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

Go to: