PlatformiOS.mm 22 KB

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