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

Author Topic: a bug of sfml graphical module  (Read 3066 times)

0 Members and 1 Guest are viewing this topic.

yingche

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
a bug of sfml graphical module
« on: March 11, 2016, 02:54:50 pm »
This is the source code:

int main()
{
   Font font;
   font.loadFromFile(String("arial.ttf"));
   RenderWindow window(VideoMode(640, 480), "1");
   Text text("8", font, 32);
   text.setColor(Color(0, 0, 0, 255));
   FloatRect r;
   r=text.getGlobalBounds();
   text.move(-r.left, 0);
   RenderTexture tex;
   tex.create(r.width, 50);
   tex.clear(Color(0, 0, 0, 0));
   tex.draw(text, RenderStates(BlendNone));
   tex.display();
   Sprite s(tex.getTexture());
   while (1){
      window.clear(Color(255, 255, 255, 255));
      s.setPosition(100, 160);//OK
      window.draw(s);
      s.setPosition(100.5, 190);//BUG
      window.draw(s);
      window.display();
      Event event;
      window.waitEvent(event);
      if (event.type == Event::Closed)break;
   }
   return 0;
}

I want to draw some text to a render target, and draw the target texture to screen. But the text on the screen
 seems abnormal.
http://pan.baidu.com/s/1bnTZ5AR
Is it a bug? If yes,I hope it can be fixed as soon as possible. After all, it is really a serious bug. Thanks very much.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: a bug of sfml graphical module
« Reply #1 on: March 11, 2016, 04:22:27 pm »
Quote
http://pan.baidu.com/s/1bnTZ5AR
Doesn't work for me (or incredibly slow?).

Quote
s.setPosition(100.5, 190);//BUG
In sf::Transformable doc:
Quote
A note on coordinates and undistorted rendering:
By default, SFML (or more exactly, OpenGL) may interpolate drawable objects such as sprites or texts when rendering. While this allows transitions like slow movements or rotations to appear smoothly, it can lead to unwanted results in some cases, for example blurred or distorted objects. In order to render a sf::Drawable object pixel-perfectly, make sure the involved coordinates allow a 1:1 mapping of pixels in the window to texels (pixels in the texture). More specifically, this means:

The object's position, origin and scale have no fractional part
The object's and the view's rotation are a multiple of 90 degrees
The view's center and size have no fractional part
Laurent Gomila - SFML developer

yingche

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: a bug of sfml graphical module
« Reply #2 on: March 12, 2016, 01:39:25 am »
First,I wonder why the center coordinate need to be an integer. I don't rotate the texture or scale it. My texture has a width of an odd number,so the center position must be a decimal. Next,it seems that sfml will dispose fractional part of left coordinate when displaying on window. So it really confuses me a lot why the bug appears.
« Last Edit: March 12, 2016, 01:45:40 am by yingche »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: a bug of sfml graphical module
« Reply #3 on: March 12, 2016, 01:51:21 am »
Your display has x amount of pixels. It can't render a pixel in between two pixels. So if you tell SFML/OpenGL to render a pixel in between two pixels, what do you expect to happen? OpenGL may approximate the pixel and color both neighboring pixels with some color, so it "looks like" there's a pixel between the two pixels. This is often unwanted, as such it's recommended to specify integer positions, that way the "pixel" of the object map directly to your display pixels.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

yingche

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: a bug of sfml graphical module
« Reply #4 on: March 12, 2016, 05:41:26 am »
I have got new findings. Change the sentence:
s.setposition(100,60);
to
s.setposition(100.2,60);
s.setposition(100.4,60);
s.setposition(100.498,60);
s.setposition(100.502,60);
s.setposition(100.6,60);
s.setposition(100.8,60);
The results are all OK.
However,as for 100.499, 100.5, 100.501, the results are bad.
So I think it must be a bug of sfml. Please run my source code, and you must agree with me, too.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: a bug of sfml graphical module
« Reply #5 on: March 12, 2016, 06:31:48 am »
Nearer the centre is harder to decide which side it should be.

Why do you want to display a pixel texture onto a pixel display at a position of a fraction of a pixel?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

yingche

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: a bug of sfml graphical module
« Reply #6 on: March 12, 2016, 08:02:33 am »
I write a function which is used to show a picture on screen. The function is like this:
void drawTexture(float centerX,float centerY);
When I need to draw a title texture, which means the texture should be centered, and I donnot konw the texture size, using this function will be very convenient. However, if the centerX parameter is an integer and the texture size is an odd number, the upper left corner coordinate will be a decimal. So you can see it is really a common situation.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: a bug of sfml graphical module
« Reply #7 on: March 13, 2016, 11:53:47 pm »
Rounding down after dividing the width and height by two isn't particularly difficult and is a simple and common solution to this "common situation".
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*