PlatformiOS.mm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 <unistd.h>
  8. #import <UIKit/UIKit.h>
  9. #import <QuartzCore/QuartzCore.h>
  10. #import <CoreMotion/CoreMotion.h>
  11. #import <OpenGLES/EAGL.h>
  12. #import <OpenGLES/EAGLDrawable.h>
  13. #import <OpenGLES/ES2/gl.h>
  14. #import <OpenGLES/ES2/glext.h>
  15. #import <mach/mach_time.h>
  16. using namespace std;
  17. using namespace gameplay;
  18. // UIScreen bounds are provided as if device was in portrait mode Gameplay defaults to landscape
  19. extern const int WINDOW_WIDTH = [[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale];
  20. extern const int WINDOW_HEIGHT = [[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale];
  21. extern const int WINDOW_SCALE = [[UIScreen mainScreen] scale];
  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. // A system version of 3.1 or greater is required to use CADisplayLink.
  73. NSString *reqSysVer = @"3.1";
  74. NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
  75. if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
  76. {
  77. // Log the system version
  78. NSLog(@"System Version: %@", currSysVer);
  79. }
  80. else
  81. {
  82. printError("Invalid OS Version: %s\n", (currSysVer == NULL?"NULL":[currSysVer cStringUsingEncoding:NSASCIIStringEncoding]));
  83. [self release];
  84. return nil;
  85. }
  86. // Configure the CAEAGLLayer and setup out the rendering context
  87. CGFloat scale = [[UIScreen mainScreen] scale];
  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. self.contentScaleFactor = scale;
  94. layer.contentsScale = scale;
  95. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  96. if (!context || ![EAGLContext setCurrentContext:context])
  97. {
  98. [self release];
  99. return nil;
  100. }
  101. if (!defaultFramebuffer)
  102. {
  103. [self createFramebuffer];
  104. }
  105. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  106. glViewport(0, 0, framebufferWidth, framebufferHeight);
  107. // Initialize Internal Defaults
  108. displayLink = nil;
  109. defaultFramebuffer = 0;
  110. colorRenderbuffer = 0;
  111. depthRenderbuffer = 0;
  112. framebufferWidth = 0;
  113. framebufferHeight = 0;
  114. swapInterval = 1;
  115. updating = FALSE;
  116. [self createFramebuffer];
  117. // Set the resource path and initalize the game
  118. NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/"];
  119. FileSystem::setResourcePath([bundlePath fileSystemRepresentation]);
  120. _game = Game::getInstance();
  121. __timeStart = getMachTimeInMilliseconds();
  122. _game->run(WINDOW_WIDTH, WINDOW_HEIGHT);
  123. }
  124. return self;
  125. }
  126. - (void) dealloc
  127. {
  128. _game->exit();
  129. [self deleteFramebuffer];
  130. if ([EAGLContext currentContext] == context)
  131. {
  132. [EAGLContext setCurrentContext:nil];
  133. }
  134. [context release];
  135. [super dealloc];
  136. }
  137. - (BOOL)canBecomeFirstResponder
  138. {
  139. // Override so we can control the keyboard
  140. return YES;
  141. }
  142. - (void) layoutSubviews
  143. {
  144. // Called on 'resize'
  145. [self deleteFramebuffer];
  146. }
  147. - (void)createFramebuffer
  148. {
  149. // iOS Requires all content go to a rendering buffer then it is swapped into the windows rendering surface
  150. assert(defaultFramebuffer == 0);
  151. // Create the default frame buffer, and render buffer
  152. glGenFramebuffers(1, &defaultFramebuffer);
  153. glGenRenderbuffers(1, &colorRenderbuffer);
  154. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  155. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  156. // request storage, width, and height of the view that we will render in
  157. [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
  158. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
  159. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
  160. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
  161. glGenRenderbuffers(1, &depthRenderbuffer);
  162. glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
  163. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24_OES, framebufferWidth, framebufferHeight);
  164. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
  165. // Sanity check, ensure that the framebuffer is valid
  166. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  167. NSLog(@"ERROR: Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
  168. }
  169. - (void)deleteFramebuffer
  170. {
  171. if (context)
  172. {
  173. [EAGLContext setCurrentContext:context];
  174. if (defaultFramebuffer)
  175. {
  176. glDeleteFramebuffers(1, &defaultFramebuffer);
  177. defaultFramebuffer = 0;
  178. }
  179. if (colorRenderbuffer)
  180. {
  181. glDeleteRenderbuffers(1, &colorRenderbuffer);
  182. colorRenderbuffer = 0;
  183. }
  184. if (depthRenderbuffer)
  185. {
  186. glDeleteRenderbuffers(1, &depthRenderbuffer);
  187. depthRenderbuffer = 0;
  188. }
  189. }
  190. }
  191. - (void)setSwapInterval:(NSInteger)interval
  192. {
  193. if (interval >= 1)
  194. {
  195. swapInterval = interval;
  196. if (updating)
  197. {
  198. [self stopUpdating];
  199. [self startUpdating];
  200. }
  201. }
  202. }
  203. - (int)swapInterval
  204. {
  205. return swapInterval;
  206. }
  207. - (void)swapBuffers
  208. {
  209. if (context != nil)
  210. {
  211. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  212. [context presentRenderbuffer:GL_RENDERBUFFER];
  213. }
  214. }
  215. - (void)startUpdating
  216. {
  217. if (!updating)
  218. {
  219. displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)];
  220. [displayLink setFrameInterval:swapInterval];
  221. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  222. _game->resume();
  223. updating = TRUE;
  224. }
  225. }
  226. - (void)stopUpdating
  227. {
  228. if (updating)
  229. {
  230. _game->pause();
  231. [displayLink invalidate];
  232. displayLink = nil;
  233. updating = FALSE;
  234. }
  235. }
  236. - (void)update:(id)sender
  237. {
  238. if (context != nil)
  239. {
  240. [EAGLContext setCurrentContext:context];
  241. if (!defaultFramebuffer)
  242. [self createFramebuffer];
  243. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
  244. glViewport(0, 0, framebufferWidth, framebufferHeight);
  245. if (_game && _game->getState() == Game::RUNNING)
  246. _game->frame();
  247. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
  248. [context presentRenderbuffer:GL_RENDERBUFFER];
  249. }
  250. }
  251. - (BOOL)showKeyboard
  252. {
  253. return [self becomeFirstResponder];
  254. }
  255. - (BOOL)dismissKeyboard
  256. {
  257. return [self resignFirstResponder];
  258. }
  259. - (void)insertText:(NSString*)text
  260. {
  261. if([text length] == 0) return;
  262. assert([text length] == 1);
  263. unichar c = [text characterAtIndex:0];
  264. int key = getKey(c);
  265. Platform::keyEventInternal(Keyboard::KEY_PRESS, key);
  266. Platform::keyEventInternal(Keyboard::KEY_RELEASE, key);
  267. }
  268. - (void)deleteBackward
  269. {
  270. Platform::keyEventInternal(Keyboard::KEY_PRESS, Keyboard::KEY_BACKSPACE);
  271. Platform::keyEventInternal(Keyboard::KEY_RELEASE, Keyboard::KEY_BACKSPACE);
  272. }
  273. - (BOOL)hasText
  274. {
  275. return YES;
  276. }
  277. - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
  278. {
  279. unsigned int touchID = 0;
  280. for(UITouch* touch in touches)
  281. {
  282. CGPoint touchPoint = [touch locationInView:self];
  283. if(self.multipleTouchEnabled == YES)
  284. {
  285. touchID = [touch hash];
  286. }
  287. Platform::touchEventInternal(Touch::TOUCH_PRESS, touchPoint.x * WINDOW_SCALE, touchPoint.y * WINDOW_SCALE, touchID);
  288. }
  289. }
  290. - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
  291. {
  292. unsigned int touchID = 0;
  293. for(UITouch* touch in touches)
  294. {
  295. CGPoint touchPoint = [touch locationInView:self];
  296. if(self.multipleTouchEnabled == YES)
  297. touchID = [touch hash];
  298. Platform::touchEventInternal(Touch::TOUCH_RELEASE, touchPoint.x * WINDOW_SCALE, touchPoint.y * WINDOW_SCALE, touchID);
  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 touchID = 0;
  309. for(UITouch* touch in touches)
  310. {
  311. CGPoint touchPoint = [touch locationInView:self];
  312. if(self.multipleTouchEnabled == YES)
  313. touchID = [touch hash];
  314. Platform::touchEventInternal(Touch::TOUCH_MOVE, touchPoint.x * WINDOW_SCALE, touchPoint.y * WINDOW_SCALE, touchID);
  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. }
  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) : _game(game)
  661. {
  662. }
  663. Platform::Platform(const Platform& copy)
  664. {
  665. }
  666. Platform::~Platform()
  667. {
  668. }
  669. Platform* Platform::create(Game* game)
  670. {
  671. Platform* platform = new Platform(game);
  672. return platform;
  673. }
  674. int Platform::enterMessagePump()
  675. {
  676. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  677. [AppDelegate load];
  678. UIApplicationMain(0, nil, NSStringFromClass([AppDelegate class]), NSStringFromClass([AppDelegate class]));
  679. [pool release];
  680. return EXIT_SUCCESS;
  681. }
  682. void Platform::signalShutdown()
  683. {
  684. // Cannot 'exit' an iOS Application
  685. assert(false);
  686. [__view stopUpdating];
  687. exit(0);
  688. }
  689. unsigned int Platform::getDisplayWidth()
  690. {
  691. return WINDOW_WIDTH;
  692. }
  693. unsigned int Platform::getDisplayHeight()
  694. {
  695. return WINDOW_HEIGHT;
  696. }
  697. long Platform::getAbsoluteTime()
  698. {
  699. __timeAbsolute = getMachTimeInMilliseconds();
  700. return __timeAbsolute;
  701. }
  702. void Platform::setAbsoluteTime(long time)
  703. {
  704. __timeAbsolute = time;
  705. }
  706. bool Platform::isVsync()
  707. {
  708. return __vsync;
  709. }
  710. void Platform::setVsync(bool enable)
  711. {
  712. __vsync = enable;
  713. }
  714. int Platform::getOrientationAngle()
  715. {
  716. return 0;
  717. }
  718. void Platform::getAccelerometerValues(float* pitch, float* roll)
  719. {
  720. [__appDelegate getAccelerometerPitch:pitch roll:roll];
  721. }
  722. void Platform::setMultiTouch(bool enabled)
  723. {
  724. __view.multipleTouchEnabled = enabled;
  725. }
  726. bool Platform::isMultiTouch()
  727. {
  728. return __view.multipleTouchEnabled;
  729. }
  730. void Platform::swapBuffers()
  731. {
  732. if (__view)
  733. [__view swapBuffers];
  734. }
  735. void Platform::displayKeyboard(bool display)
  736. {
  737. if(__view)
  738. {
  739. if(display)
  740. {
  741. [__view showKeyboard];
  742. }
  743. else
  744. {
  745. [__view dismissKeyboard];
  746. }
  747. }
  748. }
  749. void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  750. {
  751. if (!Form::touchEventInternal(evt, x, y, contactIndex))
  752. {
  753. Game::getInstance()->touchEvent(evt, x, y, contactIndex);
  754. }
  755. }
  756. void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
  757. {
  758. gameplay::Game::getInstance()->keyEvent(evt, key);
  759. Form::keyEventInternal(evt, key);
  760. }
  761. void Platform::sleep(long ms)
  762. {
  763. usleep(ms * 1000);
  764. }
  765. }
  766. #endif