rev |
line source |
nuclear@36
|
1 #include <assert.h>
|
nuclear@36
|
2 #import <UIKit/UIKit.h>
|
nuclear@36
|
3 #include <OpenGLES/ES2/gl.h>
|
nuclear@36
|
4 #include <OpenGLES/ES2/glext.h>
|
nuclear@36
|
5
|
nuclear@36
|
6 @interface GLView : UIView {
|
nuclear@36
|
7 @private
|
nuclear@36
|
8 BOOL active;
|
nuclear@36
|
9 BOOL use_disp_link;
|
nuclear@36
|
10 NSInteger frame_interval;
|
nuclear@36
|
11 id disp_link;
|
nuclear@36
|
12 NSTimer *anim_timer;
|
nuclear@36
|
13 EAGLContext *ctx;
|
nuclear@36
|
14 unsigned int fbo, rbuf_color, rbuf_depth;
|
nuclear@36
|
15 }
|
nuclear@36
|
16
|
nuclear@36
|
17 -(void)start_anim;
|
nuclear@36
|
18 -(void)stop_anim;
|
nuclear@36
|
19 -(void)draw: (id)sender;
|
nuclear@36
|
20 @end
|
nuclear@36
|
21
|
nuclear@36
|
22 @interface SGLDelegate : NSObject <UIApplicationDelegate> {
|
nuclear@36
|
23 UIWindow *win;
|
nuclear@36
|
24 GLView *view;
|
nuclear@36
|
25 }
|
nuclear@36
|
26 @end
|
nuclear@36
|
27
|
nuclear@36
|
28
|
nuclear@36
|
29
|
nuclear@36
|
30 @implementation GLView
|
nuclear@36
|
31
|
nuclear@36
|
32 @synthesize animating;
|
nuclear@36
|
33 @dynamic frame_interval;
|
nuclear@36
|
34
|
nuclear@36
|
35 +(Class)layerClass
|
nuclear@36
|
36 {
|
nuclear@36
|
37 return [CAEAGLLayer class];
|
nuclear@36
|
38 }
|
nuclear@36
|
39
|
nuclear@36
|
40 //The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
|
nuclear@36
|
41 -(id)initWithCoder: (NSCoder*)coder
|
nuclear@36
|
42 {
|
nuclear@36
|
43 if((self = [super initWithCoder: coder])) {
|
nuclear@36
|
44 // Get the layer
|
nuclear@36
|
45 CAEAGLLayer *layer = (CAEAGLLayer*)self.layer;
|
nuclear@36
|
46
|
nuclear@36
|
47 //self.contentScaleFactor = 2.0;
|
nuclear@36
|
48
|
nuclear@36
|
49 layer.opaque = TRUE;
|
nuclear@36
|
50 layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
|
nuclear@36
|
51 [NSNumber numberWithBool: FALSE],
|
nuclear@36
|
52 kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8,
|
nuclear@36
|
53 kEAGLDrawablePropertyColorFormat,
|
nuclear@36
|
54 nil];
|
nuclear@36
|
55
|
nuclear@36
|
56 ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
|
nuclear@36
|
57 if(!ctx || ![EAGLContext setCurrentContext: ctx]) {
|
nuclear@36
|
58 [self release];
|
nuclear@36
|
59 return nil;
|
nuclear@36
|
60 }
|
nuclear@36
|
61
|
nuclear@36
|
62 // initialize fbos etc...
|
nuclear@36
|
63 glGenFramebuffers(1, &fbo);
|
nuclear@36
|
64 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@36
|
65
|
nuclear@36
|
66 glGenRenderbuffers(1, &rbuf_color);
|
nuclear@36
|
67 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
|
nuclear@36
|
68 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
|
nuclear@36
|
69
|
nuclear@36
|
70 glGenRenderbuffers(1, &rbuf_depth);
|
nuclear@36
|
71 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
|
nuclear@36
|
72 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
|
nuclear@36
|
73 assert(glGetError() == GL_NO_ERROR);
|
nuclear@36
|
74
|
nuclear@36
|
75 animating = FALSE;
|
nuclear@36
|
76 use_disp_link = FALSE;
|
nuclear@36
|
77 frame_interval = 1;
|
nuclear@36
|
78 disp_link = nil;
|
nuclear@36
|
79 anim_timer = nil;
|
nuclear@36
|
80
|
nuclear@36
|
81 // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
|
nuclear@36
|
82 // class is used as fallback when it isn't available.
|
nuclear@36
|
83 NSString *req_ver = @"3.1";
|
nuclear@36
|
84 NSString *cur_ver = [[UIDevice currentDevice] systemVersion];
|
nuclear@36
|
85 if([curr_ver compare: req_ver options: NSNumericSearch] != NSOrderedAscending) {
|
nuclear@36
|
86 use_disp_link = TRUE;
|
nuclear@36
|
87 }
|
nuclear@36
|
88
|
nuclear@36
|
89 //self.multipleTouchEnabled = 1;
|
nuclear@36
|
90
|
nuclear@36
|
91 // TODO call user init ?
|
nuclear@36
|
92 }
|
nuclear@36
|
93
|
nuclear@36
|
94 return self;
|
nuclear@36
|
95 }
|
nuclear@36
|
96
|
nuclear@36
|
97 -(void)drawView: (id)sender
|
nuclear@36
|
98 {
|
nuclear@36
|
99 // TODO call display
|
nuclear@36
|
100 }
|
nuclear@36
|
101
|
nuclear@36
|
102 -(void)layoutSubviews
|
nuclear@36
|
103 {
|
nuclear@36
|
104 // TODO call reshape
|
nuclear@36
|
105 // XXX originally call to renderer resizeFromLayer
|
nuclear@36
|
106 // XXX originally call to [self drawView: nil]
|
nuclear@36
|
107 }
|
nuclear@36
|
108
|
nuclear@36
|
109 -(void)setAnimationFrameInterval: (NSInteger)interval
|
nuclear@36
|
110 {
|
nuclear@36
|
111 /* Frame interval defines how many display frames must pass
|
nuclear@36
|
112 * between each time the display link fires. The display
|
nuclear@36
|
113 * link will only fire 30 times a second when the frame
|
nuclear@36
|
114 * internal is two on a display that refreshes 60 times a
|
nuclear@36
|
115 * second. The default frame interval setting of one will
|
nuclear@36
|
116 * fire 60 times a second when the display refreshes at 60
|
nuclear@36
|
117 * times a second. A frame interval setting of less than
|
nuclear@36
|
118 * one results in undefined behavior.
|
nuclear@36
|
119 */
|
nuclear@36
|
120 if(interval >= 1) {
|
nuclear@36
|
121 frame_interval = frame_interval;
|
nuclear@36
|
122
|
nuclear@36
|
123 if(active)
|
nuclear@36
|
124 {
|
nuclear@36
|
125 [self stopAnimation];
|
nuclear@36
|
126 [self startAnimation];
|
nuclear@36
|
127 }
|
nuclear@36
|
128 }
|
nuclear@36
|
129 }
|
nuclear@36
|
130
|
nuclear@36
|
131 -(void)startAnimation
|
nuclear@36
|
132 {
|
nuclear@36
|
133 if(!active) {
|
nuclear@36
|
134 if(use_disp_link) {
|
nuclear@36
|
135 // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
|
nuclear@36
|
136 // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
|
nuclear@36
|
137 // not be called in system versions earlier than 3.1.
|
nuclear@36
|
138
|
nuclear@36
|
139 disp_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self
|
nuclear@36
|
140 selector: @selector(drawView:)];
|
nuclear@36
|
141 [displayLink setFrameInterval: frame_interval];
|
nuclear@36
|
142 [displayLink addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
|
nuclear@36
|
143 } else {
|
nuclear@36
|
144 anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * frame_interval)
|
nuclear@36
|
145 target: self selector: @selector(drawView:) userInfo: nil
|
nuclear@36
|
146 repeats: TRUE];
|
nuclear@36
|
147 }
|
nuclear@36
|
148
|
nuclear@36
|
149 active = TRUE;
|
nuclear@36
|
150 }
|
nuclear@36
|
151 }
|
nuclear@36
|
152
|
nuclear@36
|
153 - (void)stopAnimation
|
nuclear@36
|
154 {
|
nuclear@36
|
155 if(active) {
|
nuclear@36
|
156 if(use_disp_link) {
|
nuclear@36
|
157 [disp_link invalidate];
|
nuclear@36
|
158 disp_link = nil;
|
nuclear@36
|
159 } else {
|
nuclear@36
|
160 [anim_timer invalidate];
|
nuclear@36
|
161 anim_timer = nil;
|
nuclear@36
|
162 }
|
nuclear@36
|
163
|
nuclear@36
|
164 active = FALSE;
|
nuclear@36
|
165 }
|
nuclear@36
|
166 }
|
nuclear@36
|
167
|
nuclear@36
|
168 -(void)dealloc
|
nuclear@36
|
169 {
|
nuclear@36
|
170 // XXX originally [renderer release];
|
nuclear@36
|
171 [super dealloc];
|
nuclear@36
|
172 }
|
nuclear@36
|
173 @end
|
nuclear@36
|
174
|
nuclear@36
|
175 @implementation SGLDelegate
|
nuclear@36
|
176
|
nuclear@36
|
177 @synthesize win;
|
nuclear@36
|
178 @synthesize view;
|
nuclear@36
|
179
|
nuclear@36
|
180 -(BOOL)application: (UIApplication*)app didFinishLaunchingWithOptions: (NSDictionary*)opt
|
nuclear@36
|
181 {
|
nuclear@36
|
182 [view startAnimation];
|
nuclear@36
|
183 return YES;
|
nuclear@36
|
184 }
|
nuclear@36
|
185
|
nuclear@36
|
186 -(void)applicationWillResignActive: (UIApplication*)app
|
nuclear@36
|
187 {
|
nuclear@36
|
188 [view stopAnimation];
|
nuclear@36
|
189 }
|
nuclear@36
|
190
|
nuclear@36
|
191 -(void)applicationDidBecomeActive: (UIApplication*)app
|
nuclear@36
|
192 {
|
nuclear@36
|
193 [view startAnimation];
|
nuclear@36
|
194 }
|
nuclear@36
|
195
|
nuclear@36
|
196 -(void)applicationWillTerminate: (UIApplication*)app
|
nuclear@36
|
197 {
|
nuclear@36
|
198 [view stopAnimation];
|
nuclear@36
|
199 }
|
nuclear@36
|
200
|
nuclear@36
|
201 -(void)dealloc
|
nuclear@36
|
202 {
|
nuclear@36
|
203 [win release];
|
nuclear@36
|
204 [view release];
|
nuclear@36
|
205 [super dealloc];
|
nuclear@36
|
206 }
|
nuclear@36
|
207 @end
|