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

Author Topic: displaying multiple sprites  (Read 7260 times)

0 Members and 1 Guest are viewing this topic.

Wissam

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
displaying multiple sprites
« on: September 15, 2014, 01:17:29 am »
so eventually im going to get drawing a lot of sprites at once for example: the environment that the player will be in: the trees, the grass, flowers, etc etc. tilesets in general.

so I thought of going about that through making an std::vector dedicated for holding only sprites and during the main loop there will be a for loop weeding out the sprites and drawing them. But I can't seem to get it to work!

Here's what I've got so far.

defined the vector early on around where i declare the window
std::vector<sf::Sprite> object_list;

then in my main loop its going to call this void im currently working on under a class called Physics which was also declared earlier "myWindow" being the window name.
gamePhysics.draw_objects(&myWindow,&object_list);

and finally the void function itself
void draw_objects(sf::RenderWindow *gameWindow,std::vector<sf::Sprite> *objects){

    for(int i = 0;objects->size();i++){

        &gameWindow->draw(&objects[i]);
    }

};

which keeps throwing this error at me
include\physics.h|19|error: no matching function for call to 'sf::RenderWindow::draw(std::vector<sf::Sprite>*)'|

highlighting this line
&gameWindow->draw(&objects[i]);

and yes im a little knew to C++ programming but on it here and there trying my best lately.
any suggestions would be greatly appreciated!

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: displaying multiple sprites
« Reply #1 on: September 15, 2014, 01:41:26 am »
It is very imperative that you learn C++ to a decent level before trying to use SFML. Your code right now contains a very basic mistake and if you even properly knew how to use pointers it would stand out at you.

Quote
&gameWindow->draw(&objects);

You need to learn what the '&' symbol means, both of the times you use it here it is wrong. You should be dereferencing the pointer here instead of creating a pointer to a pointer.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: displaying multiple sprites
« Reply #2 on: September 15, 2014, 10:30:01 pm »
I'm no expert with pointers. In fact, I avoid them whenever possible, which is almost all of the time.
However, I did find that I needed to sort of know what they are  :P

This, I found quite descriptive and it might help you to resolve your issue.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Wissam

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: displaying multiple sprites
« Reply #3 on: September 16, 2014, 12:13:40 am »
thanks for the answers and i took a look at the link you sent. I think i'm more familiar with them than I was before.

so here's an update on what I did.

 std::vector<sf::Sprite> object_list;


    sf::Sprite Box;

    Box.setTexture(myTexture);
    object_list.push_back(Box);

        for(int i = 0; i <= object_list.size(); ++i){
        myWindow.draw(object_list[i]);

        }


this does run without any errors but as soon as the application opens up it stays for about 2 seconds then crashes.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: displaying multiple sprites
« Reply #4 on: September 16, 2014, 01:21:06 am »
object_list.size() is the number of items in the vector. So, by using <=, it tries to access an item outside of the vector's range. You need to use <.

Xornand

  • Jr. Member
  • **
  • Posts: 78
  • C++ / Python
    • View Profile
Re: displaying multiple sprites
« Reply #5 on: September 16, 2014, 01:39:36 am »
You could also write it this way instead:

std::for_each(object_list.cbegin(), object_list.cend(),
    [&](const sf::Sprite& sprite){ myWindow.draw(sprite); }
);
« Last Edit: September 16, 2014, 01:41:12 am by Xornand »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: displaying multiple sprites
« Reply #6 on: September 16, 2014, 03:46:30 am »
Personally I'd probably use
for (const auto& sprite : object_list)
    myWindow.draw(sprite);

Wissam

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: displaying multiple sprites
« Reply #7 on: September 16, 2014, 10:05:22 pm »
unfortunately, none of the suggestions have worked.


all of them are throwing some sort of error

Xornand

  • Jr. Member
  • **
  • Posts: 78
  • C++ / Python
    • View Profile
Re: displaying multiple sprites
« Reply #8 on: September 16, 2014, 11:33:46 pm »
The error must be caused by some other parts of your code. The iteration methods proposed here are correct.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: displaying multiple sprites
« Reply #9 on: September 17, 2014, 12:07:21 am »
some sort of error
What sort of error?
Did just changing the <= to < also throw an error?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*