SFML community forums

Help => Graphics => Topic started by: lolz123 on May 08, 2011, 04:08:11 pm

Title: sf::Text not rendering properly
Post by: lolz123 on May 08, 2011, 04:08:11 pm
Hello,

I have created a game using SFML and OpenGL directly, and I wanted to display the script console in the game. However, when I try to render the text, it either uses the previously bound texture (if textures are enabled) or just renders the polygon on which the character is supposed to appear with a solid colored background (textures disabled). In order to mix the SFML OpenGL implementation and my own, I used the sf::RenderTarget functions SaveGLStates and RestoreGLStates to preserve the OpenGL state. The code show below gives me textured boxes instead of text:

Code: [Select]
// Render text
text.SetFont(sf::Font::GetDefaultFont());
text.SetString("bla bla bla");
text.SetColor(sf::Color(18, 128, 34));
text.SetStyle(sf::Text::Regular);

appRenderer.SaveGLStates();
appWindow.Draw(text);
appRenderer.RestoreGLStates();


What could be causing this problem?
Title: sf::Text not rendering properly
Post by: Laurent on May 08, 2011, 04:18:11 pm
Could we see a complete and minimal code that reproduces the problem?
Title: sf::Text not rendering properly
Post by: lolz123 on May 08, 2011, 06:47:42 pm
Argh! I made a little test program to narrow down the problem, but the application crashes on exit with the following code (I am using the exact same library versions as in my game):

Code: [Select]
#include <SFML\Window.hpp>
#include <SFML\OpenGL.hpp>
#include <SFML\Graphics.hpp>
#include <string>
 
int main(int argc, char* args[])
{
sf::RenderWindow appWindow;
sf::Renderer appRenderer(appWindow);

// Create a settings description struct
sf::ContextSettings settings;

// Settings for app window
settings.AntialiasingLevel = 4;

// Create the app window
appWindow.Create(sf::VideoMode(800, 600, 32), "SFML Text", sf::Style::Close, settings);

// Enable Smooth Shading
glShadeModel(GL_SMOOTH);

// Black Background
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// Depth Buffer Setup
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

// Really Nice Perspective Calculations
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

// Resize perspective to fit screen aspect ratio
glViewport(0, 0, 800, 600);

// Enable alpha rendering (transparency) in textures
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Enable alpha blending
glEnable(GL_BLEND);

// Enable texture mapping
glEnable(GL_TEXTURE_2D);

// Enable backface culling
glEnable(GL_CULL_FACE);

if(glGetError() != GL_NO_ERROR)
abort();

// Additional settings
appWindow.SetFramerateLimit(60);
appWindow.EnableVerticalSync(true);

const sf::Input &inpt(appWindow.GetInput());

sf::Text t;
t.SetFont(sf::Font::GetDefaultFont());
t.SetString("bla bla bla");

sf::Event e;
bool quit = false;

while(!quit)
{
while(appWindow.GetEvent(e))
{
// Window closed
if (e.Type == sf::Event::Closed)
quit = true;
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor4f(0.3f, 0.5f, 0.3f, 1.0f);

glBegin(GL_QUADS);
glVertex2f(10.0f, 0.0f);
glVertex2f(10.0f, 10.0f);
glVertex2f(0.0f, 10.0f);
glVertex2f(0.0f, 0.0f);
glEnd();

appRenderer.SaveGLStates();
appWindow.Draw(t);
appRenderer.RestoreGLStates();
appWindow.Display();
}

appWindow.Close();
 
    return EXIT_SUCCESS;
}


It doesn't crash on exit if I remove the line containing the
Code: [Select]
appWindow.Draw(t); call.

What is going on here?
Title: sf::Text not rendering properly
Post by: Laurent on May 08, 2011, 07:46:52 pm
Code: [Select]
sf::Renderer appRenderer(appWindow);
What the hell are you doing? ;)
As stated in the doc, this class is not for public use, unless you're implementing your own sf::Drawable derived class.

Quote
crashes on exit

ATI graphics card with Catalyst > 10.9?
Title: sf::Text not rendering properly
Post by: lolz123 on May 08, 2011, 08:19:34 pm
I read somewhere that when using both plain OpenGL functions in a mix with the SFML ones that the only way to preserve the OpenGL states was to use those functions found in the Renderer class. I could do it with plain OpenGL commands, such as glPushAttribute, but using the Renderer class seemed easier. :)

I upgraded my catalyst to the latest version, but it still crashes when the window closes. The call stack trace brings me to a different line than before for some reason, but it is still in the OpenGL device context class.
Title: sf::Text not rendering properly
Post by: Laurent on May 08, 2011, 08:36:52 pm
Quote
I read somewhere that when using both plain OpenGL functions in a mix with the SFML ones that the only way to preserve the OpenGL states was to use those functions found in the Renderer class. I could do it with plain OpenGL commands, such as glPushAttribute, but using the Renderer class seemed easier.

Nop, you must use the same functions but from the sf::RenderWindow (sf::RenderTarget) class. They internally forward to their renderer.

Quote
I upgraded my catalyst to the latest version, but it still crashes when the window closes. The call stack trace brings me to a different line than before for some reason, but it is still in the OpenGL device context class.

Like I said, it happens with Catalyst > 10.9 so upgrading doesn't help ;)
What helps is static linking, or not using the default font at all.
Title: sf::Text not rendering properly
Post by: lolz123 on May 08, 2011, 10:14:32 pm
It doesn't do it with just the default font, it happens with all fonts.
Before I upgraded to the latest Catalyst, I had Catalyst 9.7 and it didn't work either.
Title: sf::Text not rendering properly
Post by: Laurent on May 09, 2011, 07:49:14 am
Quote
It doesn't do it with just the default font, it happens with all fonts.

To avoid instanciating the default font, you must never instanciate a sf::Text with the default constructor.

This:
Code: [Select]
sf::Text t;
t.SetFont(font);
t.SetString("bla bla bla");

... won't work, you must do this:
Code: [Select]
sf::Text t("bla bla bla", font);

Quote
Before I upgraded to the latest Catalyst, I had Catalyst 9.7 and it didn't work either.

Sorry, it's not that bug. This one happens with all versions of the driver, indeed.
Title: sf::Text not rendering properly
Post by: tntexplosivesltd on May 09, 2011, 08:56:59 am
Quote from: "Laurent"

Sorry, it's not that bug. This one happens with all versions of the driver, indeed.

I hate ATI -_-
Title: sf::Text not rendering properly
Post by: lolz123 on May 10, 2011, 01:18:44 am
Yup, that worked. Thanks for the help!