Cocos2dx IOS平台相关代码

多点触摸

[__glViewsetMultipleTouchEnabled:YES];


报告内存状态

-(void) report_memory {

   struct task_basic_info info;

   mach_msg_type_number_t size = sizeof(info);

    kern_return_t kerr = task_info(mach_task_self(),

                                  TASK_BASIC_INFO,

                                   (task_info_t)&info,

                                   &size);

    if( kerr == KERN_SUCCESS ) {

       NSLog(@"Memory in use (in bytes): %u", info.resident_size);

    } else {

       NSLog(@"Error with task_info(): %s",mach_error_string(kerr));

    }

}


阻止设备进入待机状态

[[UIApplicationsharedApplication] setIdleTimerDisabled:YES];


弹出提示框

UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"title" message:@"message" delegate:selfcancelButtonTitle:@"ok"otherButtonTitles:nil];

[alert show];

[alert release];


c++与oc字符串互转

NSString *ocs = [NSString stringWithUTF8String:"test"];

std::string cs = [ocs UTF8String];


检查络是否连接

bool connectedToNetwork(){

    struct sockaddr_in zeroAddress;

    bzero(&zeroAddress, sizeof(zeroAddress));

    zeroAddress.sin_len =sizeof(zeroAddress);

    zeroAddress.sin_family =AF_INET;

    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (structsockaddr *)&zeroAddress);

   SCNetworkReachabilityFlags flags;

    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);

    CFRelease(defaultRouteReachability);

    if (!didRetrieveFlags) 

    {

       printf("Error. Could not recover network reachability flags\n");

       return false;

    }

    

    BOOL isReachable = ((flags & kSCNetworkFlagsReachable) !=0);

    BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) !=0);

    

    return (isReachable && !needsConnection) ? true :false;

}


跳转到APP STORE进行评分(打开网页,邮箱类似)

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=500619796"]];


开启新线程

+(void) newThread{

    [NSThreaddetachNewThreadSelector:@selector(startNewThread)toTarget:selfwithObject:nil];

}


+(void) startNewThread{

    //do something

}


OPENGL截屏

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED

   #define AWIMAGE UIImage*

#else

   #define AWIMAGE CGImageRef

#endif


@implementation ScreenShotIos


+(CGImageRef) takeAsCGImage{

    CCDirector *director = CCDirector::sharedDirector();

    CCSize displaySize = director->getDisplaySizeInPixels();

    CCSize winSize = director->getWinSizeInPixels();

    

    GLuint bufferLength = displaySize.width * displaySize.height *4;

    GLubyte *buffer = (GLubyte *)malloc(bufferLength);

    

    glReadPixels(0, 0, displaySize.width, displaySize.height,GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength,NULL);

    

   CGColorSpaceRef colorSpaceRef =CGColorSpaceCreateDeviceRGB();

    CGImageRef iref = CGImageCreate(displaySize.width, displaySize.height,8, 32, displaySize.width *4, colorSpaceRef, kCGBitmapByteOrderDefault, provider, NULL,NO, kCGRenderingIntentDefault);

    

    uint32_t* pixels = (uint32_t*)malloc(winSize.width * winSize.height *4);

    CGContextRef context = CGBitmapContextCreate(pixels, winSize.width, winSize.height,8, winSize.width *4, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED

    CGContextTranslateCTM(context, 0, displaySize.height);

    CGContextScaleCTM(context, 1, -1);

    switch (director->getDeviceOrientation()) {

       case kCCDeviceOrientationPortrait:{

            

            break;

        }

       case kCCDeviceOrientationPortraitUpsideDown:{

            CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));

            CGContextTranslateCTM(context, -displaySize.width, -displaySize.height);

            break;

        }

       case kCCDeviceOrientationLandscapeLeft:{

            CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));

            CGContextTranslateCTM(context, -displaySize.height,0);

            break;

        }

       case kCCDeviceOrientationLandscapeRight:{

            CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));

            CGContextTranslateCTM(context, displaySize.height - displaySize.width, -displaySize.height);

            break;

        }

        default:{

            break;

        }

    }

    CGContextDrawImage(context, CGRectMake(0.0f,0.0f, displaySize.width, displaySize.height), iref);

#else

    CGContextTranslateCTM(context,0, winSize.height);

    CGContextScaleCTM(context,1, -1);

    

    CGContextDrawImage(context, CGRectMake(0.0f,0.0f, winSize.width, winSize.height), iref);

    

#endif

    

    CGImageRef imageRef = CGBitmapContextCreateImage(context);

    

   CGDataProviderRelease(provider);

   CGImageRelease(iref);

    CGColorSpaceRelease(colorSpaceRef);

   CGContextRelease(context);

    free(buffer);

    free(pixels);

    

    return imageRef;

}


#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED

+ (UIImage*) takeAsImage{

    CGImageRef imageRef = [self takeAsCGImage];

    UIImage *outImage = [[UIImage alloc] initWithCGImage:imageRef];

    CGImageRelease(imageRef);

    return outImage;

}

#else


+ (CGImageRef) takeAsImage{

    return [self takeAsCGImage];

}

#endif


+(void) saveImage:(UIImage *) img{

   UIImageWriteToSavedPhotosAlbum(img, nil,nil, nil);

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值