cocos2d-iphone - 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
请帮我
解决方案
哦耶~~解决了~
固定 - 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;
}
推荐阅读
- java - 如何在不获取的情况下更改多对一关系中的外键
- html - 如何垂直居中/对齐旁边的文本
标签
- node.js - Mongoose - 子文档的导入模式
- python - SqlAlchemy ORM中join的理解
- javascript - Javascript 文件之间无法访问窗口变量
- php - 有没有办法使用不同的 ID 别名获取 MySQL 记录
- python-3.x - 使用 BeautifulSoup 抓取文章标签中的所有段落 - 主要目标是 A-tag
- laravel - 点击该帖子后如何更新查看次数?
- java - 使用 ObjectMapper 解析 json,其中 json 键包含 json 作为值
- bash - 访问运行脚本的 docker 容器外部的 bash 脚本变量