forked from DocOtak/not_tanaka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TanakaGameView.m
113 lines (87 loc) · 2.42 KB
/
TanakaGameView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// TanakaGameView.m
// nottanaka
//
// Created by Alex Dodge on 8/3/10.
// Copyright 2010 Roke Foundries. All rights reserved.
//
#import "TanakaGameView.h"
@implementation TanakaGameView
@synthesize keyboardDelegate,course;
- (BOOL)acceptsFirstResponder {
return YES;
}
-(id) init {
self = [super init];
[self setNeedsDisplay:YES];
rotation = 0;
distance = 0;
return self;
}
-(void) setPlayerAngle:(float)rot Distance:(float)dist {
rotation = rot;
distance = dist;
[self setNeedsDisplay: YES];
}
void drawCircle(int n, float z, float r){
glBegin(GL_QUADS);
for(float i=0;i<M_PI*2-M_PI/(n);i+=M_PI/(n/2)){
float i2 = i+M_PI/(n/2);
glVertex3f(cos(i)*r,sin(i)*r,z/2);
glVertex3f(cos(i2)*r,sin(i2)*r,z/2);
glVertex3f(cos(i2)*r,sin(i2)*r,-z/2);
glVertex3f(cos(i)*r,sin(i)*r,-z/2);
}
glEnd();
}
-(void) drawRect: (NSRect) bounds {
[super drawRect:bounds];
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);
glEnable (GL_NICEST);
glDepthFunc(GL_LEQUAL);
NSRect b = [self bounds];
glLoadIdentity();
gluPerspective(60, b.size.width/b.size.height, .1, 400);
glViewport(0, 0, b.size.width, b.size.height);
glPushMatrix();
int segmentid = [course getSegmentIdForDistance:distance];
TanakaCourseSegment * s1 = [course segment:segmentid];
TanakaCourseSegment * s2 = [course segment:segmentid+1];
float d = (distance-s1->dfromstart)/s1->d;
float *matrixinterpolated, matrix[16];
matrixinterpolated = interpolateMatrices(s1->facingmatrix, s2->facingmatrix, d);
invertMat(matrixinterpolated, matrix);
free(matrixinterpolated);
glTranslatef(0,s1->r-1,0);
glRotatef(rotation, 0, 0, 1);
glMultMatrixf(matrix);
for(int i=0;i<[course nsegments];i++){
TanakaCourseSegment * s = [course segment:i];
glPushMatrix();
glMultMatrixf(s->matrix);
glColor3f(1,((int)i % 13)/13.0,((int)i % 11)/11.0);
drawCircle(10, 1, s->r);
glPopMatrix();
}
glPopMatrix();
[[self openGLContext] flushBuffer];
}
- (void)keyDown:(NSEvent *)event {
if(keyboardDelegate == nil) return;
NSString * s = [event characters];
for(int i=0;i<[s length];i++){
unichar c = [s characterAtIndex:i];
[keyboardDelegate key:c value:YES];
}
}
- (void)keyUp:(NSEvent *)event {
if(keyboardDelegate == nil) return;
NSString * s = [event characters];
for(int i=0;i<[s length];i++){
unichar c = [s characterAtIndex:i];
[keyboardDelegate key:c value:NO];
}
}
@end