如何通过类渲染SDL_Texture?
How to render SDL_Texture via class?
我已经包含了 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;
}
有人可以介绍我,我该如何解决这个问题?
没有问题。在绘制文本之前,我一直在清洁屏幕。
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- SDL 窗口不会弹出
- SDL 操纵杆按钮按下/C++
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- SDL 映像:无法打开映像,仅显示错误消息
- 如何在 3D OpenGL 场景上绘制 SDL 2D 矩形?
- "Undefined symbols for architecture x86_64" 在 SDL 2 (macOS) 中
- SDL_CreateTextureFromSurface() "Invalid texture"错误?
- 尝试将 sf::Texture* 推送到 std::vector 时出错
- 为什么 SDL 在 Mac 上比 Linux 慢得多?
- 在 OSX 上打开 SDL 窗口时不要打开终端窗口
- SDL + OpenGL ES 2.0 使用着色器显示纹理
- SDL GPU 为什么将两个图像分成两个单独的循环更快?
- 在 cmake 中找不到 SDL 库
- SDL/C++: 如何平滑但随机地移动矩形?
- 如何与 SDL 库链接?
- 为什么我的C++代码不呈现 SDL 矩形?
- C++ & SDL Texture 无法加载,但 SDL_GetError() 不返回任何内容