SDL_ttf 向量中的纹理会影响其他人的目标矩形

SDL_ttf textures in vector affects other's destination rectangle

我按照 Lazy Foo 的教程使用 ttf 字体创建文本,一切都很好,但我需要在几个不同的地方使用不同的字体大小和颜色创建多个文本行,所以我决定使用矢量。这是我的 TextTexture 代码(主要是 Lazy Foo 教程的副本):

#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

及其 .cpp 文件:

#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 );
}

我创建了简单的文本管理器来处理文本向量:

#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

和 .cpp 文件:

#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();
}

但是当我启动应用程序时,我看到如下内容:

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

后来我添加了输入处理程序,在按下按钮后添加了第二行文本,当只有一行文本时,一切都很好,但是当你添加第二行时,一些奇怪的事情开始了——有时第一行文本消失了,有时 'both' 他们被推了。据我了解,文本的第二个表面会以某种方式影响第一个表面,方法是将其纹理复制到第一个目标的位置。

这是我的graphics.blitSurface,如果有帮助的话:

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

我的错误在哪里?抱歉英语不好,希望你能解决我的问题。

我是随机想出来的。问题是,当我将对象添加到向量时,它会调用析构函数。

原因如下:

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