简体   繁体   English

是什么导致帧率丢失?

[英]What causes frame rate loss?

I'm writing a game with cocos2d and I have noticed that the frame rate drops as the game progresses. 我正在用cocos2d编写游戏,并且我注意到随着游戏的进行,帧速率会下降。 I've checked for leaks but everything looks fine so I'm at a loss as to what to do next. 我已经检查了泄漏,但一切看起来都很好,所以我对下一步的工作感到迷茫。 Sorry if this is a really basic question but what sort of factors cause frame rate loss? 抱歉,这是一个非常基本的问题,但是什么样的因素会导致帧频丢失?

The problem seems to get worse when I clear the sprites from a layer. 当我从图层中清除精灵时,问题似乎变得越来越严重。 Sometimes the framerate jumps back to 60 but occasionally it drops down to 30 or lower and never recovers. 有时帧速率跳回60,但偶尔下降到30或更低,再也无法恢复。 After a few minutes the animations are noticably slower and the game pretty much grinds to a halt. 几分钟后,动画明显变慢了,并且游戏几乎停止了。 I'm not sure if this is specific to cocos2d or a common issue in game development, but it renders the game unplayable :( 我不确定这是特定于cocos2d还是游戏开发中的常见问题,但这会导致游戏无法玩:(

How are you testing for memory leaks? 您如何测试内存泄漏? I don't think you should rule that out as the cause until you are absolutely certain because that sounds like classic "memory leak behaviour". 在您完全确定之前,我不认为应将其排除在外,因为这听起来像是经典的“内存泄漏行为”。

Also make sure you don't have something like a log or something in memory which is continually getting larger and larger every frame (I say so because that's happened to me in the past). 另外,请确保您没有像日志之类的东西或内存中的东西每一帧都越来越大(我之所以这么说,因为过去我曾经发生过这种事情)。

Also make sure you aren't continually loading new objects and/or sprites as your game progresses. 另外,请确保您不会随着游戏的进行不断加载新的对象和/或精灵。 Try and keep all your load calls in the initialization segment of your logic. 尝试将所有装入调用保持在逻辑的初始化段中。

Your framerate will drop if you add more sprites, obviously, because it has more to process. 如果添加更多的图片,您的帧速率将下降,这显然是因为它需要处理的内容更多。

Now, I do not know anything about cocos2d, but when you are "clearing the sprites from a layer", it sounds as though it simply hides them from view in addition to storing the fact that they are now hidden. 现在,我对cocos2d一无所知,但是当您“从图层中清除精灵”时,听起来除了存储它们现在已隐藏的事实之外,还只是从视图中隐藏了它们。

Maybe try and use fewer sprites? 也许尝试使用更少的精灵?

Are you reusing preloaded textures? 您是否正在重新使用预加载的纹理? If you aren't you should. 如果不是,那么应该。 Loading the same texture again and again is both slow, and is a leak. 一次又一次地加载相同的纹理既缓慢,又是泄漏。

Additionally, are you releasing textures you're done with? 此外,您是否要释放完成的纹理? Even though the sprite is gone the texture is still there. 即使精灵消失了,纹理仍然存在。

I recently ran into a problem with Flash involving a progressive slowdown in graphics rasterization, so perhaps something similar could be happening here. 最近,我遇到了Flash问题,涉及图形栅格化的逐渐减慢,因此这里可能发生了类似的事情。 A slowdown will generally occur when the system has to perform too much work. 当系统必须执行太多工作时,通常会出现速度降低的情况。 Memory leaks are obvious, because over-allocation and fragmentation of memory are never good, but consider this: 内存泄漏是显而易见的,因为内存的过度分配和碎片永远都不好,但是请考虑以下几点:

What I discovered was I had forgotton to call "clear" on the Graphics object, so every time I redrew the border of a sprite, it was drawing another rounded rectangle and the draw commands are cumulative. 我发现我忘记了在Graphics对象上调用“清除”,因此每次重新绘制精灵的边框时,它都会绘制另一个圆角矩形,并且绘制命令是累积的。 After a few dozen interactions, it was drawing this filled, rounded rectangle a hundred times over itself. 经过数十次交互,它在其上绘制了这个填充的圆角矩形一百倍。 The shaders, code, etc. were all taking up less than 1% of the rendering time, but that over-rasterization was killing my framerate. 着色器,代码等全部占用不到1%的渲染时间,但是过度栅格化杀死了我的帧速率。

So I would suggest checking for things like object creation, forgetting to clear things, drawing methods, etc. Make sure the system is not doing unnecessary work or accumulating unnecessary operations, not just memory objects. 因此,我建议您检查诸如对象创建,忘记清除内容,绘制方法等内容。确保系统没有在进行不必要的工作或积累不必要的操作,而不仅仅是内存对象。

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

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