简体   繁体   中英

Calling many methods of many objects many times per second

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.

I'm not sure I quite follow... But Are you making an entity that consists of several other entities? No matter how much you think this through, you'll always have to visit each entity once. It doesn't really matter what kind of loop you choose.

However, if you have an Entity that consists of a bunch of sprites, I would recommend creating a new texture and render these sprites onto that texture once. Then you just need to render that texture each frame. Makes sense?

I located a friend at work who took a look at it and helped me stabilize it. Thank you all for your tips and help. I don't think that lambdas can help a lot here.

It's hard to explain what I am doing in this project . It's not a game at all .. It's something different. I got double rendering so it's really 2* 30 times per sec. I was just playing with the load of objects that I can actually later use in my engine . But this problem got me stuck for a pretty big while.

Again. Thank you all .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM