뱀의 종류를 사용하는 게임을 만들려고합니다. 지금까지 제대로 작동했지만 잘 작동하지만 약간의 시간이 지나면 다소 느려지 며 특히 Iphone 5 이하에서는 느려집니다.DrawRect 최적화 : Snake 종류의 게임
내가 그것을 때문에의 drawRect의 알고 :
-(void) drawRect:(CGRect)rect{
NSDate *start = [NSDate date];
CGFloat rr, rg, rb, ra;
[redcolorr getRed:&rr green:&rg blue:&rb alpha:&ra];
CGFloat gr, gg, gb, ga;
[greencolorr getRed:&gr green:&gg blue:&gb alpha:&ga];
CGFloat br, bg, bb, ba;
[bluecolorr getRed:&br green:&bg blue:&bb alpha:&ba];
if ([myName isEqual:@"Server"]) {
for (int i= 0; i<ServerBody.count; i++) {
Snake *a;
a = [ServerBody objectAtIndex:i];
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, rr, rg, rb, ra);
CGContextFillEllipseInRect(ctx, CGRectMake(a.x,a.y,7.5*k*sizee, 7.5*k*sizee));
UIGraphicsPopContext();
}
}
else{
for (int i= 0; i<IntelligentServerBody.count; i++) {
Snake *a;
a = [IntelligentServerBody objectAtIndex:i];
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, rr, rg, rb, ra);
CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee));
UIGraphicsPopContext();
}
}
if ([myName isEqual:@"Client0"]) {
for (int i= 0; i<Client0Body.count; i++) {
Snake *a;
a = [Client0Body objectAtIndex:i];
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, br, bg, bb, ba); // white color
CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee));
UIGraphicsPopContext();
}
}
else{
for (int i= 0; i<IntelligentClient0Body.count; i++) {
Snake *a;
a = [IntelligentClient0Body objectAtIndex:i];
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, br, bg, bb, ba); // white color
CGContextFillEllipseInRect(ctx, CGRectMake(a.x, a.y, 7.5*k*sizee, 7.5*k*sizee));
UIGraphicsPopContext();
}
}
NSTimeInterval timeInterval = [start timeIntervalSinceNow];
}
가 있다는 사실을 숙지하십시오 :
- 의 drawRect이 모두 뱀
- 서버가 다시 그릴 때마다 0.05 초라고 먼저 아이폰, 및 클라이언트, 두 번째 아이폰 (멀티 페어)
그럼 지금 알려주세요. 코드 줄을 작성하거나 불필요한 줄이 있습니다. 뱀을 그리는 다른 방법이 있습니까? 나는 뱀이 빛을 내고 싶을 때마다 (색칠 할 때마다 색을 바꾼다.) 어쩌면 "자라다/자라다"(물방울의 크기를 바꾼다.)
'UIGraphicsPush/Pop' 호출은 중복됩니다. 'drawRect :'는 GPU를 활용하지 못하기 때문에 자주 호출 될 때마다 조금 뒤떨어 지지만 말입니다. [Core Animation] (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html) 또는 [Core Animation]을 사용하여 원하는 효과를 다시 만들려고합니다. Sprite Kit] (https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html) 대신 GPU를 활용하므로보다 효율적입니다. – Hamish
그래, 이처럼 반복 그리기 호출을 사용하고 싶지는 않을 것이다. 새로 고침 간격마다 그래픽 컨텍스트를 작성하고있다. 예를 들어, 각 뱀에 대해 작은 "세그먼트"를 만든 다음 SpriteKit을 사용하여 세그먼트 구성을 통해 렌더링하면 더 나은 결과를 얻을 수 있습니다. 그런 식으로 각 세그먼트는 최적의 방식으로 렌더링되는 GPU 텍스처입니다. 이 드로 로직 대신 X, Y 좌표 및 각도를 사용하여 세그먼트를 연결하는 코드를 작성해야합니다. 약간의 리팩토링이 필요하지만 그래픽 서브 시스템을보다 효율적으로 사용할 수 있습니다. – MoDJ