I’ve finished the Space Invaders game that I was working on, works flawlessly. However, I was wondering if there was a more efficient way to draw my aliens. Currently, my draw method for each alien is as follows:
@Override
public void draw() {
sheet.texture.bind();
glBegin(GL_QUADS);
glTexCoord2f((float) (frameX / sheet.imageWidth),
(float) (frameY / sheet.imageHeight));
glVertex2i(x, y);
glTexCoord2f((float) (frameX + spriteWidth) / sheet.imageWidth,
(float) (frameY / sheet.imageHeight));
glVertex2i(x + width, y);
glTexCoord2f((float) (frameX + spriteWidth) / sheet.imageWidth,
(float) (frameY + spriteHeight) / sheet.imageHeight);
glVertex2i(x + width, y + height);
glTexCoord2f((float) (frameX / sheet.imageWidth),
(float) (frameY + spriteHeight) / sheet.imageHeight);
glVertex2i(x, y + height);
glEnd();
}
Pretty straightforward. The issue I’m having is that for every alien on which this is called, there are multiple values that remain the same for each one, such as the frame#, the sprite size, etc. This got me thinking: If I were to create 1000 aliens, would there be an easier way to dupe/clone the sprites, than creating each one with it’s own values even though they all reference the same thing?
I was thinking I would create 1 Buffered Sprite if I needed to create multiples, and then instead of creating multiple individual alien objects, the BufferedSprite could have an Array/ArrayList(s) containing all of it’s copies’ position, speed, animation and etc. variables. So I’m wondering if it would be bad to use this design; for example, if I had hundreds of clones, the drawing and updating methods would have to iterate over enormous collections… That is just one of many problems I think I’d encounter with this, but could it save memory since I’m cutting down the amount of unique objects created? I would only use this in cases where I had a sizable number of clones, by the way.