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