Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML2 Rendering Text to GLTexture  (Read 2025 times)

0 Members and 1 Guest are viewing this topic.

PaloDeQueso

  • Newbie
  • *
  • Posts: 16
    • AOL Instant Messenger - palodequeso
    • View Profile
    • http://www.palodequeso.net
    • Email
SFML2 Rendering Text to GLTexture
« on: February 22, 2011, 04:34:06 pm »
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
Code: [Select]

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
Code: [Select]

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!
Douglas E Reisinger II
http://www.palodequeso.net

PaloDeQueso

  • Newbie
  • *
  • Posts: 16
    • AOL Instant Messenger - palodequeso
    • View Profile
    • http://www.palodequeso.net
    • Email
*bump*
« Reply #1 on: March 01, 2011, 05:31:20 pm »
Noone???

I'm about to use bitmap fonts again... yikes.
Douglas E Reisinger II
http://www.palodequeso.net

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML2 Rendering Text to GLTexture
« Reply #2 on: March 01, 2011, 05:41:03 pm »
Quote
- When I create the RenderImage do I have to create a texture?

I assume you're referring to this:
Code: [Select]
  // Do I Need This Next Line?!?
   render_image->GetImage().CreateTexture(120, 38);

I'll answer by questions: is it in the documentation? Is it in the tutorials? Does it even compile? Why do you think that you need something that is pure invention from your mind? ;)

Quote
- If so... can it resize itself to what I'm drawing inside of it?

No it doesn't. The render image and its underlying texture are resized when you call Create(width, height) and it never changes after. But you can easily copy the result in a smaller image, if you want to.

Quote
- 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.

I assume you're referring to this:
Code: [Select]
     // WHAT DO I DO AFTER I DRAW IT?
      // Would I have to create a GL Texture every frame and release it? That seems crazy.

First, you must call render_image->Display() after drawing to the render image. Then you can either use the resulting image directly as an OpenGL texture, or you can update your own external texture if you need to (with glTexSubImage2D).
Laurent Gomila - SFML developer

PaloDeQueso

  • Newbie
  • *
  • Posts: 16
    • AOL Instant Messenger - palodequeso
    • View Profile
    • http://www.palodequeso.net
    • Email
sorry
« Reply #3 on: March 01, 2011, 05:44:57 pm »
Sorry, I should have done more research. It seems a common method to accomplish this is to prerender text into a big texture just like bitmap fonts, and use texture coordinates to access it and build strings of characters. Thanks for your help! And thanks again for the great library!
Douglas E Reisinger II
http://www.palodequeso.net

 

anything