vrshoot

view 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 source
1 #import "glview.h"
2 #include "game.h"
3 #include "screen.h"
4 #include "logger.h"
6 unsigned int default_fbo;
8 static GLView *view;
10 void request_redisplay()
11 {
12 // TODO
13 }
15 void swap_buffers()
16 {
17 if(view) {
18 [view swap_buffers];
19 }
20 }
23 @implementation GLView
25 @synthesize animating;
26 @dynamic anim_frame_interval;
28 // You must implement this method
29 + (Class)layerClass
30 {
31 return [CAEAGLLayer class];
32 }
35 - (id)initWithFrame: (CGRect) frame
36 {
37 if((self = [super initWithFrame: frame]))
38 {
39 // Get the layer
40 CAEAGLLayer *eaglLayer = (CAEAGLLayer*)self.layer;
42 //self.contentScaleFactor = 2.0;
44 eaglLayer.opaque = TRUE;
45 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:FALSE],
46 kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8,
47 kEAGLDrawablePropertyColorFormat, nil];
49 // initialize OpenGL ES context
50 context = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
51 if(!context || ![EAGLContext setCurrentContext: context]) {
52 [self release];
53 return self;
54 }
56 // create the framebuffer
57 glGenFramebuffers(1, &fbo);
58 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
60 glGenRenderbuffers(1, &rbuf_color);
61 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
62 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);
64 glGenRenderbuffers(1, &rbuf_depth);
65 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
66 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
68 default_fbo = fbo;
70 animating = FALSE;
71 display_link_supported = FALSE;
72 anim_frame_interval = 1;
73 display_link = nil;
74 anim_timer = nil;
76 // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
77 // class is used as fallback when it isn't available.
78 NSString *reqSysVer = @"3.1";
79 NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
80 if([currSysVer compare: reqSysVer options: NSNumericSearch] != NSOrderedAscending) {
81 display_link_supported = TRUE;
82 }
84 self.multipleTouchEnabled = 1;
86 if(!game_init()) {
87 exit(1);
88 }
89 }
91 view = self;
92 return self;
93 }
95 - (void)drawView: (id)sender
96 {
97 game_display();
98 }
100 - (void)swap_buffers
101 {
102 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
103 [context presentRenderbuffer: GL_RENDERBUFFER];
104 }
106 - (BOOL)resize: (CAEAGLLayer*)layer
107 {
108 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
110 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
111 [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: layer];
112 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &xsz);
113 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &ysz);
115 glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
116 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, xsz, ysz);
118 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
119 error_log("resize failed, framebuffer incomplete\n");
120 return FALSE;
121 }
122 assert(glGetError() == GL_NO_ERROR);
124 game_reshape(xsz, ysz);
125 return TRUE;
126 }
128 - (void)layoutSubviews
129 {
130 [self resize: (CAEAGLLayer*)self.layer];
131 [self drawView:nil];
132 }
134 - (NSInteger)anim_frame_interval
135 {
136 return anim_frame_interval;
137 }
139 - (void)setAnimationFrameInterval: (NSInteger)frameInterval
140 {
141 // Frame interval defines how many display frames must pass between each time the
142 // display link fires. The display link will only fire 30 times a second when the
143 // frame internal is two on a display that refreshes 60 times a second. The default
144 // frame interval setting of one will fire 60 times a second when the display refreshes
145 // at 60 times a second. A frame interval setting of less than one results in undefined
146 // behavior.
147 if(frameInterval >= 1) {
148 anim_frame_interval = frameInterval;
150 if(animating) {
151 [self stopAnimation];
152 [self startAnimation];
153 }
154 }
155 }
157 - (void)startAnimation
158 {
159 if(!animating) {
160 if(display_link_supported) {
161 // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
162 // if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
163 // not be called in system versions earlier than 3.1.
165 display_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
166 [display_link setFrameInterval: anim_frame_interval];
167 [display_link addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
168 } else {
169 anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * anim_frame_interval)
170 target: self selector: @selector(drawView:) userInfo: nil repeats: TRUE];
171 }
173 animating = TRUE;
174 }
175 }
177 - (void)stopAnimation
178 {
179 if(animating) {
180 if(display_link_supported) {
181 [display_link invalidate];
182 display_link = nil;
183 } else {
184 [anim_timer invalidate];
185 anim_timer = nil;
186 }
188 animating = FALSE;
189 }
190 }
192 - (void)dealloc
193 {
194 game_cleanup();
196 if(fbo) {
197 glDeleteFramebuffers(1, &fbo);
198 fbo = 0;
199 default_fbo = 0;
200 }
201 if(rbuf_color) {
202 glDeleteRenderbuffers(1, &rbuf_color);
203 rbuf_color = 0;
204 }
205 if(rbuf_depth) {
206 glDeleteRenderbuffers(1, &rbuf_depth);
207 rbuf_depth = 0;
208 }
210 if([EAGLContext currentContext] == context) {
211 [EAGLContext setCurrentContext: nil];
212 }
213 [context release];
214 context = nil;
216 [super dealloc];
217 }
219 #define MOUSEX(x) ((int)((x) * xsz / 320.0))
220 #define MOUSEY(y) ((int)((y) * ysz / 480.0))
221 static bool bn_pressed;
223 - (void)touchesBegan: (NSSet*)touches withEvent: (UIEvent*)event
224 {
225 UITouch *touch = [[touches allObjects] objectAtIndex: 0];
226 CGPoint pt = [touch locationInView: self];
228 int x = MOUSEX(pt.x);
229 int y = MOUSEY(pt.y);
230 current_screen()->button(0, true, x, y);
232 bn_pressed = true;
233 }
235 - (void)touchesMoved: (NSSet*)touches withEvent: (UIEvent*)event
236 {
237 UITouch *touch = [[touches allObjects] objectAtIndex: 0];
238 CGPoint pt = [touch locationInView: self];
240 int x = MOUSEX(pt.x);
241 int y = MOUSEY(pt.y);
242 current_screen()->motion(x, y, bn_pressed);
243 }
245 - (void)touchesEnded: (NSSet*)touches withEvent: (UIEvent*)event
246 {
247 UITouch *touch = [[touches allObjects] objectAtIndex: 0];
248 CGPoint pt = [touch locationInView: self];
250 int x = MOUSEX(pt.x);
251 int y = MOUSEY(pt.y);
252 current_screen()->button(0, false, x, y);
253 bn_pressed = false;
254 }
256 - (void)touchesCancelled: (NSSet*)touches withEvent: (UIEvent*)event
257 {
258 [self touchesEnded: touches withEvent: event];
259 }
261 @end