sdl ttf_rendertext_blended fails randomly

1.7k views Asked by At

EDIT: Even that the problem still exists, I haven't been able to reproduce this frequently enough to examine it closer. See more info at the end of the question.


I started to develop a game, and I am currently writing basic library for it. I'm using D programming language with SDL-2 and OpenGL 3 (using Derelict3 bindings), on Linux Mint 13 (Maya). Compiler is DMD64 D Compiler v2.067.1, and I rebuild binary each time with 'rdmd'.

To render (changing) text, I create glyphs on-demand. The piece of code I use for this is:

class Font {
...

Texture render(char c) {
    if(!(c in rendered)) rendered[c] = texture(to!string(c));
    return rendered[c];
}

Texture texture(string text) {
    SDL_Color color={255, 255, 255, 255};

    auto bitmap = TTF_RenderText_Blended(
        font,
        std.string.toStringz(text),
        color
    );
    if(!bitmap) {
        throw new TTFError(
            "TTF_RenderText_Blended: " ~
            to!string(TTF_GetError()) ~ ": '" ~ text ~ "'"
        );
    }
    auto texture = new Texture(bitmap);
    SDL_FreeSurface(bitmap);
    return texture;
}

The problem is that this fails purely randomly. Sometimes it works without any problems. When it fails to render a glyph, it is interesting that it will fail to render the same glyph over and over again. Here is an example when catching the exception I throw:

...
TTF_RenderText_Blended: Text has zero width: '9'
TTF_RenderText_Blended: Text has zero width: '6'
TTF_RenderText_Blended: Text has zero width: '9'
TTF_RenderText_Blended: Text has zero width: '6'
TTF_RenderText_Blended: Text has zero width: '9'
TTF_RenderText_Blended: Text has zero width: '6'
...

(I'm printing score to screen, other numbers showing fine except those few ones). The numbers TTF_RenderText_Blended fails to render vary from run to run, and as mentioned, time to time it renders all the numbers.

One detail is that static strings I render before entering game loop have not yet failed to render, just single letters I use for changing texts.

I'm pretty much out of any ideas, and haven't found anything related to this problem by searching web. Any ideas to look for solutions are very well appreciated.


CURRENT SITUATION: I updated compiler to DMD 2.067.1 and the problem remains (compilers used so far: 2.066.1, 2.067.1). The whole - lets say project family is in the github at the moment:

https://github.com/mkoskim/games

The text glyph rendering function is located in this file:

https://github.com/mkoskim/games/blob/master/engine/ext/font.d

...and it is used from here:

https://github.com/mkoskim/games/blob/master/engine/ext/gui/label.d

The problem occurs mainly/most frequently in the pacman game (although very seldomly just right now):

https://github.com/mkoskim/games/tree/master/testbench/pacman

If you want to try it out, first, read the (hopefully complete enough) installation instructions:

https://github.com/mkoskim/games/blob/master/INSTALL

The project is made for 64-bit Linux Mint Maya, and it is currently not that user friendly and portable from building perspective. Pacman is the only demo that (hopefully) works without game controller. After successful installation of required libraries and tools, you can build it with command:

games/testbench/pacman$ make default
2

There are 2 answers

2
collisdecus On

I ran into the exact same issue, and for me it was fixed by keeping the SDL_RWops structure used to create the font (with TTF_OpenFontRW) alive for the whole lifetime of the TTF_Font created by it. I saw you're creating the font with TTF_OpenFontRW as well so I assume this will fix it for you as well. It looks like SDL_ttf relies on this being kept alive, it reads freed memory otherwise.

1
Helios555 On

I know this question is a little bit outdated but I maybe had a similar problem. I fixed it with simply call SDL_DestroyTexture() every time befor I used TTF_Render_Text_Blended() :)