SFML community forums

Help => Graphics => Topic started by: joeben123 on February 02, 2013, 11:00:14 pm

Title: Array of sf::Shape::Rectangles
Post by: joeben123 on February 02, 2013, 11:00:14 pm
For my first game I have been trying to make a basic "Snake" game. Since it runs on a grid I have an array of objects, one for each square. I also want to make an array of sf::Shape::Rectanges so that I can display the game board. How would I create that array?
Title: Re: Array of sf::Shape::Rectangles
Post by: eXpl0it3r on February 03, 2013, 12:51:11 am
By learning C++ (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ;)

SFML requires a certain level of knowledge and working with datatypes is one of the most basics things in C++, without it you won't come far.
I also advise you to use a std::vector rather than a array.
std::vector<sf::RectangleShape> rects(200);
rects[(y*row_width)+x].setPosition(pos);
Title: Re: Array of sf::Shape::Rectangles
Post by: joeben123 on February 03, 2013, 01:48:40 am
Oh never mind, instead of making one array of objects bound to another one of sf::Shapes, I just make each object contain a sf::Shape. Also a vector would have made it harder to check adjacent cells in the grid.
Title: Re: Array of sf::Shape::Rectangles
Post by: joeben123 on February 03, 2013, 02:09:49 am
Window.Clear();
    for(int c = 0; c < 14; c++){
    for(int d = 0; d < 14; d++){
    Window.Draw(board[c,d].disp);
    }}
    Window.Display();

I use nested loops for interfacing with the array, "board" that stores all of the objects. Window is the sf::RenderWindow that I am using and disp is the public sf::Shape::Rectangle for each object. However when I run this, nothing is drawn to the screen. This most likely has to do with me overlooking some blatantly obvious error but as stated above, I have not taken the time to fully comprehend C++ or SFML. Thank you for helping me regardless :D.
Title: Re: Array of sf::Shape::Rectangles
Post by: Nexus on February 03, 2013, 12:05:16 pm
I have not taken the time to fully comprehend C++ or SFML. Thank you for helping me regardless :D.
You should take the time. Otherwise we will remind you again and again :P

The index operator does not work like this. It takes only one argument.