2012-09-30 16 views
1

如何在Cocos2d中使用CoreGraphics製作雪碧圖形?如何使用CoreGraphics iOS製作雪碧Cocos2d

我試圖讓一個精靈使用核心圖形制作的圖形。

感謝您的任何見解和幫助!

+0

不是什麼卑鄙的東西,但我認爲解決方案已經在那裏,可以谷歌:http://www.cocos2d-iphone.org/forum/topic/2681 ...但是,這是非常慢。 – Mazyod

+0

我很欣賞這個鏈接,但它不完全相關,並且超過2年。我一直在盡我的努力尋找答案,但我似乎無法找到任何解釋如何使用核心圖形來創建一個在cocos2d中使用的精靈的形狀。 – PeteBob

回答

0

這解決了它。

回答以下是從我在遊戲開發網站收到回覆複製:https://gamedev.stackexchange.com/questions/38071/how-to-make-a-sprite-using-coregraphics-ios-cocos2d-iphone


我不知道cocos2d的,所以我只能給你約在iOS CoreGraphics中的一些技巧。

首先,簡單的情況下,如果你可以從UIImage或CGImageRef創建精靈。

/* function that draw your shape in a CGContextRef */ 
void DrawShape(CGContextRef ctx) 
{ 
    // draw a simple triangle 

    CGContextMoveToPoint(ctx, 50, 100); 
    CGContextAddLineToPoint(ctx, 100, 0); 
    CGContextAddLineToPoint(ctx, 0, 0); 
    CGContextClosePath(ctx); 
    } 


    void CreateImage(void) 
    { 
     UIGraphicsBeginImageContext(CGSizeMake(100, 100)); 

     DrawShape(UIGraphicsGetCurrentContext()); 

     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

     // now you've got an (autorelease) UIImage, that you can use to 
     // create your sprite. 
     // use image.CGImage if you need a CGImageRef 
    } 

如果您需要提供數據的緩衝區,並創建自己的CGContextRef:

CGContextRef CreateBitmapContextWithData(int width, int height, void *buffer) 
{ 
    CGContextRef ctx; 
    CGColorSpaceRef colorspace; 
    size_t bytesPerRow; 
    size_t bitsPerComponent; 
    size_t numberOfComponents; 
    CGImageAlphaInfo alpha; 

    bitsPerComponent = 8; 
    alpha = kCGImageAlphaPremultipliedLast; 

    numberOfComponents = 1; 
    colorspace = CGColorSpaceCreateDeviceRGB(); 

    numberOfComponents += CGColorSpaceGetNumberOfComponents(colorspace); 
    bytesPerRow = (width * numberOfComponents); 

    ctx = CGBitmapContextCreate(buffer, width, height, bitsPerComponent, bytesPerRow, colorspace, alpha); 

    CGColorSpaceRelease(colorspace); 

    return ctx; 
} 

void CreateImageOrGLTexture(void) 
{ 
    // use pow2 width and height to create your own glTexture ... 
    void *buffer = calloc(1, 128 * 128 * 4); 
    CGContextRef ctx = CreateBitmapContextWithData(128, 128, buffer); 

    // draw you shape 
    DrawShape(ctx); 

    // you can create a CGImageRef with this context 
    // CGImageRef image = CGBitmapContextCreateImage(ctx); 

    // you can create a gl texture with the current buffer 
    // glBindTexture(...) 
    // glTexParameteri(...) 
    // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 

    CGContextRelease(ctx); 
    free(buffer); 
} 

所有CGContext上功能看到蘋果文檔:https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGContext/Reference/reference.html

希望這有助於。


最終用戶回答。

3

做所有的核心圖形工作,然後你將有一個CGImageRef。使用該參考,您可以使用

+ (id)spriteWithCGImage:(CGImageRef)image key:(NSString *)key 

on CCSprite。

+0

在這裏得到了我的答案。 http://gamedev.stackexchange.com/questions/38071/how-to-make-a-sprite-using-coregraphics-ios-cocos2d-iphone/38111#38111 感謝您的幫助! – PeteBob