-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEAGLView.m
More file actions
executable file
·601 lines (363 loc) · 12.8 KB
/
EAGLView.m
File metadata and controls
executable file
·601 lines (363 loc) · 12.8 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//
// EAGLView.m
// OddFingerOut
//
// Created by cclaan on 5/24/09.
// Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import "EAGLView.h"
#define USE_DEPTH_BUFFER 0
// A class extension to declare private methods
@interface EAGLView ()
@property (nonatomic, retain) EAGLContext *context;
@property (nonatomic, assign) NSTimer *animationTimer;
- (BOOL) createFramebuffer;
- (void) destroyFramebuffer;
-(void) drawPointsCircleAtPoint:(CGPoint)p withRadius:(float) r;
-(void) drawCircleAtPoint:(CGPoint)p withRadius:(float) r;
@end
@implementation EAGLView
@synthesize context;
@synthesize animationTimer;
@synthesize animationInterval;
@synthesize useSuspensefulPick;
// You must implement this method
+ (Class)layerClass {
return [CAEAGLLayer class];
}
//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder*)coder {
if ((self = [super initWithCoder:coder])) {
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
return nil;
}
glEnable(GL_BLEND);
glEnable(GL_SMOOTH);
glEnable(GL_POINT_SMOOTH);
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"chooseSound" ofType:@"wav"];
chooseSound = [[Sound alloc] initWithContentsOfFile: filePath];
filePath = [[NSBundle mainBundle] pathForResource:@"clickSound" ofType:@"wav"];
clickSound = [[Sound alloc] initWithContentsOfFile: filePath];
filePath = [[NSBundle mainBundle] pathForResource:@"tickSound" ofType:@"wav"];
tickSound = [[Sound alloc] initWithContentsOfFile: filePath];
filePath = [[NSBundle mainBundle] pathForResource:@"touchUpSound2" ofType:@"wav"];
touchUpSound = [[Sound alloc] initWithContentsOfFile: filePath];
bgTexture = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
activeTouches = [[NSMutableArray array] retain];
touchesSet = [[NSMutableSet set] retain];
animationInterval = 1.0 / 60.0;
}
return self;
}
-(void) pickClicked {
int counter = [touchesSet count];
if ( counter == 0 ) {
[touchUpSound play];
return;
}
isPicking = YES;
//
int rander = arc4random() % counter;
NSLog(@"random: %i " , rander );
finger = rander;
if ( useSuspensefulPick ) {
isSpinningSuspensefully = YES;
} else {
isSpinningSuspensefully = NO;
[chooseSound play];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
totalSpinCount = 0;
currentSpinCount = 0;
spinInterval = 3;
//NSLog(@"pick");
}
float offset = 0;
float radius = 35.;
-(void) drawPointsCircleAtPoint:(CGPoint)p withRadius:(float) r {
int numPoints = 20;
GLfloat circle[numPoints*2];
float step = M_PI * 2. / numPoints;
int t=0;
for ( int i = 0; i < numPoints; i++ ) {
circle[t++] = p.x + r * cosf( offset + i*step );
circle[t++] = p.y + r * sinf( offset + i*step );
}
float rad = (self.frame.size.width / 35.5);
glPointSize(rad);
glVertexPointer(2, GL_FLOAT, 0, circle);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_POINTS, 0, numPoints);
}
-(void) drawPointsCircleAtPoint2:(CGPoint)p withRadius:(float) r {
int numPoints = 40;
GLfloat circle[numPoints*2];
float step = M_PI * 2. / numPoints;
int t=0;
for ( int i = 0; i < numPoints; i++ ) {
circle[t++] = p.x + r * cosf( offset + i*step );
circle[t++] = p.y + r * sinf( offset + i*step );
}
glPointSize(3.0);
glVertexPointer(2, GL_FLOAT, 0, circle);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_POINTS, 0, numPoints);
}
-(void) drawCircleAtPoint:(CGPoint)p withRadius:(float) r {
int numPoints = 20;
GLfloat circle[numPoints*2];
float step = M_PI * 2. / numPoints;
int t=0;
for ( int i = 0; i < numPoints; i++ ) {
circle[t++] = p.x + r * cosf( i*step );
circle[t++] = p.y + r * sinf( i*step );
}
glPointSize(5.0);
glVertexPointer(2, GL_FLOAT, 0, circle);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLE_FAN, 0, numPoints);
}
float stepper;
- (void)drawView {
// Replace the implementation of this method to do your own custom drawing
stepper += 0.2;
offset+=0.06;
//radius = 41. + 10. * sinf(offset*1.1);
radius = (self.frame.size.width/7.8) + 10. * sinf(offset*1.1);
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrthof(0.0f, 320.0f, 0.0f, 480.0f, -1.0f, 1.0f);
glOrthof(0.0f, self.frame.size.width, 0.0f, self.frame.size.height, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
//glRotatef(3.0f, 0.0f, 0.0f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//[bgTexture drawInRect:[UIScreen mainScreen].bounds];
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
//glPointSize(70.0);
glColor4f(0.2, 0.5, 0.95 , 1.0);
NSArray * ts = [touchesSet allObjects];
int rr = 0;
if ( isSpinningSuspensefully ) {
if (currentSpinCount > spinInterval) {
int total = [touchesSet count];
currentFinger++;
if ( currentFinger >= total ) {
currentFinger = 0;
}
currentSpinCount = 0 ;
spinInterval = spinInterval * 1.2;
[tickSound play];
}
currentSpinCount ++;
totalSpinCount ++;
if ( spinInterval > 30.0 && currentFinger == finger ) {
//if ( totalSpinCount > 200 && currentFinger == finger ) {
isSpinningSuspensefully = NO;
currentFinger = finger;
[chooseSound play];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
} else {
currentFinger = finger;
}
for ( UITouch * t in ts ) {
CGPoint p = [t locationInView:self];
p.y = self.frame.size.height - p.y;
if ( isPicking && rr == currentFinger ) {
float alpha = 0.0;
for ( float u=4; u>0; u-=0.15 ) {
float r1;
float r2;
if ( ( (int)round(stepper) ) % 2 == 0 ) {
r1 = 0.95;
r2 = 0.4;
} else {
r2 = 0.95;
r1 = 0.4;
}
if ( isSpinningSuspensefully ) {
//glColor4f( r1 , 132./255., 132./255. , alpha );
glColor4f( 1.0 , 0.7, 0.0 , alpha/4 );
} else {
glColor4f( r1 , 32./255., 32./255. , alpha );
}
[self drawCircleAtPoint:p withRadius: radius*u ];
u-=0.15;
if ( isSpinningSuspensefully ) {
glColor4f( 1.0 , 0.6, 0.0 , alpha/4 );
} else {
glColor4f( r2 ,0,0 , alpha );
}
[self drawCircleAtPoint:p withRadius: radius*u ];
alpha += 0.1;
}
} else {
glColor4f( 0,0,0 , 1.0);
[self drawCircleAtPoint:p withRadius: radius+5 ];
glColor4f(38./255., 194./255., 49./255. , 1.0);
[self drawCircleAtPoint:p withRadius: radius ];
}
if ( isPicking && rr == currentFinger ) {
//glColor4f( 255. , 186./255., 0 , 0.8);
//[self drawPointsCircleAtPoint:p withRadius:70];
} else {
float rad = (self.frame.size.width / 4.5);
glColor4f( 86./255. , 240./255., 97./255. , 0.8);
[self drawPointsCircleAtPoint:p withRadius:rad];
}
//if ( isPicking && rr == finger ) {
if ( isPicking && rr == currentFinger ) {
//glColor4f( 1. , 0.0 , 0.0 , 0.4);
//[self drawPointsCircleAtPoint2:p withRadius:65];
} else {
float rad = (self.frame.size.width / 4.92);
glColor4f( 86./255. , 240./255., 97./255. , 0.4);
[self drawPointsCircleAtPoint2:p withRadius:rad];
}
rr ++;
}
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
#pragma mark Touches ....
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[clickSound play];
isPicking = NO;
isSpinningSuspensefully = NO;
if ( hasExceededTouches ) {
[touchesSet removeAllObjects];
hasExceededTouches = NO;
}
//UITouch * touch = [touches anyObject];
//CGPoint pos = [touch locationInView:self];
//touchesSet = [touchesSet unionSet:touches];
//touchesSet = [touches copy];
//[touchesSet removeAllObjects];
[touchesSet unionSet:touches];
NSLog(@"Touches began ");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//if ( [[event allTouches] count] != 1 ) return;
//touchesSet = [touches copy];
//[touchesSet removeAllObjects];
//[touchesSet unionSet:touches];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches ended ");
isPicking = NO;
isSpinningSuspensefully = NO;
[touchesSet minusSet:touches];
[touchUpSound play];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches cancelled %i " , [touches count] );
if ( [touches count] == 5 ) {
hasExceededTouches = YES;
[self pickClicked];
}
/*
if ( [touches count] == 5 ) {
if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"hasMaxTouched"] ) {
[self pickClicked];
} else {
[self showMaxTouchAlert];
}
}
//touchesSet = [touches copy];
isPicking = NO;
[touchesSet minusSet:touches];
*/
}
-(void) showMaxTouchAlert {
UIAlertView * al = [[UIAlertView alloc] initWithTitle:@"Maximum Fingers" message:@"Sorry, the iphone only supports 5 touches, so if have 5 fingers down, your next click will choose the odd finger no matter where you touch.\n\n(This only displays once.)" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[al show];
[al release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasMaxTouched"];
}
- (void)layoutSubviews {
[EAGLContext setCurrentContext:context];
[self destroyFramebuffer];
[self createFramebuffer];
[self drawView];
}
- (BOOL)createFramebuffer {
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
if (USE_DEPTH_BUFFER) {
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
}
if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
return NO;
}
return YES;
}
- (void)destroyFramebuffer {
glDeleteFramebuffersOES(1, &viewFramebuffer);
viewFramebuffer = 0;
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;
if(depthRenderbuffer) {
glDeleteRenderbuffersOES(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
}
- (void)startAnimation {
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
}
- (void)stopAnimation {
self.animationTimer = nil;
}
- (void)setAnimationTimer:(NSTimer *)newTimer {
[animationTimer invalidate];
animationTimer = newTimer;
}
- (void)setAnimationInterval:(NSTimeInterval)interval {
animationInterval = interval;
if (animationTimer) {
[self stopAnimation];
[self startAnimation];
}
}
- (void)dealloc {
[self stopAnimation];
if ([EAGLContext currentContext] == context) {
[EAGLContext setCurrentContext:nil];
}
[context release];
[super dealloc];
}
@end