PlatformiOS.mm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. #ifdef __APPLE__
  2. #include "Base.h"
  3. #include "Platform.h"
  4. #include "FileSystem.h"
  5. #include "Game.h"
  6. #include <unistd.h>
  7. #import <UIKit/UIKit.h>
  8. #import <QuartzCore/QuartzCore.h>
  9. #import <CoreMotion/CoreMotion.h>
  10. #import <OpenGLES/EAGL.h>
  11. #import <OpenGLES/EAGLDrawable.h>
  12. #import <OpenGLES/ES2/gl.h>
  13. #import <OpenGLES/ES2/glext.h>
  14. #import <mach/mach_time.h>
  15. using namespace std;
  16. using namespace gameplay;
  17. // UIScreen bounds are provided as if device was in portrait mode
  18. // Gameplay defaults to landscape
  19. extern const int WINDOW_WIDTH = [[UIScreen mainScreen] bounds].size.height;
  20. extern const int WINDOW_HEIGHT = [[UIScreen mainScreen] bounds].size.width;
  21. static const float ACCELEROMETER_X_FACTOR = 90.0f / WINDOW_WIDTH;
  22. static const float ACCELEROMETER_Y_FACTOR = 90.0f / WINDOW_HEIGHT;
  23. @class AppDelegate;
  24. @class View;
  25. static AppDelegate *__appDelegate = NULL;
  26. static View* __view = NULL;
  27. static long __timeStart;
  28. static long __timeAbsolute;
  29. static bool __vsync = WINDOW_VSYNC;
  30. static float __pitch;
  31. static float __roll;
  32. long getMachTimeInMilliseconds();
  33. int getKey(unichar keyCode);
  34. @interface View : UIView <UIKeyInput>
  35. {
  36. EAGLContext* context;
  37. CADisplayLink* displayLink;
  38. GLuint defaultFramebuffer;
  39. GLuint colorRenderbuffer;
  40. GLuint depthRenderbuffer;
  41. GLint framebufferWidth;
  42. GLint framebufferHeight;
  43. NSInteger swapInterval;
  44. BOOL updating;
  45. Game* _game;
  46. }
  47. @property (readonly, nonatomic, getter=isUpdating) BOOL updating;
  48. @property (readonly, nonatomic, getter=getContext) EAGLContext* context;
  49. - (void)startUpdating;
  50. - (void)stopUpdating;
  51. - (void)update:(id)sender;
  52. - (void)setSwapInterval:(NSInteger)interval;
  53. - (int)swapInterval;
  54. - (void)swapBuffers;
  55. - (BOOL)showKeyboard;
  56. - (BOOL)dismissKeyboard;
  57. @end
  58. @interface View (Private)
  59. - (void)createFramebuffer;
  60. - (void)deleteFramebuffer;
  61. @end
  62. @implementation View
  63. @synthesize updating;
  64. @synthesize context;
  65. + (Class) layerClass
  66. {
  67. return [CAEAGLLayer class];
  68. }
  69. - (id) initWithFrame:(CGRect)frame
  70. {
  71. if ((self = [super initWithFrame:frame]))
  72. {
  73. // Do a sanity check
  74. // A system version of 3.1 or greater is required to use CADisplayLink.
  75. NSString *reqSysVer = @"3.1";
  76. NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
  77. if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
  78. {
  79. // Log the system version
  80. NSLog(@"System Version: %@", currSysVer);
  81. }
  82. else
  83. {
  84. printError("Invalid OS Version: %s\n", (currSysVer == NULL?"NULL":[currSysVer cStringUsingEncoding:NSASCIIStringEncoding]));
  85. [self release];
  86. return nil;
  87. }
  88. // Configure the CAEAGLLayer and setup out the rendering context
  89. CAEAGLLayer* layer = (CAEAGLLayer *)self.layer;
  90. layer.opaque = TRUE;
  91. layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  92. [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
  93. kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
  94. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  95. if (!context || ![EAGLContext setCurrentContext:context])
  96. {
  97. [self release];
  98. return nil;
  99. }
  100. if (!defaultFramebuffer)
  101. [self createFramebuffer];
  102. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  103. glViewport(0, 0, framebufferWidth, framebufferHeight);
  104. // Initialize Internal Defaults
  105. displayLink = nil;
  106. defaultFramebuffer = 0;
  107. colorRenderbuffer = 0;
  108. depthRenderbuffer = 0;
  109. framebufferWidth = 0;
  110. framebufferHeight = 0;
  111. swapInterval = 1;
  112. updating = FALSE;
  113. [self createFramebuffer];
  114. // Set the resource path and initalize the game
  115. NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/"];
  116. FileSystem::setResourcePath([bundlePath fileSystemRepresentation]);
  117. _game = Game::getInstance();
  118. __timeStart = getMachTimeInMilliseconds();
  119. _game->run(WINDOW_WIDTH, WINDOW_HEIGHT);
  120. }
  121. return self;
  122. }
  123. - (void) dealloc
  124. {
  125. _game->exit();
  126. [self deleteFramebuffer];
  127. if ([EAGLContext currentContext] == context)
  128. [EAGLContext setCurrentContext:nil];
  129. [context release];
  130. [super dealloc];
  131. }
  132. - (BOOL)canBecomeFirstResponder
  133. {
  134. // Override so we can control the keyboard
  135. return YES;
  136. }
  137. - (void) layoutSubviews
  138. {
  139. // Called on 'resize'
  140. [self deleteFramebuffer];
  141. }
  142. - (void)createFramebuffer
  143. {
  144. // iOS Requires all content go to a rendering buffer then it is swapped into the windows rendering surface
  145. assert(defaultFramebuffer == 0);
  146. // Create the default frame buffer, and render buffer
  147. glGenFramebuffers(1, &defaultFramebuffer);
  148. glGenRenderbuffers(1, &colorRenderbuffer);
  149. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  150. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  151. // request storage, width, and height of the view that we will render in
  152. [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
  153. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
  154. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
  155. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
  156. glGenRenderbuffers(1, &depthRenderbuffer);
  157. glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
  158. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, framebufferWidth, framebufferHeight);
  159. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
  160. // Sanity check, ensure that the framebuffer is valid
  161. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  162. NSLog(@"ERROR: Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
  163. }
  164. - (void)deleteFramebuffer
  165. {
  166. // Deleting the framebuffer and all the buffers it contains
  167. if (context)
  168. {
  169. [EAGLContext setCurrentContext:context];
  170. if (defaultFramebuffer)
  171. {
  172. glDeleteFramebuffers(1, &defaultFramebuffer);
  173. defaultFramebuffer = 0;
  174. }
  175. if (colorRenderbuffer)
  176. {
  177. glDeleteRenderbuffers(1, &colorRenderbuffer);
  178. colorRenderbuffer = 0;
  179. }
  180. if (depthRenderbuffer)
  181. {
  182. glDeleteRenderbuffers(1, &depthRenderbuffer);
  183. depthRenderbuffer = 0;
  184. }
  185. }
  186. }
  187. - (void)setSwapInterval:(NSInteger)interval
  188. {
  189. if (interval >= 1)
  190. {
  191. swapInterval = interval;
  192. if (updating)
  193. {
  194. [self stopUpdating];
  195. [self startUpdating];
  196. }
  197. }
  198. }
  199. - (int)swapInterval
  200. {
  201. return swapInterval;
  202. }
  203. - (void)swapBuffers
  204. {
  205. if (context != nil)
  206. {
  207. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  208. [context presentRenderbuffer:GL_RENDERBUFFER];
  209. }
  210. }
  211. - (void)startUpdating
  212. {
  213. if (!updating)
  214. {
  215. displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)];
  216. [displayLink setFrameInterval:swapInterval];
  217. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  218. _game->resume();
  219. updating = TRUE;
  220. }
  221. }
  222. - (void)stopUpdating
  223. {
  224. if (updating)
  225. {
  226. _game->pause();
  227. [displayLink invalidate];
  228. displayLink = nil;
  229. updating = FALSE;
  230. }
  231. }
  232. - (void)update:(id)sender
  233. {
  234. if (context != nil)
  235. {
  236. [EAGLContext setCurrentContext:context];
  237. if (!defaultFramebuffer)
  238. [self createFramebuffer];
  239. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  240. glViewport(0, 0, framebufferWidth, framebufferHeight);
  241. if (_game && _game->getState() == Game::RUNNING)
  242. _game->frame();
  243. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  244. [context presentRenderbuffer:GL_RENDERBUFFER];
  245. }
  246. }
  247. - (BOOL)showKeyboard
  248. {
  249. return [self becomeFirstResponder];
  250. }
  251. - (BOOL)dismissKeyboard
  252. {
  253. return [self resignFirstResponder];
  254. }
  255. /*
  256. * Virtual Keyboard Support
  257. */
  258. - (void)insertText:(NSString*)text
  259. {
  260. if([text length] == 0) return;
  261. assert([text length] == 1);
  262. unichar c = [text characterAtIndex:0];
  263. int gpk = getKey(c);
  264. Game::getInstance()->keyEvent(Keyboard::KEY_PRESS, gpk);
  265. Game::getInstance()->keyEvent(Keyboard::KEY_RELEASE, gpk);
  266. }
  267. - (void)deleteBackward
  268. {
  269. Game::getInstance()->keyEvent(Keyboard::KEY_PRESS, Keyboard::KEY_BACKSPACE);
  270. Game::getInstance()->keyEvent(Keyboard::KEY_RELEASE, Keyboard::KEY_BACKSPACE);
  271. }
  272. - (BOOL)hasText
  273. {
  274. return YES;
  275. }
  276. /*
  277. * Touch Support
  278. */
  279. - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  280. {
  281. unsigned int uniqueTouch = 0;
  282. for(UITouch *t in touches)
  283. {
  284. CGPoint touchLoc = [t locationInView:self];
  285. if(self.multipleTouchEnabled == YES)
  286. uniqueTouch = [t hash];
  287. Game::getInstance()->touchEvent(Touch::TOUCH_PRESS, touchLoc.x, touchLoc.y, uniqueTouch);
  288. }
  289. }
  290. - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event
  291. {
  292. unsigned int uniqueTouch = 0;
  293. for(UITouch* t in touches)
  294. {
  295. CGPoint touchLoc = [t locationInView:self];
  296. if(self.multipleTouchEnabled == YES)
  297. uniqueTouch = [t hash];
  298. Game::getInstance()->touchEvent(Touch::TOUCH_RELEASE, touchLoc.x, touchLoc.y, uniqueTouch);
  299. }
  300. }
  301. - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
  302. {
  303. // No equivalent for this in GamePlay -- treat as touch end
  304. [self touchesEnded:touches withEvent:event];
  305. }
  306. - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
  307. {
  308. unsigned int uniqueTouch = 0;
  309. for(UITouch* t in touches)
  310. {
  311. CGPoint touchLoc = [t locationInView:self];
  312. if(self.multipleTouchEnabled == YES)
  313. uniqueTouch = [t hash];
  314. Game::getInstance()->touchEvent(Touch::TOUCH_MOVE, touchLoc.x, touchLoc.y, uniqueTouch);
  315. }
  316. }
  317. @end
  318. @interface ViewController : UIViewController
  319. - (void)startUpdating;
  320. - (void)stopUpdating;
  321. @end
  322. @implementation ViewController
  323. - (id)init
  324. {
  325. if((self = [super init]))
  326. {
  327. }
  328. return self;
  329. }
  330. - (void)dealloc
  331. {
  332. __view = nil;
  333. [super dealloc];
  334. }
  335. - (void)didReceiveMemoryWarning
  336. {
  337. [super didReceiveMemoryWarning];
  338. // Release any cached data, images, etc that aren't in use.
  339. }
  340. #pragma mark - View lifecycle
  341. - (void)loadView
  342. {
  343. self.view = [[[View alloc] init] autorelease];
  344. if(__view == nil)
  345. {
  346. __view = (View*)self.view;
  347. }
  348. }
  349. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  350. {
  351. // Return YES for supported orientation, currently support landscape only?
  352. return UIInterfaceOrientationIsLandscape(interfaceOrientation);
  353. }
  354. - (void)startUpdating
  355. {
  356. [(View*)self.view startUpdating];
  357. }
  358. - (void)stopUpdating
  359. {
  360. [(View*)self.view stopUpdating];
  361. }
  362. @end
  363. @interface AppDelegate : UIApplication <UIApplicationDelegate>
  364. {
  365. UIWindow* window;
  366. ViewController* viewController;
  367. CMMotionManager *motionManager;
  368. }
  369. @end
  370. @implementation AppDelegate
  371. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  372. {
  373. __appDelegate = self;
  374. [UIApplication sharedApplication].statusBarHidden = YES;
  375. motionManager = [[CMMotionManager alloc] init];
  376. if([motionManager isAccelerometerAvailable] == YES)
  377. {
  378. motionManager.accelerometerUpdateInterval = 1 / 40.0; // 40Hz
  379. [motionManager startAccelerometerUpdates];
  380. }
  381. window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  382. viewController = [[ViewController alloc] init];
  383. [window setRootViewController:viewController];
  384. [window makeKeyAndVisible];
  385. return YES;
  386. }
  387. - (void)getAccelerometerPitch:(float *)pitch roll:(float *)roll
  388. {
  389. float p = 0.0f;
  390. float r = 0.0f;
  391. CMAccelerometerData *accelerometerData = motionManager.accelerometerData;
  392. if(accelerometerData != nil)
  393. {
  394. float tx, ty, tz;
  395. tx = accelerometerData.acceleration.y;
  396. ty = -accelerometerData.acceleration.x;
  397. tz = -accelerometerData.acceleration.z;
  398. p = atan(ty / sqrt(tx * tx + tz * tz)) * 180.0f * M_1_PI;
  399. r = atan(tx / sqrt(ty * ty + tz * tz)) * 180.0f * M_1_PI;
  400. }
  401. if(pitch != NULL)
  402. *pitch = p;
  403. if(roll != NULL)
  404. *roll = r;
  405. }
  406. - (void)applicationWillResignActive:(UIApplication*)application
  407. {
  408. [viewController stopUpdating];
  409. }
  410. - (void)applicationDidEnterBackground:(UIApplication*)application
  411. {
  412. [viewController stopUpdating];
  413. }
  414. - (void)applicationWillEnterForeground:(UIApplication*)application
  415. {
  416. [viewController startUpdating];
  417. }
  418. - (void)applicationDidBecomeActive:(UIApplication*)application
  419. {
  420. [viewController startUpdating];
  421. }
  422. - (void)applicationWillTerminate:(UIApplication*)application
  423. {
  424. [viewController stopUpdating];
  425. }
  426. - (void)dealloc
  427. {
  428. [window setRootViewController:nil];
  429. [viewController release];
  430. [window release];
  431. [motionManager release];
  432. [super dealloc];
  433. }
  434. @end
  435. long getMachTimeInMilliseconds()
  436. {
  437. static const int64_t kOneMillion = 1000 * 1000;
  438. static mach_timebase_info_data_t s_timebase_info;
  439. if (s_timebase_info.denom == 0)
  440. (void) mach_timebase_info(&s_timebase_info);
  441. // mach_absolute_time() returns billionth of seconds, so divide by one million to get milliseconds
  442. return (long)((mach_absolute_time() * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
  443. }
  444. int getKey(unichar keyCode)
  445. {
  446. switch(keyCode)
  447. {
  448. case 0x30:
  449. return Keyboard::KEY_ZERO;
  450. case 0x31:
  451. return Keyboard::KEY_ONE;
  452. case 0x32:
  453. return Keyboard::KEY_TWO;
  454. case 0x33:
  455. return Keyboard::KEY_THREE;
  456. case 0x34:
  457. return Keyboard::KEY_FOUR;
  458. case 0x35:
  459. return Keyboard::KEY_FIVE;
  460. case 0x36:
  461. return Keyboard::KEY_SIX;
  462. case 0x37:
  463. return Keyboard::KEY_SEVEN;
  464. case 0x38:
  465. return Keyboard::KEY_EIGHT;
  466. case 0x39:
  467. return Keyboard::KEY_NINE;
  468. case 0x41:
  469. return Keyboard::KEY_CAPITAL_A;
  470. case 0x42:
  471. return Keyboard::KEY_CAPITAL_B;
  472. case 0x43:
  473. return Keyboard::KEY_CAPITAL_C;
  474. case 0x44:
  475. return Keyboard::KEY_CAPITAL_D;
  476. case 0x45:
  477. return Keyboard::KEY_CAPITAL_E;
  478. case 0x46:
  479. return Keyboard::KEY_CAPITAL_F;
  480. case 0x47:
  481. return Keyboard::KEY_CAPITAL_G;
  482. case 0x48:
  483. return Keyboard::KEY_CAPITAL_H;
  484. case 0x49:
  485. return Keyboard::KEY_CAPITAL_I;
  486. case 0x4A:
  487. return Keyboard::KEY_CAPITAL_J;
  488. case 0x4B:
  489. return Keyboard::KEY_CAPITAL_K;
  490. case 0x4C:
  491. return Keyboard::KEY_CAPITAL_L;
  492. case 0x4D:
  493. return Keyboard::KEY_CAPITAL_M;
  494. case 0x4E:
  495. return Keyboard::KEY_CAPITAL_N;
  496. case 0x4F:
  497. return Keyboard::KEY_CAPITAL_O;
  498. case 0x50:
  499. return Keyboard::KEY_CAPITAL_P;
  500. case 0x51:
  501. return Keyboard::KEY_CAPITAL_Q;
  502. case 0x52:
  503. return Keyboard::KEY_CAPITAL_R;
  504. case 0x53:
  505. return Keyboard::KEY_CAPITAL_S;
  506. case 0x54:
  507. return Keyboard::KEY_CAPITAL_T;
  508. case 0x55:
  509. return Keyboard::KEY_CAPITAL_U;
  510. case 0x56:
  511. return Keyboard::KEY_CAPITAL_V;
  512. case 0x57:
  513. return Keyboard::KEY_CAPITAL_W;
  514. case 0x58:
  515. return Keyboard::KEY_CAPITAL_X;
  516. case 0x59:
  517. return Keyboard::KEY_CAPITAL_Y;
  518. case 0x5A:
  519. return Keyboard::KEY_CAPITAL_Z;
  520. case 0x61:
  521. return Keyboard::KEY_A;
  522. case 0x62:
  523. return Keyboard::KEY_B;
  524. case 0x63:
  525. return Keyboard::KEY_C;
  526. case 0x64:
  527. return Keyboard::KEY_D;
  528. case 0x65:
  529. return Keyboard::KEY_E;
  530. case 0x66:
  531. return Keyboard::KEY_F;
  532. case 0x67:
  533. return Keyboard::KEY_G;
  534. case 0x68:
  535. return Keyboard::KEY_H;
  536. case 0x69:
  537. return Keyboard::KEY_I;
  538. case 0x6A:
  539. return Keyboard::KEY_J;
  540. case 0x6B:
  541. return Keyboard::KEY_K;
  542. case 0x6C:
  543. return Keyboard::KEY_L;
  544. case 0x6D:
  545. return Keyboard::KEY_M;
  546. case 0x6E:
  547. return Keyboard::KEY_N;
  548. case 0x6F:
  549. return Keyboard::KEY_O;
  550. case 0x70:
  551. return Keyboard::KEY_P;
  552. case 0x71:
  553. return Keyboard::KEY_Q;
  554. case 0x72:
  555. return Keyboard::KEY_R;
  556. case 0x73:
  557. return Keyboard::KEY_S;
  558. case 0x74:
  559. return Keyboard::KEY_T;
  560. case 0x75:
  561. return Keyboard::KEY_U;
  562. case 0x76:
  563. return Keyboard::KEY_V;
  564. case 0x77:
  565. return Keyboard::KEY_W;
  566. case 0x78:
  567. return Keyboard::KEY_X;
  568. case 0x79:
  569. return Keyboard::KEY_Y;
  570. case 0x7A:
  571. return Keyboard::KEY_Z;
  572. default:
  573. break;
  574. // Symbol Row 3
  575. case 0x2E:
  576. return Keyboard::KEY_PERIOD;
  577. case 0x2C:
  578. return Keyboard::KEY_COMMA;
  579. case 0x3F:
  580. return Keyboard::KEY_QUESTION;
  581. case 0x21:
  582. return Keyboard::KEY_EXCLAM;
  583. case 0x27:
  584. return Keyboard::KEY_APOSTROPHE;
  585. // Symbols Row 2
  586. case 0x2D:
  587. return Keyboard::KEY_MINUS;
  588. case 0x2F:
  589. return Keyboard::KEY_SLASH;
  590. case 0x3A:
  591. return Keyboard::KEY_COLON;
  592. case 0x3B:
  593. return Keyboard::KEY_SEMICOLON;
  594. case 0x28:
  595. return Keyboard::KEY_LEFT_PARENTHESIS;
  596. case 0x29:
  597. return Keyboard::KEY_RIGHT_PARENTHESIS;
  598. case 0x24:
  599. return Keyboard::KEY_DOLLAR;
  600. case 0x26:
  601. return Keyboard::KEY_AMPERSAND;
  602. case 0x40:
  603. return Keyboard::KEY_AT;
  604. case 0x22:
  605. return Keyboard::KEY_QUOTE;
  606. // Numeric Symbols Row 1
  607. case 0x5B:
  608. return Keyboard::KEY_LEFT_BRACKET;
  609. case 0x5D:
  610. return Keyboard::KEY_RIGHT_BRACKET;
  611. case 0x7B:
  612. return Keyboard::KEY_LEFT_BRACE;
  613. case 0x7D:
  614. return Keyboard::KEY_RIGHT_BRACE;
  615. case 0x23:
  616. return Keyboard::KEY_NUMBER;
  617. case 0x25:
  618. return Keyboard::KEY_PERCENT;
  619. case 0x5E:
  620. return Keyboard::KEY_CIRCUMFLEX;
  621. case 0x2A:
  622. return Keyboard::KEY_ASTERISK;
  623. case 0x2B:
  624. return Keyboard::KEY_PLUS;
  625. case 0x3D:
  626. return Keyboard::KEY_EQUAL;
  627. // Numeric Symbols Row 2
  628. case 0x5F:
  629. return Keyboard::KEY_UNDERSCORE;
  630. case 0x5C:
  631. return Keyboard::KEY_BACK_SLASH;
  632. case 0x7C:
  633. return Keyboard::KEY_BAR;
  634. case 0x7E:
  635. return Keyboard::KEY_TILDE;
  636. case 0x3C:
  637. return Keyboard::KEY_LESS_THAN;
  638. case 0x3E:
  639. return Keyboard::KEY_GREATER_THAN;
  640. case 0x80:
  641. return Keyboard::KEY_EURO;
  642. case 0xA3:
  643. return Keyboard::KEY_POUND;
  644. case 0xA5:
  645. return Keyboard::KEY_YEN;
  646. case 0xB7:
  647. return Keyboard::KEY_MIDDLE_DOT;
  648. }
  649. return Keyboard::KEY_NONE;
  650. }
  651. namespace gameplay
  652. {
  653. extern void printError(const char* format, ...)
  654. {
  655. va_list argptr;
  656. va_start(argptr, format);
  657. vfprintf(stderr, format, argptr);
  658. fprintf(stderr, "\n");
  659. va_end(argptr);
  660. }
  661. Platform::Platform(Game* game)
  662. : _game(game)
  663. {
  664. }
  665. Platform::Platform(const Platform& copy)
  666. {
  667. // hidden
  668. }
  669. Platform::~Platform()
  670. {
  671. }
  672. Platform* Platform::create(Game* game)
  673. {
  674. Platform* platform = new Platform(game);
  675. return platform;
  676. }
  677. int Platform::enterMessagePump()
  678. {
  679. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  680. [AppDelegate load];
  681. UIApplicationMain(0, nil, NSStringFromClass([AppDelegate class]), NSStringFromClass([AppDelegate class]));
  682. [pool release];
  683. return EXIT_SUCCESS;
  684. }
  685. void Platform::signalShutdown() {
  686. // Cannot 'exit' an iOS Application
  687. assert(false);
  688. [__view stopUpdating];
  689. exit(0);
  690. }
  691. unsigned int Platform::getDisplayWidth()
  692. {
  693. return WINDOW_WIDTH;
  694. }
  695. unsigned int Platform::getDisplayHeight()
  696. {
  697. return WINDOW_HEIGHT;
  698. }
  699. long Platform::getAbsoluteTime()
  700. {
  701. __timeAbsolute = getMachTimeInMilliseconds();
  702. return __timeAbsolute;
  703. }
  704. void Platform::setAbsoluteTime(long time)
  705. {
  706. __timeAbsolute = time;
  707. }
  708. bool Platform::isVsync()
  709. {
  710. return __vsync;
  711. }
  712. void Platform::setVsync(bool enable)
  713. {
  714. __vsync = enable;
  715. }
  716. int Platform::getOrientationAngle()
  717. {
  718. return 0;
  719. }
  720. void Platform::getAccelerometerValues(float* pitch, float* roll)
  721. {
  722. [__appDelegate getAccelerometerPitch:pitch roll:roll];
  723. }
  724. void Platform::setMultiTouch(bool enabled)
  725. {
  726. __view.multipleTouchEnabled = enabled;
  727. }
  728. bool Platform::isMultiTouch()
  729. {
  730. return __view.multipleTouchEnabled;
  731. }
  732. void Platform::swapBuffers()
  733. {
  734. if (__view)
  735. [__view swapBuffers];
  736. }
  737. void Platform::displayKeyboard(bool display)
  738. {
  739. if(__view)
  740. {
  741. if(display) [__view showKeyboard];
  742. else [__view dismissKeyboard];
  743. }
  744. }
  745. void Platform::sleep(long ms)
  746. {
  747. usleep(ms * 1000);
  748. }
  749. }
  750. #endif