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