0
\$\begingroup\$

I can't seem to be able to render my font, it crashes and I can't figure out why. It seems to load the font correctly, but the parts relating to surface crashes.

Why does my texture crash?


Here is the code bits relating to it. SDL2 and TTF has been initialized properly.

        TTF_Font *arial;
        SDL_Color mm_txt_color = {255, 255, 255, 0};
        SDL_Texture *myTexture;
        SDL_Rect myRect;
        load_font(MM_FONT_ARIAL, 18,  &arial);

        get_text_and_rect(0, 0, "Exit", arial, &myTexture, &myRect);


int load_font(char *filepath, int size, TTF_Font **font)
{
    (*font) == TTF_OpenFont(filepath, size);
    if((*font) == NULL ) {
        printf("ERROR: Failed to load font: %s.\n", filepath);
        return FAILURE;
    }
    return SUCCESS;
}


    void get_text_and_rect(int x, int y, char *text, TTF_Font *font, 
        SDL_Texture **texture, SDL_Rect *rect)
    {
        int text_width;
        int text_height;
        SDL_Surface *surface;
        SDL_Color textColor = {255, 255, 255, 0};

        surface = TTF_RenderText_Solid(font, text, textColor);
        *texture = SDL_CreateTextureFromSurface(renderer, surface);
        text_width = surface->w;
        text_height = surface->h;
        SDL_FreeSurface(surface);
        rect->x = x;
        rect->y = y;
        rect->w = text_width;
        rect->h = text_height;
    }
\$\endgroup\$
  • \$\begingroup\$ Did you check what TTF_RenderText returns? Do you check SDL_GetError()? \$\endgroup\$ – Tyyppi_77 Jan 8 '17 at 22:41
  • \$\begingroup\$ well i know that it crashes on surface = TTF_RenderText_Solid(...), I just don't know why. \$\endgroup\$ – Pinzu Jan 8 '17 at 22:46
  • \$\begingroup\$ Solved it using TTF_GetError(), nothing really wrong with the code, just failed to enter correct filepath. \$\endgroup\$ – Pinzu Jan 9 '17 at 9:54
1
\$\begingroup\$

Using printf("%s\n", TTF_GetError()); i got the answer that the font hadn't been loaded correctly, so you provided the correct answer, nothing with the code but just the entered filepath on my part.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.