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

时间:2016-09-04 14:33:42

标签: c++ sdl sdl-2 true-type-fonts sdl-ttf

我正在关注使用ttf字体创建文本的Lazy Foo教程,一切都很好,但我需要在不同的地方创建几个不同字体和颜色的文本行,所以我决定使用矢量。这是我的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

后来我添加了输入处理程序,在按下按钮后添加了第二行文本,当只有一行文本时,一切正常,但是当你添加第二行时,一些奇怪的东西开始 - 有时候第一个文本消失,有时候'他们两个都被推了推。据我了解,文本的第二个表面通过在第一个目的地的位置复制纹理,以某种方式影响第一个表面。

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

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

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

1 个答案:

答案 0 :(得分:0)

我以某种方式随机地想出来了。问题是当我向vector添加对象时,它会调用析构函数。

原因如下:

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