Calling many methods of many objects many times per second

I have such a structure

abstract Class Entity { //some variables... //some methods ... public abstract void render(Graphics g); } 

Here is the parent .. Now I have 3 children ..

 Class A extends Entity{} Class B extends Entity{} Class C extends Entity{} 

Each class has different things that render. One example, for example, drawing a yellow circle, the second is green text, and the third is an 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 rendering method that displays 60x per second .. And I have to call each render method from each 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 assume that I have many more such objects, and I call this method 60x per second ... I find it really ... really bad practice. I don’t know how to solve this better ... I don’t think that for each cycle this is actually the best solution or not. Any ideas?

I was thinking about how to realize such a child:

 Entity x = new A(); ... Entity y = new B(); ... 

and therefore, but some of the classes have other methods that should be looped in this way, and I cannot name them from the parent.

For the rendering method ... Just stick to the fact that you need to loop a lot of things in a short amount of time for a long time.

I can’t get through this ... I’ve been stuck here for a long time and I’m not sure how to solve it.

+5
source share
3 answers

Since your main problem is whether or not certain objects are rendered, regardless of whether they are children, you should do some research on optimizing the general graphical programming tricks and what engines do to improve performance. A large performance boost in rendering engines does not do the work that is not needed.

One of them has already been mentioned to determine which one has actually changed state and only display them. Another is to check the boundaries of your view on the borders of each object on a graphic canvas and display only objects that are within the borders. If you have 2D or 3D, you can have overlays on top of each other, so you can determine which objects are closed and avoid rendering them. If your child entities are within the parent, you can avoid displaying the entire tree of your graph of objects and avoid repeating many elements.

In addition, these things can be performed in partial order by checking the part of the object that needs to be re-displayed, the part that is not closed, etc. This may require some changes to be made to your API to go within boundaries where entities are expected to display instead of the entire graphic context for each element.

If you do not know anything about any of your visualized objects, such as a dirty state or borders, then you get stuck with iterating them all and causing the visualization of each of them.

+1
source

I'm not sure that I am fully following ... But are you creating an entity consisting of several other objects? No matter how much you think about it, you will always have to visit each object once. It doesn't matter which cycle you choose.

However, if you have an Entity consisting of many sprites, I would recommend creating a new texture and visualizing these sprites on this texture once. Then you just need to display this texture in every frame. Has the meaning?

0
source

I found a friend at work who looked at him and helped me stabilize him. Thanks to everyone for the advice and help. I don't think lambdas can help much here.

It is hard to explain what I am doing in this project. This is not a game at all .. It is something else. I got double rendering, so it really is 2 * 30 times per second. I just played with loading objects that I can use later in my engine. But this problem made me stuck for quite some time.

Yet again. Thanks to everyone.

0
source

All Articles