如何通过类呈现SDL_Texture?

时间:2018-05-23 07:33:35

标签: c++ text sdl-2

我已经包含了SDL的所有重要文件(文本,图像等)。我知道它的工作原理是因为当我把简单的代码放在教程中时(类似于“一切都进入主要部分”仍然有用)。但我想把它放到课堂上。当我使用调试器时,一切似乎都没问题。但我在屏幕上看不到任何文字......

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

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

1 个答案:

答案 0 :(得分:0)

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