@implementation GameView
-(void)mainTimerFired:(id) sender { //Move the square based off arrow key states.
if (upPressed == YES) {
vertVec = 0.01;
}
if (downPressed == YES) {
vertVec = -0.01;
}
if (rightPressed == YES) {
horiVec = 0.01;
}
if (leftPressed == YES) {
horiVec = -0.01;
}
[ self setNeedsDisplay:YES];
}
//Needed, if you don't include it the keys wont work.
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void) prepareOpenGL {
upPressed = NO;
downPressed = NO;
leftPressed = NO;
rightPressed = NO;
vertVec = 0;
horiVec = 0;
rotY = 0;
rotX = 0;
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
mainTimer=[ [ NSTimer scheduledTimerWithTimeInterval:0.017 target:self selector:@selector(mainTimerFired:) userInfo:nil repeats:YES ] retain ] ;
return;
}
-(void) keyUp:(NSEvent *)theEvent { //Stop movement when the key is released.
switch( [theEvent keyCode] ) {
case 126: //Up arrow
upPressed = NO;
vertVec = 0.0;
break;
case 125: //Down arrow
downPressed = NO;
vertVec = 0.0;
break;
}
switch( [theEvent keyCode] ) {
case 124: //Right arrow
rightPressed = NO;
horiVec = 0.0;
break;
case 123: //Left arrow
leftPressed = NO;
horiVec = 0.0;
break;
}
}
//Key events.
-(void) keyDown:(NSEvent *)theEvent { //Tell the game loop that I want to move the square.
switch( [theEvent keyCode] ) {
case 126: //Up arrow
upPressed = YES;
break;
case 125: //Down arrow
downPressed = YES;
break;
}
switch( [theEvent keyCode] ) {
case 124: //Right arrow
rightPressed = YES;
break;
case 123: //Left arrow
leftPressed = YES;
break;
}
}
- (void)drawRect:(NSRect)rect { //In combination draw this is where the drawing happens.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport( 0,0,[self frame].size.width,[self frame].size.height );
glMatrixMode(GL_MODELVIEW);
[self draw];
glFlush();
}
- (void)draw {
glViewport( 0, 0, [self frame].size.width, [self frame].size.height );
glTranslatef(horiVec, 0, vertVec);
glBegin(GL_TRIANGLES);
{
glVertex3f( 0.0, 1.0, -1.0);
glVertex3f( -1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0 ,-1.0);
}
glEnd();
}
@end
A brief explanation of my code:
The keyUp:(NSEvent *) and keyDown:(NSEvent *) tell my game loop mainTimerFired:(id) to change horiVec or vertVec. Those are used in the 2 drawing methods to with glTranslatef to move a triangle.
So I am using an NSOpenGLView, a triangle does display, I can move it left and right, but when I try moving it forward and backward nothing happens until it suddenly disappears after holding either the up or down key for a while. I can then bring it back by holding the opposite arrow key. Also, I tried getting info about my GL_DEPTH_BITS by doing:
int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);
NSLog(@"%i bits depth", depth);
And I got 24 bits depth. Which is odd because in Interface Builder I set it up to be 32-bit in my NSOpenGLView.