SFML community forums

Help => Graphics => Topic started by: Jeckie on July 25, 2013, 12:29:37 pm

Title: Array and tiles
Post by: Jeckie on July 25, 2013, 12:29:37 pm
I made 2 pictures and loaded them:
        sf::Texture grass;
        if(!grass.loadFromFile("Data/grass.png"))
        {
                return EXIT_FAILURE;
        }

        sf::Texture water;
        if(!water.loadFromFile("Data/water.png"))
        {
                return EXIT_FAILURE;
        }
Rendered:
        sf::RectangleShape t1;
        t1.setSize(sf::Vector2f(50, 50));
        t1.setTexture(&grass);
       
        sf::RectangleShape t0;
        t0.setSize(sf::Vector2f(50, 50));
        t0.setTexture(&water);
Then I made this multidimensional array called tile:
sf::RectangleShape tile[4][4] = {
                {t1,t1,t1,t1},
                {t1,t0,t0,t0},
                {t1,t1,t1,t1},
                {t0,t0,t0,t0}
        };
and added those 2 pictures into it.
Now when I draw this:
                        window.clear();

windw.draw(tile[0][0]);
                       
                        window.display();
Program draws the t1 rectangle shape.

So, my question is how I can draw all those 16 items in array so they are placed like in array.
I tried to make a for loop like this:
for(int x=0;x<4;x++)
{
      for(int y=0;y<4;y++)
      {
            window.draw(tile[x][y]);
      }
}
 
Program just draws the tile[3][3] which is t0;

I hope you understand my english and sorry if this is a stupid question.
Title: Re: Array and tiles
Post by: Laurent on July 25, 2013, 12:42:07 pm
for (int x = 0; x < 4; ++x)
    for (int y = 0; y < 4; ++y)
        tile[x][y].setPosition(x * TILE_WIDTH, y * TILE_HEIGHT);
Title: Re: Array and tiles
Post by: Jeckie on July 25, 2013, 06:12:06 pm
How I can make collision with t0 rectangle shape?
Title: Re: Array and tiles
Post by: Atani on July 25, 2013, 07:52:11 pm
How I can make collision with t0 rectangle shape?
Code something like this might work:
if(t0.getGlobalBounds().intersects(otherShape.getGlobalBounds())) {
// do something since targetShape has collided with t0 shape.
}
 

This assumes that you are using shape for both your t0 and other object that you are checking has collided.
Title: Re: Array and tiles
Post by: Jeckie on July 25, 2013, 08:18:35 pm
Quote
if(t0.getGlobalBounds().intersects(otherShape.getGlobalBounds())) {
// do something since targetShape has collided with t0 shape.
}
I already did that and it's not working.
My shape can't move.
Btw its position is 0,0 as t1(grass) position, it doesn't draw on t0(water) at the start.