需要帮助优化我在 iPhone 上的 2D 绘图

发布于 2024-07-24 23:18:18 字数 568 浏览 5 评论 0原文

我正在编写一个游戏,显示 56 个六边形棋子,以棋盘的形状填充屏幕。 我目前正在使用单例渲染类绘制每个部分,当调用该类来绘制一个部分时,会根据传入的坐标从 6 个点创建一条路径。该路径填充有纯色,然后填充有 alpha 的 59x59 png白色渐变叠加在绘图上,使作品具有闪亮的外观。 请注意,我目前正在 Core Graphics 中执行此操作。

我的第一个想法是,每次绘制时创建一条路径的成本很高,而且似乎我可以以某种方式执行一次然后重复使用它,但我不确定最好的方法。 当我查看 Shark 的瓶颈时,看起来 png 的绘制是该过程中最费力的部分。 我尝试过仅渲染 png 叠加层或仅渲染没有叠加层的路径,两者都给我带来了一些帧增益,尽管删除 png 叠加层会产生最多的帧。

我目前的想法是,在启动时,我应该渲染 6 个路径(我拥有的每个颜色块 1 个)并用 png 覆盖它们,然后存储这些块的图像,然后每次需要时重新绘制这些块。 是否有一种有效的机制来存储您曾经绘制过的内容并重新绘制它? 听起来有点像我会再次经常遇到整个绘图 png 的事情,但也许有一种不太费力的方法可以做类似的事情......

任何建议都非常感谢。 谢谢!

I'm writing a game that displays 56 hexagon pieces filling the screen in the shape of a board. I'm currently drawing each piece using a singleton rendering class that when called to draw a piece, creates a path from 6 points based of the coordinate passed in. This path is filled with a solid color and then a 59x59 png with an alpha to white gradient is overlayed over the drawing to give the piece a shiny look. Note I'm currently doing this in Core Graphics.

My first thought is that creating a path everytime I draw is costly and seems like I can somehow do this once and then reuse it, but I'm not sure of the best approach for this. When I look at the bottlenecks with Shark, it looks like the drawing of the png is the most taxing part of the process. I've tried just rendering the png overlay or just rendering the path without the overlay and both give me some frame gains, although removing the png overlay yields the most frames.

My current thought is that at startup, I should render 6 paths (1 for each color piece I have) and overlay them with the png and then store an image of these pieces and then just redraw the pieces each time I need them. Is there an effecient machanism for storing something you've drawn once and redrawing it? It kinda just sounds like I'd be running into the whole drawing pngs too often thing again, but maybe there's a less taxing method that does a similar thing...

Any suggestions are much appreciated.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫性小仙女 2024-07-31 23:18:18

您可以尝试 CGLayerCALayer

You might try CGLayer or CALayer.

三人与歌 2024-07-31 23:18:18

总体想法:

  • iPhone 上的游戏编程通常需要 OpenGL。 Core Graphics 更容易使用,但 OpenGL 针对速度进行了优化。
  • 尽可能地将这种“闪亮的外观”预渲染到纹理中(例如:在将它们插入项目之前在 Photoshop 中进行此操作)。 Alpha 混合对性能来说是“地狱”。
  • 也许尝试 PVRTC (也是这个 教程),因为它是 iPhone GPU 制造商使用的格式。 话又说回来,这可能会让事情变得更糟,具体取决于你的瓶颈在哪里。

General thoughts:

  • Game programming on iPhone usually necessitates OpenGL. Core Graphics is a bit easier to work with, but OpenGL is optimized for speed.
  • Prerender this "shiny look" into the textures as much as is possible (as in: do it in Photoshop before you even insert them into your project). Alpha blending is hell on performance.
  • Maybe try PVRTC (also this tutorial) as it's a format used by iPhone's GPU's manufacturer. Then again, this could make things worse depending on where your bottleneck is.
生生漫 2024-07-31 23:18:18

如果你确实需要速度,你必须走 OpenGL 路线。 如果您想混合使用 OpenGL 和 Core Animation,请小心,它们可能会发生冲突。

如果你没有用过 OpenGL,那么它会很痛苦。 听起来您可以使用核心动画并使每个图块成为一个图层。 除非您更改某些内容,否则 CA 不会再次调用重绘,因此您应该能够移动该图层而不会受到重大影响。 另请注意,CA 将图层存储在纹理内存中,因此它应该更快。

If you really need speed you have to go the OpenGL route. Be careful if you want to mix OpenGL and Core Animation, they can conflict.

OpenGL is a pain if you haven't done much with it. It sounds like you could use Core Animation and make each tile a layer. CA doesn't call the redraw again unless you change something, so you should be able to just move that layer around without taking a big hit. Also note that CA stores the layer in the texture memory so it should be much faster.

老旧海报 2024-07-31 23:18:18

其他一些人提到您应该使用 OpenGL。 这是专门针对 iPhone 的精彩介绍: OpenGL ES 从头开始​​:目录

Some others have mentioned that you should use OpenGL. Here's a nice introduction specifically for the iPhone: OpenGL ES from the Ground Up: Table of Contents

堇年纸鸢 2024-07-31 23:18:18

您可能还想查看 cocos2d。 在我的测试中,它似乎比使用 CoreAnimation 快得多,并且为游戏提供了许多有用的东西。

You might also want to look at cocos2d. It seems to be significantly faster than using CoreAnimation in my tests, and provides lots of useful stuff for games.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文