PlatformiOS.mm 44 KB

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