使用 UIImage imageNamed 方法渲染图块的性能问题
我有 5 块瓷砖。每个都是 16x16 像素,大小为 8kb。它们都是使用 imageNamed 加载的。例如,[UIImage imageNamed:@"dirt.png"]。它们在drawRect之外的init中加载。我有一个名为“地图”的数组,用于绘制这些图块的去向。在 DrawRect 中,我有一些使用两个 for 循环渲染舞台的代码,如下所示。
tileNum = startPoint;
for(int i = 0; i< stageHeight; i++){
for(int ii = 0; ii< stageWidth; ii++){
if(map[tileNum] == 0){
[dirt drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}else if(map[tileNum] ==1){
[tree1 drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}else if(map[tileNum] ==2){
[tree2 drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}else if(map[tileNum] ==3){
[tree3 drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}else if(map[tileNum] ==4){
[tree4 drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}
tileNum++;
}
tileNum += (mapWidth - stageWidth);
}
模拟器中的一切都很棒,正是我想要的。但当我将其安装到设备 (3GS) 上时,帧速率减慢到极点(大约每秒 1 帧)。我怀疑这可能与 imageNamed 的使用有关。或者只是因为我渲染了太多图块? (在 16x16 下,我每帧渲染 600 个图块,但它们是小文件,并且绘制的图块中 90% 是“污垢”图块)。如果是这样,创建简单的图块引擎的更好方法是什么?
这是我第一次尝试发布问题。感谢任何提供帮助的人。
I have 5 tiles. Each are 16x16 pixels, 8kb in size. They're all loaded using imageNamed. For example, [UIImage imageNamed:@"dirt.png"]. They're loaded outside the drawRect, in the init. I have an array called "map" that maps out where these tiles go. In DrawRect I have a bit of code that renders the stage using two for loops, as shown below.
tileNum = startPoint;
for(int i = 0; i< stageHeight; i++){
for(int ii = 0; ii< stageWidth; ii++){
if(map[tileNum] == 0){
[dirt drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}else if(map[tileNum] ==1){
[tree1 drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}else if(map[tileNum] ==2){
[tree2 drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}else if(map[tileNum] ==3){
[tree3 drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}else if(map[tileNum] ==4){
[tree4 drawInRect:CGRectMake(ii*tileSize, i*tileSize, tileSize, tileSize)];
}
tileNum++;
}
tileNum += (mapWidth - stageWidth);
}
Everything works awesome in the simulator, just how I want it. But when I put in on the device (3GS) the frame rate slows to a crawl (about 1 frame per second). I suspect it might have to do with the use of imageNamed. Or is it just because I'm rendering too many tiles? (At 16x16 I'm rendering 600 tiles per frame, but they're tiny files and 90% of the tiles drawn are the 'dirt' tile). And if so, what's the better way to create a simple tile engine?
This is my first attempt at posting a question. Thanks to anyone who helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于此类工作,使用纹理图集可能会比使用单独的图像文件幸运得多。很难准确地说出那里发布的代码量,但我猜您正在开发某种游戏,这意味着您可能希望使用 opengl 进行绘图。
我建议查看 Apple 的 OpenGL ES 编程指南 作为起点,特别是关于 纹理数据,它帮助我解决了当我开始深入游戏开发时,发生了很多事情。
For this sort of work you would probably end up having much better luck with a texture atlas than separate image files. Its hard to say exactly with the amount of code posted there, but I'm guessing you're working on a game of some sort, which means you'll probably want to be using opengl for drawing.
I'd recommend checking out the OpenGL ES Programming Guide from Apple as a starting point, particularly the section on Texture Data, it helped me out a lot when I was starting to dive into gamedev.