PlatformiOS.mm 45 KB

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