0

I have an animation class ready for my game. I think there are some optimization issues in this classroom.
Let's say there are more than 10 living entities with the same animations in my game.
Although they all have different animation times, the pictures of their animations are the same. When the code is like this, instead of keeping 2 pictures in RAM, wouldn't 20 pictures be kept? Code;

Animation idle;
BufferedImage idle1,idle2;
ArrayList<Soldier> enemyArmyArray = new ArrayList<Soldier>();
ArrayList<Animation> armyIdleAnimations = new ArrayList<Animation>();
w_idleImage1 = ImageIO.read(getClass().getResource("/Data/idle1.png"));
w_idleImage2 = ImageIO.read(getClass().getResource("/Data/idle2.png"));
for(int z = 0 ; z <= 10 ; z++) {            
    Soldier s = new Soldier();
    armyArray.add(s);
    idle = new Animation();
    idle.addFrame(idle1, 400);
    idle.addFrame(idle2, 400);
    armyIdleAnimations.add(idle);
}

and this is my animation class:

public class Animation {
    private ArrayList frames;
    private int currentFrame;
    private long animTime; // long takes up more memory than int but can hold
                            // more accurate numbers.
    private long totalDuration;

    public Animation() {
        frames = new ArrayList();
        totalDuration = 0;

        synchronized (this) {
            animTime = 0;
            currentFrame = 0;
        }
    }
    
    
    public synchronized void addFrame(BufferedImage image, long duration) {
        totalDuration += duration;
        frames.add(new AnimFrame(image, totalDuration));
    }

    public synchronized void update(long elapsedTime) {
        if (frames.size() > 1) {
            animTime += elapsedTime;
            if (animTime >= totalDuration) {
                animTime = animTime % totalDuration;
                currentFrame = 0;
            }

            while (animTime >= getFrame(currentFrame).endTime) {
                currentFrame++;

            }
        }
    }

    public synchronized BufferedImage getImage() {
        if (frames.size() == 0) {
            return null;
        } else {
            return getFrame(currentFrame).image;
        }
    }

    private AnimFrame getFrame(int i) {
        return (AnimFrame) frames.get(i);
    }
    
    
    private class AnimFrame {

           BufferedImage image;
           long endTime;

           public AnimFrame(BufferedImage image, long endTime) {
              this.image = image;
              this.endTime = endTime;
           }
    }
    
    public int getCurrentFrame() {
        return currentFrame;
    }


    public void setCurrentFrame(int currentFrame) {
        this.currentFrame = currentFrame;
    }


    public long getAnimTime() {
        return animTime;
    }


    public void setAnimTime(long animTime) {
        this.animTime = animTime;
    }
}   
  • 1
    You only read 2 images and you don't make copies of them. There's no magic that makes 18 other images appear into the memory. If you go to see the Mona Lisa, and there are 19 people looking at it with you, does that mean there are 20 Mona Lisas? No. They all refer to the same 2 images, the data is not duplicated in memory. – Kayaman Mar 29 '21 at 11:07
  • @Kayaman Hm, but when I think of the object as a different object rather than a image, I create an object x in my main class and send it to the y object I derived from the y class that requests the x object. When I change object x in my main class, object x in object y does not change. The places where it is kept in memory are different. So when the x object in question is an image, does it happen as you say? – kralarmerlin Mar 29 '21 at 12:02
  • There's no special handling for images, it's the same for all objects. If you pass a reference to any mutable object (such as `StringBuilder`) and mutate the object, you'll notice everyone sees the changes, meaning there's only a single copy of the object. I recommend going through [this](https://stackoverflow.com/q/40480/2541560) to see if you can understand how references (i.e. pointers) work in Java. You're also worrying way too much about memory, it doesn't matter whether you use `int` or `long` unless you're working very very very very low-level. – Kayaman Mar 29 '21 at 12:19
  • @Kayaman oh got it. Thanks for the help – kralarmerlin Mar 29 '21 at 12:53

0 Answers0