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

Author Topic: All sprites shifted down and right when resizing the window  (Read 4469 times)

0 Members and 1 Guest are viewing this topic.

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
All sprites shifted down and right when resizing the window
« on: August 05, 2011, 07:09:09 am »
I am using window resizing by factors of 2 and 3 for zooming in, and I have noticed that whenever the window is resized by a factor of 2 or 3 (I have not tried other sizes), All of the sprites are shifted down and right by exactly one pixel.  Here is some code showing the problem, what do I do?

Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(200, 200),
                         "SFML Testing",
                         sf::Style::Titlebar | sf::Style::Close);

    sf::Font myFont;
    if (!myFont.LoadFromFile ("6x13font.ttf", 12))
    {
        // Error
    }

    sf::Image spriteSheet;
    spriteSheet.LoadFromFile ("Go_sprites.png");
    spriteSheet.SetSmooth (false);

    sf::Sprite mySprite;
    mySprite.SetImage (spriteSheet);
    while (App.IsOpened())
    {
        // Here we process the events list
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            if (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Space)
            {
                App.SetSize (400, 400);
            }

        }

        // Clear the screen with a color
        App.Clear(sf::Color::Black);

        // Here you will draw all stuff in the frame buffer
        App.Draw (mySprite);
        // Render the frame on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
All sprites shifted down and right when resizing the window
« Reply #1 on: August 05, 2011, 10:43:05 pm »
Does anyone have any idea why this is happening?

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
All sprites shifted down and right when resizing the window
« Reply #2 on: August 07, 2011, 08:24:45 am »
None of the 72 people who have seen this thread know anything helpful?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
All sprites shifted down and right when resizing the window
« Reply #3 on: August 07, 2011, 08:44:27 am »
Quote from: "blueeyedlion"
None of the 72 people who have seen this thread know anything helpful?
The "Views" field doesn't tell you how many different people have seen your thread. It tells you how often the thread has been downloaded, which is different, already because of search engine spiders.

Besides, it is quite unnecessary to push the thread only after three hours, especially that early in the morning :roll:

Quote from: "blueeyedlion"
All of the sprites are shifted down and right by exactly one pixel.
I can't reproduce this behavior. Are you sure the translation is not only a result of the scale? Can you show two screenshots and explain how your program deviates from the expected behavior?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
All sprites shifted down and right when resizing the window
« Reply #4 on: August 07, 2011, 09:22:55 am »
Quote
it is quite unnecessary to push the thread only after three hours, especially that early in the morning Rolling Eyes

Actually that is 33 hours, and due to time zones, it encompassed 2 entire afternoons in western north america

Now the problem:

This is the default size, everything is fine.


This is zoomed in x2. Note the thin black lines at the left and top of the sprite:

how do I stop those lines from appearing?

Also, when I use strings, only the sprites are moved, the strings stay where they are supposed to.

EDIT: Why aren't the images working?

SCPM

  • Newbie
  • *
  • Posts: 11
    • View Profile
All sprites shifted down and right when resizing the window
« Reply #5 on: August 07, 2011, 04:07:22 pm »
Hello:

For the images, they need to be set to the file itself, not the link:
This:
http://www.freeimagehosting.net/newuploads/0f786.png
Not:
http://www.freeimagehosting.net/0f786

TL;DR - Solution: For now, use RenderWindow's Create function to change the resolution and Views to change the "zoom" of the screen.

I have been able to replicate this SetSize problem.  I created a 1 x 1 render window, set up a sprite, and then before calling the main loop I re-sized the window to 1280 x 1024.  The image was proportionally offset by the window border and title bar's sizes.  On my Windows XP, the left of the sprite was 11 pixels away from the border, which is 4 pixels wide, and the top of the sprite was 376 pixels away from the bottom of the title bar, which is 30 pixels tall.  The greater the proportional difference between the original window and the re-sized window, the greater the offset will be (as far as I can tell, this only occurs when going from a smaller size to larger).  Not only that, the window's contents will be similarly stretched out.  Consider the following code:
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    // Create the main window
    sf::RenderWindow App(sf::VideoMode(500, 500, 32), "SFML Events");

    sf::Shape Rect = sf::Shape::Rectangle(1, 1, 20, 20, sf::Color::Green);

    App.SetSize (800, 800);

    // Start main loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        // Get elapsed time
        float ElapsedTime = App.GetFrameTime();

        // Move the shape
        if (App.GetInput().IsKeyDown(sf::Key::Left))  Rect.Move(-80 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Right)) Rect.Move( 80 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Up))    Rect.Move(0, -80 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Down))  Rect.Move(0,  80 * ElapsedTime);

        // Clear the screen (fill it with white color)
        App.Clear(sf::Color(255, 255, 255));

        // Draw shape
        App.Draw(Rect);

        // Display window on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


Which produces a small square that you can move around.  But only change the initial width and height of the RenderWindow to 50, 50 and the square will then be stretched out along the y axis.

Since I've only been using SFML for about a week, and I don't have much programming experience since it's only a hobby for me, I don't know what the culprit is, or of any other solution than to use RenderWindow's Create function to change the resolution, and setting a View to change the "zoom" of the Window content.  Sorry I couldn't be of more help!

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
All sprites shifted down and right when resizing the window
« Reply #6 on: August 07, 2011, 08:20:42 pm »
That could work, except I don't like that the window moves back to the centre of the screen whenever it is created.  Is there any way to fix that or the original problem?

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
All sprites shifted down and right when resizing the window
« Reply #7 on: August 18, 2011, 06:32:19 am »
Does anyone know why only the sprites are moved, and not anything else?

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
All sprites shifted down and right when resizing the window
« Reply #8 on: August 23, 2011, 12:14:20 am »
Does anyone know why only the sprites are moving, and have any idea what-so-ever as to how to go about lining the sprites up with everything else?

And if no one knows what to do, does anyone know somewhere else I could look for help?

And if that fails: how does everyone else handle zooming in without changing the amount of area visible?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
All sprites shifted down and right when resizing the window
« Reply #9 on: August 23, 2011, 10:09:40 am »
No idea about the problem... But you could try SFML 2. Code using a current revision is also easier to analyze, since many things have changed and SFML 1 is not developed any further.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

blueeyedlion

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
All sprites shifted down and right when resizing the window
« Reply #10 on: August 23, 2011, 11:15:16 pm »
How much of my code would I have to change to switch to SFML 2?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
All sprites shifted down and right when resizing the window
« Reply #11 on: August 24, 2011, 12:23:23 am »
This thread can give you an impression about the API changes.

In your code, this would be:
  • sf::Input -> now sf::Mouse, sf::Keyboard and sf::Joystick
  • sf::Key -> now sf::Keyboard
  • sf::RenderWindow::GetFrameTime() -> returns now sf::Uint32 milliseconds
  • sf::RenderWindow::GetEvent() -> renamed to PollEvent()
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything