PlatformiOS.mm 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. #ifdef __APPLE__
  2. #include "Base.h"
  3. #include "Platform.h"
  4. #include "FileSystem.h"
  5. #include "Game.h"
  6. #include "Form.h"
  7. #include "ScriptController.h"
  8. #include <unistd.h>
  9. #import <UIKit/UIKit.h>
  10. #import <QuartzCore/QuartzCore.h>
  11. #import <CoreMotion/CoreMotion.h>
  12. #import <OpenGLES/EAGL.h>
  13. #import <OpenGLES/EAGLDrawable.h>
  14. #import <OpenGLES/ES2/gl.h>
  15. #import <OpenGLES/ES2/glext.h>
  16. #import <mach/mach_time.h>
  17. #define UIInterfaceOrientationEnum(x) ([x isEqualToString:@"UIInterfaceOrientationPortrait"]?UIInterfaceOrientationPortrait: \
  18. ([x isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]?UIInterfaceOrientationPortraitUpsideDown: \
  19. ([x isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]?UIInterfaceOrientationLandscapeLeft: \
  20. UIInterfaceOrientationLandscapeRight)))
  21. #define DeviceOrientedSize(o) ((o == UIInterfaceOrientationPortrait || o == UIInterfaceOrientationPortraitUpsideDown)? \
  22. CGSizeMake([[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale], [[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale]): \
  23. CGSizeMake([[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale], [[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale]))
  24. using namespace std;
  25. using namespace gameplay;
  26. // UIScreen bounds are provided as if device was in portrait mode Gameplay defaults to landscape
  27. extern const int WINDOW_WIDTH = [[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale];
  28. extern const int WINDOW_HEIGHT = [[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale];
  29. extern const int WINDOW_SCALE = [[UIScreen mainScreen] scale];
  30. @class AppDelegate;
  31. @class View;
  32. static AppDelegate *__appDelegate = NULL;
  33. static View* __view = NULL;
  34. static double __timeStart;
  35. static double __timeAbsolute;
  36. static bool __vsync = WINDOW_VSYNC;
  37. static float __pitch;
  38. static float __roll;
  39. double getMachTimeInMilliseconds();
  40. int getKey(unichar keyCode);
  41. int getUnicode(int key);
  42. @interface View : UIView <UIKeyInput>
  43. {
  44. EAGLContext* context;
  45. CADisplayLink* displayLink;
  46. BOOL updateFramebuffer;
  47. GLuint defaultFramebuffer;
  48. GLuint colorRenderbuffer;
  49. GLuint depthRenderbuffer;
  50. GLint framebufferWidth;
  51. GLint framebufferHeight;
  52. GLuint multisampleFramebuffer;
  53. GLuint multisampleRenderbuffer;
  54. GLuint multisampleDepthbuffer;
  55. NSInteger swapInterval;
  56. BOOL updating;
  57. Game* game;
  58. BOOL oglDiscardSupported;
  59. UITapGestureRecognizer *_tapRecognizer;
  60. UIPinchGestureRecognizer *_pinchRecognizer;
  61. UISwipeGestureRecognizer *_swipeRecognizer;
  62. }
  63. @property (readonly, nonatomic, getter=isUpdating) BOOL updating;
  64. @property (readonly, nonatomic, getter=getContext) EAGLContext* context;
  65. - (void)startGame;
  66. - (void)startUpdating;
  67. - (void)stopUpdating;
  68. - (void)update:(id)sender;
  69. - (void)setSwapInterval:(NSInteger)interval;
  70. - (int)swapInterval;
  71. - (void)swapBuffers;
  72. - (BOOL)showKeyboard;
  73. - (BOOL)dismissKeyboard;
  74. @end
  75. @interface View (Private)
  76. - (BOOL)createFramebuffer;
  77. - (void)deleteFramebuffer;
  78. @end
  79. @implementation View
  80. @synthesize updating;
  81. @synthesize context;
  82. + (Class) layerClass
  83. {
  84. return [CAEAGLLayer class];
  85. }
  86. - (id) initWithFrame:(CGRect)frame
  87. {
  88. if ((self = [super initWithFrame:frame]))
  89. {
  90. // A system version of 3.1 or greater is required to use CADisplayLink.
  91. NSString *reqSysVer = @"3.1";
  92. NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
  93. if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
  94. {
  95. // Log the system version
  96. NSLog(@"System Version: %@", currSysVer);
  97. }
  98. else
  99. {
  100. GP_ERROR("Invalid OS Version: %s\n", (currSysVer == NULL?"NULL":[currSysVer cStringUsingEncoding:NSASCIIStringEncoding]));
  101. [self release];
  102. return nil;
  103. }
  104. // Check for OS 4.0+ features
  105. if ([currSysVer compare:@"4.0" options:NSNumericSearch] != NSOrderedAscending)
  106. {
  107. oglDiscardSupported = YES;
  108. }
  109. else
  110. {
  111. oglDiscardSupported = NO;
  112. }
  113. // Configure the CAEAGLLayer and setup out the rendering context
  114. CGFloat scale = [[UIScreen mainScreen] scale];
  115. CAEAGLLayer* layer = (CAEAGLLayer *)self.layer;
  116. layer.opaque = TRUE;
  117. layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  118. [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
  119. kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
  120. self.contentScaleFactor = scale;
  121. layer.contentsScale = scale;
  122. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  123. if (!context || ![EAGLContext setCurrentContext:context])
  124. {
  125. GP_ERROR("Failed to make context current.");
  126. [self release];
  127. return nil;
  128. }
  129. // Initialize Internal Defaults
  130. displayLink = nil;
  131. updateFramebuffer = YES;
  132. defaultFramebuffer = 0;
  133. colorRenderbuffer = 0;
  134. depthRenderbuffer = 0;
  135. framebufferWidth = 0;
  136. framebufferHeight = 0;
  137. multisampleFramebuffer = 0;
  138. multisampleRenderbuffer = 0;
  139. multisampleDepthbuffer = 0;
  140. swapInterval = 1;
  141. updating = FALSE;
  142. game = nil;
  143. // Set the resource path and initalize the game
  144. NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/"];
  145. FileSystem::setResourcePath([bundlePath fileSystemRepresentation]);
  146. }
  147. return self;
  148. }
  149. - (void) dealloc
  150. {
  151. if (game)
  152. game->exit();
  153. [self deleteFramebuffer];
  154. if ([EAGLContext currentContext] == context)
  155. {
  156. [EAGLContext setCurrentContext:nil];
  157. }
  158. [context release];
  159. [super dealloc];
  160. }
  161. - (BOOL)canBecomeFirstResponder
  162. {
  163. // Override so we can control the keyboard
  164. return YES;
  165. }
  166. - (void) layoutSubviews
  167. {
  168. // Called on 'resize'.
  169. // Mark that framebuffer needs to be updated.
  170. // NOTE: Current disabled since we need to have a way to reset the default frame buffer handle
  171. // in FrameBuffer.cpp (for FrameBuffer:bindDefault). This means that changing orientation at
  172. // runtime is currently not supported until we fix this.
  173. //updateFramebuffer = YES;
  174. }
  175. - (BOOL)createFramebuffer
  176. {
  177. // iOS Requires all content go to a rendering buffer then it is swapped into the windows rendering surface
  178. assert(defaultFramebuffer == 0);
  179. // Create the default frame buffer
  180. GL_ASSERT( glGenFramebuffers(1, &defaultFramebuffer) );
  181. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer) );
  182. // Create a color buffer to attach to the frame buffer
  183. GL_ASSERT( glGenRenderbuffers(1, &colorRenderbuffer) );
  184. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer) );
  185. // Associate render buffer storage with CAEAGLLauyer so that the rendered content is display on our UI layer.
  186. [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
  187. // Attach the color buffer to our frame buffer
  188. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer) );
  189. // Retrieve framebuffer size
  190. GL_ASSERT( glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth) );
  191. GL_ASSERT( glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight) );
  192. NSLog(@"width: %d, height: %d", framebufferWidth, framebufferHeight);
  193. // If multisampling is enabled in config, create and setup a multisample buffer
  194. Properties* config = Game::getInstance()->getConfig()->getNamespace("window", true);
  195. int samples = config ? config->getInt("samples") : 0;
  196. if (samples < 0)
  197. samples = 0;
  198. if (samples)
  199. {
  200. // Create multisample framebuffer
  201. GL_ASSERT( glGenFramebuffers(1, &multisampleFramebuffer) );
  202. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, multisampleFramebuffer) );
  203. // Create multisample render and depth buffers
  204. GL_ASSERT( glGenRenderbuffers(1, &multisampleRenderbuffer) );
  205. GL_ASSERT( glGenRenderbuffers(1, &multisampleDepthbuffer) );
  206. // Try to find a supported multisample configuration starting with the defined sample count
  207. while (samples)
  208. {
  209. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, multisampleRenderbuffer) );
  210. GL_ASSERT( glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, samples, GL_RGBA8_OES, framebufferWidth, framebufferHeight) );
  211. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, multisampleRenderbuffer) );
  212. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, multisampleDepthbuffer) );
  213. GL_ASSERT( glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, samples, GL_DEPTH_COMPONENT24_OES, framebufferWidth, framebufferHeight) );
  214. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, multisampleDepthbuffer) );
  215. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
  216. break; // success!
  217. NSLog(@"Creation of multisample buffer with samples=%d failed. Attempting to use configuration with samples=%d instead: %x", samples, samples / 2, glCheckFramebufferStatus(GL_FRAMEBUFFER));
  218. samples /= 2;
  219. }
  220. // Re-bind the default framebuffer
  221. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer) );
  222. if (samples == 0)
  223. {
  224. // Unable to find a valid/supported multisample configuratoin - fallback to no multisampling
  225. GL_ASSERT( glDeleteRenderbuffers(1, &multisampleRenderbuffer) );
  226. GL_ASSERT( glDeleteRenderbuffers(1, &multisampleDepthbuffer) );
  227. GL_ASSERT( glDeleteFramebuffers(1, &multisampleFramebuffer) );
  228. multisampleFramebuffer = multisampleRenderbuffer = multisampleDepthbuffer = 0;
  229. }
  230. }
  231. // Create default depth buffer and attach to the frame buffer.
  232. // Note: If we are using multisample buffers, we can skip depth buffer creation here since we only
  233. // need the color buffer to resolve to.
  234. if (multisampleFramebuffer == 0)
  235. {
  236. GL_ASSERT( glGenRenderbuffers(1, &depthRenderbuffer) );
  237. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer) );
  238. GL_ASSERT( glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, framebufferWidth, framebufferHeight) );
  239. GL_ASSERT( glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer) );
  240. }
  241. // Sanity check, ensure that the framebuffer is valid
  242. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  243. {
  244. NSLog(@"ERROR: Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
  245. [self deleteFramebuffer];
  246. return NO;
  247. }
  248. // If multisampling is enabled, set the currently bound framebuffer to the multisample buffer
  249. // since that is the buffer code should be drawing into (and FrameBuffr::initialize will detect
  250. // and set this bound buffer as the default one during initialization.
  251. if (multisampleFramebuffer)
  252. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, multisampleFramebuffer) );
  253. return YES;
  254. }
  255. - (void)deleteFramebuffer
  256. {
  257. if (context)
  258. {
  259. [EAGLContext setCurrentContext:context];
  260. if (defaultFramebuffer)
  261. {
  262. GL_ASSERT( glDeleteFramebuffers(1, &defaultFramebuffer) );
  263. defaultFramebuffer = 0;
  264. }
  265. if (colorRenderbuffer)
  266. {
  267. GL_ASSERT( glDeleteRenderbuffers(1, &colorRenderbuffer) );
  268. colorRenderbuffer = 0;
  269. }
  270. if (depthRenderbuffer)
  271. {
  272. GL_ASSERT( glDeleteRenderbuffers(1, &depthRenderbuffer) );
  273. depthRenderbuffer = 0;
  274. }
  275. if (multisampleFramebuffer)
  276. {
  277. GL_ASSERT( glDeleteFramebuffers(1, &multisampleFramebuffer) );
  278. multisampleFramebuffer = 0;
  279. }
  280. if (multisampleRenderbuffer)
  281. {
  282. GL_ASSERT( glDeleteRenderbuffers(1, &multisampleRenderbuffer) );
  283. multisampleRenderbuffer = 0;
  284. }
  285. if (multisampleDepthbuffer)
  286. {
  287. GL_ASSERT( glDeleteRenderbuffers(1, &multisampleDepthbuffer) );
  288. multisampleDepthbuffer = 0;
  289. }
  290. }
  291. }
  292. - (void)setSwapInterval:(NSInteger)interval
  293. {
  294. if (interval >= 1)
  295. {
  296. swapInterval = interval;
  297. if (updating)
  298. {
  299. [self stopUpdating];
  300. [self startUpdating];
  301. }
  302. }
  303. }
  304. - (int)swapInterval
  305. {
  306. return swapInterval;
  307. }
  308. - (void)swapBuffers
  309. {
  310. if (context)
  311. {
  312. if (multisampleFramebuffer)
  313. {
  314. // Multisampling is enabled: resolve the multisample buffer into the default framebuffer
  315. GL_ASSERT( glBindFramebuffer(GL_DRAW_FRAMEBUFFER_APPLE, defaultFramebuffer) );
  316. GL_ASSERT( glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, multisampleFramebuffer) );
  317. GL_ASSERT( glResolveMultisampleFramebufferAPPLE() );
  318. if (oglDiscardSupported)
  319. {
  320. // Performance hint that the GL driver can discard the contents of the multisample buffers
  321. // since they have now been resolved into the default framebuffer
  322. const GLenum discards[] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT };
  323. GL_ASSERT( glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, discards) );
  324. }
  325. }
  326. else
  327. {
  328. if (oglDiscardSupported)
  329. {
  330. // Performance hint to the GL driver that the depth buffer is no longer required.
  331. const GLenum discards[] = { GL_DEPTH_ATTACHMENT };
  332. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer) );
  333. GL_ASSERT( glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, discards) );
  334. }
  335. }
  336. // Present the color buffer
  337. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer) );
  338. [context presentRenderbuffer:GL_RENDERBUFFER];
  339. }
  340. }
  341. - (void)startGame
  342. {
  343. if (game == nil)
  344. {
  345. game = Game::getInstance();
  346. __timeStart = getMachTimeInMilliseconds();
  347. game->run();
  348. }
  349. }
  350. - (void)startUpdating
  351. {
  352. if (!updating)
  353. {
  354. displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)];
  355. [displayLink setFrameInterval:swapInterval];
  356. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  357. if (game)
  358. game->resume();
  359. updating = TRUE;
  360. }
  361. }
  362. - (void)stopUpdating
  363. {
  364. if (updating)
  365. {
  366. if (game)
  367. game->pause();
  368. [displayLink invalidate];
  369. displayLink = nil;
  370. updating = FALSE;
  371. }
  372. }
  373. - (void)update:(id)sender
  374. {
  375. if (context != nil)
  376. {
  377. // Ensure our context is current
  378. [EAGLContext setCurrentContext:context];
  379. // If the framebuffer needs (re)creating, do so
  380. if (updateFramebuffer)
  381. {
  382. updateFramebuffer = NO;
  383. [self deleteFramebuffer];
  384. [self createFramebuffer];
  385. // Start the game after our framebuffer is created for the first time.
  386. if (game == nil)
  387. {
  388. [self startGame];
  389. // HACK: Skip the first display update after creating buffers and initializing the game.
  390. // If we don't do this, the first frame (which includes any drawing during initialization)
  391. // does not make it to the display for some reason.
  392. return;
  393. }
  394. }
  395. // Bind our framebuffer for rendering.
  396. // If multisampling is enabled, bind the multisample buffer - otherwise bind the default buffer
  397. GL_ASSERT( glBindFramebuffer(GL_FRAMEBUFFER, multisampleFramebuffer ? multisampleFramebuffer : defaultFramebuffer) );
  398. GL_ASSERT( glViewport(0, 0, framebufferWidth, framebufferHeight) );
  399. // Execute a single game frame
  400. if (game)
  401. game->frame();
  402. // Present the contents of the color buffer
  403. [self swapBuffers];
  404. }
  405. }
  406. - (BOOL)showKeyboard
  407. {
  408. return [self becomeFirstResponder];
  409. }
  410. - (BOOL)dismissKeyboard
  411. {
  412. return [self resignFirstResponder];
  413. }
  414. - (void)insertText:(NSString*)text
  415. {
  416. if([text length] == 0) return;
  417. assert([text length] == 1);
  418. unichar c = [text characterAtIndex:0];
  419. int key = getKey(c);
  420. Platform::keyEventInternal(Keyboard::KEY_PRESS, key);
  421. int character = getUnicode(key);
  422. if (character)
  423. {
  424. Platform::keyEventInternal(Keyboard::KEY_CHAR, /*character*/c);
  425. }
  426. Platform::keyEventInternal(Keyboard::KEY_RELEASE, key);
  427. }
  428. - (void)deleteBackward
  429. {
  430. Platform::keyEventInternal(Keyboard::KEY_PRESS, Keyboard::KEY_BACKSPACE);
  431. Platform::keyEventInternal(Keyboard::KEY_CHAR, getUnicode(Keyboard::KEY_BACKSPACE));
  432. Platform::keyEventInternal(Keyboard::KEY_RELEASE, Keyboard::KEY_BACKSPACE);
  433. }
  434. - (BOOL)hasText
  435. {
  436. return YES;
  437. }
  438. - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  439. {
  440. unsigned int touchID = 0;
  441. for(UITouch* touch in touches)
  442. {
  443. CGPoint touchPoint = [touch locationInView:self];
  444. if(self.multipleTouchEnabled == YES)
  445. {
  446. touchID = [touch hash];
  447. }
  448. Platform::touchEventInternal(Touch::TOUCH_PRESS, touchPoint.x * WINDOW_SCALE, touchPoint.y * WINDOW_SCALE, touchID);
  449. }
  450. }
  451. - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
  452. {
  453. unsigned int touchID = 0;
  454. for(UITouch* touch in touches)
  455. {
  456. CGPoint touchPoint = [touch locationInView:self];
  457. if(self.multipleTouchEnabled == YES)
  458. touchID = [touch hash];
  459. Platform::touchEventInternal(Touch::TOUCH_RELEASE, touchPoint.x * WINDOW_SCALE, touchPoint.y * WINDOW_SCALE, touchID);
  460. }
  461. }
  462. - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
  463. {
  464. // No equivalent for this in GamePlay -- treat as touch end
  465. [self touchesEnded:touches withEvent:event];
  466. }
  467. - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
  468. {
  469. unsigned int touchID = 0;
  470. for(UITouch* touch in touches)
  471. {
  472. CGPoint touchPoint = [touch locationInView:self];
  473. if(self.multipleTouchEnabled == YES)
  474. touchID = [touch hash];
  475. Platform::touchEventInternal(Touch::TOUCH_MOVE, touchPoint.x * WINDOW_SCALE, touchPoint.y * WINDOW_SCALE, touchID);
  476. }
  477. }
  478. // Gesture support for Mac OS X Trackpads
  479. - (bool)isGestureRegistered: (Gesture::GestureEvent) evt
  480. {
  481. switch(evt) {
  482. case Gesture::GESTURE_SWIPE:
  483. return (_swipeRecognizer != NULL);
  484. case Gesture::GESTURE_PINCH:
  485. return (_pinchRecognizer != NULL);
  486. case Gesture::GESTURE_TAP:
  487. return (_tapRecognizer != NULL);
  488. default:
  489. break;
  490. }
  491. return false;
  492. }
  493. - (void)registerGesture: (Gesture::GestureEvent) evt
  494. {
  495. if((evt & Gesture::GESTURE_SWIPE) == Gesture::GESTURE_SWIPE && _swipeRecognizer == NULL)
  496. {
  497. // right swipe (default)
  498. _swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
  499. [self addGestureRecognizer:_swipeRecognizer];
  500. // left swipe
  501. UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
  502. swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
  503. [self addGestureRecognizer:swipeGesture];
  504. [swipeGesture release];
  505. // up swipe
  506. UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
  507. swipeGesture2.direction = UISwipeGestureRecognizerDirectionUp;
  508. [self addGestureRecognizer:swipeGesture2];
  509. [swipeGesture2 release];
  510. // down swipe
  511. UISwipeGestureRecognizer *swipeGesture3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
  512. swipeGesture3.direction = UISwipeGestureRecognizerDirectionDown;
  513. [self addGestureRecognizer:swipeGesture3];
  514. [swipeGesture3 release];
  515. }
  516. if((evt & Gesture::GESTURE_PINCH) == Gesture::GESTURE_PINCH && _pinchRecognizer == NULL)
  517. {
  518. _pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
  519. [self addGestureRecognizer:_pinchRecognizer];
  520. }
  521. if((evt & Gesture::GESTURE_TAP) == Gesture::GESTURE_TAP && _tapRecognizer == NULL)
  522. {
  523. _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
  524. [self addGestureRecognizer:_tapRecognizer];
  525. }
  526. }
  527. - (void)unregisterGesture: (Gesture::GestureEvent) evt
  528. {
  529. if((evt & Gesture::GESTURE_SWIPE) == Gesture::GESTURE_SWIPE && _swipeRecognizer != NULL)
  530. {
  531. [self removeGestureRecognizer:_swipeRecognizer];
  532. [_swipeRecognizer release];
  533. _swipeRecognizer = NULL;
  534. }
  535. if((evt & Gesture::GESTURE_PINCH) == Gesture::GESTURE_PINCH && _pinchRecognizer != NULL)
  536. {
  537. [self removeGestureRecognizer:_pinchRecognizer];
  538. [_pinchRecognizer release];
  539. _pinchRecognizer = NULL;
  540. }
  541. if((evt & Gesture::GESTURE_TAP) == Gesture::GESTURE_TAP && _tapRecognizer != NULL)
  542. {
  543. [self removeGestureRecognizer:_tapRecognizer];
  544. [_tapRecognizer release];
  545. _tapRecognizer = NULL;
  546. }
  547. }
  548. - (void)handleTapGesture:(UITapGestureRecognizer*)sender
  549. {
  550. CGPoint location = [sender locationInView:self];
  551. game->gestureTapEvent(location.x, location.y);
  552. }
  553. - (void)handlePinchGesture:(UIPinchGestureRecognizer*)sender
  554. {
  555. CGFloat factor = [sender scale];
  556. CGPoint location = [sender locationInView:self];
  557. game->gesturePinchEvent(location.x, location.y, factor);
  558. }
  559. - (void)handleSwipeGesture:(UISwipeGestureRecognizer*)sender
  560. {
  561. UISwipeGestureRecognizerDirection direction = [sender direction];
  562. CGPoint location = [sender locationInView:self];
  563. int gameplayDirection = 0;
  564. switch(direction) {
  565. case UISwipeGestureRecognizerDirectionRight:
  566. gameplayDirection = Gesture::SWIPE_DIRECTION_RIGHT;
  567. break;
  568. case UISwipeGestureRecognizerDirectionLeft:
  569. gameplayDirection = Gesture::SWIPE_DIRECTION_LEFT;
  570. break;
  571. case UISwipeGestureRecognizerDirectionUp:
  572. gameplayDirection = Gesture::SWIPE_DIRECTION_UP;
  573. break;
  574. case UISwipeGestureRecognizerDirectionDown:
  575. gameplayDirection = Gesture::SWIPE_DIRECTION_DOWN;
  576. break;
  577. }
  578. game->gestureSwipeEvent(location.x, location.y, gameplayDirection);
  579. }
  580. @end
  581. @interface ViewController : UIViewController
  582. - (void)startUpdating;
  583. - (void)stopUpdating;
  584. @end
  585. @implementation ViewController
  586. - (id)init
  587. {
  588. if((self = [super init]))
  589. {
  590. }
  591. return self;
  592. }
  593. - (void)dealloc
  594. {
  595. __view = nil;
  596. [super dealloc];
  597. }
  598. - (void)didReceiveMemoryWarning
  599. {
  600. [super didReceiveMemoryWarning];
  601. }
  602. #pragma mark - View lifecycle
  603. - (void)loadView
  604. {
  605. self.view = [[[View alloc] init] autorelease];
  606. if(__view == nil)
  607. {
  608. __view = (View*)self.view;
  609. }
  610. }
  611. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  612. {
  613. // Fetch the supported orientations array
  614. NSArray *supportedOrientations = NULL;
  615. if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  616. {
  617. supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~ipad"];
  618. }
  619. else if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  620. {
  621. supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~iphone"];
  622. }
  623. if(supportedOrientations == NULL)
  624. {
  625. supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
  626. }
  627. // If no supported orientations default to v1.0 handling (landscape only)
  628. if(supportedOrientations == nil) {
  629. return UIInterfaceOrientationIsLandscape(interfaceOrientation);
  630. }
  631. for(NSString *s in supportedOrientations) {
  632. if(interfaceOrientation == UIInterfaceOrientationEnum(s)) return YES;
  633. }
  634. return NO;
  635. }
  636. - (void)startUpdating
  637. {
  638. [(View*)self.view startUpdating];
  639. }
  640. - (void)stopUpdating
  641. {
  642. [(View*)self.view stopUpdating];
  643. }
  644. @end
  645. @interface AppDelegate : UIApplication <UIApplicationDelegate>
  646. {
  647. UIWindow* window;
  648. ViewController* viewController;
  649. CMMotionManager *motionManager;
  650. }
  651. @property (nonatomic, retain) ViewController *viewController;
  652. @end
  653. @implementation AppDelegate
  654. @synthesize viewController;
  655. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  656. {
  657. __appDelegate = self;
  658. [UIApplication sharedApplication].statusBarHidden = YES;
  659. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  660. motionManager = [[CMMotionManager alloc] init];
  661. if([motionManager isAccelerometerAvailable] == YES)
  662. {
  663. motionManager.accelerometerUpdateInterval = 1 / 40.0; // 40Hz
  664. [motionManager startAccelerometerUpdates];
  665. }
  666. window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  667. viewController = [[ViewController alloc] init];
  668. [window setRootViewController:viewController];
  669. [window makeKeyAndVisible];
  670. return YES;
  671. }
  672. - (void)getAccelerometerPitch:(float*)pitch roll:(float*)roll
  673. {
  674. float p = 0.0f;
  675. float r = 0.0f;
  676. CMAccelerometerData* accelerometerData = motionManager.accelerometerData;
  677. if(accelerometerData != nil)
  678. {
  679. float tx, ty, tz;
  680. switch ([[UIApplication sharedApplication] statusBarOrientation])
  681. {
  682. case UIInterfaceOrientationLandscapeRight:
  683. tx = -accelerometerData.acceleration.y;
  684. ty = accelerometerData.acceleration.x;
  685. break;
  686. case UIInterfaceOrientationLandscapeLeft:
  687. tx = accelerometerData.acceleration.y;
  688. ty = -accelerometerData.acceleration.x;
  689. break;
  690. case UIInterfaceOrientationPortraitUpsideDown:
  691. tx = -accelerometerData.acceleration.y;
  692. ty = -accelerometerData.acceleration.x;
  693. break;
  694. case UIInterfaceOrientationPortrait:
  695. tx = accelerometerData.acceleration.x;
  696. ty = accelerometerData.acceleration.y;
  697. break;
  698. }
  699. tz = accelerometerData.acceleration.z;
  700. p = atan(ty / sqrt(tx * tx + tz * tz)) * 180.0f * M_1_PI;
  701. r = atan(tx / sqrt(ty * ty + tz * tz)) * 180.0f * M_1_PI;
  702. }
  703. if(pitch != NULL)
  704. *pitch = p;
  705. if(roll != NULL)
  706. *roll = r;
  707. }
  708. - (void)applicationWillResignActive:(UIApplication*)application
  709. {
  710. [viewController stopUpdating];
  711. }
  712. - (void)applicationDidEnterBackground:(UIApplication*)application
  713. {
  714. [viewController stopUpdating];
  715. }
  716. - (void)applicationWillEnterForeground:(UIApplication*)application
  717. {
  718. [viewController startUpdating];
  719. }
  720. - (void)applicationDidBecomeActive:(UIApplication*)application
  721. {
  722. [viewController startUpdating];
  723. }
  724. - (void)applicationWillTerminate:(UIApplication*)application
  725. {
  726. [viewController stopUpdating];
  727. }
  728. - (void)dealloc
  729. {
  730. [window setRootViewController:nil];
  731. [viewController release];
  732. [window release];
  733. [motionManager release];
  734. [super dealloc];
  735. }
  736. @end
  737. double getMachTimeInMilliseconds()
  738. {
  739. static const double kOneMillion = 1000 * 1000;
  740. static mach_timebase_info_data_t s_timebase_info;
  741. if (s_timebase_info.denom == 0)
  742. (void) mach_timebase_info(&s_timebase_info);
  743. // mach_absolute_time() returns billionth of seconds, so divide by one million to get milliseconds
  744. GP_ASSERT(s_timebase_info.denom);
  745. return ((double)mach_absolute_time() * (double)s_timebase_info.numer) / (kOneMillion * (double)s_timebase_info.denom);
  746. }
  747. int getKey(unichar keyCode)
  748. {
  749. switch(keyCode)
  750. {
  751. case 0x0A:
  752. return Keyboard::KEY_RETURN;
  753. case 0x20:
  754. return Keyboard::KEY_SPACE;
  755. case 0x30:
  756. return Keyboard::KEY_ZERO;
  757. case 0x31:
  758. return Keyboard::KEY_ONE;
  759. case 0x32:
  760. return Keyboard::KEY_TWO;
  761. case 0x33:
  762. return Keyboard::KEY_THREE;
  763. case 0x34:
  764. return Keyboard::KEY_FOUR;
  765. case 0x35:
  766. return Keyboard::KEY_FIVE;
  767. case 0x36:
  768. return Keyboard::KEY_SIX;
  769. case 0x37:
  770. return Keyboard::KEY_SEVEN;
  771. case 0x38:
  772. return Keyboard::KEY_EIGHT;
  773. case 0x39:
  774. return Keyboard::KEY_NINE;
  775. case 0x41:
  776. return Keyboard::KEY_CAPITAL_A;
  777. case 0x42:
  778. return Keyboard::KEY_CAPITAL_B;
  779. case 0x43:
  780. return Keyboard::KEY_CAPITAL_C;
  781. case 0x44:
  782. return Keyboard::KEY_CAPITAL_D;
  783. case 0x45:
  784. return Keyboard::KEY_CAPITAL_E;
  785. case 0x46:
  786. return Keyboard::KEY_CAPITAL_F;
  787. case 0x47:
  788. return Keyboard::KEY_CAPITAL_G;
  789. case 0x48:
  790. return Keyboard::KEY_CAPITAL_H;
  791. case 0x49:
  792. return Keyboard::KEY_CAPITAL_I;
  793. case 0x4A:
  794. return Keyboard::KEY_CAPITAL_J;
  795. case 0x4B:
  796. return Keyboard::KEY_CAPITAL_K;
  797. case 0x4C:
  798. return Keyboard::KEY_CAPITAL_L;
  799. case 0x4D:
  800. return Keyboard::KEY_CAPITAL_M;
  801. case 0x4E:
  802. return Keyboard::KEY_CAPITAL_N;
  803. case 0x4F:
  804. return Keyboard::KEY_CAPITAL_O;
  805. case 0x50:
  806. return Keyboard::KEY_CAPITAL_P;
  807. case 0x51:
  808. return Keyboard::KEY_CAPITAL_Q;
  809. case 0x52:
  810. return Keyboard::KEY_CAPITAL_R;
  811. case 0x53:
  812. return Keyboard::KEY_CAPITAL_S;
  813. case 0x54:
  814. return Keyboard::KEY_CAPITAL_T;
  815. case 0x55:
  816. return Keyboard::KEY_CAPITAL_U;
  817. case 0x56:
  818. return Keyboard::KEY_CAPITAL_V;
  819. case 0x57:
  820. return Keyboard::KEY_CAPITAL_W;
  821. case 0x58:
  822. return Keyboard::KEY_CAPITAL_X;
  823. case 0x59:
  824. return Keyboard::KEY_CAPITAL_Y;
  825. case 0x5A:
  826. return Keyboard::KEY_CAPITAL_Z;
  827. case 0x61:
  828. return Keyboard::KEY_A;
  829. case 0x62:
  830. return Keyboard::KEY_B;
  831. case 0x63:
  832. return Keyboard::KEY_C;
  833. case 0x64:
  834. return Keyboard::KEY_D;
  835. case 0x65:
  836. return Keyboard::KEY_E;
  837. case 0x66:
  838. return Keyboard::KEY_F;
  839. case 0x67:
  840. return Keyboard::KEY_G;
  841. case 0x68:
  842. return Keyboard::KEY_H;
  843. case 0x69:
  844. return Keyboard::KEY_I;
  845. case 0x6A:
  846. return Keyboard::KEY_J;
  847. case 0x6B:
  848. return Keyboard::KEY_K;
  849. case 0x6C:
  850. return Keyboard::KEY_L;
  851. case 0x6D:
  852. return Keyboard::KEY_M;
  853. case 0x6E:
  854. return Keyboard::KEY_N;
  855. case 0x6F:
  856. return Keyboard::KEY_O;
  857. case 0x70:
  858. return Keyboard::KEY_P;
  859. case 0x71:
  860. return Keyboard::KEY_Q;
  861. case 0x72:
  862. return Keyboard::KEY_R;
  863. case 0x73:
  864. return Keyboard::KEY_S;
  865. case 0x74:
  866. return Keyboard::KEY_T;
  867. case 0x75:
  868. return Keyboard::KEY_U;
  869. case 0x76:
  870. return Keyboard::KEY_V;
  871. case 0x77:
  872. return Keyboard::KEY_W;
  873. case 0x78:
  874. return Keyboard::KEY_X;
  875. case 0x79:
  876. return Keyboard::KEY_Y;
  877. case 0x7A:
  878. return Keyboard::KEY_Z;
  879. default:
  880. break;
  881. // Symbol Row 3
  882. case 0x2E:
  883. return Keyboard::KEY_PERIOD;
  884. case 0x2C:
  885. return Keyboard::KEY_COMMA;
  886. case 0x3F:
  887. return Keyboard::KEY_QUESTION;
  888. case 0x21:
  889. return Keyboard::KEY_EXCLAM;
  890. case 0x27:
  891. return Keyboard::KEY_APOSTROPHE;
  892. // Symbols Row 2
  893. case 0x2D:
  894. return Keyboard::KEY_MINUS;
  895. case 0x2F:
  896. return Keyboard::KEY_SLASH;
  897. case 0x3A:
  898. return Keyboard::KEY_COLON;
  899. case 0x3B:
  900. return Keyboard::KEY_SEMICOLON;
  901. case 0x28:
  902. return Keyboard::KEY_LEFT_PARENTHESIS;
  903. case 0x29:
  904. return Keyboard::KEY_RIGHT_PARENTHESIS;
  905. case 0x24:
  906. return Keyboard::KEY_DOLLAR;
  907. case 0x26:
  908. return Keyboard::KEY_AMPERSAND;
  909. case 0x40:
  910. return Keyboard::KEY_AT;
  911. case 0x22:
  912. return Keyboard::KEY_QUOTE;
  913. // Numeric Symbols Row 1
  914. case 0x5B:
  915. return Keyboard::KEY_LEFT_BRACKET;
  916. case 0x5D:
  917. return Keyboard::KEY_RIGHT_BRACKET;
  918. case 0x7B:
  919. return Keyboard::KEY_LEFT_BRACE;
  920. case 0x7D:
  921. return Keyboard::KEY_RIGHT_BRACE;
  922. case 0x23:
  923. return Keyboard::KEY_NUMBER;
  924. case 0x25:
  925. return Keyboard::KEY_PERCENT;
  926. case 0x5E:
  927. return Keyboard::KEY_CIRCUMFLEX;
  928. case 0x2A:
  929. return Keyboard::KEY_ASTERISK;
  930. case 0x2B:
  931. return Keyboard::KEY_PLUS;
  932. case 0x3D:
  933. return Keyboard::KEY_EQUAL;
  934. // Numeric Symbols Row 2
  935. case 0x5F:
  936. return Keyboard::KEY_UNDERSCORE;
  937. case 0x5C:
  938. return Keyboard::KEY_BACK_SLASH;
  939. case 0x7C:
  940. return Keyboard::KEY_BAR;
  941. case 0x7E:
  942. return Keyboard::KEY_TILDE;
  943. case 0x3C:
  944. return Keyboard::KEY_LESS_THAN;
  945. case 0x3E:
  946. return Keyboard::KEY_GREATER_THAN;
  947. case 0x80:
  948. return Keyboard::KEY_EURO;
  949. case 0xA3:
  950. return Keyboard::KEY_POUND;
  951. case 0xA5:
  952. return Keyboard::KEY_YEN;
  953. case 0xB7:
  954. return Keyboard::KEY_MIDDLE_DOT;
  955. }
  956. return Keyboard::KEY_NONE;
  957. }
  958. /**
  959. * Returns the unicode value for the given keycode or zero if the key is not a valid printable character.
  960. */
  961. int getUnicode(int key)
  962. {
  963. switch (key)
  964. {
  965. case Keyboard::KEY_BACKSPACE:
  966. return 0x0008;
  967. case Keyboard::KEY_TAB:
  968. return 0x0009;
  969. case Keyboard::KEY_RETURN:
  970. case Keyboard::KEY_KP_ENTER:
  971. return 0x000A;
  972. case Keyboard::KEY_ESCAPE:
  973. return 0x001B;
  974. case Keyboard::KEY_SPACE:
  975. case Keyboard::KEY_EXCLAM:
  976. case Keyboard::KEY_QUOTE:
  977. case Keyboard::KEY_NUMBER:
  978. case Keyboard::KEY_DOLLAR:
  979. case Keyboard::KEY_PERCENT:
  980. case Keyboard::KEY_CIRCUMFLEX:
  981. case Keyboard::KEY_AMPERSAND:
  982. case Keyboard::KEY_APOSTROPHE:
  983. case Keyboard::KEY_LEFT_PARENTHESIS:
  984. case Keyboard::KEY_RIGHT_PARENTHESIS:
  985. case Keyboard::KEY_ASTERISK:
  986. case Keyboard::KEY_PLUS:
  987. case Keyboard::KEY_COMMA:
  988. case Keyboard::KEY_MINUS:
  989. case Keyboard::KEY_PERIOD:
  990. case Keyboard::KEY_SLASH:
  991. case Keyboard::KEY_ZERO:
  992. case Keyboard::KEY_ONE:
  993. case Keyboard::KEY_TWO:
  994. case Keyboard::KEY_THREE:
  995. case Keyboard::KEY_FOUR:
  996. case Keyboard::KEY_FIVE:
  997. case Keyboard::KEY_SIX:
  998. case Keyboard::KEY_SEVEN:
  999. case Keyboard::KEY_EIGHT:
  1000. case Keyboard::KEY_NINE:
  1001. case Keyboard::KEY_COLON:
  1002. case Keyboard::KEY_SEMICOLON:
  1003. case Keyboard::KEY_LESS_THAN:
  1004. case Keyboard::KEY_EQUAL:
  1005. case Keyboard::KEY_GREATER_THAN:
  1006. case Keyboard::KEY_QUESTION:
  1007. case Keyboard::KEY_AT:
  1008. case Keyboard::KEY_CAPITAL_A:
  1009. case Keyboard::KEY_CAPITAL_B:
  1010. case Keyboard::KEY_CAPITAL_C:
  1011. case Keyboard::KEY_CAPITAL_D:
  1012. case Keyboard::KEY_CAPITAL_E:
  1013. case Keyboard::KEY_CAPITAL_F:
  1014. case Keyboard::KEY_CAPITAL_G:
  1015. case Keyboard::KEY_CAPITAL_H:
  1016. case Keyboard::KEY_CAPITAL_I:
  1017. case Keyboard::KEY_CAPITAL_J:
  1018. case Keyboard::KEY_CAPITAL_K:
  1019. case Keyboard::KEY_CAPITAL_L:
  1020. case Keyboard::KEY_CAPITAL_M:
  1021. case Keyboard::KEY_CAPITAL_N:
  1022. case Keyboard::KEY_CAPITAL_O:
  1023. case Keyboard::KEY_CAPITAL_P:
  1024. case Keyboard::KEY_CAPITAL_Q:
  1025. case Keyboard::KEY_CAPITAL_R:
  1026. case Keyboard::KEY_CAPITAL_S:
  1027. case Keyboard::KEY_CAPITAL_T:
  1028. case Keyboard::KEY_CAPITAL_U:
  1029. case Keyboard::KEY_CAPITAL_V:
  1030. case Keyboard::KEY_CAPITAL_W:
  1031. case Keyboard::KEY_CAPITAL_X:
  1032. case Keyboard::KEY_CAPITAL_Y:
  1033. case Keyboard::KEY_CAPITAL_Z:
  1034. case Keyboard::KEY_LEFT_BRACKET:
  1035. case Keyboard::KEY_BACK_SLASH:
  1036. case Keyboard::KEY_RIGHT_BRACKET:
  1037. case Keyboard::KEY_UNDERSCORE:
  1038. case Keyboard::KEY_GRAVE:
  1039. case Keyboard::KEY_A:
  1040. case Keyboard::KEY_B:
  1041. case Keyboard::KEY_C:
  1042. case Keyboard::KEY_D:
  1043. case Keyboard::KEY_E:
  1044. case Keyboard::KEY_F:
  1045. case Keyboard::KEY_G:
  1046. case Keyboard::KEY_H:
  1047. case Keyboard::KEY_I:
  1048. case Keyboard::KEY_J:
  1049. case Keyboard::KEY_K:
  1050. case Keyboard::KEY_L:
  1051. case Keyboard::KEY_M:
  1052. case Keyboard::KEY_N:
  1053. case Keyboard::KEY_O:
  1054. case Keyboard::KEY_P:
  1055. case Keyboard::KEY_Q:
  1056. case Keyboard::KEY_R:
  1057. case Keyboard::KEY_S:
  1058. case Keyboard::KEY_T:
  1059. case Keyboard::KEY_U:
  1060. case Keyboard::KEY_V:
  1061. case Keyboard::KEY_W:
  1062. case Keyboard::KEY_X:
  1063. case Keyboard::KEY_Y:
  1064. case Keyboard::KEY_Z:
  1065. case Keyboard::KEY_LEFT_BRACE:
  1066. case Keyboard::KEY_BAR:
  1067. case Keyboard::KEY_RIGHT_BRACE:
  1068. case Keyboard::KEY_TILDE:
  1069. return key;
  1070. default:
  1071. return 0;
  1072. }
  1073. }
  1074. namespace gameplay
  1075. {
  1076. extern void print(const char* format, ...)
  1077. {
  1078. GP_ASSERT(format);
  1079. va_list argptr;
  1080. va_start(argptr, format);
  1081. vfprintf(stderr, format, argptr);
  1082. va_end(argptr);
  1083. }
  1084. Platform::Platform(Game* game) : _game(game)
  1085. {
  1086. }
  1087. Platform::~Platform()
  1088. {
  1089. }
  1090. Platform* Platform::create(Game* game, void* attachToWindow)
  1091. {
  1092. Platform* platform = new Platform(game);
  1093. return platform;
  1094. }
  1095. int Platform::enterMessagePump()
  1096. {
  1097. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  1098. [AppDelegate load];
  1099. UIApplicationMain(0, nil, NSStringFromClass([AppDelegate class]), NSStringFromClass([AppDelegate class]));
  1100. [pool release];
  1101. return EXIT_SUCCESS;
  1102. }
  1103. void Platform::signalShutdown()
  1104. {
  1105. // Cannot 'exit' an iOS Application
  1106. assert(false);
  1107. [__view stopUpdating];
  1108. exit(0);
  1109. }
  1110. bool Platform::canExit()
  1111. {
  1112. return false;
  1113. }
  1114. unsigned int Platform::getDisplayWidth()
  1115. {
  1116. CGSize size = DeviceOrientedSize([__appDelegate.viewController interfaceOrientation]);
  1117. return size.width;
  1118. }
  1119. unsigned int Platform::getDisplayHeight()
  1120. {
  1121. CGSize size = DeviceOrientedSize([__appDelegate.viewController interfaceOrientation]);
  1122. return size.height;
  1123. }
  1124. double Platform::getAbsoluteTime()
  1125. {
  1126. __timeAbsolute = getMachTimeInMilliseconds();
  1127. return __timeAbsolute;
  1128. }
  1129. void Platform::setAbsoluteTime(double time)
  1130. {
  1131. __timeAbsolute = time;
  1132. }
  1133. bool Platform::isVsync()
  1134. {
  1135. return __vsync;
  1136. }
  1137. void Platform::setVsync(bool enable)
  1138. {
  1139. __vsync = enable;
  1140. }
  1141. void Platform::swapBuffers()
  1142. {
  1143. if (__view)
  1144. [__view swapBuffers];
  1145. }
  1146. void Platform::sleep(long ms)
  1147. {
  1148. usleep(ms * 1000);
  1149. }
  1150. void Platform::getAccelerometerValues(float* pitch, float* roll)
  1151. {
  1152. [__appDelegate getAccelerometerPitch:pitch roll:roll];
  1153. }
  1154. bool Platform::hasMouse()
  1155. {
  1156. // not supported
  1157. return false;
  1158. }
  1159. void Platform::setMouseCaptured(bool captured)
  1160. {
  1161. // not supported
  1162. }
  1163. bool Platform::isMouseCaptured()
  1164. {
  1165. // not supported
  1166. return false;
  1167. }
  1168. void Platform::setCursorVisible(bool visible)
  1169. {
  1170. // not supported
  1171. }
  1172. bool Platform::isCursorVisible()
  1173. {
  1174. // not supported
  1175. return false;
  1176. }
  1177. void Platform::setMultiTouch(bool enabled)
  1178. {
  1179. __view.multipleTouchEnabled = enabled;
  1180. }
  1181. bool Platform::isMultiTouch()
  1182. {
  1183. return __view.multipleTouchEnabled;
  1184. }
  1185. void Platform::displayKeyboard(bool display)
  1186. {
  1187. if(__view)
  1188. {
  1189. if(display)
  1190. {
  1191. [__view showKeyboard];
  1192. }
  1193. else
  1194. {
  1195. [__view dismissKeyboard];
  1196. }
  1197. }
  1198. }
  1199. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1200. {
  1201. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  1202. {
  1203. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  1204. Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
  1205. }
  1206. }
  1207. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  1208. {
  1209. if (!Form::keyEventInternal(evt, key))
  1210. {
  1211. Game::getInstance()->keyEvent(evt, key);
  1212. Game::getInstance()->getScriptController()->keyEvent(evt, key);
  1213. }
  1214. }
  1215. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1216. {
  1217. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  1218. {
  1219. return true;
  1220. }
  1221. else if (Game::getInstance()->mouseEvent(evt, x, y, wheelDelta))
  1222. {
  1223. return true;
  1224. }
  1225. else
  1226. {
  1227. return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
  1228. }
  1229. }
  1230. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1231. {
  1232. return true;
  1233. }
  1234. void Platform::registerGesture(Gesture::GestureEvent evt)
  1235. {
  1236. [__view registerGesture:evt];
  1237. }
  1238. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1239. {
  1240. [__view unregisterGesture:evt];
  1241. }
  1242. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1243. {
  1244. return [__view isGestureRegistered:evt];
  1245. }
  1246. void Platform::pollGamepadState(Gamepad* gamepad)
  1247. {
  1248. }
  1249. bool Platform::launchURL(const char *url)
  1250. {
  1251. if (url == NULL || *url == '\0')
  1252. return false;
  1253. return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithUTF8String: url]]];
  1254. }
  1255. }
  1256. #endif