SFML community forums
Help => Graphics => Topic started by: divineqn90 on April 12, 2010, 11:42:58 am
-
Hi,
I was just testing the SFML graphics package (version 2.0), and I've found that, ...
Load an image
Load a font
Bind texture
Draw vertices
Set font
Draw text // shows the image instead of font
Then, when I remove bind texture, draw text works. If I'm not wrong, the internal font texture isn't binded during draw? Is it a bug, shouldn't it be binded?
Cheers!
-
Can you show the corresponding code?
If you mix SFML and OpenGL calls you have to be careful about render states.
-
That's some quick response!
sf::RenderWindow win(sf::VideoMode(800, 600, 32), "Untitled!", sf::Style::Close, sf::ContextSettings(24, 8, 0, 1, 5));
win.Show(true);
win.SetActive();
Timer myTime;
Uint32 fps = 0;
Str typed = (Char)20140;
typed += (Char)20540;
typed += (Char)20940;
sf::Image img;
img.LoadFromFile(Str("Data/Raw/image.png").GetFilePath());
sf::Font font;
font.LoadFromFile(Str("Data/Raw/font2.ttc").GetFilePath());
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
sf::Text text;
while(1)
{
++fps;
if(myTime.IsOver())
{
SetWindowText(GetActiveWindow(), StrHelper.Put("Untitled! - %@1 fps", fps).ToWCString());
fps = 0;
myTime.SetTimer(1.0f);
}
sf::Event e;
while(win.GetEvent(e))
{
if(e.Type == sf::Event::Closed)
{
win.Close();
return;
}
if(e.Type == sf::Event::TextEntered)
{
if(e.Text.Unicode == '\b')
typed = typed.Substr(0, typed.GetSize()-1);
else
typed += (Char)e.Text.Unicode;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glViewport(0, 0, 400, 300);
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// gluPerspective(45.f, 1.333f, 1.f, 500.f);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
// glTranslatef(0.f, 0.f, -200.f);
// glRotatef(Time.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
// glRotatef(Time.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
// glRotatef(Time.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
// img.Bind();
// glBegin(GL_QUADS);
// glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
// //glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
// glTexCoord2f(0, 0);
// glVertex3f(-50.f, -50.f, -50.f);
// glTexCoord2f(1, 0);
// glVertex3f(-50.f, 50.f, -50.f);
// glTexCoord2f(1, 1);
// glVertex3f( 50.f, 50.f, -50.f);
// glTexCoord2f(0, 1);
// glVertex3f( 50.f, -50.f, -50.f);
// //glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
// glTexCoord2f(0, 0);
// glVertex3f(-50.f, -50.f, 50.f);
// glTexCoord2f(1, 0);
// glVertex3f(-50.f, 50.f, 50.f);
// glTexCoord2f(1, 1);
// glVertex3f( 50.f, 50.f, 50.f);
// glTexCoord2f(0, 1);
// glVertex3f( 50.f, -50.f, 50.f);
// //glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
// glTexCoord2f(0, 0);
// glVertex3f(-50.f, -50.f, -50.f);
// glTexCoord2f(1, 0);
// glVertex3f(-50.f, 50.f, -50.f);
// glTexCoord2f(1, 1);
// glVertex3f(-50.f, 50.f, 50.f);
// glTexCoord2f(0, 1);
// glVertex3f(-50.f, -50.f, 50.f);
// //glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
// glTexCoord2f(0, 0);
// glVertex3f(50.f, -50.f, -50.f);
// glTexCoord2f(1, 0);
// glVertex3f(50.f, 50.f, -50.f);
// glTexCoord2f(1, 1);
// glVertex3f(50.f, 50.f, 50.f);
// glTexCoord2f(0, 1);
// glVertex3f(50.f, -50.f, 50.f);
// //glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
// glTexCoord2f(0, 0);
// glVertex3f(-50.f, -50.f, 50.f);
// glTexCoord2f(1, 0);
// glVertex3f(-50.f, -50.f, -50.f);
// glTexCoord2f(1, 1);
// glVertex3f( 50.f, -50.f, -50.f);
// glTexCoord2f(0, 1);
// glVertex3f( 50.f, -50.f, 50.f);
// //glColor4f(0.0f, 1.0f, 1.0f, 1.0f);
// glTexCoord2f(0, 0);
// glVertex3f(-50.f, 50.f, 50.f);
// glTexCoord2f(1, 0);
// glVertex3f(-50.f, 50.f, -50.f);
// glTexCoord2f(1, 1);
// glVertex3f( 50.f, 50.f, -50.f);
// glTexCoord2f(0, 1);
// glVertex3f( 50.f, 50.f, 50.f);
// glEnd();
glViewport(400, 300, 400, 300);
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.f, 1.333f, 1.f, 500.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
glRotatef(Time.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
glRotatef(Time.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
glRotatef(Time.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
glBegin(GL_QUADS);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f( 50.f, 50.f, 50.f);
glVertex3f( 50.f, -50.f, 50.f);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f, 50.f);
glColor4f(0.0f, 1.0f, 1.0f, 1.0f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, 50.f);
glEnd();
glEnable(GL_TEXTURE_2D);
glViewport(0, 0, 800, 600);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 800, 600, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
text.SetFont(font);
text.SetString(typed.ToWCString());
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
win.Draw(text);
glEnable(GL_DEPTH_TEST);
win.Display();
}
-
You must call SaveGLStates/RestoreGLStates around SFML code. You can look at the OpenGL sample for an example.
-
Oh! ><! Ok, thank you very much!
-
Wait, just one more question, so, everytime I call SFML, I have to restore the OpenGL state by myself right? (If I don't want to use those Draw() functions, cuz I only plan to use it for fonts).
-
No, I think it's ok for all other functions. Only Draw can mess up the OpenGL states.
It has not been fully tested yet, so if you find an exception please let me know ;)
-
Ok, thanks soooo much! I really really appreciate it! :D