nuclear@36: #include nuclear@36: #import nuclear@37: #import nuclear@36: #include nuclear@36: #include nuclear@36: nuclear@37: @interface SGLView : UIView { nuclear@36: @private nuclear@36: BOOL active; nuclear@36: BOOL use_disp_link; nuclear@36: NSInteger frame_interval; nuclear@36: id disp_link; nuclear@36: NSTimer *anim_timer; nuclear@36: EAGLContext *ctx; nuclear@36: unsigned int fbo, rbuf_color, rbuf_depth; nuclear@36: } nuclear@36: nuclear@37: @property (readonly, nonatomic, getter=isAnimating) BOOL active; nuclear@37: @property (nonatomic) NSInteger frame_interval; nuclear@37: nuclear@36: -(void)start_anim; nuclear@36: -(void)stop_anim; nuclear@37: -(void)drawView: (id)sender; nuclear@36: @end nuclear@36: nuclear@36: @interface SGLDelegate : NSObject { nuclear@36: UIWindow *win; nuclear@37: SGLView *view; nuclear@36: } nuclear@37: nuclear@37: @property (nonatomic, retain) IBOutlet UIWindow *win; nuclear@37: @property (nonatomic, retain) IBOutlet SGLView *view; nuclear@36: @end nuclear@36: nuclear@36: nuclear@36: nuclear@37: @implementation SGLView nuclear@36: nuclear@37: @synthesize active; nuclear@36: @dynamic frame_interval; nuclear@36: nuclear@36: +(Class)layerClass nuclear@36: { nuclear@36: return [CAEAGLLayer class]; nuclear@36: } nuclear@36: nuclear@36: //The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: nuclear@36: -(id)initWithCoder: (NSCoder*)coder nuclear@36: { nuclear@36: if((self = [super initWithCoder: coder])) { nuclear@36: // Get the layer nuclear@36: CAEAGLLayer *layer = (CAEAGLLayer*)self.layer; nuclear@36: nuclear@36: //self.contentScaleFactor = 2.0; nuclear@36: nuclear@36: layer.opaque = TRUE; nuclear@36: layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: nuclear@36: [NSNumber numberWithBool: FALSE], nuclear@36: kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, nuclear@36: kEAGLDrawablePropertyColorFormat, nuclear@36: nil]; nuclear@36: nuclear@36: ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2]; nuclear@36: if(!ctx || ![EAGLContext setCurrentContext: ctx]) { nuclear@36: [self release]; nuclear@36: return nil; nuclear@36: } nuclear@36: nuclear@36: // initialize fbos etc... nuclear@36: glGenFramebuffers(1, &fbo); nuclear@36: glBindFramebuffer(GL_FRAMEBUFFER, fbo); nuclear@36: nuclear@36: glGenRenderbuffers(1, &rbuf_color); nuclear@36: glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color); nuclear@36: glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color); nuclear@36: nuclear@36: glGenRenderbuffers(1, &rbuf_depth); nuclear@36: glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth); nuclear@36: glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth); nuclear@36: assert(glGetError() == GL_NO_ERROR); nuclear@36: nuclear@37: active = FALSE; nuclear@36: use_disp_link = FALSE; nuclear@36: frame_interval = 1; nuclear@36: disp_link = nil; nuclear@36: anim_timer = nil; nuclear@36: nuclear@36: // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer nuclear@36: // class is used as fallback when it isn't available. nuclear@36: NSString *req_ver = @"3.1"; nuclear@36: NSString *cur_ver = [[UIDevice currentDevice] systemVersion]; nuclear@37: if([cur_ver compare: req_ver options: NSNumericSearch] != NSOrderedAscending) { nuclear@36: use_disp_link = TRUE; nuclear@36: } nuclear@36: nuclear@36: //self.multipleTouchEnabled = 1; nuclear@36: nuclear@36: // TODO call user init ? nuclear@36: } nuclear@36: nuclear@36: return self; nuclear@36: } nuclear@36: nuclear@36: -(void)drawView: (id)sender nuclear@36: { nuclear@36: // TODO call display nuclear@36: } nuclear@36: nuclear@36: -(void)layoutSubviews nuclear@36: { nuclear@36: // TODO call reshape nuclear@36: // XXX originally call to renderer resizeFromLayer nuclear@36: // XXX originally call to [self drawView: nil] nuclear@36: } nuclear@36: nuclear@36: -(void)setAnimationFrameInterval: (NSInteger)interval nuclear@36: { nuclear@36: /* Frame interval defines how many display frames must pass nuclear@36: * between each time the display link fires. The display nuclear@36: * link will only fire 30 times a second when the frame nuclear@36: * internal is two on a display that refreshes 60 times a nuclear@36: * second. The default frame interval setting of one will nuclear@36: * fire 60 times a second when the display refreshes at 60 nuclear@36: * times a second. A frame interval setting of less than nuclear@36: * one results in undefined behavior. nuclear@36: */ nuclear@36: if(interval >= 1) { nuclear@36: frame_interval = frame_interval; nuclear@36: nuclear@36: if(active) nuclear@36: { nuclear@37: [self stop_anim]; nuclear@37: [self start_anim]; nuclear@36: } nuclear@36: } nuclear@36: } nuclear@36: nuclear@37: -(void)start_anim nuclear@36: { nuclear@36: if(!active) { nuclear@36: if(use_disp_link) { nuclear@36: // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed nuclear@36: // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will nuclear@36: // not be called in system versions earlier than 3.1. nuclear@36: nuclear@36: disp_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self nuclear@36: selector: @selector(drawView:)]; nuclear@37: [disp_link setFrameInterval: frame_interval]; nuclear@37: [disp_link addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode]; nuclear@36: } else { nuclear@36: anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * frame_interval) nuclear@36: target: self selector: @selector(drawView:) userInfo: nil nuclear@36: repeats: TRUE]; nuclear@36: } nuclear@36: nuclear@36: active = TRUE; nuclear@36: } nuclear@36: } nuclear@36: nuclear@37: - (void)stop_anim nuclear@36: { nuclear@36: if(active) { nuclear@36: if(use_disp_link) { nuclear@36: [disp_link invalidate]; nuclear@36: disp_link = nil; nuclear@36: } else { nuclear@36: [anim_timer invalidate]; nuclear@36: anim_timer = nil; nuclear@36: } nuclear@36: nuclear@36: active = FALSE; nuclear@36: } nuclear@36: } nuclear@36: nuclear@36: -(void)dealloc nuclear@36: { nuclear@36: // XXX originally [renderer release]; nuclear@36: [super dealloc]; nuclear@36: } nuclear@36: @end nuclear@36: nuclear@36: @implementation SGLDelegate nuclear@36: nuclear@36: @synthesize win; nuclear@36: @synthesize view; nuclear@36: nuclear@36: -(BOOL)application: (UIApplication*)app didFinishLaunchingWithOptions: (NSDictionary*)opt nuclear@36: { nuclear@37: [view start_anim]; nuclear@36: return YES; nuclear@36: } nuclear@36: nuclear@36: -(void)applicationWillResignActive: (UIApplication*)app nuclear@36: { nuclear@37: [view stop_anim]; nuclear@36: } nuclear@36: nuclear@36: -(void)applicationDidBecomeActive: (UIApplication*)app nuclear@36: { nuclear@37: [view start_anim]; nuclear@36: } nuclear@36: nuclear@36: -(void)applicationWillTerminate: (UIApplication*)app nuclear@36: { nuclear@37: [view stop_anim]; nuclear@36: } nuclear@36: nuclear@36: -(void)dealloc nuclear@36: { nuclear@36: [win release]; nuclear@36: [view release]; nuclear@36: [super dealloc]; nuclear@36: } nuclear@36: @end