我这边的环境的是 cocos2dx 3.13 macos10.15 catalina
表现为:
对照这个PR 做修改即可:
https://github.com/cocos2d/cocos2d-x/pull/20124/files
//cocos/platform/mac/CCDevice-mac.mm 按照上面的变更图修改即可
NSSize realDimensions;
if (overflow == 2) {
if (overflow == 2)
realDimensions = _calculateRealSizeForString(&stringWithAttributes, font, dimensions, enableWrap);
} else {
else
realDimensions = _calculateStringSize(stringWithAttributes, font, &dimensions, enableWrap, overflow);
}
// Mac crashes if the width or height is 0
CC_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
if(dimensions.width <= 0.f) {
if(dimensions.width <= 0.f)
dimensions.width = realDimensions.width;
}
if (dimensions.height <= 0.f) {
if (dimensions.height <= 0.f)
dimensions.height = realDimensions.height;
}
//Alignment
CGFloat xPadding = FontUtils::_calculateTextDrawStartWidth(align, realDimensions, dimensions);
CGFloat yPadding = _calculateTextDrawStartHeight(align, realDimensions, dimensions);
NSInteger POTWide = dimensions.width;
NSInteger POTHigh = dimensions.height;
NSRect textRect = NSMakeRect(xPadding, POTHigh - dimensions.height + yPadding,
realDimensions.width, realDimensions.height);
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(POTWide, POTHigh)];
[image lockFocus];
// patch for mac retina display and lableTTF
[[NSAffineTransform transform] set];
NSBitmapImageRep* offscreenRep = [[[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:offscreenRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
[stringWithAttributes drawInRect:textRect];
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect (0.0f, 0.0f, POTWide, POTHigh)];
[image unlockFocus];
[NSGraphicsContext restoreGraphicsState];
auto data = (unsigned char*) [bitmap bitmapData]; //Use the same buffer to improve the performance.
auto data = (unsigned char*) [offscreenRep bitmapData]; //Use the same buffer to improve the performance.
NSUInteger textureSize = POTWide * POTHigh * 4;
auto dataNew = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
@@ -333,8 +335,6 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
info->isPremultipliedAlpha = true;
ret = true;
}
[bitmap release];
[image release];
} while (0);
return ret;
}