cocos2d 1.0.1 for Mac - Text output problem on iMac 5K with Catalina

53 Views Asked by At

I have developed a mac app with cocos2d 1.0.1 for mac.

Text display (CCLabelTTF) doesn't work on iMac 5K with Catalina.

Mac pro, iMac, & MacBook are no problem.

I think this code is problem.

CCTexture2D.m:

elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)

-(id)initWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment attributedString:(NSAttributedString*)stringWithAttributes { NSAssert( stringWithAttributes, @"Invalid 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

please help me

1

There are 1 best solutions below

0
user2061160 On

OH yeah~~ solved~

fixed - CCTexture2D.m -

-(id) initWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment attributedString:(NSAttributedString*)stringWithAttributes { NSAssert(stringWithAttributes, @"Invalid 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;

}