The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_ttf: Creating surfaces every time
dandago


Joined: 30 Aug 2013
Posts: 34
This is a snippet from a game I'm developing:

Code:
void BottomRightView::Render()
{
    auto renderer = this->GetRenderer();

    SDL_DestroyTexture(this->fontTexture);
    SDL_FreeSurface(this->fontSurface);

    std::string linesStr;
    this->player->GetPlayerStateSummary(linesStr);

    // TODO recreating these every time seems very inefficient
    this->fontSurface = TTF_RenderText_Blended_Wrapped(font, linesStr.c_str(), this->fontColour, 300);
    this->fontTexture = SDL_CreateTextureFromSurface(renderer, fontSurface);

    SDL_Rect dstrect = { 504, 336, 0, 0 };
    SDL_QueryTexture(fontTexture, NULL, NULL, &dstrect.w, &dstrect.h);

    SDL_RenderCopy(renderer, this->fontTexture, NULL, &dstrect);
}


Since the player state that I'm drawing to the screen may change at any time, I'm having to create a new surface and texture every frame.

This seems really wasteful. Is there a more efficient way to do this?
capehill


Joined: 13 Feb 2016
Posts: 18
You could keep the old texture until status string actually changes, and then update the texture.
SDL_ttf: Creating surfaces every time
Jonny D


Joined: 12 Sep 2009
Posts: 932
http://stackoverflow.com/questions/29064904/rendering-fonts-and-text-with-sdl2-efficiently/29725751#29725751


Jonny D




On Thu, Jan 5, 2017 at 1:39 PM, capehill wrote:
Quote:
You could keep the old texture until status string actually changes, and then update the texture.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: SDL_ttf: Creating surfaces every time
dandago


Joined: 30 Aug 2013
Posts: 34


Interesting project! Is there any documentation on the library's capabilities other than the tiny snippet on the Github project homepage?
SDL_ttf: Creating surfaces every time
Jonny D


Joined: 12 Sep 2009
Posts: 932
Nope!  That snippet should eventually be expanded, but my time right now is very limited.  Check the header (SDL_FontCache.h) and the test program for more details.  I'm also around for any questions and if that makes me produce useful information, I'll add it to the project page.

Jonny D


On Fri, Jan 6, 2017 at 7:24 AM, dandago wrote:
Quote:



Jonny D wrote:

http://stackoverflow.com/questions/29064904/rendering-fonts-and-text-with-sdl2-efficiently/29725751#29725751


Jonny D




Interesting project! Is there any documentation on the library's capabilities other than the tiny snippet on the Github project homepage?



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org