私は本当にこれに頭を悩ませています!
ビューでいくつかのアニメーションを実行するために、NSOpenGLView をサブクラス化しています。アニメーションとは、いくつかの画像をビューの左から右に移動することなどを意味します。
これが私がすることです
a) OpenGL システム コードを初期化します。
- (id)initWithFrame:(NSRect)frame
{
NSOpenGLPixelFormatAttribute attrs[] = {
NSOpenGLPFANoRecovery, // Enable automatic use of OpenGL "share" contexts.
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFADepthSize, 16,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
0
};
// Create our pixel format.
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
self = [super initWithFrame:frame pixelFormat:pixelFormat];
return self;
}
// Synchronize buffer swaps with vertical refresh rate
- (void)prepareOpenGL
{
GLint swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
}
b) 開始時にタイマーを設定する
Code:
// Put our timer in -awakeFromNib, so it can start up right from the beginning
-(void)awakeFromNib
{
if( gameTimer != nil )
[gameTimer invalidate];
gameTimer = [NSTimer timerWithTimeInterval:0.02 //time interval
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:gameTimer
forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:gameTimer
forMode:NSEventTrackingRunLoopMode]; //Ensure timer fires during resize*/
}
// Timer callback method
- (void)timerFired:(id)sender
{
//The timer fires this method every second
currentTime ++;
// All we do here is tell the display it needs a refresh
[self setNeedsDisplay:YES];
}
c) drawRect で自分のものをアニメーション化する
Code:
- (void)drawRect:(NSRect)rect
{
[self animateFrame:rect];
// the correct way to do double buffering is this:
[[self openGLContext] flushBuffer];
}
d) animateFrame メソッドは、さまざまな長方形の位置に画像を描画するだけです
Code:
[curImage drawInRect:targetRect
fromRect:sourceRect
operation:NSCompositeSourceOver
fraction:1.0f];
ここに問題があります
アプリを起動すると、時間タイマーが起動され、drawRect が呼び出され、画像が描画されていることがわかります。
ただし、ウィンドウをドラッグしてウィンドウを移動すると、画像のアニメーションしか表示されません。ウィンドウがまだ静止しているときは、画像はフリーズしたままです。ウィンドウを移動すると、画像が動いていることがわかります...または、ウィンドウの焦点が外れて再び焦点が合った場合でも、画像の位置が変わることがわかります...
静的な場合、OpenGLView 自体が描画されていないように感じます...他に何をすべきかわかりません...
[[self openGLContext] flushBuffer]; を呼び出す必要がありますか? ビューが常に描画されるようにするにはどうすればよいですか?
誰かがここで何が起こっているのか、または私が見逃したものに光を当てることができますか?
助けていただければ幸いです。前もって感謝します!カミーFC