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

Author Topic: Array of shapes  (Read 10497 times)

0 Members and 1 Guest are viewing this topic.

KeY_Str0ke

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Array of shapes
« on: March 13, 2017, 07:58:41 pm »
Hey,

I'm learning C++ in school(I already know i/o, decision making, loops, arrays, structs, strings, and functions), but I wanted to go a little far and learn how to create 2d graphics, I found SFML and it's going fine.

I'm trying to replicate a Space Invaders game using this video(it's a long video, you don't need to watch), but it is in JavaScript so I'm translating it to C++. But I'm stuck creating the aliens (shapes), because I don't know how to create an array of them. Do I make a struct and each time I call alien.show() it creates a new one, or should I create them outside the struct and then make it appear (by setting its position)?

Sorry if this kind of question appears here very often, or I did not explain myself very good but I'm new to the forum and English is not my main language.
Any help appreciated!

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Array of shapes
« Reply #1 on: March 13, 2017, 09:06:15 pm »
Using the STL, you can have an array (or vector) of objects, thus:
class Alien; // define your Alien

std::vector<Alien> aliens(20); // create 20 aliens (this could instead be just a vector of sf::Sprites or sf::RectangleShapes or whatever if the only thing the aliens need is their appearance)

// ...

// draw all aliens
for (auto& alien : aliens)
    window.draw(alien);

If you're unable to use C++11 (or later) for whatever reason, to iterate the aliens (to draw them as above), you'd need to access them via iterator:
for (std::vector<Alien>::iterator it = aliens.begin(); it != aliens.end(); ++it)
    window.draw(*it);
or index:
for (unsigned int i = 0u; i < aliens.size(); ++i)
    window.draw(aliens[i]);


For more information about some of the STL containers (e.g. vectors), have a look at the following pages:
VECTOR:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector
ARRAY:
http://www.cplusplus.com/reference/array/array/
http://en.cppreference.com/w/cpp/container/array
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

KeY_Str0ke

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Array of shapes
« Reply #2 on: March 13, 2017, 10:39:41 pm »
Can I do this in a structure? I don't really know a lot about classes and I don't like to have code in my program that I don't fully understand...

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Array of shapes
« Reply #3 on: March 14, 2017, 12:26:01 am »
A struct is a class.

Using the struct keyword creates a class that is public by default. Therefore these are equivalent:
struct Alien
{
};
class Alien
{
public:
};

To answer your question, yes, since a struct is a class and you can have a vector of objects based on classes (and therefore structures).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

bluszcz

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Array of shapes
« Reply #4 on: October 18, 2019, 11:05:07 pm »
Long time, but, since I am optimizing now my game - I was trying to do so by changing way of iterating.

So, due to my Visual Studio profiler - when using auto c++11 vs using index, auto construction is slower by about 5% (and I fps are going down from ~2000 to 1500) - I am drawing about 120  sprites...

Using the STL, you can have an array (or vector) of objects, thus:
class Alien; // define your Alien

std::vector<Alien> aliens(20); // create 20 aliens (this could instead be just a vector of sf::Sprites or sf::RectangleShapes or whatever if the only thing the aliens need is their appearance)

// ...

// draw all aliens
for (auto& alien : aliens)
    window.draw(alien);

If you're unable to use C++11 (or later) for whatever reason, to iterate the aliens (to draw them as above), you'd need to access them via iterator:
for (std::vector<Alien>::iterator it = aliens.begin(); it != aliens.end(); ++it)
    window.draw(*it);
or index:
for (unsigned int i = 0u; i < aliens.size(); ++i)
    window.draw(aliens[i]);


For more information about some of the STL containers (e.g. vectors), have a look at the following pages:
VECTOR:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector
ARRAY:
http://www.cplusplus.com/reference/array/array/
http://en.cppreference.com/w/cpp/container/array

nogoodname

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Array of shapes
« Reply #5 on: October 18, 2019, 11:55:11 pm »
Long time, but, since I am optimizing now my game - I was trying to do so by changing way of iterating.

So, due to my Visual Studio profiler - when using auto c++11 vs using index, auto construction is slower by about 5% (and I fps are going down from ~2000 to 1500) - I am drawing about 120  sprites...

Are you compiling with optimization enabled?
And could you show your code?
« Last Edit: October 19, 2019, 12:00:17 am by nogoodname »

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Array of shapes
« Reply #6 on: October 21, 2019, 03:16:25 pm »
Honestly, with that kind of FPS, I wouldn't bother. A moment where your computer find something more important could make them drop by a few hundred at that point. They are most probably not accurate and important so high. Don't bother with optimizing something like this.

The auto method is basically the same as the iterator, pointing to a reference instead of the iterator (in fact, it's an iterator under the hood).

Finally, with optimization, the assemble generated is basically the same, so yeah, auto is not slower by any means. Use a real good profiler to compare and not FPS. FPS is not accurate.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

 

anything