sgl

view src/wsys_ios.m @ 37:b3374e30361c

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