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

Author Topic: first program, help making grid  (Read 5242 times)

0 Members and 1 Guest are viewing this topic.

mrsebw

  • Newbie
  • *
  • Posts: 2
    • View Profile
first program, help making grid
« on: January 23, 2015, 07:15:17 am »
hello i am new to the forum as well as sfml and mostly new to programming. just wondering if someone can help me figure out what i did wrong in making a grid, i can only get to be 2x2.

#include <SFML/Graphics.hpp>
int main()
{
    int columns = 3;
    int rows = 3;
    sf::RenderWindow window(sf::VideoMode(800, 600), "TicTacToe");
    sf::RectangleShape grid[columns][rows];


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

        window.clear();
        for(int i=0;i<columns;i++){
            for(int j=0;j<rows;j++){
                sf::Vector2f cellSize(200.0f, 200.0f);
                sf::Vector2f cellPos(5.0f,5.0f);
                sf::Vector2f cellPos2(5.0f,5.0f);
                grid[i][j].setSize(cellSize);
                grid[i][j].setOutlineColor(sf::Color::Blue);
                grid[i][j].setOutlineThickness(5.0f);

                if(i%2==1 && j%2==1){
                    grid[i][j].setPosition(cellPos.x + cellSize.x, cellPos.y + cellSize.y);
                }else if(i%2==1 && j%2==0){
                    grid[i][j].setPosition(cellPos.x + cellSize.x, cellPos.y);
                }else if(i%2==0 && j%2==1){
                    grid[i][j].setPosition(cellPos.x, cellPos.y + cellSize.y);
                }else{
                    grid[i][j].setPosition(cellPos);
                }


                window.draw(grid[i][j]);


            }
        }

        window.display();

    }
    return 0;
}

Buluphont

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: first program, help making grid
« Reply #1 on: January 23, 2015, 07:41:55 am »
Without having closely read your code, I think a problem is your use of the modulo ( % )  function. I'd read up on it, but basically what it does is return the remainder of the left side divided by the right side. So anything modulo 2 can return either 0 or 1.

Edit: seems I've misjudged the purpose of them in the code. Sorry, viewing from a mobile device.

Edit2: So yeah, I sat down and took a look at it. The problem is that your
Code: [Select]
sf::Vector2f cellPosis declared and initialized inside that for loop, and that it isn't actually changed.

You initialize it at (5, 5), and this never changes. Thus, the math using it

Code: [Select]
grid[i][j].setPosition(cellPos.x + cellSize.x, cellPos.y + cellSize.y);
can only work once.

You add cellPos.x to cellSize.x, but for the third square in that row, you're doing the same calculation and thus getting the same coordinates.

That said, there are probably better ways to accomplish what you're trying to do, such as, say, setting the position of your desired square to (your grid[][] position * cellSize).

Example:

#include <SFML/Graphics.hpp>
int main()
{
    int columns = 3;
    int rows = 3;
    sf::RenderWindow window(sf::VideoMode(800, 600), "TicTacToe");
    sf::RectangleShape grid[columns][rows];


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

        window.clear();
        sf::Vector2f cellSize(200.0f, 200.0f);

        for(int i=0;i<columns;i++){
            for(int j=0;j<rows;j++){
                grid[i][j].setSize(cellSize);
                grid[i][j].setOutlineColor(sf::Color::Blue);
                grid[i][j].setOutlineThickness(5.0f);

                grid[i][j].setPosition(i*cellSize.x + 5.0f, j*cellSize.y + 5.0f);

                window.draw(grid[i][j]);

            }
        }
        window.display();
    }
    return 0;
}
 
« Last Edit: January 23, 2015, 07:57:18 am by Buluphont »

grok

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • Email
Re: first program, help making grid
« Reply #2 on: January 23, 2015, 08:02:04 am »
Quote
i can only get to be 2x2.
that's probably because your grid is 3x3 items. change your columns/rows variables and try again.

One more notice worth to mention:
why do you reposition  your shapes during every loop iteration?
if the shapes are "static", it will be better to just setup them once (set their size, outline color, position, etc) and then just loop through the items during the render and draw them. No need to setup them on each loop iteration.
« Last Edit: January 23, 2015, 08:04:02 am by grok »

mrsebw

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: first program, help making grid
« Reply #3 on: January 23, 2015, 08:22:59 am »
Thank you soo much guys, that helps alot, sorry for bein such noob. ive changed the code and
played around with "static" and got it working, thanks alot.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: first program, help making grid
« Reply #4 on: January 23, 2015, 08:25:30 am »
Not important for such a tiny array, but I just want to point out that whether you use column major or row major traversal can make a big difference in performance. Always attempt to traverse data structures in the most CPU cache friendly way possible.
« Last Edit: January 23, 2015, 01:11:05 pm by Jesper Juhl »

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: first program, help making grid
« Reply #5 on: January 26, 2015, 03:59:39 am »
I remember doing something like this before, though mine is not mathematically expressive as yours. I am using similar grid on the project I am using now. I would like to share with you what I have done:

        for (int i = 0; i <= fieldSize.y; ++i)
        {
                sf::RectangleShape hline;
                hline.setSize(sf::Vector2f(fieldSize.x * offset, 1.0f));
                hline.setPosition(sf::Vector2f(pos.y, i * offset));
                hline.setFillColor(sf::Color::Blue);
                horizontalGrid.push_back(hline);
        }

        for (int i = 0; i <= fieldSize.x; ++i)
        {
                sf::RectangleShape vline;
                vline.setSize(sf::Vector2f(1.0f, fieldSize.y * offset));
                vline.setPosition(sf::Vector2f(i * offset, pos.x));
                vline.setFillColor(sf::Color::Blue);
                verticalGrid.push_back(vline);
        }

Though I admit this is not brilliant, I use this code for awhile as a placeholder so I would know my objects are on the right offsets when moved. You could try this one. I created 2 vectors one for horizontal lines and vertical lines.

Vectors are like containers for objects. It is a very important concept in programming that I think by now you should learn. Vectors are part of the Standard Template Library which provides you generic containers where you can put objects. It acts like a list, or special form of array. It can shrink and grow on runtime as well. Do not confuse vector(containers) with vector(physics) though both refers to different thing.

Here is my favorite link on the docu:
http://www.cplusplus.com/reference/vector/vector/push_back/

Controlling the loops controls the number of lines generated per orientation.

Might work for you. I hope it helps.

 

anything