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

Author Topic: Graphics Stretching on Window Resize  (Read 8578 times)

0 Members and 1 Guest are viewing this topic.

mike38

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Michael's Software Site
Graphics Stretching on Window Resize
« on: March 27, 2013, 06:38:19 pm »
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

« Last Edit: March 27, 2013, 06:48:41 pm by mike38 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Graphics Stretching on Window Resize
« Reply #1 on: March 27, 2013, 11:29:19 pm »
You'll have to update SFML's view when the window size changes (e.g. see here). :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mike38

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Michael's Software Site
Re: Graphics Stretching on Window Resize
« Reply #2 on: March 28, 2013, 07:56:13 pm »
Thanks very much I got it working! :)

mike38

 

anything