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

Author Topic: how to create a grid?  (Read 19552 times)

0 Members and 1 Guest are viewing this topic.

ntrncx

  • Newbie
  • *
  • Posts: 5
    • View Profile
how to create a grid?
« on: November 22, 2011, 08:40:04 am »
in cmd you create a 2d array

Code: [Select]
for(int x=0;x<100;x++)
{
      for(int y=0;y<100;y++)
      {
            grid[x][y];
       }
}


in sfml how i create a grid and draw the needed part?
any tutorial?or link?
i thought to create an object for each part but i dont know how to make it,
i want to explain me the logic and give me a tutorial or link with an example if you can,i searched on google but didnt found anything

thanks for your time

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
how to create a grid?
« Reply #1 on: November 22, 2011, 10:21:38 am »
Read the SFML tutorials on creating windows and printing sprites. After that, you should be able to make your own logic on how to do what you want.
You may want to search for printing "tiles" as well, as I think that is what you want to do.

By the way, your fors misses the closing parentheses at the end: ")".

ntrncx

  • Newbie
  • *
  • Posts: 5
    • View Profile
how to create a grid?
« Reply #2 on: November 22, 2011, 11:02:51 am »
well i did watched them,i already made 2 small games. but they say the basics,i want to study and read about it,but i need the start.the logic,i don't want code or "ready solution",

if you can describe by words would be helpful :D or link of a tutorial will be appreciated

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
how to create a grid?
« Reply #3 on: November 22, 2011, 03:05:06 pm »
So, lets put it into steps.

1) Load your image(s) containing the tiles. I suggest using one only image with all the tiles, and using the sprite to set the correct SubRect for each tile. That way, you will have an array of sprites, each representing one different kind of tile, and one image.
2) Create your map matrix. Usually, this is done using a matrix of numbers: each number representing one position of the sprites array. Lets supose that the tile on the second sprite of the array is on the second and third columns of the first line; The map matrix will have:
Code: [Select]
map[0][1] = 2;
map[0][2] = 2;

Remember that, as the positions start at 0, the second position is 1, and 2 is the third.
3) Loop though your map matrix, as you specified on your opening post, and print one sprite at each iteration. Don't forget to set the right position to the sprite before printing it, or else all of your sprites would be printed on the default position, one on top of the other.
3.1) To get the right sprite is easy:
If the number is zero, then we have no tile on the position... Nothing to be done. Any other positive number will give us the sprite by that formula:
Code: [Select]
sf::Sprite rightSprite = sprites[map[line][column] - 1];
We've put - 1 on the positions because they starts at 0 on the array, and on our map we consider them starting from 1.
3.2) To set the right position, we must know the size for each tile. The best way is to have tiles of the same size. Then, we can get the position with:
Code: [Select]
int xPosition = column * tileSize;
int yPosition = line * tileSize;

That way, if we are printing the first line (line = 0), its y position would be 0. If we are printing the second (line = 1), it y position is tilesize. And so on.
Then to put the position on the sprite:
Code: [Select]
sprite.SetX(xPosition);
sprite.SetY(yPosition);


I think that is more than enough for you to make a tile based map.

ntrncx

  • Newbie
  • *
  • Posts: 5
    • View Profile
how to create a grid?
« Reply #4 on: November 22, 2011, 06:34:28 pm »
stupid me i got confused without reason
i did this
Code: [Select]

for(int x=0;x<800;x+=30)
    {
        for(int y=0;y<600;y+=45)
        {      
            testSprite.SetPosition(x,y);
            App.Draw(testSprite);
        }  
    }


why it blinks like crazy?this code is out of the game loop;[/code]
just to test and understand how it works.but why it blinks?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
how to create a grid?
« Reply #5 on: November 22, 2011, 06:44:37 pm »
Quote
this code is out of the game loop;

It must be inside.
Laurent Gomila - SFML developer

RKim

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: how to create a grid?
« Reply #6 on: April 25, 2018, 09:56:06 pm »
I am trying to do the similar task by creating the Grid class. Basically, I am trying to create 2D sf::RectangleShape arrays. The following is the snippet of the code.  However, when I try to draw by looping through each element in Rects[][], it doesn't show any. Can anyone advise the mistake(s) that I made?

Thanks!!


#include <SFML/Graphics.hpp>

class Grid {
private:
    sf::RectangleShape** Rects;
    sf::RenderWindow *gridWindow;
    int row;
    int col;
public:
    Grid() {
    }
    Grid(int r, int c, sf::RenderWindow *window): row(r), col(c), gridWindow(window) {
        int w = gridWindow->getSize().x * 1.0 / row;
        int h = gridWindow->getSize().y * 1.0 / col;

        Rects = new sf::RectangleShape*[r];
        for (int i = 0; i < row; i++)
            Rects = new sf::RectangleShape[col];

        // need to create an actual cell
        int baseX = 100;
        int baseY = 100;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                Rects[j] = sf::RectangleShape();
                Rects[j].setPosition(baseX, baseY);
                Rects[j].setFillColor(sf::Color::Yellow);
                Rects[j].setOutlineColor(sf::Color::White);
                Rects[j].setOutlineThickness(10);
                baseX += w;
            }
            baseX = 10;
            baseY += h;
        }
    }

 

anything