-1

I have a program which contains a class that I can't figure out how to make it work. When I make the code run without "errors" it does not show any text on screen.

I have an object from this class on my main loop, where I render it. Calling the render() void function from the class.

I figured out where the code crashes. But I don't have any clue on why it does.

Thanks for the help.

class for text objects: (text.cpp)

#include "text.h"

void Text::init(const char* text, int x, int y, int r, int g, int b, int size, SDL_Renderer *renderer){
    font = TTF_OpenFont("arial.ttf", size);
    color.r = r;
    color.g = g;
    color.b = b;
    color.a = 255;

    surface = TTF_RenderText_Solid(font, text, color);
    texture = SDL_CreateTextureFromSurface(renderer, surface);

    rect.x = x;
    rect.y = y;
    rect.w = 300; //surface->w; //Here is one error, if i compile the commented part, there is no problem untill I run it. Then it crashes.
    rect.h = 300; //surface->h; //The same happens with this
}

Text::~Text(){
    SDL_FreeSurface(surface);
    SDL_DestroyTexture(texture);
}

void Text::render(SDL_Renderer *renderer){
    SDL_RenderCopy(renderer, texture, NULL, &rect); // But even with that, this shows no text on screen
}

text.h file:

#ifndef TEXT_H
#define TEXT_H

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

class Text{
    private:
    SDL_Surface* surface;
    SDL_Texture* texture;
    TTF_Font* font;
    SDL_Color color;
    SDL_Rect rect;

    public:
    Text() {};
    ~Text();

    void init(const char* text, int x, int y, int r, int g, int b, int size, SDL_Renderer *renderer);
    void render(SDL_Renderer *renderer);

};

#endif //TEXT_H
  • MVCE please, if you run this in a main() function initializing just the SDL stuff does it crashes too? – aram Jul 16 '18 at 12:38

1 Answers1

1

First: Add internal tests (like assert()) on the returned value of SDL function like TTF_OpenFont (returns NULL if it fails).

Secondly: use SDL_GetError() for details on the reason it failed.

Thirdly: Use -g option with your compiler and then execute the program with gdb, it will gives more details about where the program crashes.

Hypotheses:

  1. One of your SDL function calls fails.
  2. A texture is renderer specific, that means the renderer used to create the texture is the only one you can render this texture on.
  3. A renderer as to be updated to show modifications with SDL_RenderPresent(renderer)
  4. There is several printing methods when it comes to RGBA, makes sure the texture is not transparent, and the renderer is on the proper mode.
uben
  • 1,221
  • 2
  • 11
  • 20
  • Thanks, but I cant use this, as the program crashes when it runs those specific lines (I know because I've put "cout"s and see where they do or do not print the message). Maybe I don't understand how this work. The error is on the execution of the program... I'm new to SDL and c++ so maybe I'm not doing something obvious. -- I'm using the same renderer for creating it and for rendering it. – ovni jeroqui Jul 14 '18 at 17:08
  • On the linux terminal it displays: Violació de segment (bolcat de la imatge del nucli) As I have the system in catalan: I'v serched and it seams that in english it would say: segfault or something similar – ovni jeroqui Jul 14 '18 at 17:16
  • @ovnijeroqui *"I cant use this, as the program crashes when it runs those specific lines"* It probably crashes *after* some SDL function returns a error code. – HolyBlackCat Jul 14 '18 at 17:33
  • maybe try to print text with C language directly in your main function to see if it works, if it does not, your doing something wrong, if not your c++ class is inconsistent – uben Jul 14 '18 at 17:58
  • @ovnijeroqui Check the documentation for SDL functions you use. Some of them can fail, and you have to check their return values to detect that. E.g. `SDL_CreateTextureFromSurface` can return `0` if it's unable to create the texture, but you never check if it returned non-null. – HolyBlackCat Jul 14 '18 at 22:07
  • @ovnijeroqui Also, make sure to use `@username` when replying to people (not under their questions/answers). Otherwise we might not get a notification and never reply. – HolyBlackCat Jul 14 '18 at 22:08