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

Author Topic: Array and tiles  (Read 2424 times)

0 Members and 1 Guest are viewing this topic.

Jeckie

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Array and tiles
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Array and tiles
« Reply #1 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);
Laurent Gomila - SFML developer

Jeckie

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Array and tiles
« Reply #2 on: July 25, 2013, 06:12:06 pm »
How I can make collision with t0 rectangle shape?
« Last Edit: July 25, 2013, 07:13:20 pm by Jeckie »

Atani

  • Newbie
  • *
  • Posts: 30
    • MSN Messenger - atanisoft@hotmail.com
    • AOL Instant Messenger - AtaniSYSOP
    • Yahoo Instant Messenger - atanisoft
    • View Profile
    • Email
Re: Array and tiles
« Reply #3 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.

Jeckie

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Array and tiles
« Reply #4 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.
« Last Edit: July 25, 2013, 08:20:10 pm by Jeckie »