Problems Re-sizing SFML RenderWindow

I’ve been using the newly-released SFML 2.0 as an excuse to learn C++. It’s been quite interesting.

While messing around with the SFML graphics capabilities I found that I can’t seem to reliably re-size the SFML render window without the contents getting completely scrambled.

Here is a very simple example (forgive how WordPress mangles it):


#include <SFML/Graphics.hpp>

int width = 200;
int height = 200;

sf::VertexArray varray(sf::Points);
sf::Vertex vertex;

void stripes()
{
 varray.clear();
 varray.resize(width * height);
 for (int y = 0; y < height; y++)
 {
 for (int x = 0; x < width; x++)
 {
 vertex.position = sf::Vector2f(x, y);
 vertex.color = sf::Color::White;
 if ((x % 10) == 0){vertex.color = sf::Color::Red;}
 varray.append(vertex);
 }
 }
}

int main()
{
 sf::RenderWindow window(sf::VideoMode(width, height), "Resize Test");
 stripes();

while (window.isOpen())
 {
 sf::Event event;
 while (window.pollEvent(event))
 {
 if (event.type == sf::Event::Closed)
 window.close();
 }
 window.clear();
 window.draw(varray);
 window.display();
 }
 return 0;
}

This produces a striped image.

resizetest1Neat. Ok, now I’ll re-size my window.

resizetest2Oof, not so neat. But, it kind of looks like it’s taking my 200 x 200 array of data and stretching it across the window equally. That makes sense, I suppose. Well, the RenderWindow class can detect when it’s resized, so what I’ll do is capture that event and re-calculate what I’m displaying to be correct to the new size.

Here’s main() with some re-size detection code that re-runs the function that generates the array of points that gets plotted.


int main()
{
 sf::Vector2u windowsize;
 sf::RenderWindow window(sf::VideoMode(width, height), "Resize Test");
 stripes();

while (window.isOpen())
 {
 sf::Event event;
 while (window.pollEvent(event))
 {
 if (event.type == sf::Event::Closed)
 window.close();

if (event.type == sf::Event::Resized)
 {
 windowsize = window.getSize();
 width = windowsize.x;
 height = windowsize.y;
 stripes();
 }
 }
 window.clear();
 window.draw(varray);
 window.display();
 }
 return 0;
}

However, once again after I re-size the window…

resizetest3

Bleh.

I’ve done this several ways, but no dice.

EDIT: I figured out a solution. I don’t know if it’s THE solution, however. I realized that I’m initializing the RenderWindow at a specific resolution, and that re-sizing the window might not be telling it to re-size the video mode. At the end of my if-statement where I regenerate the vertex array, I put:


window.create(sf::VideoMode(width, height), "Resize Test");

So it starts of like this:

resizetest1

And re-sizes into this:
resizetest4There is one lingering problem. After I release the edge the window, the whole window “jumps” to a slightly different location. This must be the result of re-creating the window. It’s first closing it, and then re-creating it.

I wonder if there is a way to put it back right where it was, or if there is a more graceful solution.

Here is a gist of the functional program: https://gist.github.com/anonymous/5709180

This entry was posted in Hobbies, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s