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