PlatformiOS.mm 21 KB

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