I have a structure like that
abstract Class Entity {
//some variables...
//some methods ...
public abstract void render(Graphics g);
}
Thats the parent ..Now I have 3 children..
Class A extends Entity{}
Class B extends Entity{}
Class C extends Entity{}
Every class has some different stuff do render .One is for example drawing yellow circle , second green text and the third is displaying image.
But ... there is a thing.
Class A have List<B>...
Class B have List<C>...
One Entity has for example 10 Bs ... and each B has 20 Cs ... So now .. I have a render method that renders 60x per second.. And I have to call every render method from every object.
So I have something like this
for(A a : listOfAs){
for(B b : listOfBs){
for(C c : listOfCs){
c.render(g);
}b.render(g);
}a.render(g);
}
Now if you imagine I have much more objects like that and I call this method 60x per second ... I find this really ...really bad practice.. I don't know how to solve this better or so... I don't think that this for each loop is actually the best solution or not. Anyone any ideas ?
I was thinking about implementing the child like that :
Entity x = new A(); ...
Entity y = new B(); ...
and so but some of the classes have other methods that have to be looped like that and I cannot call them from parent.
For the render method ... Just stick to the fact that you have to loop something many times in a short period of time for a long time.
I cannot progress through this ... I got stuck here for a long time and I am not sure how to solve this.
Since your primary concern seems to be rendering or not rendering certain entities, regardless of whether they are child entities, you should do some research into general graphics programming optimizations and tricks and what rendering engines do to improve performance. A big gain in performance in rendering engines is not doing work that is not necessary.
One was already mentioned, which is to determine which actually changed state and only render them. Another is to check the bounds of your view against the bounds of each entity on the graphics canvas and only render things that are within the bounds. Whether you have 2D or 3D, you might have things overlapping, so you could determine what entities are occluded and avoid rendering them. If your child entities are within the bounds of the parent, then you can avoid rendering an entire tree of your object graph and avoid iteration of many elements.
Also, these things can be done in partials by checking for a part of an entity that must be re-rendered, a part that is not occluded, etc. This may require some changes to your API to pass in bounds where entities are expected to render instead of the entire graphics context to each item.
If you don't know anything else about any of your renderable entities such as dirty state or bounds, then you are stuck with iterating them all and invoking render one each of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With