我按照Lazy Foo的教程使用ttf字体创建文本,一切都很好,但我需要在几个不同的地方创建几个不同字体大小和颜色的文本行,所以我决定使用vector。这是我的TextTexture代码(主要是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和它的.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 );
}我的错误在哪里?很抱歉我的英语不好,我希望你能理解我的问题。
发布于 2016-09-06 21:30:11
不知何故,我随机地弄明白了。问题是,当我将对象添加到向量中时,它会调用析构函数。
原因如下:
Why does my class's destructor get called when I add instances to a vector?
https://stackoverflow.com/questions/39318019
复制相似问题