PlatformiOS.mm 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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. _swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
  498. [self addGestureRecognizer:_swipeRecognizer];
  499. }
  500. if((evt & Gesture::GESTURE_PINCH) == Gesture::GESTURE_PINCH && _pinchRecognizer == NULL)
  501. {
  502. _pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
  503. [self addGestureRecognizer:_pinchRecognizer];
  504. }
  505. if((evt & Gesture::GESTURE_TAP) == Gesture::GESTURE_TAP && _tapRecognizer == NULL)
  506. {
  507. _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
  508. [self addGestureRecognizer:_tapRecognizer];
  509. }
  510. }
  511. - (void)unregisterGesture: (Gesture::GestureEvent) evt
  512. {
  513. if((evt & Gesture::GESTURE_SWIPE) == Gesture::GESTURE_SWIPE && _swipeRecognizer != NULL)
  514. {
  515. [self removeGestureRecognizer:_swipeRecognizer];
  516. [_swipeRecognizer release];
  517. _swipeRecognizer = NULL;
  518. }
  519. if((evt & Gesture::GESTURE_PINCH) == Gesture::GESTURE_PINCH && _pinchRecognizer != NULL)
  520. {
  521. [self removeGestureRecognizer:_pinchRecognizer];
  522. [_pinchRecognizer release];
  523. _pinchRecognizer = NULL;
  524. }
  525. if((evt & Gesture::GESTURE_TAP) == Gesture::GESTURE_TAP && _tapRecognizer != NULL)
  526. {
  527. [self removeGestureRecognizer:_tapRecognizer];
  528. [_tapRecognizer release];
  529. _tapRecognizer = NULL;
  530. }
  531. }
  532. - (void)handleTapGesture:(UITapGestureRecognizer*)sender
  533. {
  534. CGPoint location = [sender locationInView:self];
  535. game->gestureTapEvent(location.x, location.y);
  536. }
  537. - (void)handlePinchGesture:(UIPinchGestureRecognizer*)sender
  538. {
  539. CGFloat factor = [sender scale];
  540. CGPoint location = [sender locationInView:self];
  541. game->gesturePinchEvent(location.x, location.y, factor);
  542. }
  543. - (void)handleSwipeGesture:(UISwipeGestureRecognizer*)sender
  544. {
  545. UISwipeGestureRecognizerDirection direction = [sender direction];
  546. CGPoint location = [sender locationInView:self];
  547. int gameplayDirection = 0;
  548. switch(direction) {
  549. case UISwipeGestureRecognizerDirectionRight:
  550. gameplayDirection = Gesture::SWIPE_DIRECTION_RIGHT;
  551. break;
  552. case UISwipeGestureRecognizerDirectionLeft:
  553. gameplayDirection = Gesture::SWIPE_DIRECTION_LEFT;
  554. break;
  555. case UISwipeGestureRecognizerDirectionUp:
  556. gameplayDirection = Gesture::SWIPE_DIRECTION_UP;
  557. break;
  558. case UISwipeGestureRecognizerDirectionDown:
  559. gameplayDirection = Gesture::SWIPE_DIRECTION_DOWN;
  560. break;
  561. }
  562. game->gestureSwipeEvent(location.x, location.y, gameplayDirection);
  563. }
  564. @end
  565. @interface ViewController : UIViewController
  566. - (void)startUpdating;
  567. - (void)stopUpdating;
  568. @end
  569. @implementation ViewController
  570. - (id)init
  571. {
  572. if((self = [super init]))
  573. {
  574. }
  575. return self;
  576. }
  577. - (void)dealloc
  578. {
  579. __view = nil;
  580. [super dealloc];
  581. }
  582. - (void)didReceiveMemoryWarning
  583. {
  584. [super didReceiveMemoryWarning];
  585. }
  586. #pragma mark - View lifecycle
  587. - (void)loadView
  588. {
  589. self.view = [[[View alloc] init] autorelease];
  590. if(__view == nil)
  591. {
  592. __view = (View*)self.view;
  593. }
  594. }
  595. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  596. {
  597. // Fetch the supported orientations array
  598. NSArray *supportedOrientations = NULL;
  599. if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  600. {
  601. supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~ipad"];
  602. }
  603. else if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  604. {
  605. supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~iphone"];
  606. }
  607. if(supportedOrientations == NULL)
  608. {
  609. supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
  610. }
  611. // If no supported orientations default to v1.0 handling (landscape only)
  612. if(supportedOrientations == nil) {
  613. return UIInterfaceOrientationIsLandscape(interfaceOrientation);
  614. }
  615. for(NSString *s in supportedOrientations) {
  616. if(interfaceOrientation == UIInterfaceOrientationEnum(s)) return YES;
  617. }
  618. return NO;
  619. }
  620. - (void)startUpdating
  621. {
  622. [(View*)self.view startUpdating];
  623. }
  624. - (void)stopUpdating
  625. {
  626. [(View*)self.view stopUpdating];
  627. }
  628. @end
  629. @interface AppDelegate : UIApplication <UIApplicationDelegate>
  630. {
  631. UIWindow* window;
  632. ViewController* viewController;
  633. CMMotionManager *motionManager;
  634. }
  635. @property (nonatomic, retain) ViewController *viewController;
  636. @end
  637. @implementation AppDelegate
  638. @synthesize viewController;
  639. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  640. {
  641. __appDelegate = self;
  642. [UIApplication sharedApplication].statusBarHidden = YES;
  643. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  644. motionManager = [[CMMotionManager alloc] init];
  645. if([motionManager isAccelerometerAvailable] == YES)
  646. {
  647. motionManager.accelerometerUpdateInterval = 1 / 40.0; // 40Hz
  648. [motionManager startAccelerometerUpdates];
  649. }
  650. window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  651. viewController = [[ViewController alloc] init];
  652. [window setRootViewController:viewController];
  653. [window makeKeyAndVisible];
  654. return YES;
  655. }
  656. - (void)getAccelerometerPitch:(float*)pitch roll:(float*)roll
  657. {
  658. float p = 0.0f;
  659. float r = 0.0f;
  660. CMAccelerometerData* accelerometerData = motionManager.accelerometerData;
  661. if(accelerometerData != nil)
  662. {
  663. float tx, ty, tz;
  664. switch ([[UIApplication sharedApplication] statusBarOrientation])
  665. {
  666. case UIInterfaceOrientationLandscapeRight:
  667. tx = -accelerometerData.acceleration.y;
  668. ty = accelerometerData.acceleration.x;
  669. break;
  670. case UIInterfaceOrientationLandscapeLeft:
  671. tx = accelerometerData.acceleration.y;
  672. ty = -accelerometerData.acceleration.x;
  673. break;
  674. case UIInterfaceOrientationPortraitUpsideDown:
  675. tx = -accelerometerData.acceleration.y;
  676. ty = -accelerometerData.acceleration.x;
  677. break;
  678. case UIInterfaceOrientationPortrait:
  679. tx = accelerometerData.acceleration.x;
  680. ty = accelerometerData.acceleration.y;
  681. break;
  682. }
  683. tz = accelerometerData.acceleration.z;
  684. p = atan(ty / sqrt(tx * tx + tz * tz)) * 180.0f * M_1_PI;
  685. r = atan(tx / sqrt(ty * ty + tz * tz)) * 180.0f * M_1_PI;
  686. }
  687. if(pitch != NULL)
  688. *pitch = p;
  689. if(roll != NULL)
  690. *roll = r;
  691. }
  692. - (void)applicationWillResignActive:(UIApplication*)application
  693. {
  694. [viewController stopUpdating];
  695. }
  696. - (void)applicationDidEnterBackground:(UIApplication*)application
  697. {
  698. [viewController stopUpdating];
  699. }
  700. - (void)applicationWillEnterForeground:(UIApplication*)application
  701. {
  702. [viewController startUpdating];
  703. }
  704. - (void)applicationDidBecomeActive:(UIApplication*)application
  705. {
  706. [viewController startUpdating];
  707. }
  708. - (void)applicationWillTerminate:(UIApplication*)application
  709. {
  710. [viewController stopUpdating];
  711. }
  712. - (void)dealloc
  713. {
  714. [window setRootViewController:nil];
  715. [viewController release];
  716. [window release];
  717. [motionManager release];
  718. [super dealloc];
  719. }
  720. @end
  721. double getMachTimeInMilliseconds()
  722. {
  723. static const double kOneMillion = 1000 * 1000;
  724. static mach_timebase_info_data_t s_timebase_info;
  725. if (s_timebase_info.denom == 0)
  726. (void) mach_timebase_info(&s_timebase_info);
  727. // mach_absolute_time() returns billionth of seconds, so divide by one million to get milliseconds
  728. GP_ASSERT(s_timebase_info.denom);
  729. return ((double)mach_absolute_time() * (double)s_timebase_info.numer) / (kOneMillion * (double)s_timebase_info.denom);
  730. }
  731. int getKey(unichar keyCode)
  732. {
  733. switch(keyCode)
  734. {
  735. case 0x0A:
  736. return Keyboard::KEY_RETURN;
  737. case 0x20:
  738. return Keyboard::KEY_SPACE;
  739. case 0x30:
  740. return Keyboard::KEY_ZERO;
  741. case 0x31:
  742. return Keyboard::KEY_ONE;
  743. case 0x32:
  744. return Keyboard::KEY_TWO;
  745. case 0x33:
  746. return Keyboard::KEY_THREE;
  747. case 0x34:
  748. return Keyboard::KEY_FOUR;
  749. case 0x35:
  750. return Keyboard::KEY_FIVE;
  751. case 0x36:
  752. return Keyboard::KEY_SIX;
  753. case 0x37:
  754. return Keyboard::KEY_SEVEN;
  755. case 0x38:
  756. return Keyboard::KEY_EIGHT;
  757. case 0x39:
  758. return Keyboard::KEY_NINE;
  759. case 0x41:
  760. return Keyboard::KEY_CAPITAL_A;
  761. case 0x42:
  762. return Keyboard::KEY_CAPITAL_B;
  763. case 0x43:
  764. return Keyboard::KEY_CAPITAL_C;
  765. case 0x44:
  766. return Keyboard::KEY_CAPITAL_D;
  767. case 0x45:
  768. return Keyboard::KEY_CAPITAL_E;
  769. case 0x46:
  770. return Keyboard::KEY_CAPITAL_F;
  771. case 0x47:
  772. return Keyboard::KEY_CAPITAL_G;
  773. case 0x48:
  774. return Keyboard::KEY_CAPITAL_H;
  775. case 0x49:
  776. return Keyboard::KEY_CAPITAL_I;
  777. case 0x4A:
  778. return Keyboard::KEY_CAPITAL_J;
  779. case 0x4B:
  780. return Keyboard::KEY_CAPITAL_K;
  781. case 0x4C:
  782. return Keyboard::KEY_CAPITAL_L;
  783. case 0x4D:
  784. return Keyboard::KEY_CAPITAL_M;
  785. case 0x4E:
  786. return Keyboard::KEY_CAPITAL_N;
  787. case 0x4F:
  788. return Keyboard::KEY_CAPITAL_O;
  789. case 0x50:
  790. return Keyboard::KEY_CAPITAL_P;
  791. case 0x51:
  792. return Keyboard::KEY_CAPITAL_Q;
  793. case 0x52:
  794. return Keyboard::KEY_CAPITAL_R;
  795. case 0x53:
  796. return Keyboard::KEY_CAPITAL_S;
  797. case 0x54:
  798. return Keyboard::KEY_CAPITAL_T;
  799. case 0x55:
  800. return Keyboard::KEY_CAPITAL_U;
  801. case 0x56:
  802. return Keyboard::KEY_CAPITAL_V;
  803. case 0x57:
  804. return Keyboard::KEY_CAPITAL_W;
  805. case 0x58:
  806. return Keyboard::KEY_CAPITAL_X;
  807. case 0x59:
  808. return Keyboard::KEY_CAPITAL_Y;
  809. case 0x5A:
  810. return Keyboard::KEY_CAPITAL_Z;
  811. case 0x61:
  812. return Keyboard::KEY_A;
  813. case 0x62:
  814. return Keyboard::KEY_B;
  815. case 0x63:
  816. return Keyboard::KEY_C;
  817. case 0x64:
  818. return Keyboard::KEY_D;
  819. case 0x65:
  820. return Keyboard::KEY_E;
  821. case 0x66:
  822. return Keyboard::KEY_F;
  823. case 0x67:
  824. return Keyboard::KEY_G;
  825. case 0x68:
  826. return Keyboard::KEY_H;
  827. case 0x69:
  828. return Keyboard::KEY_I;
  829. case 0x6A:
  830. return Keyboard::KEY_J;
  831. case 0x6B:
  832. return Keyboard::KEY_K;
  833. case 0x6C:
  834. return Keyboard::KEY_L;
  835. case 0x6D:
  836. return Keyboard::KEY_M;
  837. case 0x6E:
  838. return Keyboard::KEY_N;
  839. case 0x6F:
  840. return Keyboard::KEY_O;
  841. case 0x70:
  842. return Keyboard::KEY_P;
  843. case 0x71:
  844. return Keyboard::KEY_Q;
  845. case 0x72:
  846. return Keyboard::KEY_R;
  847. case 0x73:
  848. return Keyboard::KEY_S;
  849. case 0x74:
  850. return Keyboard::KEY_T;
  851. case 0x75:
  852. return Keyboard::KEY_U;
  853. case 0x76:
  854. return Keyboard::KEY_V;
  855. case 0x77:
  856. return Keyboard::KEY_W;
  857. case 0x78:
  858. return Keyboard::KEY_X;
  859. case 0x79:
  860. return Keyboard::KEY_Y;
  861. case 0x7A:
  862. return Keyboard::KEY_Z;
  863. default:
  864. break;
  865. // Symbol Row 3
  866. case 0x2E:
  867. return Keyboard::KEY_PERIOD;
  868. case 0x2C:
  869. return Keyboard::KEY_COMMA;
  870. case 0x3F:
  871. return Keyboard::KEY_QUESTION;
  872. case 0x21:
  873. return Keyboard::KEY_EXCLAM;
  874. case 0x27:
  875. return Keyboard::KEY_APOSTROPHE;
  876. // Symbols Row 2
  877. case 0x2D:
  878. return Keyboard::KEY_MINUS;
  879. case 0x2F:
  880. return Keyboard::KEY_SLASH;
  881. case 0x3A:
  882. return Keyboard::KEY_COLON;
  883. case 0x3B:
  884. return Keyboard::KEY_SEMICOLON;
  885. case 0x28:
  886. return Keyboard::KEY_LEFT_PARENTHESIS;
  887. case 0x29:
  888. return Keyboard::KEY_RIGHT_PARENTHESIS;
  889. case 0x24:
  890. return Keyboard::KEY_DOLLAR;
  891. case 0x26:
  892. return Keyboard::KEY_AMPERSAND;
  893. case 0x40:
  894. return Keyboard::KEY_AT;
  895. case 0x22:
  896. return Keyboard::KEY_QUOTE;
  897. // Numeric Symbols Row 1
  898. case 0x5B:
  899. return Keyboard::KEY_LEFT_BRACKET;
  900. case 0x5D:
  901. return Keyboard::KEY_RIGHT_BRACKET;
  902. case 0x7B:
  903. return Keyboard::KEY_LEFT_BRACE;
  904. case 0x7D:
  905. return Keyboard::KEY_RIGHT_BRACE;
  906. case 0x23:
  907. return Keyboard::KEY_NUMBER;
  908. case 0x25:
  909. return Keyboard::KEY_PERCENT;
  910. case 0x5E:
  911. return Keyboard::KEY_CIRCUMFLEX;
  912. case 0x2A:
  913. return Keyboard::KEY_ASTERISK;
  914. case 0x2B:
  915. return Keyboard::KEY_PLUS;
  916. case 0x3D:
  917. return Keyboard::KEY_EQUAL;
  918. // Numeric Symbols Row 2
  919. case 0x5F:
  920. return Keyboard::KEY_UNDERSCORE;
  921. case 0x5C:
  922. return Keyboard::KEY_BACK_SLASH;
  923. case 0x7C:
  924. return Keyboard::KEY_BAR;
  925. case 0x7E:
  926. return Keyboard::KEY_TILDE;
  927. case 0x3C:
  928. return Keyboard::KEY_LESS_THAN;
  929. case 0x3E:
  930. return Keyboard::KEY_GREATER_THAN;
  931. case 0x80:
  932. return Keyboard::KEY_EURO;
  933. case 0xA3:
  934. return Keyboard::KEY_POUND;
  935. case 0xA5:
  936. return Keyboard::KEY_YEN;
  937. case 0xB7:
  938. return Keyboard::KEY_MIDDLE_DOT;
  939. }
  940. return Keyboard::KEY_NONE;
  941. }
  942. /**
  943. * Returns the unicode value for the given keycode or zero if the key is not a valid printable character.
  944. */
  945. int getUnicode(int key)
  946. {
  947. switch (key)
  948. {
  949. case Keyboard::KEY_BACKSPACE:
  950. return 0x0008;
  951. case Keyboard::KEY_TAB:
  952. return 0x0009;
  953. case Keyboard::KEY_RETURN:
  954. case Keyboard::KEY_KP_ENTER:
  955. return 0x000A;
  956. case Keyboard::KEY_ESCAPE:
  957. return 0x001B;
  958. case Keyboard::KEY_SPACE:
  959. case Keyboard::KEY_EXCLAM:
  960. case Keyboard::KEY_QUOTE:
  961. case Keyboard::KEY_NUMBER:
  962. case Keyboard::KEY_DOLLAR:
  963. case Keyboard::KEY_PERCENT:
  964. case Keyboard::KEY_CIRCUMFLEX:
  965. case Keyboard::KEY_AMPERSAND:
  966. case Keyboard::KEY_APOSTROPHE:
  967. case Keyboard::KEY_LEFT_PARENTHESIS:
  968. case Keyboard::KEY_RIGHT_PARENTHESIS:
  969. case Keyboard::KEY_ASTERISK:
  970. case Keyboard::KEY_PLUS:
  971. case Keyboard::KEY_COMMA:
  972. case Keyboard::KEY_MINUS:
  973. case Keyboard::KEY_PERIOD:
  974. case Keyboard::KEY_SLASH:
  975. case Keyboard::KEY_ZERO:
  976. case Keyboard::KEY_ONE:
  977. case Keyboard::KEY_TWO:
  978. case Keyboard::KEY_THREE:
  979. case Keyboard::KEY_FOUR:
  980. case Keyboard::KEY_FIVE:
  981. case Keyboard::KEY_SIX:
  982. case Keyboard::KEY_SEVEN:
  983. case Keyboard::KEY_EIGHT:
  984. case Keyboard::KEY_NINE:
  985. case Keyboard::KEY_COLON:
  986. case Keyboard::KEY_SEMICOLON:
  987. case Keyboard::KEY_LESS_THAN:
  988. case Keyboard::KEY_EQUAL:
  989. case Keyboard::KEY_GREATER_THAN:
  990. case Keyboard::KEY_QUESTION:
  991. case Keyboard::KEY_AT:
  992. case Keyboard::KEY_CAPITAL_A:
  993. case Keyboard::KEY_CAPITAL_B:
  994. case Keyboard::KEY_CAPITAL_C:
  995. case Keyboard::KEY_CAPITAL_D:
  996. case Keyboard::KEY_CAPITAL_E:
  997. case Keyboard::KEY_CAPITAL_F:
  998. case Keyboard::KEY_CAPITAL_G:
  999. case Keyboard::KEY_CAPITAL_H:
  1000. case Keyboard::KEY_CAPITAL_I:
  1001. case Keyboard::KEY_CAPITAL_J:
  1002. case Keyboard::KEY_CAPITAL_K:
  1003. case Keyboard::KEY_CAPITAL_L:
  1004. case Keyboard::KEY_CAPITAL_M:
  1005. case Keyboard::KEY_CAPITAL_N:
  1006. case Keyboard::KEY_CAPITAL_O:
  1007. case Keyboard::KEY_CAPITAL_P:
  1008. case Keyboard::KEY_CAPITAL_Q:
  1009. case Keyboard::KEY_CAPITAL_R:
  1010. case Keyboard::KEY_CAPITAL_S:
  1011. case Keyboard::KEY_CAPITAL_T:
  1012. case Keyboard::KEY_CAPITAL_U:
  1013. case Keyboard::KEY_CAPITAL_V:
  1014. case Keyboard::KEY_CAPITAL_W:
  1015. case Keyboard::KEY_CAPITAL_X:
  1016. case Keyboard::KEY_CAPITAL_Y:
  1017. case Keyboard::KEY_CAPITAL_Z:
  1018. case Keyboard::KEY_LEFT_BRACKET:
  1019. case Keyboard::KEY_BACK_SLASH:
  1020. case Keyboard::KEY_RIGHT_BRACKET:
  1021. case Keyboard::KEY_UNDERSCORE:
  1022. case Keyboard::KEY_GRAVE:
  1023. case Keyboard::KEY_A:
  1024. case Keyboard::KEY_B:
  1025. case Keyboard::KEY_C:
  1026. case Keyboard::KEY_D:
  1027. case Keyboard::KEY_E:
  1028. case Keyboard::KEY_F:
  1029. case Keyboard::KEY_G:
  1030. case Keyboard::KEY_H:
  1031. case Keyboard::KEY_I:
  1032. case Keyboard::KEY_J:
  1033. case Keyboard::KEY_K:
  1034. case Keyboard::KEY_L:
  1035. case Keyboard::KEY_M:
  1036. case Keyboard::KEY_N:
  1037. case Keyboard::KEY_O:
  1038. case Keyboard::KEY_P:
  1039. case Keyboard::KEY_Q:
  1040. case Keyboard::KEY_R:
  1041. case Keyboard::KEY_S:
  1042. case Keyboard::KEY_T:
  1043. case Keyboard::KEY_U:
  1044. case Keyboard::KEY_V:
  1045. case Keyboard::KEY_W:
  1046. case Keyboard::KEY_X:
  1047. case Keyboard::KEY_Y:
  1048. case Keyboard::KEY_Z:
  1049. case Keyboard::KEY_LEFT_BRACE:
  1050. case Keyboard::KEY_BAR:
  1051. case Keyboard::KEY_RIGHT_BRACE:
  1052. case Keyboard::KEY_TILDE:
  1053. return key;
  1054. default:
  1055. return 0;
  1056. }
  1057. }
  1058. namespace gameplay
  1059. {
  1060. extern void print(const char* format, ...)
  1061. {
  1062. GP_ASSERT(format);
  1063. va_list argptr;
  1064. va_start(argptr, format);
  1065. vfprintf(stderr, format, argptr);
  1066. va_end(argptr);
  1067. }
  1068. Platform::Platform(Game* game) : _game(game)
  1069. {
  1070. }
  1071. Platform::~Platform()
  1072. {
  1073. }
  1074. Platform* Platform::create(Game* game, void* attachToWindow)
  1075. {
  1076. Platform* platform = new Platform(game);
  1077. return platform;
  1078. }
  1079. int Platform::enterMessagePump()
  1080. {
  1081. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  1082. [AppDelegate load];
  1083. UIApplicationMain(0, nil, NSStringFromClass([AppDelegate class]), NSStringFromClass([AppDelegate class]));
  1084. [pool release];
  1085. return EXIT_SUCCESS;
  1086. }
  1087. void Platform::signalShutdown()
  1088. {
  1089. // Cannot 'exit' an iOS Application
  1090. assert(false);
  1091. [__view stopUpdating];
  1092. exit(0);
  1093. }
  1094. bool Platform::canExit()
  1095. {
  1096. return false;
  1097. }
  1098. unsigned int Platform::getDisplayWidth()
  1099. {
  1100. CGSize size = DeviceOrientedSize([__appDelegate.viewController interfaceOrientation]);
  1101. return size.width;
  1102. }
  1103. unsigned int Platform::getDisplayHeight()
  1104. {
  1105. CGSize size = DeviceOrientedSize([__appDelegate.viewController interfaceOrientation]);
  1106. return size.height;
  1107. }
  1108. double Platform::getAbsoluteTime()
  1109. {
  1110. __timeAbsolute = getMachTimeInMilliseconds();
  1111. return __timeAbsolute;
  1112. }
  1113. void Platform::setAbsoluteTime(double time)
  1114. {
  1115. __timeAbsolute = time;
  1116. }
  1117. bool Platform::isVsync()
  1118. {
  1119. return __vsync;
  1120. }
  1121. void Platform::setVsync(bool enable)
  1122. {
  1123. __vsync = enable;
  1124. }
  1125. void Platform::swapBuffers()
  1126. {
  1127. if (__view)
  1128. [__view swapBuffers];
  1129. }
  1130. void Platform::sleep(long ms)
  1131. {
  1132. usleep(ms * 1000);
  1133. }
  1134. void Platform::getAccelerometerValues(float* pitch, float* roll)
  1135. {
  1136. [__appDelegate getAccelerometerPitch:pitch roll:roll];
  1137. }
  1138. bool Platform::hasMouse()
  1139. {
  1140. // not supported
  1141. return false;
  1142. }
  1143. void Platform::setMouseCaptured(bool captured)
  1144. {
  1145. // not supported
  1146. }
  1147. bool Platform::isMouseCaptured()
  1148. {
  1149. // not supported
  1150. return false;
  1151. }
  1152. void Platform::setCursorVisible(bool visible)
  1153. {
  1154. // not supported
  1155. }
  1156. bool Platform::isCursorVisible()
  1157. {
  1158. // not supported
  1159. return false;
  1160. }
  1161. void Platform::setMultiTouch(bool enabled)
  1162. {
  1163. __view.multipleTouchEnabled = enabled;
  1164. }
  1165. bool Platform::isMultiTouch()
  1166. {
  1167. return __view.multipleTouchEnabled;
  1168. }
  1169. void Platform::displayKeyboard(bool display)
  1170. {
  1171. if(__view)
  1172. {
  1173. if(display)
  1174. {
  1175. [__view showKeyboard];
  1176. }
  1177. else
  1178. {
  1179. [__view dismissKeyboard];
  1180. }
  1181. }
  1182. }
  1183. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  1184. {
  1185. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  1186. {
  1187. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  1188. Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
  1189. }
  1190. }
  1191. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  1192. {
  1193. if (!Form::keyEventInternal(evt, key))
  1194. {
  1195. Game::getInstance()->keyEvent(evt, key);
  1196. Game::getInstance()->getScriptController()->keyEvent(evt, key);
  1197. }
  1198. }
  1199. bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  1200. {
  1201. if (Form::mouseEventInternal(evt, x, y, wheelDelta))
  1202. {
  1203. return true;
  1204. }
  1205. else if (Game::getInstance()->mouseEvent(evt, x, y, wheelDelta))
  1206. {
  1207. return true;
  1208. }
  1209. else
  1210. {
  1211. return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
  1212. }
  1213. }
  1214. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1215. {
  1216. return true;
  1217. }
  1218. void Platform::registerGesture(Gesture::GestureEvent evt)
  1219. {
  1220. [__view registerGesture:evt];
  1221. }
  1222. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1223. {
  1224. [__view unregisterGesture:evt];
  1225. }
  1226. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1227. {
  1228. return [__view isGestureRegistered:evt];
  1229. }
  1230. unsigned int Platform::getGamepadsConnected()
  1231. {
  1232. return 0;
  1233. }
  1234. bool Platform::isGamepadConnected(unsigned int gamepadHandle)
  1235. {
  1236. return false;
  1237. }
  1238. const char* Platform::getGamepadId(unsigned int gamepadHandle)
  1239. {
  1240. return NULL;
  1241. }
  1242. unsigned int Platform::getGamepadButtonCount(unsigned int gamepadHandle)
  1243. {
  1244. return 0;
  1245. }
  1246. bool Platform::getGamepadButtonState(unsigned int gamepadHandle, unsigned int buttonIndex)
  1247. {
  1248. return false;
  1249. }
  1250. unsigned int Platform::getGamepadJoystickCount(unsigned int gamepadHandle)
  1251. {
  1252. return 0;
  1253. }
  1254. bool Platform::isGamepadJoystickActive(unsigned int gamepadHandle, unsigned int joystickIndex)
  1255. {
  1256. return false;
  1257. }
  1258. float Platform::getGamepadJoystickAxisX(unsigned int gamepadHandle, unsigned int joystickIndex)
  1259. {
  1260. return 0.0f;
  1261. }
  1262. float Platform::getGamepadJoystickAxisY(unsigned int gamepadHandle, unsigned int joystickIndex)
  1263. {
  1264. return 0.0f;
  1265. }
  1266. void Platform::getGamepadJoystickAxisValues(unsigned int gamepadHandle, unsigned int joystickIndex, Vector2* outValue)
  1267. {
  1268. }
  1269. unsigned int Platform::getGamepadTriggerCount(unsigned int gamepadHandle)
  1270. {
  1271. return 0;
  1272. }
  1273. float Platform::getGamepadTriggerValue(unsigned int gamepadHandle, unsigned int triggerIndex)
  1274. {
  1275. return 0.0f;
  1276. }
  1277. bool Platform::launchURL(const char *url)
  1278. {
  1279. if (url == NULL || *url == '\0')
  1280. return false;
  1281. return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithUTF8String: url]]];
  1282. }
  1283. }
  1284. #endif