Hi all,
I've been using SFML for a while now and it's a fantastic package; I extend my thanks to all involved in the creation and development.
I have been using an OpenGL background and SFML text in my 2D scene. Everything works great apart from when I resize the window. I want the OpenGL background to resize with the window, but the SFML text to stay at the same size and proportion affixed in the upper left.
However, on resizing the window the text appears stretched (as if I was scaling
everything) for a split second, then returns to it's original size. The OpenGL background scales correctly. You can see this in the following image (the text is slightly stretched horizontally):
(after a split second the text returns to normal.)
Here's the relevant code. Sorry if it seems long but I have no idea where the annoying behavior is coming from.
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), _PROGRAM_NAME, sf::Style::Default,
resizeOpenGLWindow(window.getSize().x,window.getSize().y);
DocumentView mainDocumentView(window);
mainDocumentView.setup(vector2D(0,0),vector2D(0,0),
10.0f,sf::Color::White,sf::Color::Black,
mainDocumentFont);
while (applicationState.isRunning)
{
applicationState.view = sf::View(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
window.setView(applicationState.view);
handleEvents(&window,&applicationState); // handleEvents calls 'resizeOpenGLWindow' and resizes the mainDocumentView
// set up mainDocumentView
glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
mainDocumentView.update();
window.display();
}
return 0;
}
This essentially is updating the text:
int DocumentView :: update()
{
drawRichString(vector2D(topLeft.x + textMargin,topLeft.y + textMargin),
document.at(0).text,mainDocumentFont,textColour,
linkedRenderWindowPointer);
return 0;
}
And the graphics functions:
void resizeOpenGLWindow(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);
}
void drawRichString(vector2D position, RichString richString,sf::Font font,sf::Color textColour, sf::RenderWindow & renderWindow)
{
renderWindow.pushGLStates();
float characterWidth;
float lastCharacterWidth;
currentDrawText.setColor(textColour);
for (loopCounterType currentDrawCharacter = 0;
currentDrawCharacter < richString.characters.size();
currentDrawCharacter++)
{
currentDrawText.setFont(font);
lastCharacterWidth = currentDrawText.getFont().getGlyph(currentDrawText.getString().getData()[0],
currentDrawText.getCharacterSize(),false).advance;
currentDrawText.setString(richString.characters.at(currentDrawCharacter).rawChar);
currentDrawText.setPosition(position.x +
(float(currentDrawCharacter) * lastCharacterWidth)
,position.y);
renderWindow.draw(currentDrawText);
}
renderWindow.popGLStates();
}
If you feel you need more information I would be more than happy to supply you.
Thanks in advance,
mike38