SFML community forums

Help => Graphics => Topic started by: Metal_MAN on April 28, 2011, 04:03:01 pm

Title: Trouble with View resizing
Post by: Metal_MAN on April 28, 2011, 04:03:01 pm
Hi,

First of all, I'm using SFML 1.6 on ubuntu machine. I tried to write simple image viewer with smooth scrolling, but I've stucked with silly problem. When my Application window have odd height "View.SetCenter" can't precisely point to window center. Thanks to this my image is rescaled :cry: cause view is always fitting the window size. Does anyone have a workaround for this kind of issue?
Title: Trouble with View resizing
Post by: Laurent on April 28, 2011, 04:29:52 pm
Can you post a screenshot, or give a complete and minimal code that reproduces your problem?
Title: Trouble with View resizing
Post by: Metal_MAN on April 28, 2011, 11:13:38 pm
Here's a small part of screenshot that shows issue. On top is original, second is resized by my app. (4-th line of text is distorted)

(http://img819.imageshack.us/img819/4049/compareh.png) (http://img819.imageshack.us/i/compareh.png/)

Here is a minimal code
 
Code: [Select]


#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;


int main(){


sf::RenderWindow App(sf::VideoMode(800, 765, 32), "Fail");

sf::View View( App.GetDefaultView().GetHalfSize(),App.GetDefaultView().GetHalfSize() );

   sf::Image ibackground;
   ibackground.LoadFromFile("test.png");
   ibackground.SetSmooth(false);

   sf::Sprite background;
   background.SetImage(ibackground);

   while (App.IsOpened()){
      sf::Event Event;
        while (App.GetEvent(Event)){
            if (Event.Type == sf::Event::Closed)
                App.Close();

            if( Event.Type == sf::Event::Resized )
            {
                View.SetHalfSize( Event.Size.Width /2, Event.Size.Height /2 );
                View.SetCenter(   Event.Size.Width /2, Event.Size.Height /2 );
                App.GetDefaultView().SetHalfSize( View.GetHalfSize() );
                App.GetDefaultView().SetCenter( View.GetCenter() );
        cout<<Event.Size.Width<<" "<<Event.Size.Height<<endl;
        cout<<"Center: "<<View.GetCenter().x<<" "<<View.GetCenter().y<<endl;
            }
        }
      App.Clear();
      App.SetView(View);
      App.Draw(background);
      App.SetView( App.GetDefaultView() );
      App.Display();
  }
   return 0;
}


Now that I tried to prepare a minimal code, I see that when application start having odd height image behave normal. But when I try to resize on y axis some distortion appear. So probably my Event::Resized is guilty


I have also another question. My desktop have 1600x900 px, when I try to set VideoMode() in above code to this:
Code: [Select]

sf::RenderWindow App(sf::VideoMode(1600, 900, 32), "test");

After run, my application enter twice Evet::Resized.

first with:
Quote

Event.Size.Width = 1592 and Event.Size.Height = 822


and second with what was set in sf::VideoMode()
Quote

Event.Size.Width = 1600 and Event.Size.Height = 900


Why it is like that ?
Title: Trouble with View resizing
Post by: Laurent on April 28, 2011, 11:28:42 pm
Quote
Code: [Select]
View.SetHalfSize( Event.Size.Width /2, Event.Size.Height /2 );
View.SetCenter(   Event.Size.Width /2, Event.Size.Height /2 );

You divide an int by an int, so the result is an int; with odd numbers you'll get a rounded result instead of a decimal number.
Do this instead:
Code: [Select]
View.SetHalfSize( Event.Size.Width /2.f, Event.Size.Height /2.f );
View.SetCenter(   Event.Size.Width /2.f, Event.Size.Height /2.f );


Quote
Why it is like that ?

Probably because the size is too high for your desktop. It first resizes the window to the maximum valid size (ie. desktop size minus the window's decorations minus the task bar height), then the window gets its actual size.
But well, I have no idea what happens exactly, you should look at the MSDN.
Title: Trouble with View resizing
Post by: Metal_MAN on April 29, 2011, 10:09:45 am
Thanks Laurent, dividing by float solved problem. But one thing that I can't understand is how can a center of View can point to a half of pixel and why is that when using float, image isn't distorted. It should be anyway or am I wrong?
Title: Trouble with View resizing
Post by: Laurent on April 29, 2011, 11:01:20 am
Quote
But one thing that I can't understand is how can a center of View can point to a half of pixel

It's not a pixel, it's a unit of the 2D world. In fact, views have nothing to do with pixels, it's the window that takes what the view sees, and maps it to its pixels.

Quote
why is that when using float, image isn't distorted

Why should it be distorted?
Title: Trouble with View resizing
Post by: Metal_MAN on April 29, 2011, 11:13:03 am
Quote

(...)views have nothing to do with pixels, it's the window that takes what the view sees, and maps it to its pixels.

I did not know that I was dealing with 2D world units. But as you sad it is anyway mapped to pixels, so some units transformations eg. round are needed. So in the end original image is resized by some transformation algorithm, and with more or less effect it will be distorted.
Title: Trouble with View resizing
Post by: Laurent on April 29, 2011, 11:38:05 am
Quote
So in the end original image is resized by some transformation algorithm, and with more or less effect it will be distorted.

Yep.
Title: Trouble with View resizing
Post by: Metal_MAN on April 29, 2011, 11:58:38 am
Before posting to forum I had idea how to workaround this issue. To keep original size of image (when we dealing with odd height) one could add additional line of pixels to change odd height to even. Thanks to this view should always set Center correctly and image shouldn't be rescaled. But It will be to much trouble to me to implement this, so I think I stay with your advice.