gpt4 book ai didi

c++ - vector 中的 SDL_ttf 纹理会影响其他目标矩形

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:40 25 4
gpt4 key购买 nike

我按照 Lazy Foo 的教程使用 ttf 字体创建文本,一切都很好,但我需要在几个不同的地方使用不同的字体大小和颜色创建多个文本行,所以我决定使用 vector 。这是我的 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 );
}

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

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

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

最佳答案

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

原因如下:

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

关于c++ - vector 中的 SDL_ttf 纹理会影响其他目标矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39318019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com