简体   繁体   中英

How to render SDL_Texture via class?

I have included all important files of SDL (text, image etc). I know that it works because when I put simple code from tutorial (something like "everything into main" in still works). But I am trying to put this into class. When I use debugger, everything seems to be ok. But I don't see any text on the screen...

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

Can somebody introduce me, how can I fix this problem?

There is no problem. I have been cleaning screen before I draw text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM