Reputation: 455
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.
Upvotes: 1
Views: 376
Reputation: 1553
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.
Upvotes: 1