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

Author Topic: Trouble understanding spritesheets  (Read 1535 times)

0 Members and 1 Guest are viewing this topic.

stardent

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Trouble understanding spritesheets
« on: May 10, 2020, 11:19:25 am »
Hey everyone I am very new to this so please bear with me. I've been following this tutorial:
https://www.gamefromscratch.com/post/2015/10/26/SFML-CPP-Tutorial-Spritesheets-and-Animation.aspx
I am on the very first  part.

I am not using his example image, but a very small 32x32 png divided into 4x4 squares of colour.
Now, whatever I use as coordinates, I get nothing, or maybe something but totally unexpected results. The way I understand it is the first two values specify the top corner of the rectangle, the second two its size.
But if I use this:
sf::Sprite sprite(texture, sf::IntRect(0, 0, 16, 16);
I expect a 16x16 pixel chunk of the top left corner of the image. However, when I run it, I get nothing.
Can someone help explain how this works? I not getting it at all!
Thanks!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Trouble understanding spritesheets
« Reply #1 on: May 10, 2020, 05:17:55 pm »
It sounds correct. The 16x16 part on the top-left of the 32x32 texture should be assigned to the sprite.

By, "get nothing", what do you mean? Are you drawing the sprite to the window? Are you sure the texture is loading correctly (are you testing the return value)? Does it show if you don't set a texture rectangle?

Since what you've said seems to be okay, it would help to see some code, preferably a complete and minimal example.

EDIT:
I should also mention that you can simplify spritesheet animation using Gallery Sprite, which is a part of Selba Ward - a collection of SFML drawables.
« Last Edit: May 10, 2020, 05:23:15 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

stardent

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Trouble understanding spritesheets
« Reply #2 on: May 10, 2020, 07:25:42 pm »
Hi thanks for looking...sorry I wasn't more specific - by nothing I meant the image is not displayed. The window opens fine and no errors. I simply get a black background and that's it. If I change the last to values to something that doesn't make sense...say 300, 400 I do see something. I see a large rectangle of the colour of one of the quadrants in the sheet.
The other thing is, is if I display the whole sheet as is - just as a single image its truncated at the top. I think it seems to be because the title of the window cuts off the image?

https://imgur.com/a/vopWmrU

Here is a 800x600 image displayed in a 800x600 window...there is some of the image missing at the top. I think that is my issue - the sprite is beneath the title bar.
(some further experimenting later) Yes that is the issue. My sprites were so small they were hidden beneath the title bar. Is that expected behaviour? If I offset the sprite vertically bay say 32 pixels it's visible.Anyway here's the code :

// Demonstrate creating a spritesheet
#include "SFML/Graphics.hpp"

int main(int argc, char** argv) {
    sf::RenderWindow renderWindow(sf::VideoMode(640, 480), "Demo Game");

    sf::Event event;
    sf::Texture texture;
    texture.loadFromFile("sheet.png");

    sf::Sprite sprite(texture, sf::IntRect(0, 0, 32, 32));
    sprite.setPosition(32, 32);


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

        renderWindow.clear();
        renderWindow.draw(sprite);
        renderWindow.display();
    }
}

Thanks for looking...I'll definitely check those links.




Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Trouble understanding spritesheets
« Reply #3 on: May 10, 2020, 10:28:27 pm »
It could be a problem with loading that image. You should be testing the return value of "loadFromFile". I had a look at the tutorial you linked in the original post and I noticed they didn't do that but you should:
sf::Texture texture;
if (!texture.loadFromFile("sheet.png"))
    return EXIT_FAILURE;
This will cause the program to quit if the image was unable to be loaded into the texture.
The seemingly most common reason for this is incorrect path for the file.
The first thing to try is to use the absolute path for the file. If this works, the program isn't looking where you think it is.
This would be something like:
if (!texture.loadFromFile("c:/images/heet.png"))
    return EXIT_FAILURE;
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything