简体   繁体   English

每秒多次调用许多对象的许多方法

[英]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.. 那是父母..现在我有3个孩子..

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. 一个实体有例如10个B ...每个B有20个C ...所以现在..我有一个渲染方法,每秒渲染60x ..我必须从每个对象调用每个渲染方法。

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. 现在,如果你想象我有更多这样的对象,我称这种方法每秒60次......我发现这真的......真的很糟糕...我不知道如何更好地解决这个问题......我不要认为每个循环的这个实际上是最好的解决方案。 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. 无论您是2D还是3D,都可能会使事物重叠,因此您可以确定哪些实体被遮挡并避免渲染它们。 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. 此外,这些事情可以通过检查必须重新渲染的实体的一部分,未被遮挡的部分等来部分地完成。这可能需要对API进行一些更改以传入预期实体的边界。为每个项目渲染而不是整个图形上下文。

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. 我不认为lambdas在这里可以提供很多帮助。

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. 我有双重渲染,所以每秒真的是2 * 30次。 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 . 谢谢你们 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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