gpmark

diff src/ios/glview.mm @ 0:5019d031b485

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 05 Jun 2013 22:33:37 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/ios/glview.mm	Wed Jun 05 22:33:37 2013 +0300
     1.3 @@ -0,0 +1,212 @@
     1.4 +#import "glview.h"
     1.5 +#include "game.h"
     1.6 +#include "screen.h"
     1.7 +#include "logger.h"
     1.8 +
     1.9 +static GLView *view;
    1.10 +
    1.11 +void request_redisplay()
    1.12 +{
    1.13 +	// TODO
    1.14 +}
    1.15 +
    1.16 +void swap_buffers()
    1.17 +{
    1.18 +	if(view) {
    1.19 +		[view swap_buffers];
    1.20 +	}
    1.21 +}
    1.22 +
    1.23 +
    1.24 +@implementation GLView
    1.25 +
    1.26 +@synthesize animating;
    1.27 +@dynamic anim_frame_interval;
    1.28 +
    1.29 +// You must implement this method
    1.30 ++ (Class)layerClass
    1.31 +{
    1.32 +	return [CAEAGLLayer class];
    1.33 +}
    1.34 +
    1.35 +
    1.36 +- (id)initWithFrame: (CGRect) frame
    1.37 +{
    1.38 +	if((self = [super initWithFrame: frame]))
    1.39 +	{
    1.40 +		// Get the layer
    1.41 +		CAEAGLLayer *eaglLayer = (CAEAGLLayer*)self.layer;
    1.42 +
    1.43 +		//self.contentScaleFactor = 2.0;
    1.44 +
    1.45 +		eaglLayer.opaque = TRUE;
    1.46 +		eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:FALSE],
    1.47 +				kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8,
    1.48 +				kEAGLDrawablePropertyColorFormat, nil];
    1.49 +
    1.50 +		// initialize OpenGL ES context
    1.51 +		context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
    1.52 +		if(!context || ![EAGLContext setCurrentContext: context]) {
    1.53 +			[self release];
    1.54 +			return self;
    1.55 +		}
    1.56 +
    1.57 +		// create the framebuffer
    1.58 +		glGenFramebuffers(1, &fbo);
    1.59 +		glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    1.60 +
    1.61 +		glGenRenderbuffers(1, &rbuf_color);
    1.62 +		glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
    1.63 +		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
    1.64 +
    1.65 +		glGenRenderbuffers(1, &rbuf_depth);
    1.66 +		glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
    1.67 +		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
    1.68 +
    1.69 +		animating = FALSE;
    1.70 +		display_link_supported = FALSE;
    1.71 +		anim_frame_interval = 1;
    1.72 +		display_link = nil;
    1.73 +		anim_timer = nil;
    1.74 +
    1.75 +		// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
    1.76 +		// class is used as fallback when it isn't available.
    1.77 +		NSString *reqSysVer = @"3.1";
    1.78 +		NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    1.79 +		if([currSysVer compare: reqSysVer options: NSNumericSearch] != NSOrderedAscending) {
    1.80 +			display_link_supported = TRUE;
    1.81 +		}
    1.82 +
    1.83 +		self.multipleTouchEnabled = 1;
    1.84 +
    1.85 +		if(!game_init()) {
    1.86 +			exit(1);
    1.87 +		}
    1.88 +	}
    1.89 +
    1.90 +	view = self;
    1.91 +	return self;
    1.92 +}
    1.93 +
    1.94 +- (void)drawView: (id)sender
    1.95 +{
    1.96 +	game_display();
    1.97 +}
    1.98 +
    1.99 +- (void)swap_buffers
   1.100 +{
   1.101 +	glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
   1.102 +	[context presentRenderbuffer: GL_RENDERBUFFER];
   1.103 +}
   1.104 +
   1.105 +- (BOOL)resize: (CAEAGLLayer*)layer
   1.106 +{
   1.107 +	glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
   1.108 +	[context renderbufferStorage: GL_RENDERBUFFER fromDrawable: layer];
   1.109 +	glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &xsz);
   1.110 +	glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &ysz);
   1.111 +
   1.112 +	glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
   1.113 +	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, xsz, ysz);
   1.114 +
   1.115 +	if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
   1.116 +		NSLog(@"resize failed, framebuffer incomplete\n");
   1.117 +		return FALSE;
   1.118 +	}
   1.119 +	assert(glGetError() == GL_NO_ERROR);
   1.120 +
   1.121 +	//current_screen()->reshape(xsz, ysz);
   1.122 +	return TRUE;
   1.123 +}
   1.124 +
   1.125 +- (void)layoutSubviews
   1.126 +{
   1.127 +	[self resize: (CAEAGLLayer*)self.layer];
   1.128 +	[self drawView:nil];
   1.129 +}
   1.130 +
   1.131 +- (NSInteger)anim_frame_interval
   1.132 +{
   1.133 +	return anim_frame_interval;
   1.134 +}
   1.135 +
   1.136 +- (void)setAnimationFrameInterval: (NSInteger)frameInterval
   1.137 +{
   1.138 +	// Frame interval defines how many display frames must pass between each time the
   1.139 +	// display link fires. The display link will only fire 30 times a second when the
   1.140 +	// frame internal is two on a display that refreshes 60 times a second. The default
   1.141 +	// frame interval setting of one will fire 60 times a second when the display refreshes
   1.142 +	// at 60 times a second. A frame interval setting of less than one results in undefined
   1.143 +	// behavior.
   1.144 +	if(frameInterval >= 1) {
   1.145 +		anim_frame_interval = frameInterval;
   1.146 +
   1.147 +		if(animating) {
   1.148 +			[self stopAnimation];
   1.149 +			[self startAnimation];
   1.150 +		}
   1.151 +	}
   1.152 +}
   1.153 +
   1.154 +- (void)startAnimation
   1.155 +{
   1.156 +	if(!animating) {
   1.157 +		if(display_link_supported) {
   1.158 +			// CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
   1.159 +			// if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
   1.160 +			// not be called in system versions earlier than 3.1.
   1.161 +
   1.162 +			display_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
   1.163 +			[display_link setFrameInterval: anim_frame_interval];
   1.164 +			[display_link addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
   1.165 +		} else {
   1.166 +			anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * anim_frame_interval)
   1.167 +					target: self selector: @selector(drawView:) userInfo: nil repeats: TRUE];
   1.168 +		}
   1.169 +
   1.170 +		animating = TRUE;
   1.171 +	}
   1.172 +}
   1.173 +
   1.174 +- (void)stopAnimation
   1.175 +{
   1.176 +	if(animating) {
   1.177 +		if(display_link_supported) {
   1.178 +			[display_link invalidate];
   1.179 +			display_link = nil;
   1.180 +		} else {
   1.181 +			[anim_timer invalidate];
   1.182 +			anim_timer = nil;
   1.183 +		}
   1.184 +
   1.185 +		animating = FALSE;
   1.186 +	}
   1.187 +}
   1.188 +
   1.189 +- (void)dealloc
   1.190 +{
   1.191 +	game_cleanup();
   1.192 +
   1.193 +	if(fbo) {
   1.194 +		glDeleteFramebuffers(1, &fbo);
   1.195 +		fbo = 0;
   1.196 +	}
   1.197 +	if(rbuf_color) {
   1.198 +		glDeleteRenderbuffers(1, &rbuf_color);
   1.199 +		rbuf_color = 0;
   1.200 +	}
   1.201 +	if(rbuf_depth) {
   1.202 +		glDeleteRenderbuffers(1, &rbuf_depth);
   1.203 +		rbuf_depth = 0;
   1.204 +	}
   1.205 +
   1.206 +	if([EAGLContext currentContext] == context) {
   1.207 +		[EAGLContext setCurrentContext: nil];
   1.208 +	}
   1.209 +	[context release];
   1.210 +	context = nil;
   1.211 +
   1.212 +	[super dealloc];
   1.213 +}
   1.214 +
   1.215 +@end