我真的在这个问题上撕毁了我的头!
我正在继承 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