entry_ios.mm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_IOS
  7. #import <Foundation/Foundation.h>
  8. #import <UIKit/UIKit.h>
  9. #import <QuartzCore/CAEAGLLayer.h>
  10. #include <bgfxplatform.h>
  11. #include <bx/uint32_t.h>
  12. #include <bx/thread.h>
  13. namespace entry
  14. {
  15. struct MainThreadEntry
  16. {
  17. int m_argc;
  18. char** m_argv;
  19. static int32_t threadFunc(void* _userData);
  20. };
  21. struct Context
  22. {
  23. Context(uint32_t _width, uint32_t _height)
  24. {
  25. const char* argv[1] = { "ios" };
  26. m_mte.m_argc = 1;
  27. m_mte.m_argv = const_cast<char**>(argv);
  28. m_eventQueue.postSizeEvent(_width, _height);
  29. // Prevent render thread creation.
  30. bgfx::renderFrame();
  31. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  32. }
  33. ~Context()
  34. {
  35. m_thread.shutdown();
  36. }
  37. MainThreadEntry m_mte;
  38. bx::Thread m_thread;
  39. EventQueue m_eventQueue;
  40. };
  41. static Context* s_ctx;
  42. int32_t MainThreadEntry::threadFunc(void* _userData)
  43. {
  44. CFBundleRef mainBundle = CFBundleGetMainBundle();
  45. CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
  46. char path[PATH_MAX];
  47. if (CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX) )
  48. {
  49. chdir(path);
  50. }
  51. CFRelease(resourcesURL);
  52. MainThreadEntry* self = (MainThreadEntry*)_userData;
  53. int32_t result = main(self->m_argc, self->m_argv);
  54. return result;
  55. }
  56. const Event* poll()
  57. {
  58. return s_ctx->m_eventQueue.poll();
  59. }
  60. void release(const Event* _event)
  61. {
  62. s_ctx->m_eventQueue.release(_event);
  63. }
  64. void setWindowSize(uint32_t _width, uint32_t _height)
  65. {
  66. BX_UNUSED(_width, _height);
  67. }
  68. void toggleWindowFrame()
  69. {
  70. }
  71. void setMouseLock(bool _lock)
  72. {
  73. BX_UNUSED(_lock);
  74. }
  75. } // namespace entry
  76. using namespace entry;
  77. @interface View : UIView
  78. {
  79. CADisplayLink* m_displayLink;
  80. }
  81. @end
  82. @implementation View
  83. + (Class)layerClass
  84. {
  85. return [CAEAGLLayer class];
  86. }
  87. - (id)initWithFrame:(CGRect)rect
  88. {
  89. self = [super initWithFrame:rect];
  90. if (nil == self)
  91. {
  92. return nil;
  93. }
  94. CAEAGLLayer* layer = (CAEAGLLayer*)self.layer;
  95. bgfx::iosSetEaglLayer(layer);
  96. return self;
  97. }
  98. - (void)start
  99. {
  100. if (nil == m_displayLink)
  101. {
  102. m_displayLink = [self.window.screen displayLinkWithTarget:self selector:@selector(renderFrame)];
  103. //[m_displayLink setFrameInterval:1];
  104. //[m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  105. // [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop]];
  106. [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  107. }
  108. }
  109. - (void)stop
  110. {
  111. if (nil != m_displayLink)
  112. {
  113. [m_displayLink invalidate];
  114. m_displayLink = nil;
  115. }
  116. }
  117. - (void)renderFrame
  118. {
  119. bgfx::renderFrame();
  120. }
  121. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  122. {
  123. UITouch *touch = [[event allTouches] anyObject];
  124. CGPoint touchLocation = [touch locationInView:self];
  125. s_ctx->m_eventQueue.postMouseEvent(touchLocation.x, touchLocation.y);
  126. s_ctx->m_eventQueue.postMouseEvent(touchLocation.x, touchLocation.y, MouseButton::Left, true);
  127. }
  128. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  129. {
  130. UITouch *touch = [[event allTouches] anyObject];
  131. CGPoint touchLocation = [touch locationInView:self];
  132. s_ctx->m_eventQueue.postMouseEvent(touchLocation.x, touchLocation.y, MouseButton::Left, false);
  133. }
  134. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  135. {
  136. UITouch *touch = [[event allTouches] anyObject];
  137. CGPoint touchLocation = [touch locationInView:self];
  138. s_ctx->m_eventQueue.postMouseEvent(touchLocation.x, touchLocation.y);
  139. }
  140. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  141. {
  142. UITouch *touch = [[event allTouches] anyObject];
  143. CGPoint touchLocation = [touch locationInView:self];
  144. s_ctx->m_eventQueue.postMouseEvent(touchLocation.x, touchLocation.y, MouseButton::Left, false);
  145. }
  146. @end
  147. @interface AppDelegate : UIResponder<UIApplicationDelegate>
  148. {
  149. UIWindow* m_window;
  150. View* m_view;
  151. }
  152. @property (nonatomic, retain) UIWindow* m_window;
  153. @property (nonatomic, retain) View* m_view;
  154. @end
  155. @implementation AppDelegate
  156. @synthesize m_window;
  157. @synthesize m_view;
  158. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  159. {
  160. BX_UNUSED(application, launchOptions);
  161. CGRect rect = [ [UIScreen mainScreen] bounds];
  162. m_window = [ [UIWindow alloc] initWithFrame: rect];
  163. m_view = [ [View alloc] initWithFrame: rect];
  164. [m_window addSubview: m_view];
  165. [m_window makeKeyAndVisible];
  166. //float scaleFactor = [[UIScreen mainScreen] scale]; // should use this, but ui is too small on ipad retina
  167. float scaleFactor = 1.0f;
  168. [m_view setContentScaleFactor: scaleFactor ];
  169. s_ctx = new Context((uint32_t)(scaleFactor*rect.size.width), (uint32_t)(scaleFactor*rect.size.height));
  170. return YES;
  171. }
  172. - (void)applicationWillResignActive:(UIApplication *)application
  173. {
  174. BX_UNUSED(application);
  175. [m_view stop];
  176. }
  177. - (void)applicationDidEnterBackground:(UIApplication *)application
  178. {
  179. BX_UNUSED(application);
  180. }
  181. - (void)applicationWillEnterForeground:(UIApplication *)application
  182. {
  183. BX_UNUSED(application);
  184. }
  185. - (void)applicationDidBecomeActive:(UIApplication *)application
  186. {
  187. BX_UNUSED(application);
  188. [m_view start];
  189. }
  190. - (void)applicationWillTerminate:(UIApplication *)application
  191. {
  192. BX_UNUSED(application);
  193. [m_view stop];
  194. }
  195. - (void)dealloc
  196. {
  197. [m_window release];
  198. [m_view release];
  199. [super dealloc];
  200. }
  201. @end
  202. int main(int _argc, char* _argv[])
  203. {
  204. NSAutoreleasePool* pool = [ [NSAutoreleasePool alloc] init];
  205. int exitCode = UIApplicationMain(_argc, _argv, @"UIApplication", NSStringFromClass([AppDelegate class]) );
  206. [pool release];
  207. return exitCode;
  208. }
  209. #endif // BX_PLATFORM_IOS