ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / Examples / iPhone / FreeFlight / Classes / EAGLView.m
1 //
2 //  EAGLView.m
3 //  FreeFlight
4 //
5 //  Created by Frédéric D'HAEYER on 16/10/09.
6 //  Copyright Parrot SA 2009. All rights reserved.
7 //
8 #import "EAGLView.h"
9 #import "ES1Renderer.h"
10
11 @implementation EAGLView
12 @synthesize animating;
13 @dynamic animationFrameInterval;
14
15 // You must implement this method
16 + (Class) layerClass
17 {
18     return [CAEAGLLayer class];
19 }
20
21 //The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
22 //- (id) initWithCoder:(NSCoder*)coder
23 //{
24 //    if ((self = [super initWithCoder:coder]))
25 - (id)initWithFrame:(CGRect)frame
26 {
27         if ((self = [super initWithFrame: frame]))
28         {
29         // Get the layer
30         CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
31
32         eaglLayer.opaque = NO;
33         eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
34                                         [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
35                 
36                 drone = nil;
37                 renderer = [[ES1Renderer alloc] init];
38                 
39                 if (!renderer)
40                 {
41                         [self release];
42                         return nil;
43                 }
44         
45                 animating = FALSE;
46                 animationFrameInterval = 2; // is 2 * (1 / 60) = 1 / 30 <=> 30 fps 
47                 animationTimer = nil;
48         }
49         
50     return self;
51 }
52
53 - (void) setDrone:(ARDrone*)_drone
54 {
55         drone = _drone;
56 }
57
58 - (void) drawView
59 {
60         [renderer render:drone];
61 }
62
63 - (void) layoutSubviews
64 {
65         [renderer resizeFromLayer:(CAEAGLLayer*)self.layer];
66     [self drawView];
67 }
68
69 - (NSInteger) animationFrameInterval
70 {
71         return animationFrameInterval;
72 }
73
74 - (void)changeState:(BOOL)inGame
75 {
76         if(inGame)
77         {
78                 self.hidden = NO;
79                 if(!animating)
80                 {
81                         animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView) userInfo:nil repeats:TRUE];
82                         animating = TRUE;
83                 }
84         }
85         else
86         {
87                 self.hidden = YES;
88                 if (animating)
89                 {
90                         [animationTimer invalidate];
91                         animationTimer = nil;
92                         animating = FALSE;
93                 }
94         }
95 }
96
97 - (void) dealloc
98 {
99     [renderer release];
100         
101     [super dealloc];
102 }
103
104 @end