为什么在 iPhone 上使用 UIImage 渲染不同图像时性能不佳
我正在使用 UIImage 从 32x32 块渲染游戏地图。 代码如下
for( int x = 0; x < rwidth; ++x)
{
for(int y = 0; y < rheight; ++y)
{
int bindex = [current_map GetTileIndex:x :y];
CGPoint p;
p.x = x * tile_size_x;
p.y = y * tile_size_x;
[img_list[bindex] drawAtPoint:p];
}
}
最终渲染了大约 150 个图块。 我的问题是,当我运行此代码时,我的渲染时间会下降到每秒 2-3 帧。
我以为 iPhone 是填充绑定的,但是如果我强制 bindex = 1(即渲染同一块 150 次),那么我会获得完整的帧速率。
我不敢相信从不同的 UIImage 渲染的成本如此之高。
有人可以告诉我我做错了什么吗...
哦,我忘了提及使用 CGImageCreateWithImageInRect 从更大的纹理页面创建的图像列表。
谢谢 富有的
I'm using UIImage to render a game map from 32x32 blocks. The code is as follows
for( int x = 0; x < rwidth; ++x)
{
for(int y = 0; y < rheight; ++y)
{
int bindex = [current_map GetTileIndex:x :y];
CGPoint p;
p.x = x * tile_size_x;
p.y = y * tile_size_x;
[img_list[bindex] drawAtPoint:p];
}
}
This ends up rendering about 150 tiles. My problem is when I run this code my render time goes down to 2-3 frames a second.
I thought the iPhone was fill bound, however if I force bindex to = 1 (ie render the same block 150 times) then I get full framerate.
I can't believe it is that expensive to render from different UIImages.
Could someone please tell me what I'm doing wrong ...
Oh I forgot to mention my image list created from a bigger texture page using CGImageCreateWithImageInRect.
Thanks
Rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
打开 Instruments 并分析您的代码。 看起来您正在绘制到 UIView 中,并且它们并没有真正针对游戏进行优化。由于 UIView 是分层支持的,因此 drawAtPoint: 需要在图像出现在屏幕上之前多次绘制图像(到屏幕外缓冲区)。
话虽这么说,UIImage 并不是真正为游戏设计的。 成为 UIKit 的一部分有很多开销。 你并不真的应该拥有数百个。
按大约速度顺序(从最慢到最快):
Crack open Instruments and profile your code. It looks like you are drawing into a UIView, and they are not really optimized for games.. Since UIViews are layer-backed, drawAtPoint: needs to draw the image several times (to offscreen buffers) before it appears on screen.
That being said, UIImage isn't really designed for games. There's a lot of overhead with regards to being part of UIKit. You're not really meant to have hundreds of them.
In approx order of speed (slowest to fastest):