2

I was following Lazy Foo' tutorial to create text using ttf font, and everything was fine, but I needed to create several text lines in several different places with different font size and color, so I decided to use vector. Here is my code of TextTexture (mostly copy of Lazy Foo tutorial):

#ifndef TEXT_TEXTURE_HPP
#define TEXT_TEXTURE_HPP

#include "graphics.hpp"
#include "vector2.hpp"

#include <SDL2/SDL_ttf.h>

#include <string>

class TextTexture {
public:
    TextTexture(
        Graphics& graphics,
        TTF_Font* font,
        std::string textureText,
        SDL_Color textColor,
        Vector2 coordinates
    );

    ~TextTexture();

    void draw( Graphics& graphics );

private:
    SDL_Texture* mTexture;

    int mWidth;
    int mHeight;

    int mX;
    int mY;

};

#endif // TEXT_TEXTURE_HPP

And .cpp file for it:

#include "text_texture.hpp"
#include "vector2.hpp"

#include <iostream>
#include <unistd.h>

TextTexture::TextTexture (
    Graphics& graphics,
    TTF_Font* font,
    std::string textureText,
    SDL_Color textColor,
    Vector2 coordinates
) :
    mTexture(NULL),
    mWidth(0),
    mHeight(0),
    mX(0),
    mY(0)
{
    //Render temp surface
     SDL_Surface* tempSurface = TTF_RenderUTF8_Blended (font, textureText.c_str(), textColor);

    if ( tempSurface == NULL ) {
        std::cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << std::endl;
    } else {
        this -> mTexture = SDL_CreateTextureFromSurface(graphics.getRenderer(), tempSurface);
        if ( this -> mTexture == NULL ) {
            std::cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << std::endl;
        } else {
            //Get image dimensions
            mWidth = tempSurface -> w;
            mHeight = tempSurface -> h;
            // Get coordinates
            this -> mX = coordinates.getX();
            this -> mY = coordinates.getY();
        }
        SDL_FreeSurface (tempSurface);
        tempSurface = NULL;
    }
}

TextTexture::~TextTexture() {
    //Free texture if it exists
    if ( mTexture != NULL ) {
        SDL_DestroyTexture( mTexture );
    }
    mTexture = NULL;
    mWidth = 0;
    mHeight = 0;
}

// FIXME somewhy affects previous dest rects
void TextTexture::draw (Graphics& graphics) {
    //Set rendering space and render to screen
    SDL_Rect destinationRectangle = { mX, mY, this -> mWidth, this -> mHeight };
    //Render to screen
    graphics.blitSurface( mTexture, NULL, &destinationRectangle );
}

I created simple Text Manager to handle vector of texts:

#ifndef TEXT_MANAGER_HPP
#define TEXT_MANAGER_HPP

#include "graphics.hpp"
#include "text_texture.hpp"
#include "vector2.hpp"

#include <string>
#include <vector>

enum fontSize {
    SMALL = 16,
    NORMAL = 32,
    BIG = 48,
    TITLE = 72
};

enum fontColor {
    WHITE,
    ORANGE,
    BLACK
};

class TextManager {
public:
    TextManager(Graphics& graphics);
    ~TextManager();

    void addText(std::string, fontSize, fontColor, Vector2);
    void draw();
    void clearText();

private:
    Graphics& graphics;
    std::vector <TextTexture> gText;
};

#endif // TEXT_MANAGER_HPP

and .cpp file:

#include "text_manager.hpp"

#include <iostream>

TextManager::TextManager(Graphics& graphics) :
    graphics(graphics)
{}

TextManager::~TextManager() {}

void TextManager::addText(std::string text, fontSize size, fontColor color, Vector2 coordinates) {

    TTF_Font* tempFont = TTF_OpenFont( "resources/fonts/pixel.ttf", fontSize::TITLE );
    SDL_Color tempColor = { 255, 255, 255 };

    // Switch removed for shorter code

    this -> gText.emplace_back(graphics, tempFont, text, tempColor, coordinates);

    TTF_CloseFont(tempFont);
    tempFont = NULL;
}
// FIXME
void TextManager::draw() {
    std::vector<TextTexture>::iterator it;
    for(it = gText.begin(); it != gText.end(); ++it) {
        it -> draw(graphics);
    }

}

void TextManager::clearText() {
    gText.clear();
}

But when I start the application, I see something like this:

Second string is printed, but font and bonding rectangle of first line is saved, hovewer

Later I added input handler that added second line of text after pressing a button, and when there is only one line of text, everything fine, but when you add second, something weird is beginning - sometimes first text disappears, sometimes 'both' of them is shoved. As I understand, second surface of text somehow affects first one, by copying it texture on the place of the first's destination.

Here is my graphics.blitSurface, if it will help:

void Graphics::blitSurface(SDL_Texture* texture, SDL_Rect* sourceRectangle, SDL_Rect* destinationRectangle)
{
    SDL_RenderCopy ( this -> _renderer, texture, sourceRectangle, destinationRectangle );
}

Where is my mistake? Sorry for bad english, I hope you will get my problem.

SuperPrower
  • 111
  • 1
  • 12
  • Your last loop "disables" drawing because it copies the `TextTexture` but you don't have a proper copy-constructor. – tkausl Sep 05 '16 at 09:54
  • For your last edit: it isn't, version without iterator uses temporary variable, and once its destructor is called you're destroying your texture. As for initial problem, it would be simpler if you describe it better. E.g. I see no code that switches fonts depending on passed size, but you imply that it should happen (also, this `enter code here` is a bit suspicious, but I expect it to be copypaste error). – keltar Sep 05 '16 at 09:55
  • Oh, this actually makes sense. Thank you! – SuperPrower Sep 05 '16 at 09:57

1 Answers1

0

I figured it out somehow randomly. The thing is that when I adding object to vector, it's calls a destructor.

Here is why:

Why does my class's destructor get called when I add instances to a vector?

Community
  • 1
  • 1
SuperPrower
  • 111
  • 1
  • 12