What I am trying to accomplish is rendering text to a GLTexture to be used however I wish. What I would like to do, is create a single RenderImage to render any text into. Then somehow generate an OpenGL Texture ID from that.
So I have a "TextManager" in my engine...
I just started rewriting it from the SFML1.6 version so it's still very short and very simple and basically does nothing, hah. So I'll just post my code for it.
Declaration
class TextManager{
public:
TextManager(void);
void AddFont(std::string name, std::string font_path);
unsigned int DrawTextToTexture(std::string text, unsigned int font_size = 30, std::string font_name = "default");
private:
std::map<std::string, sf::Font *> fonts;
sf::RenderImage *render_image;
};
Definition
TextManager::TextManager(void){
AddFont("default", "Common/Fonts/Vera.ttf");
render_image = new sf::RenderImage();
// Do I Need This Next Line?!?
render_image->GetImage().CreateTexture(120, 38);
}
void TextManager::AddFont(std::string name, std::string font_path){
if (fonts.count(name) < 1){
sf::Font *font = new sf::Font();
font->LoadFromFile(font_path);
fonts[name] = font;
}
}
unsigned int TextManager::DrawTextToTexture(std::string text, unsigned int font_size, std::string font_name){
if (fonts.count(font_name) > 0){
sf::Text sf_text(text.c_str(), *fonts[font_name], text.size());
sf_text.SetCharacterSize(font_size);
sf_text.SetPosition(0.0f, 0.0f);
render_image->Draw(sf_text);
// WHAT DO I DO AFTER I DRAW IT?
// Would I have to create a GL Texture every frame and release it? That seems crazy.
}
}
I have comments in there showing my dilemmas
Basically:
- When I create the RenderImage do I have to create a texture?
- If so... can it resize itself to what I'm drawing inside of it?
- I have a texture manager which creates gl textures from sf::Images but that doesn't get done every frame, so I'm confused as to how to handle this code specifically.
One idea I had was to manage sf::Texts as well, and keep them in an associative array by IDs so they can be easily referenced and updated, and they would each have their own RenderImage, however I'd like to avoid this in favor or a simpler approach if I can.
Thoughts?
Thanks in advance!