Creating 100+ ofImages

Hi all,
I am playing around with boids. I am creating 100 or so and each of them I want represented with a small image rather than a geometric shape. The problem is that when my code is running and the boid is a triangle it runs at 60fps.

Then, as I prepare my code to load an ofImage and without even loading or displaying anything, merely declaring an ofImage in the .h file of the boid class makes the whole thing grind to a halt.

Is it too much to ask for it to allocate memory for 100 small images?

thanks

It can depend on your GPU. Every ofImage you create allocates a texture on your graphics card which takes up memory. Typically the best way to do is declare a bunch of them at once, store them in a vector and re-use them (as opposed to re-allocating them).

Thank you @jvcleave for your response.
The strange this is that I am only creating 1 image in the constructor of each boid. Then I am just creating 150 boids. If I do the image declaration it grinds to a halt, otherwise it’s pretty smooth.
The difference between the two scenarios is basically this line:

ofImage boidImage;

in the constructor of each boid. Once they boids are constructed it’s not called again (as is the case with all constructors), and I don’t create any more boids.

I am baffled :confused:
marinero

I am guessing you are using the same image representation for each boid? Why would you bother to declare an ofImage instance for each of them? (It is however weird that you machine can’t handle the 150…)

(Btw :: it is not the declaration of the image that reserves the necessary memory, but the allocation of the image. So if your application crashes and you are only calling “ofImage boidImage” in the constructor, then there’s something else going on.)

(Btw2 :: why would you call “ofImage boidImage” in the constructor of the boid? The constructor is meant for allocating memory and creating instances of member variables. When you define the ofImage instance inside the constructor it won’t be accessible by other methods of the boid-object. You would probably like to declare ofImage boidImage in the header file and allocate the image in the constructor)

You might wanna call the “draw” function top-level and give a reference to a defined ofImage instance.

void drawBoid(ofImage & img){
   const float radius = 30; // get the radius
   const ofVec2f location = boidLoc; // get the location
   // you would prob. like to do some rotation as well…
   img.draw(location.x-radius/2,location.y-radius/2,radius, radius);
}

Or you could work with a pointer to an ofImage object in each of the boids instances in order to avoid the need to pass the ofImage instance as an argument…

[EDIT ::] I didn’t think of this at the time, but it makes a lot more sense if one would use a static member var for this problem…

Thank you @subtiv. I meant to say that I declared it in the .h file.

I rewrote parts of the code and made it like ofBox2D. It just chunks out numbers, it does not know about that objects it moves around. In a couple of weeks I plan to release the code on ofxAddons for others to extend and play with.

Thanks for the tips.
marinero