2
\$\begingroup\$

I'm working with openGL ES 2.0 on Android and I've created a custom Quad class.

This allows me to make a sprite object like so:

Quad mySprite = new Quad();
mySprite.setSize(w,h);
mySprite.setTexture(myTexture);

Then, I can draw it like so:

mySprite.draw();

The draw() method uses mySprite's internal x and y coordinates which are declared within the Quad Class.

I also have another method which allows me to draw the quad specifying other 'external' co-ordinates like so:

mySprite.draw(x,y);

Now let's say that on one of the levels of my game, I have the need to draw 3 of these sprites at different positions and move them all independently of each other.

At the moment, I think I have 2 options, which are:

1) Create different quad's with the same texture and use them with their internal coordinates or....

2) Use the same object but hold an array of external coordinates and just draw it three times at different places.

My preferred option would be 1 because it's just cleaner to work with but clearly inefficient as I'm having to create multiple objects that are essentially the same (inefficient memory-wise?)

So is there a way of using just one version of the sprite? Maybe using clones or something along those lines so that I can just keep track of each clone's internal position and states rather than using external arrays?

\$\endgroup\$
2
  • \$\begingroup\$ How much memory does Quad take up? If it's lightweight, I don't think it would be an issue to have multiple instances of it. If it's not, create a lightweight wrapper around Quad. \$\endgroup\$ Commented Jul 6, 2013 at 1:44
  • \$\begingroup\$ Hi @VittorioRomeo thank you for your comment, I was under the impression Wrapper classes were relating to primitives, could you please explain how I would use this concept with my custom class - if there were only one of two objects of this class I would just create different objects but I can see on certain levels I may need like 20+ so would prefer a better solution to just creating different objects! Please could you explain how a wrapper would help in this situation? Thanks! \$\endgroup\$ Commented Jul 6, 2013 at 12:07

1 Answer 1

2
\$\begingroup\$

Do not draw the same Sprite to represent the image at multiple locations. Holding the coordinates somewhere else - this defeats the purpose of object-oriented programming. The most memory is taken by the texture (or texture region) itself, the overhead of your custom class is negligible. Unless you will be making millions of Quads, do not worry about memory. It will grow as needed.

Premature optimization is the root of all evil, however there is one performance consideration you can make. If you are going to create a lot of objects and immediately throw them away, you can use Object Pool Pattern to reuse these objects, especially if there is any CPU intesive initialization and clean up taking place. The performance is also affected later during garbage collection, which can create peaks in CPU usage. Using Object Pool prevents that, but one must be very careful to recycle the objects properly.

I'd recommend your option 1) and don't bother with the Object Pool unless you hit the CPU usage spikes. Also the modern Java Garbage Collector is very well optimized for short-lived small objects, make use of it.

\$\endgroup\$
4
  • \$\begingroup\$ I would like to sum that he probably thinks that each sprite have a full texture on it, and maybe he fears that having one image on each sprite is a waste. It WOULD be, but actually each sprite should have only a "pointer", it's not the full texture, so create many sprites with no fear! \$\endgroup\$ Commented Jul 8, 2013 at 3:31
  • \$\begingroup\$ Wise words @sm4, I will be creating probably a max of 20 or 30 So I shall stick with creating multiple versions! \$\endgroup\$ Commented Jul 9, 2013 at 0:21
  • \$\begingroup\$ @GustavoMaciel, that's exactly what I was worried about, I was thinking that the texture would be created many times. \$\endgroup\$ Commented Jul 9, 2013 at 0:22
  • \$\begingroup\$ Java copy everything but primitives by reference. That mean you'll just have another reference, not another object. \$\endgroup\$ Commented Jul 9, 2013 at 0:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .