sgl

view src/wsys_ios.m @ 40:f7de32814f34

sortof made the ios backend to compile and run ... sortof
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 28 Jun 2012 03:42:26 +0300
parents e1a27aa24956
children
line source
1 /* SimplyGL window system module for iOS */
2 /* mac-framework: -framework UIKit */
4 #ifdef USE_WSYS_MODULE_IOS
6 #include <assert.h>
7 #import <UIKit/UIKit.h>
8 #import <QuartzCore/QuartzCore.h>
9 #include <OpenGLES/ES2/gl.h>
10 #include <OpenGLES/ES2/glext.h>
11 #include "sgl.h"
12 #include "wsys.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 }
39 @property (nonatomic, retain) IBOutlet UIWindow *win;
40 @property (nonatomic, retain) IBOutlet SGLView *view;
41 @end
45 static int init(void);
46 static void shutdown(void);
48 /* video mode switching */
49 static int set_vidmode(int xsz, int ysz);
50 static int get_vidmode(int *xsz, int *ysz);
52 /* create/destroy windows */
53 static int create_window(int xsz, int ysz, unsigned int flags);
54 static void close_window(int wid);
56 /* window management */
57 static int set_active(int wid);
58 static int set_title(const char *str);
59 static void redisplay(void);
60 static void swap_buffers(void);
62 static int get_modifiers(void);
64 /* event handling and friends */
65 static void set_event(int idx, int enable);
66 static int process_events(void);
69 static struct wsys_module ws = {
70 "ios", 0,
71 init,
72 shutdown,
73 set_vidmode,
74 get_vidmode,
75 create_window,
76 close_window,
77 set_active,
78 set_title,
79 redisplay,
80 swap_buffers,
81 get_modifiers,
82 set_event,
83 process_events,
84 0
85 };
88 void sgl_register_ios(void)
89 {
90 sgl_register_module(&ws);
91 }
94 @implementation SGLView
96 @synthesize active;
97 @dynamic frame_interval;
99 +(Class)layerClass
100 {
101 return [CAEAGLLayer class];
102 }
104 /*The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: */
105 -(id)initWithCoder: (NSCoder*)coder
106 {
107 if((self = [super initWithCoder: coder])) {
108 /* Get the layer */
109 CAEAGLLayer *layer = (CAEAGLLayer*)self.layer;
111 /*self.contentScaleFactor = 2.0; */
113 layer.opaque = TRUE;
114 layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
115 [NSNumber numberWithBool: FALSE],
116 kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8,
117 kEAGLDrawablePropertyColorFormat,
118 nil];
120 ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
121 if(!ctx || ![EAGLContext setCurrentContext: ctx]) {
122 [self release];
123 return nil;
124 }
126 /* initialize fbos etc... */
127 glGenFramebuffers(1, &fbo);
128 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
130 glGenRenderbuffers(1, &rbuf_color);
131 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
132 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
134 glGenRenderbuffers(1, &rbuf_depth);
135 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
136 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
137 assert(glGetError() == GL_NO_ERROR);
139 active = FALSE;
140 use_disp_link = FALSE;
141 frame_interval = 1;
142 disp_link = nil;
143 anim_timer = nil;
145 /* A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer */
146 /* class is used as fallback when it isn't available. */
147 NSString *req_ver = @"3.1";
148 NSString *cur_ver = [[UIDevice currentDevice] systemVersion];
149 if([cur_ver compare: req_ver options: NSNumericSearch] != NSOrderedAscending) {
150 use_disp_link = TRUE;
151 }
153 /*self.multipleTouchEnabled = 1; */
155 /* TODO call user init ? */
156 }
158 return self;
159 }
161 -(void)drawView: (id)sender
162 {
163 /* TODO call display */
164 }
166 -(void)layoutSubviews
167 {
168 /* TODO call reshape */
169 /* XXX originally call to renderer resizeFromLayer */
170 /* XXX originally call to [self drawView: nil] */
171 }
173 -(void)setAnimationFrameInterval: (NSInteger)interval
174 {
175 /* Frame interval defines how many display frames must pass
176 * between each time the display link fires. The display
177 * link will only fire 30 times a second when the frame
178 * internal is two on a display that refreshes 60 times a
179 * second. The default frame interval setting of one will
180 * fire 60 times a second when the display refreshes at 60
181 * times a second. A frame interval setting of less than
182 * one results in undefined behavior.
183 */
184 if(interval >= 1) {
185 frame_interval = frame_interval;
187 if(active)
188 {
189 [self stop_anim];
190 [self start_anim];
191 }
192 }
193 }
195 -(void)start_anim
196 {
197 if(!active) {
198 if(use_disp_link) {
199 /* CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
200 * if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
201 * not be called in system versions earlier than 3.1.
202 */
204 disp_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self
205 selector: @selector(drawView:)];
206 [disp_link setFrameInterval: frame_interval];
207 [disp_link addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
208 } else {
209 anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * frame_interval)
210 target: self selector: @selector(drawView:) userInfo: nil
211 repeats: TRUE];
212 }
214 active = TRUE;
215 }
216 }
218 - (void)stop_anim
219 {
220 if(active) {
221 if(use_disp_link) {
222 [disp_link invalidate];
223 disp_link = nil;
224 } else {
225 [anim_timer invalidate];
226 anim_timer = nil;
227 }
229 active = FALSE;
230 }
231 }
233 -(void)dealloc
234 {
235 /* XXX originally [renderer release]; */
236 [super dealloc];
237 }
238 @end
240 @implementation SGLDelegate
242 @synthesize win;
243 @synthesize view;
245 -(BOOL)application: (UIApplication*)app didFinishLaunchingWithOptions: (NSDictionary*)opt
246 {
247 [view start_anim];
248 return YES;
249 }
251 -(void)applicationWillResignActive: (UIApplication*)app
252 {
253 [view stop_anim];
254 }
256 -(void)applicationDidBecomeActive: (UIApplication*)app
257 {
258 [view start_anim];
259 }
261 -(void)applicationWillTerminate: (UIApplication*)app
262 {
263 [view stop_anim];
264 }
266 -(void)dealloc
267 {
268 [win release];
269 [view release];
270 [super dealloc];
271 }
272 @end
276 static int init(void)
277 {
278 return -1;
279 }
281 static void shutdown(void)
282 {
283 }
285 /* video mode switching */
286 static int set_vidmode(int xsz, int ysz)
287 {
288 return -1;
289 }
291 static int get_vidmode(int *xsz, int *ysz)
292 {
293 return -1;
294 }
296 /* create/destroy windows */
297 static int create_window(int xsz, int ysz, unsigned int flags)
298 {
299 return -1;
300 }
302 static void close_window(int wid)
303 {
304 }
306 /* window management */
307 static int set_active(int wid)
308 {
309 return -1;
310 }
312 static int set_title(const char *str)
313 {
314 return -1;
315 }
317 static void redisplay(void)
318 {
319 }
321 static void swap_buffers(void)
322 {
323 }
325 static int get_modifiers(void)
326 {
327 return 0;
328 }
330 /* event handling and friends */
331 static void set_event(int idx, int enable)
332 {
333 }
335 static int process_events(void)
336 {
337 char *argv[] = {"foobar", 0};
338 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
340 int res = UIApplicationMain(1, argv, 0, @"SGLDelegate");
342 [pool release];
343 return res;
344 }
350 /* only compile the following if we're building in xcode and we
351 * don't have generated modules.c file
352 */
353 #ifdef XCODE_BUILD
354 @interface sgl : NSObject
355 /* XXX no need for init yet... */
356 @end
358 @implementation sgl
360 - (id)init
361 {
362 self = [super init];
363 if(self) {
364 /* XXX Initialization code here. */
365 }
366 return self;
367 }
369 @end
371 void sgl_modules_init(void)
372 {
373 sgl_register_ios();
374 }
376 #endif /* XCODE_BUILD */
379 #else
380 int sgl_wsys_ios_silence_the_fucking_empty_file_warnings;
381 #endif /* USE_WSYS_MODULE_IOS */