如何通过类渲染SDL_Texture?

How to render SDL_Texture via class?

本文关键字:Texture SDL 何通过      更新时间:2023-10-16

我已经包含了 SDL 的所有重要文件(文本、图像等(。我知道它有效,因为当我从教程中输入简单的代码时(类似于"一切都进入main"之类的东西仍然有效(。但我正试图把它放在课堂上。当我使用调试器时,一切似乎都很好。但是我在屏幕上看不到任何文字...

class Text
{
/* ===Objects=== */
private:
SDL_Texture* textTexture;
SDL_Rect textRect;
/* ===Methods=== */
public:
void init(const std::string& fontPath, int fontSize, const std::string& message, const SDL_Color& color, const std::shared_ptr<SDL_Renderer>& renderer);
void display(const std::shared_ptr<SDL_Renderer>& renderer);
SDL_Texture* setText(const std::string& fontPath, int fontSize, const std::string& message, const SDL_Color& color, const std::shared_ptr<SDL_Renderer>& renderer);
void setPosition(const Vector2<float>& position);
};
#include "Text.hpp"
void Text::init(const std::string & fontPath, int fontSize, const std::string & message, const SDL_Color & color, const std::shared_ptr<SDL_Renderer>& renderer)
{
textTexture = setText(fontPath, fontSize, message, color, renderer);
SDL_QueryTexture(&*textTexture, nullptr, nullptr, &textRect.w, &textRect.h);
}
void Text::display(const std::shared_ptr<SDL_Renderer>& renderer)
{
SDL_RenderCopy(&*renderer, &*textTexture, nullptr, &textRect);
}
SDL_Texture* Text::setText(const std::string & fontPath, int fontSize, const std::string & message, const SDL_Color & color, const std::shared_ptr<SDL_Renderer>& renderer)
{
TTF_Font* font = TTF_OpenFont(fontPath.c_str(), fontSize);
auto textSurface = TTF_RenderText_Solid(font, message.c_str() , color);
auto tempTexture = SDL_CreateTextureFromSurface(&*renderer, textSurface);
SDL_FreeSurface(textSurface);
TTF_CloseFont(font);
return tempTexture;
}
void Text::setPosition(const Vector2<float>& position)
{
textRect.x = position.x - textRect.w / 2.f;
textRect.y = position.y - textRect.h / 2.f;
}

有人可以介绍我,我该如何解决这个问题?

没有问题。在绘制文本之前,我一直在清洁屏幕。