I'm (a newbee) trying to port an old game into IPhone.
The problem Im facing is a crash rendering (using Quartz) the current scene in a UIView from setNeedsDisplay. At random points in a rendering method I get EXC_BAD_ACCESS. Using the debugger it seem to occur at random code lines.
The 1st rendering scene always works fine, but when I call setNeedsDisplay from a touch event and try to render the 2nd scene I get the crash.
Note that the rendering methods in the UIView derived class is static, but I changed it into non static, same result.
Anyone got the same problem ?
Code:
-( void )drawRect:( CGRect )rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
switch( renderMode ) {
case RENDER_MODE_TITLE:
[ MasqueradeDeluxeView renderTitleMode:ctx ];
break;
case RENDER_MODE_SELECT_GAME:
[ MasqueradeDeluxeView renderSelectGameMode:ctx ];
break;
case RENDER_MODE_MENU:
[ MasqueradeDeluxeView renderMenuMode:ctx ];
break;
case RENDER_MODE_OPTIONS:
[ MasqueradeDeluxeView renderOptionsMode:ctx ];
break;
}
//CGContextFlush( ctx );
}
Code:
///
/// Renders the title mode.
///
+( void )renderTitleMode:( CGContextRef )ctx {
CGContextTranslateCTM( ctx, 0, background.size.height );
CGContextScaleCTM( ctx, 1.0, -1.0 );
CGContextDrawImage( ctx, CGRectMake( 0, 0, 480, 320 ), background.CGImage );
[ MasqueradeTextHandler drawTextRoundedSelection:ctx bounds:titleFrame cornerRad:10 ];
[ MasqueradeTextHandler drawFixedWidthStringCentered:ctx x:SCREEN_WIDTH >> 1 y:30 text:@"PRESS TO START..." ];
}
Code:
///
/// Renders the menu mode.
///
+( void )renderMenuMode:( CGContextRef )ctx {
CGContextTranslateCTM( ctx, 0, background.size.height );
CGContextScaleCTM( ctx, 1.0, -1.0 );
CGContextDrawImage( ctx, CGRectMake( 0, 0, 480, 320 ), background.CGImage );
for( int mo = MENU_OPTIONS.count -1; mo >= 0 ; mo-- ) {
CGRect moRect = menuFrames[ mo ];
int x = moRect.origin.x + ( ( int )moRect.size.width >> 1 );
[ MasqueradeTextHandler drawTextRoundedSelection:ctx bounds:menuFrames[ mo ] cornerRad:10 ];
[ MasqueradeTextHandler drawFixedWidthStringCentered:ctx
x:x
y:30
text:[ MENU_OPTIONS objectAtIndex: mo ] ];
}
}
Code:
///
/// Renders a rounded rect of supplied bounds and radius on the corners.
/// Fill and rect color are globally setup from initialize.
///
+( void )drawTextRoundedSelection:( CGContextRef )ctx bounds:( CGRect )b cornerRad:( int )cr {
CGContextSetLineWidth( ctx, 2.0F );
CGContextSetStrokeColorWithColor( ctx, optionsRectColor );
CGContextSetFillColorWithColor( ctx, optionsSelectedColor );
CGContextSetShouldAntialias( ctx, TRUE );
CGFloat minx = CGRectGetMinX( b );
CGFloat midx = CGRectGetMidX( b );
CGFloat maxx = CGRectGetMaxX( b );
CGFloat miny = CGRectGetMinY( b );
CGFloat midy = CGRectGetMidY( b );
CGFloat maxy = CGRectGetMaxY( b );
CGContextMoveToPoint( ctx, minx, midy );
CGContextAddArcToPoint( ctx, minx, miny, midx, miny, cr );
CGContextAddArcToPoint( ctx, maxx, miny, maxx, midy, cr );
CGContextAddArcToPoint( ctx, maxx, maxy, midx, maxy, cr );
CGContextAddArcToPoint( ctx, minx, maxy, minx, midy, cr );
CGContextClosePath( ctx );
CGContextDrawPath( ctx, kCGPathFillStroke );
}
The crash seems to be random at any point calling the CG* method from within the drawTextRoundedSelection method.
URGENT!!
If the question isn't formulated (sorry for the poor english), please let me know and I'll reformulate dito.