首页 > 解决方案 > cocos2d 1.0.1 for Mac - 带有 Catalina 的 iMac 5K 上的文本输出问题

问题描述

我已经为 mac 开发了一个带有 cocos2d 1.0.1 的 mac 应用程序。

文本显示 ( CCLabelTTF) 在配备 Catalina 的 iMac 5K 上不起作用。

Mac pro、iMac 和 MacBook 都没有问题。

我认为这段代码有问题。

CCTexture2D.m:

elif 定义(__MAC_OS_X_VERSION_MAX_ALLOWED)

-(id)initWithString:(NSString *)字符串尺寸:(CGSize)尺寸对齐:(CCTextAlignment)对齐属性字符串:(NSAttributedString *)stringWithAttributes { NSAssert(stringWithAttributes,@“无效的stringWithAttributes”);

NSUInteger POTWide = ccNextPOT(dimensions.width);
NSUInteger POTHigh = ccNextPOT(dimensions.height);
unsigned char*            data;

NSSize realDimensions = [stringWithAttributes size];

//Alignment
float xPadding = 0;

// Mac crashes if the width or height is 0
if( realDimensions.width > 0 && realDimensions.height > 0 ) {
    switch (alignment) {
        case CCTextAlignmentLeft: xPadding = 0; break;
        case CCTextAlignmentCenter: xPadding = (dimensions.width-realDimensions.width)/2.0f; break;
        case CCTextAlignmentRight: xPadding = dimensions.width-realDimensions.width; break;
        default: break;
    }

    //Disable antialias
    [[NSGraphicsContext currentContext] setShouldAntialias:NO];

    NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(POTWide, POTHigh)];

    [image lockFocus];


    [stringWithAttributes drawAtPoint:NSMakePoint(xPadding, POTHigh-dimensions.height)]; // draw at offset position


    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect (0.0f, 0.0f, POTWide, POTHigh)];
    [image unlockFocus];

    data = (unsigned char*) [bitmap bitmapData];  //Use the same buffer to improve the performance.

    NSUInteger textureSize =  POTWide*POTHigh   ;

    for(int i = 0; i<textureSize; i++) //Convert RGBA8888 to A8
    {
        data[i] = data[i*4+3];

    }


    data = [self keepData:data length:textureSize];

    self = [self initWithData:data pixelFormat:kCCTexture2DPixelFormat_A8 pixelsWide:POTWide pixelsHigh:POTHigh contentSize:dimensions];


    [bitmap release];
    [image release];

} else {
    [self release];
    return nil;
}

return self;

}

endif // __MAC_OS_X_VERSION_MAX_ALLOWED

请帮我

标签: cocos2d-iphone

解决方案


哦耶~~解决了~

固定 - CCTexture2D.m -

-(id)initWithString:(NSString *)字符串尺寸:(CGSize)尺寸对齐:(CCTextAlignment)对齐属性字符串:(NSAttributedString *)stringWithAttributes { NSAssert(stringWithAttributes,@“无效的stringWithAttributes”);

// get nearest power of two
NSSize POTSize = NSMakeSize(ccNextPOT(dimensions.width), ccNextPOT(dimensions.height));

// Get actual rendered dimensions
NSRect boundingRect = [stringWithAttributes boundingRectWithSize:NSSizeFromCGSize(dimensions) options:NSStringDrawingUsesLineFragmentOrigin];

// Mac crashes if the width or height is 0
if( POTSize.width == 0 )
    POTSize.width = 2;

if( POTSize.height == 0)
    POTSize.height = 2;

CGSize offset = CGSizeMake(0, POTSize.height - dimensions.height);

//Alignment
switch (alignment) {
    case CCTextAlignmentLeft: break;
    case CCTextAlignmentCenter: offset.width = (dimensions.width-boundingRect.size.width)/2.0f; break;
    case CCTextAlignmentRight: offset.width = dimensions.width-boundingRect.size.width; break;
    default: break;
}


CGRect drawArea = CGRectMake(offset.width, offset.height, boundingRect.size.width, boundingRect.size.height);



NSInteger POTWide = POTSize.width;
NSInteger POTHigh = POTSize.height;


NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc]
                                   initWithBitmapDataPlanes:NULL
                                   pixelsWide:POTWide
                                   pixelsHigh:POTHigh
                                   bitsPerSample:8
                                   samplesPerPixel:4
                                   hasAlpha:YES
                                   isPlanar:NO
                                   colorSpaceName:NSDeviceRGBColorSpace
                                   bitmapFormat: 0
                                   bytesPerRow:4 * POTWide
                                   bitsPerPixel:32] autorelease];

NSGraphicsContext* g = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];

[stringWithAttributes drawWithRect:drawArea options:NSStringDrawingUsesLineFragmentOrigin];
[NSGraphicsContext restoreGraphicsState];

unsigned char *data = (unsigned char*) [bitmap bitmapData];  //Use the same buffer to improve the performance.

NSUInteger textureSize = POTWide * POTHigh  ;

unsigned char *dst = (unsigned char*)data;
for(int i = 0; i<textureSize; i++)
{
    dst[i] = data[i*4+3];                    //Convert RGBA8888 to A8
}

data = [self keepData:dst length:textureSize];

self = [self initWithData:data pixelFormat:kCCTexture2DPixelFormat_A8 pixelsWide:POTSize.width pixelsHigh:POTSize.height contentSize:dimensions];

//[bitmap release];
//[image release];

return self;

}


推荐阅读