sgl

view src/wsys_ios.m @ 39:e1a27aa24956

fixed broken build
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 27 Jun 2012 05:54:57 +0300
parents b3374e30361c
children f7de32814f34
line source
1 /* SimplyGL window system module for iOS */
2 /* mac-framework: -framework UIKit */
4 #include "config.h"
6 #ifdef USE_WSYS_MODULE_IOS
8 #include <assert.h>
9 #import <UIKit/UIKit.h>
10 #import <QuartzCore/QuartzCore.h>
11 #include <OpenGLES/ES2/gl.h>
12 #include <OpenGLES/ES2/glext.h>
14 @interface SGLView : UIView {
15 @private
16 BOOL active;
17 BOOL use_disp_link;
18 NSInteger frame_interval;
19 id disp_link;
20 NSTimer *anim_timer;
21 EAGLContext *ctx;
22 unsigned int fbo, rbuf_color, rbuf_depth;
23 }
25 @property (readonly, nonatomic, getter=isAnimating) BOOL active;
26 @property (nonatomic) NSInteger frame_interval;
28 -(void)start_anim;
29 -(void)stop_anim;
30 -(void)drawView: (id)sender;
31 @end
33 @interface SGLDelegate : NSObject <UIApplicationDelegate> {
34 UIWindow *win;
35 SGLView *view;
36 }
38 @property (nonatomic, retain) IBOutlet UIWindow *win;
39 @property (nonatomic, retain) IBOutlet SGLView *view;
40 @end
44 static int init(void);
45 static void shutdown(void);
47 /* video mode switching */
48 static int set_vidmode(int xsz, int ysz);
49 static int get_vidmode(int *xsz, int *ysz);
51 /* create/destroy windows */
52 static int create_window(int xsz, int ysz, unsigned int flags);
53 static void close_window(int wid);
55 /* window management */
56 static int set_active(int wid);
57 static int set_title(const char *str);
58 static void redisplay(void);
59 static void swap_buffers(void);
61 static int get_modifiers(void);
63 /* event handling and friends */
64 static void set_event(int idx, int enable);
65 static int process_events(void);
68 static struct wsys_module ws = {
69 "ios", 0,
70 init,
71 shutdown,
72 set_vidmode,
73 get_vidmode,
74 create_window,
75 close_window,
76 set_active,
77 set_title,
78 redisplay,
79 swap_buffers,
80 get_modifiers,
81 set_event,
82 process_events,
83 0
84 };
87 void sgl_register_ios(void)
88 {
89 sgl_register_module(&ws);
90 }
93 @implementation SGLView
95 @synthesize active;
96 @dynamic frame_interval;
98 +(Class)layerClass
99 {
100 return [CAEAGLLayer class];
101 }
103 /*The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: */
104 -(id)initWithCoder: (NSCoder*)coder
105 {
106 if((self = [super initWithCoder: coder])) {
107 /* Get the layer */
108 CAEAGLLayer *layer = (CAEAGLLayer*)self.layer;
110 /*self.contentScaleFactor = 2.0; */
112 layer.opaque = TRUE;
113 layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
114 [NSNumber numberWithBool: FALSE],
115 kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8,
116 kEAGLDrawablePropertyColorFormat,
117 nil];
119 ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
120 if(!ctx || ![EAGLContext setCurrentContext: ctx]) {
121 [self release];
122 return nil;
123 }
125 /* initialize fbos etc... */
126 glGenFramebuffers(1, &fbo);
127 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
129 glGenRenderbuffers(1, &rbuf_color);
130 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
131 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
133 glGenRenderbuffers(1, &rbuf_depth);
134 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
135 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
136 assert(glGetError() == GL_NO_ERROR);
138 active = FALSE;
139 use_disp_link = FALSE;
140 frame_interval = 1;
141 disp_link = nil;
142 anim_timer = nil;
144 /* A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer */
145 /* class is used as fallback when it isn't available. */
146 NSString *req_ver = @"3.1";
147 NSString *cur_ver = [[UIDevice currentDevice] systemVersion];
148 if([cur_ver compare: req_ver options: NSNumericSearch] != NSOrderedAscending) {
149 use_disp_link = TRUE;
150 }
152 /*self.multipleTouchEnabled = 1; */
154 /* TODO call user init ? */
155 }
157 return self;
158 }
160 -(void)drawView: (id)sender
161 {
162 /* TODO call display */
163 }
165 -(void)layoutSubviews
166 {
167 /* TODO call reshape */
168 /* XXX originally call to renderer resizeFromLayer */
169 /* XXX originally call to [self drawView: nil] */
170 }
172 -(void)setAnimationFrameInterval: (NSInteger)interval
173 {
174 /* Frame interval defines how many display frames must pass
175 * between each time the display link fires. The display
176 * link will only fire 30 times a second when the frame
177 * internal is two on a display that refreshes 60 times a
178 * second. The default frame interval setting of one will
179 * fire 60 times a second when the display refreshes at 60
180 * times a second. A frame interval setting of less than
181 * one results in undefined behavior.
182 */
183 if(interval >= 1) {
184 frame_interval = frame_interval;
186 if(active)
187 {
188 [self stop_anim];
189 [self start_anim];
190 }
191 }
192 }
194 -(void)start_anim
195 {
196 if(!active) {
197 if(use_disp_link) {
198 /* CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
199 * if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
200 * not be called in system versions earlier than 3.1.
201 */
203 disp_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self
204 selector: @selector(drawView:)];
205 [disp_link setFrameInterval: frame_interval];
206 [disp_link addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
207 } else {
208 anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * frame_interval)
209 target: self selector: @selector(drawView:) userInfo: nil
210 repeats: TRUE];
211 }
213 active = TRUE;
214 }
215 }
217 - (void)stop_anim
218 {
219 if(active) {
220 if(use_disp_link) {
221 [disp_link invalidate];
222 disp_link = nil;
223 } else {
224 [anim_timer invalidate];
225 anim_timer = nil;
226 }
228 active = FALSE;
229 }
230 }
232 -(void)dealloc
233 {
234 /* XXX originally [renderer release]; */
235 [super dealloc];
236 }
237 @end
239 @implementation SGLDelegate
241 @synthesize win;
242 @synthesize view;
244 -(BOOL)application: (UIApplication*)app didFinishLaunchingWithOptions: (NSDictionary*)opt
245 {
246 [view start_anim];
247 return YES;
248 }
250 -(void)applicationWillResignActive: (UIApplication*)app
251 {
252 [view stop_anim];
253 }
255 -(void)applicationDidBecomeActive: (UIApplication*)app
256 {
257 [view start_anim];
258 }
260 -(void)applicationWillTerminate: (UIApplication*)app
261 {
262 [view stop_anim];
263 }
265 -(void)dealloc
266 {
267 [win release];
268 [view release];
269 [super dealloc];
270 }
271 @end
275 static int init(void)
276 {
277 return -1;
278 }
280 static void shutdown(void)
281 {
282 }
284 /* video mode switching */
285 static int set_vidmode(int xsz, int ysz)
286 {
287 return -1;
288 }
290 static int get_vidmode(int *xsz, int *ysz)
291 {
292 return -1;
293 }
295 /* create/destroy windows */
296 static int create_window(int xsz, int ysz, unsigned int flags)
297 {
298 return -1;
299 }
301 static void close_window(int wid)
302 {
303 }
305 /* window management */
306 static int set_active(int wid)
307 {
308 return -1;
309 }
311 static int set_title(const char *str)
312 {
313 return -1;
314 }
316 static void redisplay(void)
317 {
318 }
320 static void swap_buffers(void)
321 {
322 }
324 static int get_modifiers(void)
325 {
326 return 0;
327 }
329 /* event handling and friends */
330 static void set_event(int idx, int enable)
331 {
332 }
334 static int process_events(void)
335 {
336 return -1;
337 }
343 /* only compile the following if we're building in xcode and we
344 * don't have generated modules.c file
345 */
346 #ifdef XCODE_BUILD
347 @interface sgl : NSObject
348 /* XXX no need for init yet... */
349 @end
351 @implementation sgl
353 - (id)init
354 {
355 self = [super init];
356 if(self) {
357 /* XXX Initialization code here. */
358 }
359 return self;
360 }
362 @end
364 void sgl_modules_init(void)
365 {
366 sgl_register_uikit();
367 }
369 #endif /* XCODE_BUILD */
372 #else
373 int sgl_wsys_ios_silence_the_fucking_empty_file_warnings;
374 #endif /* USE_WSYS_MODULE_IOS */