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

Author Topic: Sprites doesn't render properly  (Read 1762 times)

0 Members and 1 Guest are viewing this topic.

Enok

  • Newbie
  • *
  • Posts: 16
    • View Profile
Sprites doesn't render properly
« on: April 27, 2014, 05:28:25 am »
Hello, I'm having a bit of a problem here and I can't quite see why.

I have a folder that contains 16 images and I want to render them to the screen and spread them out.




This is how I load them. I want all items to show up, but instead only 1 item shows up in 16 different positions...


sf::Sprite mazeSprite[16];

initialize()
{
      sf::Texture texture;
      std::string filePath = "C:/users/enok/desktop/sprites/cube";

      float x = 10, y = 10;

      for (int i = 0; i <= 15; i++)
      {
                std::string id = std::to_string(static_cast<long long>(i)) + ".png";

                texture.loadFromFile(filePath + id);

                mazeSprite[i].setTexture(texture);
                mazeSprite[i].setTextureRect(sf::IntRect(10,10,32,32));
                mazeSprite[i].setPosition(x,y);
                x+= 20;
                std::cout << filePath + id << std::endl;
      }

}
 

this is the same sprite lined up 16 times.
http://puu.sh/8p4jz.png


This is the render() function

rWindow.clear();

                for(int i = 0; i <= 15; i++)
                {
                        rWindow.draw(mazeSprite[i]);
                }
                rWindow.display();
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Sprites doesn't render properly
« Reply #1 on: April 27, 2014, 05:32:26 am »
Because if reuse the same texture object across all your sprites each sprite will have the same texture. And if you tell that texture to load a different file then each sprite using that texture will have that file that was just loaded.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Enok

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Sprites doesn't render properly
« Reply #2 on: April 27, 2014, 06:15:21 am »
I did like so instead:

sf::Texture texture[16];

texture[i].loadFromFile(filePath + id);

mazeSprite[i].setTexture(texture[i]);
 

This code
         sf::Sprite mazeSprite[arraySize];
         sf::Texture texture[arraySize];

        //Runs once
        void engine::initialize()
        {
                std::string filePath = "C:/users/enok/desktop/sprites/cube";

                float x = 10, y = 10;

                for (int i = 0; i < arraySize; i++)
                {
                        std::string id = std::to_string(static_cast<long long>(i)) + ".png";

                        if(!texture[i].loadFromFile(filePath + id))
                        {
                                std::cout << "NOPE" << std::endl;
                        }

                        mazeSprite[i].setTexture(texture[i]);
                        mazeSprite[i].setTextureRect(sf::IntRect(10,10,32,32));
                        mazeSprite[i].setPosition(x,y);

                        x += 32;

                        std::cout << filePath + id << std::endl;
                }
        }
 

Gives me this result http://puu.sh/8pGd9.png

But only when I run it with release, not debug. If I try debug, I get access violation writing location.

Additional dependencies:(Debug) sfml-graphics-s-d.lib;sfml-window-s-d.lib;sfml-system-s-d.lib;
« Last Edit: April 27, 2014, 06:58:08 pm by Enok »