sgl

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