SFML Images

I want to load the same image in the same window in more that one place at a time, any ways to go about it?
Use sf::Sprite. You should be able to load the image once, and apply it to multiple sprite objects.
So I would have to keep track of multiple Sprite objects, the best way would probably be to have an array of Sprites, right?
Yea some sort of container. Let's say you have a game with some enemies that look identical, you can just create a sprite object for each one and store them in a container. Preferably a vector as you'll probably need random access for that situation. But you get the point.
Thanks, just one more question, I keep hearing about vectors, i hear they are good, but are they as fast as arrays. What are the pros and cons of each, when should you use which one?
I believe the C style array is slightly faster than vectors, but not enough to outweigh the benefits of vectors. Can't say about std::array (C++11) because I haven't even touched those yet.

Vectors work the same as arrays, but they can resize when they need to, and you don't need to specify a size when you make one. They are also a class type, which means they get access a bunch of a different methods to help when working with them. Pretty much, they are really robust arrays that handle size reallocation on their own. Oh and you get access to std::iterator which makes traversing pretty simple and faster than the bracket [] access, which you can use in vectors if you want.

Arrays are nice because they have no overhead. You don't need to include anything to use them (except of course for std::array).

Can't really think of any cons for vectors, other than you'll see a slight slowdown whenever it needs to resize, especially if it needs to move to a different spot in memory. But that can be mitigated.

Cons for arrays, they are really basic. They remained unchanged from C to C++, so they are about as close to the hardware as you can get. That being said, you're on own when working with them. No bounds checking, if you run out of room, you have to make more yourself which can be error prone.


If you're using C++, use vectors in place of arrays. The benefits far outweigh any con it might have.

http://www.cplusplus.com/reference/stl/vector/

EDIT:
Something to keep in mind when using std::iterator with std::vector, if your vector resizes, your iterator could become invalidated if the vector actually moves to a different location in memory.
Last edited on
An std::vector in C++ is pretty much a smart, dynamically allocated, resizable array.

I guess it's not always "faster" but it's almost always more convenient to use, also it can mimic a C array if you reference whatever front() returns.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>

void print_int_array(const int *pi, unsigned int size)
{
    while (size-- != 0)
        std::cout << *pi++ << ' ';

    std::cout << std::endl;
}

int main()
{
    std::vector<int> vi;

    vi.push_back(32);
    vi.push_back(105);
    vi.push_back(0);
    vi.push_back(-1);
    print_int_array(&vi.front(), vi.size());
}

Topic archived. No new replies allowed.