PlatformiOS.mm 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. void Platform::gamepadEventConnectedInternal(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
  1231. unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString)
  1232. {
  1233. Gamepad::add(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
  1234. }
  1235. void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
  1236. {
  1237. Gamepad::remove(handle);
  1238. }
  1239. void Platform::shutdownInternal()
  1240. {
  1241. Game::getInstance()->shutdown();
  1242. }
  1243. bool Platform::isGestureSupported(Gesture::GestureEvent evt)
  1244. {
  1245. return true;
  1246. }
  1247. void Platform::registerGesture(Gesture::GestureEvent evt)
  1248. {
  1249. [__view registerGesture:evt];
  1250. }
  1251. void Platform::unregisterGesture(Gesture::GestureEvent evt)
  1252. {
  1253. [__view unregisterGesture:evt];
  1254. }
  1255. bool Platform::isGestureRegistered(Gesture::GestureEvent evt)
  1256. {
  1257. return [__view isGestureRegistered:evt];
  1258. }
  1259. void Platform::pollGamepadState(Gamepad* gamepad)
  1260. {
  1261. }
  1262. bool Platform::launchURL(const char *url)
  1263. {
  1264. if (url == NULL || *url == '\0')
  1265. return false;
  1266. return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithUTF8String: url]]];
  1267. }
  1268. }
  1269. #endif