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

Author Topic: SFML1.6: SetSubRect animations don't work on 32bit systems?!  (Read 5090 times)

0 Members and 2 Guests are viewing this topic.

wilbefast

  • Newbie
  • *
  • Posts: 31
    • View Profile
    • http://wilbefast.com
Please help!  :cry:

For those who don't know, I've been working on a debugging-themed top-down shooter called Bugger! for the last 9 months: the above video shows the animations working on Ubuntu 10.04 64bit. They've been working since very early on in fact.
However on Windows systems and now on Ubuntu 10.10 32bit, using exactly the same animation code , the offsets are incorrect! For example for this sheet:



We end up with bits of the green separating pixels appearing in frame:


(ignore the tearing - that's just Ubuntu's screen-capture playing up)




:cry: It's driving me insane!

Minimal code:

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

using namespace std;
using namespace sf;

void moveRect(IntRect& rect, int x, int y)
{
    rect.Offset(-rect.Left,-rect.Top);
    rect.Offset(x,y);
}

int main()
{
    //Create the main window
    RenderWindow window(VideoMode(640, 480, 32),"Animation Test");
    window.UseVerticalSync(true);
    window.SetFramerateLimit(5);

    //Load a sprite to display
    Image image;
    if (!image.LoadFromFile("sheet.png"))
        return EXIT_FAILURE;

    IntRect frame(0,0,64,64);
    Sprite sprite(image);
    sprite.SetPosition(300,300);

    //Start the game loop
    int i = 0;
    while (window.IsOpened())
    {
        i = (i+1)%5; //increment frame number and loop around
        moveRect(frame,i*(frame.GetWidth()+1),0); //move rectangle
        sprite.SetSubRect(frame); //bind rectangle to sprite-sheet to cut out correct frame

        //Process events
        Event event;
        while (window.GetEvent(event))
        {
            // Close window : exit
            if (event.Type == Event::Closed)
                window.Close();

        }

        // Clear screen
        window.Clear(Color(255,255,0));

        // Draw the sprite
        window.Draw(sprite);

        // Update the window
        window.Display();
    }

    return EXIT_SUCCESS;
}


File used for minimal example:


Whether the animation works or not may depend heavily on your system. Let me know what you see and what OS you're using so we can pinpoint exactly what the problem is.

Thanks,

William

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML1.6: SetSubRect animations don't work on 32bit systems?!
« Reply #1 on: May 07, 2011, 11:51:04 am »
Try this:
Code: [Select]
image.SetSmooth(false);
Laurent Gomila - SFML developer

wilbefast

  • Newbie
  • *
  • Posts: 31
    • View Profile
    • http://wilbefast.com
SFML1.6: SetSubRect animations don't work on 32bit systems?!
« Reply #2 on: May 09, 2011, 08:58:27 am »
That worked  :shock: Haha! So it wasn't my fault  :D

Sorry I didn't reply before: I had trouble with my internet connection.

Wait a minute though... I'm still getting little border-lines very rarely: when I'm travelling along I'll see a flash of yellow border-pixels, every so often, but haven't been able to reproduce it effectively  :?
NB - I'm not actually using SFML's views because I was too lazy to read the manual, so wrote my own using sf::FloatRect.

wilbefast

  • Newbie
  • *
  • Posts: 31
    • View Profile
    • http://wilbefast.com
SFML1.6: SetSubRect animations don't work on 32bit systems?!
« Reply #3 on: May 12, 2011, 08:39:53 pm »
The borderlines appear occasionally when the camera has a very specific offset  - specific down to the pixel. Well, actually it seems to be a questions of fractions of a pixel. I can't figure out the pattern of it though. I got lucky a few times and managed to sit in the sweet-spot to take a screenshot:

too big to embed in post (shrinking would make it harder to see the problem)

What's weird is:
A/ if part of the border is appearing then the sub rectangle used my the background tiles must be off by a pixel, BUT...
B/ it isn't (I've made sure - the offsets are always correct), which is not at all surprising since...
C/ the position of the camera in no way influences how the sub rectangles are calculated anyway :shock:


So why do bits of borderline appear?

Remember that I'm using an sf::FloatRect for views because I didn't realise SFML already had one? Well, I changed this line:
Code: [Select]
stamp.SetPosition(x*CELL_SIZE-view->Left,y*CELL_SIZE-view->Top);

To this:
Code: [Select]
stamp.SetPosition((int)(x*CELL_SIZE-view->Left),(int)(y*CELL_SIZE-view->Top));

In other words the Sprite used for printing all the background tiles is snapped to the nearest pixel. This causes the problem to stop. The only possible explanation is that, even when smoothing is turn off:

 :o Moving a Sprite to a position that is a fraction of a pixel may cause parts of the source image that are not in the defined sub rectangle to be drawn!

Phew - I figured it out  :D And while fairly dirty, casting to int before drawing isn't really a visible difference.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
SFML1.6: SetSubRect animations don't work on 32bit systems?!
« Reply #4 on: May 12, 2011, 08:58:54 pm »
Well in my project group at the University we dropped spritesheets entirely, they are just a pain to create and maintain(Just think of the event when we need to cut the game down by several megabytes to get to an acceptable limit).

Easy way to get flexibility: 1 image = 1 Frame
So we dedicate an entire folder for one animation. Though there is a huge negative on this approach. We'll have to send several different textures to the GPU.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

wilbefast

  • Newbie
  • *
  • Posts: 31
    • View Profile
    • http://wilbefast.com
SFML1.6: SetSubRect animations don't work on 32bit systems?!
« Reply #5 on: May 12, 2011, 09:48:03 pm »
Ironically though using separate images will generally take up more space, both in memory and on the disk, than using atlases: it might be harder to cut things down, but you'll need to cut them down less. I also find it more convenient to edit a single file for each character/monster in the game.

If you ever need a hand with sprite-sheets, I've got it all working again (though the pixel-snap is less pleasing on the eyes than the interpolation) and would be happy to help  :wink:

edit: I should mention that I'm also using XML files to manage the sheets, rather than hard-coding everything. This certainly makes it a lot less fiddly than it was before  :?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
SFML1.6: SetSubRect animations don't work on 32bit systems?!
« Reply #6 on: May 13, 2011, 01:13:15 am »
Quote from: "wilbefast"
Moving a Sprite to a position that is a fraction of a pixel may cause parts of the source image that are not in the defined sub rectangle to be drawn!
I think a recent post of mine concerns the same issue.

By the way, I still don't know whether there is a solution except of the rounding workaround, maybe I should add this issue to the SFML task tracker. And I should also do some OpenGL research...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything