主题 : 在cocos2d/cocos2d-x中,一种简单的将sprite变灰的方法

在cocos2d/cocos2d-x中,一种简单的将sprite变灰的方法   

思路很简单,网上我们可以很容易的找到一种将UIImage加灰的方法,代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@implementation UIImage (grayscale)
typedef enum {
     ALPHA = 0,
     BLUE = 1,
     GREEN = 2,
     RED = 3
} PIXELS;
 
- (UIImage *)convertToGrayscale {
     CGSize size = [ self size];
     int width = size.width;
     int height = size.height;
     
     // the pixels will be painted to this array
     uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof (uint32_t));
     
     // clear the pixels so any transparency is preserved
     memset(pixels, 0, width * height * sizeof (uint32_t));
     
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
     
     // create a context with RGBA pixels
     CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof (uint32_t), colorSpace,
                                                  kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
     
     // paint the bitmap to our context which will fill in the pixels array
     CGContextDrawImage(context, CGRectMake(0, 0, width, height), [ self CGImage]);
     
     for ( int y = 0; y < height; y++) {
         for ( int x = 0; x < width; x++) {
             uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
             
             uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
             
             // set the pixels to gray
             rgbaPixel[RED] = gray;
             rgbaPixel[GREEN] = gray;
             rgbaPixel[BLUE] = gray;
         }
     }
     
     // create a new CGImageRef from our context with the modified pixels
     CGImageRef image = CGBitmapContextCreateImage(context);
     
     // we're done with the context, color space, and pixels
     CGContextRelease(context);
     CGColorSpaceRelease(colorSpace);
     free(pixels);
     
     // make a new UIImage to return
     UIImage *resultUIImage = [UIImage imageWithCGImage:image];
     
     // we're done with image now too
     CGImageRelease(image);
     
     return resultUIImage;
}
 
@end


由于cocos2d与cocos2d-x的区别,下面我将分开来说具体的方法:

在cocos2d中,我们知道CCSprite有一个用CGImage构造的参数,而CGImage又能从UIImage获得,为了得到一个灰色的sprite,我们实际上只需要先用上面的方法给原始UIimage生成一个加灰的UIImage,然后用这个UIImage的CGImage去构造一个CCSprite就行了。cocos2d的比较简单,我就不上代码了。

在cocos2d-x中,为了跨平台的问题,CCSprite没有用CGImage构造的方法,但cocos2d-x提供了一个叫CCImage的类,该类支持使用图片的data来构造,而我们知道CCTexture2D可以用CCImage来构造,用CCTexture2D就可以去构造一个CCSprite了。于是我们就在UIImage和CCSprite之间建立了一座桥梁。以下是简单的示例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
     UIImage *sourceImage = [UIImage imageNamed:@ "HelloWorld.png" ];
     
     UIImage *grayImage = [sourceImage convertToGrayscale];
     
     NSData *grayImageData = UIImagePNGRepresentation(grayImage);
     
     CCImage *finalImage = new CCImage();
     
     void *data = malloc([grayImageData length]);
     
     [grayImageData getBytes:data];
     
     finalImage->initWithImageData(data,
                                   [grayImageData length],
                                   CCImage::kFmtPng,
                                   grayImage.size.width,
                                   grayImage.size.height,
                                   8);
     
     CCTexture2D *texture = new CCTexture2D();
     
     texture->initWithImage(finalImage);
     
     texture->autorelease();
     
     delete finalImage;
     
     CCSize winSize = CCDirector::sharedDirector()->getWinSize();
     
     CCSprite *pSprite = CCSprite::spriteWithTexture(texture);
     
     // position the sprite on the center of the screen
     pSprite->setPosition( ccp(winSize.width/2, winSize.height/2) );
 
     // add the sprite as a child to this layer
     this ->addChild(pSprite, 0);

P.S.文字表达能力可能有点差,写程序写得不会写中文了。。。有任何问题回帖问我:)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值