Reputation: 436
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
Upvotes: 1
Views: 669
Reputation: 143
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.
Upvotes: 2