iDevGames Forums
What's wrong with Core Graphics and UI? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: What's wrong with Core Graphics and UI? (/thread-2157.html)



What's wrong with Core Graphics and UI? - Lazarwulf - Nov 12, 2008 04:29 AM

I was having troubles when rendering more that 10 or so "sprites" using CG. So to elminate the possibility that was my game logic, I wrote this test render loop that makes six rows of PNG "creatures" on the screen. It is called from a timer at 30fps. When running on the simulator this is fine. If I run this code on the iPhone, it brings it to it's knees. So much that no the UI stops responding. I cant imagine that Core Graphics cant handle rendering 60 sprites from a CGLayer. I feel like I am missing something obvious.



Code:
testCreature = PNG loaded into CGImageRef



-(void)render
{
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextTranslateCTM(context, 0.0, kBackGroundHeight);
     CGContextScaleCTM(context, 1.0, -1.0);
    
     CGRect creatureRect  = CGRectMake (0, 0, kCreatureSize, kCreatureSize);
     CGLayerRef   creatureLayer = CGLayerCreateWithContext (context, creatureRect.size, NULL);
     CGContextRef creatureContext = CGLayerGetContext (creatureLayer);
     CGContextDrawImage(creatureContext, creatureRect, testCreature);

     // draw creatures
    
     static int move = 0; // just so we see something moving...
    
     CGContextSaveGState(context);
     for(int ndx = 0;ndx < 10;++ndx)
     {
          CGPoint point = CGPointMake( kCreatureSize + move, kBackGroundHeight - (ndx * kCreatureSize) );
          
          CGContextDrawLayerAtPoint (context, point, creatureLayer);
     }
    
     CGContextTranslateCTM (context, kCreatureSize, 0);
    
     for(int ndx = 0;ndx < 10;++ndx)
     {
          CGPoint point = CGPointMake( kCreatureSize, kBackGroundHeight - (ndx * kCreatureSize) );
          
          CGContextDrawLayerAtPoint (context, point, creatureLayer);
     }
    
     CGContextTranslateCTM (context, kCreatureSize, 0);
    
     for(int ndx = 0;ndx < 10;++ndx)
     {
          CGPoint point = CGPointMake( kCreatureSize, kBackGroundHeight - (ndx * kCreatureSize) );
          
          CGContextDrawLayerAtPoint (context, point, creatureLayer);
     }
    
     CGContextTranslateCTM (context, kCreatureSize, 0);
    
     for(int ndx = 0;ndx < 10;++ndx)
     {
          CGPoint point = CGPointMake( kCreatureSize, kBackGroundHeight - (ndx * kCreatureSize) );
          
          CGContextDrawLayerAtPoint (context, point, creatureLayer);
     }
     CGContextTranslateCTM (context, kCreatureSize, 0);
    
     for(int ndx = 0;ndx < 10;++ndx)
     {
          CGPoint point = CGPointMake( kCreatureSize, kBackGroundHeight - (ndx * kCreatureSize) );
          
          CGContextDrawLayerAtPoint (context, point, creatureLayer);
     }
     CGContextTranslateCTM (context, kCreatureSize, 0);
    
     for(int ndx = 0;ndx < 10;++ndx)
     {
          CGPoint point = CGPointMake( kCreatureSize, kBackGroundHeight - (ndx * kCreatureSize) );
          
          CGContextDrawLayerAtPoint (context, point, creatureLayer);
     }
    
     CGContextRestoreGState(context);
          
     CGLayerRelease(creatureLayer);
    
     move++;
    
     if(move > kCreatureSize)
          move = 0;
}



What's wrong with Core Graphics and UI? - longjumper - Nov 12, 2008 12:33 PM

If you want to do any sort of drawing that requires constant updates, you are going to need to use OpenGL.

Core Graphics just can't do it. It's main usage on the iPhone is for implementing static imagery for UIView subclasses.