解決策:フレームごとではなく、すべてのテクスチャを前に 1 回セットアップしました。GLKBaseEffect についても同じことを行い、修正しました。:D
わかりました、この問題は多くの問題を引き起こしているため、真剣に助けが必要です。
私はゲーム エンジンを作成しているので、基本的にはその物理レンダリングとゲーム ループなどです。シミュレーターでこのアプリを実行するたびに、スムーズかつ高速に実行され、基本的に問題ありません。ただし、アプリを iPhone 3GS (開発に使用するためにジェイルブレイク) にデプロイすると、動作が非常に遅くなります。実体の動きは信じられないほど遅く、滑らかではなく非常にぎくしゃくした動きをしています。それは一種の前にジャンプします。
GLKViewController を使用してゲームを描画および更新しています。
画面上のエンティティを減らし、画像をアニメーション化していない場合、滑らかさが向上することがわかりましたが、それでもエンティティの移動が非常に遅く、1つだけでゲームを行うことができないため、それは解決策にはなりませんその中のオブジェクト…</p>
iPhoneでスムーズに同じ速度で実行する方法を知っている人はいますか? 助けてください、私は過去 2 日間、これを修正しようとしてきました (文字通り 48 時間の最高の部分)
vvv すべてのコード vvv
これは、カスタム GLKViewController クラスです。
#import "RWViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface RWViewController()
@property (strong, nonatomic) EAGLContext *context;
@end
@implementation RWViewController
@synthesize context = _context;
@synthesize rwEngine;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if(!self.context)
NSLog(@"Failed To Create ES Context");
GLKView *view = (GLKView *) self.view;
self.preferredFramesPerSecond = 60;
view.enableSetNeedsDisplay = true;
view.context = self.context;
[EAGLContext setCurrentContext:self.context];
rwEngine = [[RWEngine alloc] ignition];
}
#pragma mark - GLKViewDelegate
-(void) glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(104.0/255.0, 0, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
[rwEngine draw];
}
- (void)update {
[rwEngine update];
}
@end
これは rwEngine クラス コードです。
#import "RWEngine.h"
@implementation RWEngine
TestWorld * world;
-(RWEngine *) ignition
{
//Game Loop Tick Set to ~60 fps
world = [[TestWorld alloc] load];
return self;
}
-(void)update
{
[[self getActiveWorld] update];
}
-(void)draw
{
[[self getActiveWorld] draw];
}
-(NSMutableArray *) getWorlds
{
}
-(World *) getActiveWorld
{
return world;
}
@end
描画パスはViewController>Engine>World>Room>Entity
エンティティ描画メソッドは次のとおりです。
-(void) draw
{
CGPoint drawPoint = (self.room == nil) ? self.location : CGPointMake(self.location.x - self.room.viewPoint.x, self.location.y - self.room.viewPoint.y);
if(isAnimated)
{
SpriteInfo * frame = [[self getAnimation] getCurrentFrame];
[RWImageRenderer renderSprite:frame.imageName position:drawPoint spritePosition:frame.spritePosition spriteDimensions:frame.spriteSize];
}
else if(!isAnimated)
{
SpriteInfo * spriteInfo = [self getSprite];
NSString * imageName = spriteInfo.imageName;
CGSize spriteSize = spriteInfo.spriteSize;
CGPoint spritePosition = spriteInfo.spritePosition;
[RWImageRenderer renderSprite:imageName position:drawPoint spritePosition:spritePosition spriteDimensions:spriteSize];
}
}
これが RWImageRenderer コードです。
+(void)renderSprite:(NSString *)filePath position:(CGPoint) position spritePosition:(CGPoint)spriteP spriteDimensions:(CGSize)spriteD
{
[self renderSpriteWithRotation:filePath position:position spritePosition: spriteP spriteDimensions:spriteD rotationOrigin:CGPointMake(0, 0) rotationDegrees:0];
}
+(void)renderSpriteWithRotation:(NSString *)filePath position:(CGPoint) position spritePosition:(CGPoint)spriteP spriteDimensions:(CGSize)spriteD rotationOrigin:(CGPoint) origin rotationDegrees:(int) degrees
{
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool: YES], GLKTextureLoaderOriginBottomLeft, nil];
NSError * error;
NSString * path = [[NSBundle mainBundle] pathForResource:filePath ofType:nil];
GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
NSInteger spriteWidth, spriteHeight;
spriteWidth = (spriteD.width == -1 && spriteD.height == -1) ? textureInfo.width : spriteD.width;
spriteHeight = (spriteD.width == -1 && spriteD.height == -1) ? textureInfo.height : spriteD.height;
GLKBaseEffect* effect = [[GLKBaseEffect alloc] init];
effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(0, 480, 320, 0, -1024, 1024);
if(textureInfo == nil)
{
NSLog(@"Error Loading image: %@", [error localizedDescription]);
return;
}
float spriteX = spriteP.x / textureInfo.width;
float spriteY = spriteP.y / textureInfo.height;
float textureWidth = ((float)spriteWidth) / textureInfo.width;
float textureHeight = ((float)spriteHeight) / textureInfo.height;
VertexQuad quad;
if(degrees != 0)
{
quad.tl.positionVertex = [self rotatePoint:CGPointMake(position.x, position.y) origin:origin angleofRotation:degrees];
quad.tr.positionVertex = [self rotatePoint:CGPointMake(position.x+spriteWidth, position.y) origin:origin angleofRotation:degrees];
quad.bl.positionVertex = [self rotatePoint:CGPointMake(position.x, position.y + spriteHeight) origin:origin angleofRotation:degrees];
quad.br.positionVertex = [self rotatePoint:CGPointMake(position.x+spriteWidth, position.y + spriteHeight) origin:origin angleofRotation:degrees];
//quad.tr.positionVertex = CGPointMake(position.x+spriteWidth, position.y);
//quad.bl.positionVertex = CGPointMake(position.x, position.y + spriteHeight);
//quad.br.positionVertex = CGPointMake(position.x+spriteWidth, position.y + spriteHeight);
}
else if(degrees == 0)
{
quad.tl.positionVertex = CGPointMake(position.x, position.y);
quad.tr.positionVertex = CGPointMake(position.x+spriteWidth, position.y);
quad.bl.positionVertex = CGPointMake(position.x, position.y + spriteHeight);
quad.br.positionVertex = CGPointMake(position.x+spriteWidth, position.y + spriteHeight);
}
quad.tl.textureVertex = CGPointMake(spriteX, 1 - spriteY);
quad.tr.textureVertex = CGPointMake(spriteX + textureWidth, 1 - spriteY);
quad.bl.textureVertex = CGPointMake(spriteX, 1 - spriteY - textureHeight);
quad.br.textureVertex = CGPointMake(spriteX + textureWidth, 1 - spriteY - textureHeight);
effect.texture2d0.name = textureInfo.name;
effect.texture2d0.enabled = YES;
[effect prepareToDraw];
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
long offset = (long)&quad;
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImageVertex), (void *) (offset + offsetof(ImageVertex, positionVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(ImageVertex), (void *) (offset + offsetof(ImageVertex, textureVertex)));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}