0

No matter what i try i cant get my text to load into a texture in SDL 2.0 using SDL_ttf.

Here is my textToTexture code

void sdlapp::textToTexture(string text, SDL_Color textColor,SDL_Texture* textTexture)
{
    //free prevoius texture in textTexture if texture exists
    if (textTexture != nullptr || NULL)
    {
        SDL_DestroyTexture(textTexture);
    }
    SDL_Surface* textSurface = TTF_RenderText_Solid(m_font, text.c_str(), textColor);
    textTexture = SDL_CreateTextureFromSurface(m_renderer, textSurface);
    //free surface
    SDL_FreeSurface(textSurface);
}

And then here is me loading the texture and text

bool sdlapp::loadMedia()
{
    bool success = true;
    //load media here

    //load font
    m_font = TTF_OpenFont("Fonts/MotorwerkOblique.ttf", 28);

    //load text
    SDL_Color textColor = { 0x255, 0x255, 0x235 };
    textToTexture("im a texture thing", textColor, m_font_texture);

    return success;
}

And then this is the code i am using to render it

void sdlapp::render()
{
    //clear the screen
    SDL_RenderClear(m_renderer);
    //do render stuff here
    SDL_Rect rect= { 32, 64, 128, 32 };
    SDL_RenderCopy(m_renderer, m_font_texture, NULL, NULL);
    //update the screen to the current render
    SDL_RenderPresent(m_renderer);
}

Does anyone know what i am doing wrong? Thanks in Advance, JustinWeq.

JustinWeq
  • 158
  • 11
  • First thing to do is get some error checking in your code - checking for null values after loading assets or creating textures/surfaces – TPS Jan 02 '15 at 11:04
  • 1
    Thanks for your advice, after checking for null values i immediately noticed that both m_font and m_font_texture where null. This lead me to realize that both the font loading and getting a texture from the font where loading. I then recognized that that meant TTF had failed so my first reaction was that i had not called TTF_Init() and this turned out to be the problem. – JustinWeq Jan 02 '15 at 17:22

1 Answers1

0

textToTexture renders the text with SDL_ttf, the resulting SDL_Texture address is then assigned to a variable called textTexture. Problem is, textTexture is a local variable pointing to the same address as m_font_texture. They're not the same variable, they're different variables poiting to the same place, thus you're not changing any callee variables.

For clarification on pointers, I'd recommend seeing question 4.8 of the C-FAQ

I'd make textToTexture return the new texture address, and don't bother freeing resources that are not managed by it (m_font_texture belongs to sdlapp, it should be managed by it).

Leonardo
  • 1,834
  • 1
  • 17
  • 23
  • Okay i took your advice and made textToTexture return a pointer to a new texture(textTexture) but it is still not working. It must be something else in the code as well, thanks for your reply. I would upvote you if i could but unfortunately my reputation is not high enough. – JustinWeq Jan 02 '15 at 17:14
  • 2
    Okay i got it to work, it seems that i just forgot to call TTF_Init(). Thanks for your help. – JustinWeq Jan 02 '15 at 17:18