我下面的懶惰富」教程使用TTF字體來創建文本,一切都很好,但我需要建立在幾個不同的地方用不同的字體大小和顏色的幾個文本行,所以我決定使用矢量。這裏是我的TextTexture的代碼(主要是懶富教程的複製):SDL_ttf紋理矢量影響其他的目標矩形
#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);
}
哪裏是我的錯?對不起英語不好,我希望你會得到我的問題。
你的最後一個循環「禁用」繪製,因爲它複製'TextTexture'但你沒有正確的拷貝構造函數。 – tkausl
對於你最後的編輯:它不是,沒有迭代器的版本使用臨時變量,一旦它的析構函數被調用,你正在破壞你的紋理。至於最初的問題,如果你更好地描述它會更簡單。例如。我沒有看到根據傳遞大小切換字體的代碼,但是您意味着它應該發生(同樣,「此處輸入代碼」有點可疑,但我認爲它是copypaste錯誤)。 – keltar
哦,這實際上是有道理的。謝謝! – SuperPrower