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