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

Author Topic: Array of sf::Shape::Rectangles  (Read 12146 times)

0 Members and 1 Guest are viewing this topic.

joeben123

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Array of sf::Shape::Rectangles
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Array of sf::Shape::Rectangles
« Reply #1 on: February 03, 2013, 12:51:11 am »
By learning C++ ;)

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);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

joeben123

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Array of sf::Shape::Rectangles
« Reply #2 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.

joeben123

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Array of sf::Shape::Rectangles
« Reply #3 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Array of sf::Shape::Rectangles
« Reply #4 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: