sgl

changeset 36:af9d2e895594

started work on ios module
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 24 Feb 2012 08:49:29 +0200
parents 3d6ee9fb9ac1
children b3374e30361c
files src/wsys_ios.m
diffstat 1 files changed, 207 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/wsys_ios.m	Fri Feb 24 08:49:29 2012 +0200
     1.3 @@ -0,0 +1,207 @@
     1.4 +#include <assert.h>
     1.5 +#import <UIKit/UIKit.h>
     1.6 +#include <OpenGLES/ES2/gl.h>
     1.7 +#include <OpenGLES/ES2/glext.h>
     1.8 +
     1.9 +@interface GLView : UIView {
    1.10 +@private
    1.11 +	BOOL active;
    1.12 +	BOOL use_disp_link;
    1.13 +	NSInteger frame_interval;
    1.14 +	id disp_link;
    1.15 +	NSTimer *anim_timer;
    1.16 +	EAGLContext *ctx;
    1.17 +	unsigned int fbo, rbuf_color, rbuf_depth;
    1.18 +}
    1.19 +
    1.20 +-(void)start_anim;
    1.21 +-(void)stop_anim;
    1.22 +-(void)draw: (id)sender;
    1.23 +@end
    1.24 +
    1.25 +@interface SGLDelegate : NSObject <UIApplicationDelegate> {
    1.26 +	UIWindow *win;
    1.27 +	GLView *view;
    1.28 +}
    1.29 +@end
    1.30 +
    1.31 +
    1.32 +
    1.33 +@implementation GLView
    1.34 +
    1.35 +@synthesize animating;
    1.36 +@dynamic frame_interval;
    1.37 +
    1.38 ++(Class)layerClass
    1.39 +{
    1.40 +	return [CAEAGLLayer class];
    1.41 +}
    1.42 +
    1.43 +//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
    1.44 +-(id)initWithCoder: (NSCoder*)coder
    1.45 +{
    1.46 +	if((self = [super initWithCoder: coder])) {
    1.47 +		// Get the layer
    1.48 +		CAEAGLLayer *layer = (CAEAGLLayer*)self.layer;
    1.49 +
    1.50 +		//self.contentScaleFactor = 2.0;
    1.51 +
    1.52 +		layer.opaque = TRUE;
    1.53 +		layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    1.54 +			[NSNumber numberWithBool: FALSE],
    1.55 +			kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8,
    1.56 +			kEAGLDrawablePropertyColorFormat,
    1.57 +			nil];
    1.58 +
    1.59 +		ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
    1.60 +		if(!ctx || ![EAGLContext setCurrentContext: ctx]) {
    1.61 +			[self release];
    1.62 +			return nil;
    1.63 +		}
    1.64 +
    1.65 +		// initialize fbos etc...
    1.66 +		glGenFramebuffers(1, &fbo);
    1.67 +		glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    1.68 +
    1.69 +		glGenRenderbuffers(1, &rbuf_color);
    1.70 +		glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
    1.71 +		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
    1.72 +
    1.73 +		glGenRenderbuffers(1, &rbuf_depth);
    1.74 +		glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
    1.75 +		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
    1.76 +		assert(glGetError() == GL_NO_ERROR);
    1.77 +
    1.78 +		animating = FALSE;
    1.79 +		use_disp_link = FALSE;
    1.80 +		frame_interval = 1;
    1.81 +		disp_link = nil;
    1.82 +		anim_timer = nil;
    1.83 +
    1.84 +		// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
    1.85 +		// class is used as fallback when it isn't available.
    1.86 +		NSString *req_ver = @"3.1";
    1.87 +		NSString *cur_ver = [[UIDevice currentDevice] systemVersion];
    1.88 +		if([curr_ver compare: req_ver options: NSNumericSearch] != NSOrderedAscending) {
    1.89 +			use_disp_link = TRUE;
    1.90 +		}
    1.91 +
    1.92 +		//self.multipleTouchEnabled = 1;
    1.93 +
    1.94 +		// TODO call user init ?
    1.95 +	}
    1.96 +
    1.97 +	return self;
    1.98 +}
    1.99 +
   1.100 +-(void)drawView: (id)sender
   1.101 +{
   1.102 +	// TODO call display
   1.103 +}
   1.104 +
   1.105 +-(void)layoutSubviews
   1.106 +{
   1.107 +	// TODO call reshape
   1.108 +	// XXX originally call to renderer resizeFromLayer
   1.109 +	// XXX originally call to [self drawView: nil]
   1.110 +}
   1.111 +
   1.112 +-(void)setAnimationFrameInterval: (NSInteger)interval
   1.113 +{
   1.114 +	/* Frame interval defines how many display frames must pass
   1.115 +	 * between each time the display link fires. The display
   1.116 +	 * link will only fire 30 times a second when the frame
   1.117 +	 * internal is two on a display that refreshes 60 times a
   1.118 +	 * second. The default frame interval setting of one will
   1.119 +	 * fire 60 times a second when the display refreshes at 60
   1.120 +	 * times a second. A frame interval setting of less than
   1.121 +	 * one results in undefined behavior.
   1.122 +	 */
   1.123 +	if(interval >= 1) {
   1.124 +		frame_interval = frame_interval;
   1.125 +
   1.126 +		if(active)
   1.127 +		{
   1.128 +			[self stopAnimation];
   1.129 +			[self startAnimation];
   1.130 +		}
   1.131 +	}
   1.132 +}
   1.133 +
   1.134 +-(void)startAnimation
   1.135 +{
   1.136 +	if(!active) {
   1.137 +		if(use_disp_link) {
   1.138 +			// CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
   1.139 +			// if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
   1.140 +			// not be called in system versions earlier than 3.1.
   1.141 +
   1.142 +			disp_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self
   1.143 +				selector: @selector(drawView:)];
   1.144 +			[displayLink setFrameInterval: frame_interval];
   1.145 +			[displayLink addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
   1.146 +		} else {
   1.147 +			anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * frame_interval)
   1.148 +				target: self selector: @selector(drawView:) userInfo: nil
   1.149 +				repeats: TRUE];
   1.150 +		}
   1.151 +
   1.152 +		active = TRUE;
   1.153 +	}
   1.154 +}
   1.155 +
   1.156 +- (void)stopAnimation
   1.157 +{
   1.158 +	if(active) {
   1.159 +		if(use_disp_link) {
   1.160 +			[disp_link invalidate];
   1.161 +			disp_link = nil;
   1.162 +		} else {
   1.163 +			[anim_timer invalidate];
   1.164 +			anim_timer = nil;
   1.165 +		}
   1.166 +
   1.167 +		active = FALSE;
   1.168 +	}
   1.169 +}
   1.170 +
   1.171 +-(void)dealloc
   1.172 +{
   1.173 +	// XXX originally [renderer release];
   1.174 +	[super dealloc];
   1.175 +}
   1.176 +@end
   1.177 +
   1.178 +@implementation SGLDelegate
   1.179 +
   1.180 +@synthesize win;
   1.181 +@synthesize view;
   1.182 +
   1.183 +-(BOOL)application: (UIApplication*)app didFinishLaunchingWithOptions: (NSDictionary*)opt
   1.184 +{
   1.185 +	[view startAnimation];
   1.186 +	return YES;
   1.187 +}
   1.188 +
   1.189 +-(void)applicationWillResignActive: (UIApplication*)app
   1.190 +{
   1.191 +	[view stopAnimation];
   1.192 +}
   1.193 +
   1.194 +-(void)applicationDidBecomeActive: (UIApplication*)app
   1.195 +{
   1.196 +	[view startAnimation];
   1.197 +}
   1.198 +
   1.199 +-(void)applicationWillTerminate: (UIApplication*)app
   1.200 +{
   1.201 +	[view stopAnimation];
   1.202 +}
   1.203 +
   1.204 +-(void)dealloc
   1.205 +{
   1.206 +	[win release];
   1.207 +	[view release];
   1.208 +	[super dealloc];
   1.209 +}
   1.210 +@end