1

SUMMARY:

Basically I want to be able to render items based on their Image/Batch/Sprite to speed up my game, but I also have to sort the entities based on y position so that it draws in the correct order.

QUESTION:

How can I do this theoretically? (block of code is nice, but reasoning is better, thanks!)


FULL SITUATION:

I am creating a 2.5 D side-scroller in which I have found that ~500 fps is decent for the early stage of development (on my crap computer at least) but i want this to be higher as I am going to be adding more aspects later (further reducing fps) so I want to be as optimized as I can right now.

What I was wondering is if it is possible to render all entities with the same image file at the same time, to save on time loading up the image files. The struggle I have is that I can't just sort entities by their image file (or at least not that I can think of, that's why I'm asking) because I also need to render them from back to front because its a 2.5 D game.

Right now I am storing the Images in a HashMap, so it isn't completely terrible, I just want to know what I can improve on.

EDIT: Also to note I am using the Jframe and Jpanel classes with Graphics for rendering

You don't need to post a block of code if you don't want. I just want more of the theoretical reasoning. Thanks!

-Kore

Kore
  • 436
  • 2
  • 14

1 Answers1

2

One thought that comes to mind is that if you have an image that you know will be used a lot, you can cache it and that might help.

There are ways to directly reference an image file using BufferedImage and ImageIcon (I would link directly, but i don't have enough points :/ ).

Possibly mixing caching and a direct reference with the image file could help speed up the process. Along with the caching you could possibly find a way to group all of the objects that need the same image file together (this could be done by making an arrayList of all of the objects that use this sprite.), so once one of the objects renders the file, they all render it from the cache at that time. Then the cache is cleared for the next image.

Also, if you were to have variables that pointed to your sprites, then you could have the objects load the variable instead of them all having to point to the image file.
i.e.

String path = "Image1.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);

and then all of your objects referenced image for the source of their image. However, I am not sure if this would be any faster than individually pointing each object to the file, this was just a thought.

Hope some of this answers what you were asking. Good luck!

---edit---

So my next thought could be implemented using a parent class and or just another class that would hold an ArrayList. So the idea is that you don't want to have to use the method images.get(imgPath) every time you want to load an image.

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.ImageIcon;

class SomeClass{

    ImageIcon imgOne = new ImageIcon(new BufferedImage(images.get(imgPath)))
    ImageIcon imgTwo = new ImageIcon(new BufferedImage(images.get(imgPathTwo)))
    private ArrayList<ImageIcon> imagesHolder = new ArrayList<ImageIcon>(){{
        add(imgOne);
        add(imgTwo);
    }};

    public ImageIcon getImageIcon(int index){
        return imagesHolder.get(index);
    }
}

class test{
    SomeClass imageHolder = new SomeClass();

    JLabel label = new JLabel(imageHolder.getImageIcon(0));
    JFrame f = new JFrame();
    f.getContentPane().add(label);
}

In this, we have a class (someclass) whose object (that we only need one of) is storing every single image in an arraylist to the most processed state (imageIcon or whatever you are using can be used here) before actually putting it on the JFrame can create an object from to get all of the already rendered images.

This means that all you have to do at this point is add the image to the JFrame. you don't have to do a lot of the preprocessing of each image over and over again, because it's ready to just go on the JFrame.

While this does not particularly help a lot with rendering (cause most of this is processing of the image) it does help a lot with memory, because you are just creating 1 of each processed image and just repainting that same processed image.

hopefully this idea of processing all of the images once can help lead you to a way to have to only render the image once.

  • 1
    Also, you might find more expert knowledge over at http://gamedev.stackexchange.com/ – JeffreyWorley Jan 12 '16 at 06:26
  • +1 for the information on 'cache'ing, i didn't know that, but I guess I should have been more clear when I said that I am storing them in a HashMap. I am actually storing an Image object inside of a HashMap being sorted by imagePath, such that if I call images.get(imgPath) it returns a reference to an Image object, I am not simply loading it each time. What I was more asking for was to find a way to sort the entities based on their Image so that I would not have to call the images.get(imgPath) method for every single entity, and instead I could just reuse it – Kore Jan 12 '16 at 06:32
  • No problem. I'm a high school student and one day I see myself working in the video game industry, so this question interests me. sorry did not mean to press enter. The rest of my response will come slowly as i am working out the details now. – JeffreyWorley Jan 12 '16 at 07:03
  • I wish I were in the industry XD I'm a junior in HS right now – Kore Jan 12 '16 at 07:04
  • Alright. I added an edit to my original comment. It probably doesn't make a whole lot of sense, and doesn't completely answer your question, I hope it helps. It's currently 1 a.m. where I live, so I will check on this tomorrow when I can. Good Luck! – JeffreyWorley Jan 12 '16 at 07:49
  • Thats similar to what I am doing with the hashmap, I will have to try speed tests between the two methods and see if that helps – Kore Jan 12 '16 at 22:19
  • Oh and thanks so much for your help, I hope these 50 rep points will help you (able to give in 2hrs) – Kore Jan 13 '16 at 02:48