entry_ios.mm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright 2011-2015 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. #if __IPHONE_8_0 && !TARGET_IPHONE_SIMULATOR // check if sdk/target supports metal
  11. # import <Metal/Metal.h>
  12. # import <QuartzCore/CAMetalLayer.h>
  13. //# define HAS_METAL_SDK
  14. #endif
  15. #include <bgfx/bgfxplatform.h>
  16. #include <bx/uint32_t.h>
  17. #include <bx/thread.h>
  18. namespace entry
  19. {
  20. struct MainThreadEntry
  21. {
  22. int m_argc;
  23. char** m_argv;
  24. static int32_t threadFunc(void* _userData);
  25. };
  26. static WindowHandle s_defaultWindow = { 0 };
  27. struct Context
  28. {
  29. Context(uint32_t _width, uint32_t _height)
  30. {
  31. static const char* argv[1] = { "ios" };
  32. m_mte.m_argc = 1;
  33. m_mte.m_argv = const_cast<char**>(argv);
  34. m_eventQueue.postSizeEvent(s_defaultWindow, _width, _height);
  35. // Prevent render thread creation.
  36. bgfx::renderFrame();
  37. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  38. }
  39. ~Context()
  40. {
  41. m_thread.shutdown();
  42. }
  43. MainThreadEntry m_mte;
  44. bx::Thread m_thread;
  45. EventQueue m_eventQueue;
  46. };
  47. static Context* s_ctx;
  48. int32_t MainThreadEntry::threadFunc(void* _userData)
  49. {
  50. CFBundleRef mainBundle = CFBundleGetMainBundle();
  51. if ( mainBundle != nil )
  52. {
  53. CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
  54. if ( resourcesURL != nil )
  55. {
  56. char path[PATH_MAX];
  57. if (CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX) )
  58. {
  59. chdir(path);
  60. }
  61. CFRelease(resourcesURL);
  62. }
  63. }
  64. MainThreadEntry* self = (MainThreadEntry*)_userData;
  65. int32_t result = main(self->m_argc, self->m_argv);
  66. return result;
  67. }
  68. const Event* poll()
  69. {
  70. return s_ctx->m_eventQueue.poll();
  71. }
  72. const Event* poll(WindowHandle _handle)
  73. {
  74. return s_ctx->m_eventQueue.poll(_handle);
  75. }
  76. void release(const Event* _event)
  77. {
  78. s_ctx->m_eventQueue.release(_event);
  79. }
  80. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  81. {
  82. BX_UNUSED(_x, _y, _width, _height, _flags, _title);
  83. WindowHandle handle = { UINT16_MAX };
  84. return handle;
  85. }
  86. void destroyWindow(WindowHandle _handle)
  87. {
  88. BX_UNUSED(_handle);
  89. }
  90. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  91. {
  92. BX_UNUSED(_handle, _x, _y);
  93. }
  94. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  95. {
  96. BX_UNUSED(_handle, _width, _height);
  97. }
  98. void setWindowTitle(WindowHandle _handle, const char* _title)
  99. {
  100. BX_UNUSED(_handle, _title);
  101. }
  102. void toggleWindowFrame(WindowHandle _handle)
  103. {
  104. BX_UNUSED(_handle);
  105. }
  106. void toggleFullscreen(WindowHandle _handle)
  107. {
  108. BX_UNUSED(_handle);
  109. }
  110. void setMouseLock(WindowHandle _handle, bool _lock)
  111. {
  112. BX_UNUSED(_handle, _lock);
  113. }
  114. } // namespace entry
  115. using namespace entry;
  116. #ifdef HAS_METAL_SDK
  117. static id<MTLDevice> m_device = NULL;
  118. #else
  119. static void* m_device = NULL;
  120. #endif
  121. @interface ViewController : UIViewController
  122. @end
  123. @implementation ViewController
  124. - (BOOL)prefersStatusBarHidden
  125. {
  126. return YES;
  127. }
  128. @end
  129. @interface View : UIView
  130. {
  131. CADisplayLink* m_displayLink;
  132. }
  133. @end
  134. @implementation View
  135. + (Class)layerClass
  136. {
  137. #ifdef HAS_METAL_SDK
  138. Class metalClass = NSClassFromString(@"CAMetalLayer"); //is metal runtime sdk available
  139. if ( metalClass != nil)
  140. {
  141. m_device = MTLCreateSystemDefaultDevice(); // is metal supported on this device (is there a better way to do this - without creating device ?)
  142. if (m_device)
  143. {
  144. [m_device retain];
  145. return metalClass;
  146. }
  147. }
  148. #endif
  149. return [CAEAGLLayer class];
  150. }
  151. - (id)initWithFrame:(CGRect)rect
  152. {
  153. self = [super initWithFrame:rect];
  154. if (nil == self)
  155. {
  156. return nil;
  157. }
  158. bgfx::PlatformData pd;
  159. pd.ndt = NULL;
  160. pd.nwh = self.layer;
  161. pd.context = m_device;
  162. pd.backBuffer = NULL;
  163. pd.backBufferDS = NULL;
  164. bgfx::setPlatformData(pd);
  165. return self;
  166. }
  167. - (void)layoutSubviews
  168. {
  169. uint32_t frameW = (uint32_t)(self.contentScaleFactor * self.frame.size.width);
  170. uint32_t frameH = (uint32_t)(self.contentScaleFactor * self.frame.size.height);
  171. s_ctx->m_eventQueue.postSizeEvent(s_defaultWindow, frameW, frameH);
  172. }
  173. - (void)start
  174. {
  175. if (nil == m_displayLink)
  176. {
  177. m_displayLink = [self.window.screen displayLinkWithTarget:self selector:@selector(renderFrame)];
  178. //[m_displayLink setFrameInterval:1];
  179. //[m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  180. // [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop]];
  181. [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  182. }
  183. }
  184. - (void)stop
  185. {
  186. if (nil != m_displayLink)
  187. {
  188. [m_displayLink invalidate];
  189. m_displayLink = nil;
  190. }
  191. }
  192. - (void)renderFrame
  193. {
  194. bgfx::renderFrame();
  195. }
  196. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  197. {
  198. BX_UNUSED(touches);
  199. UITouch *touch = [[event allTouches] anyObject];
  200. CGPoint touchLocation = [touch locationInView:self];
  201. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0);
  202. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, true);
  203. }
  204. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  205. {
  206. BX_UNUSED(touches);
  207. UITouch *touch = [[event allTouches] anyObject];
  208. CGPoint touchLocation = [touch locationInView:self];
  209. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, false);
  210. }
  211. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  212. {
  213. BX_UNUSED(touches);
  214. UITouch *touch = [[event allTouches] anyObject];
  215. CGPoint touchLocation = [touch locationInView:self];
  216. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0);
  217. }
  218. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  219. {
  220. BX_UNUSED(touches);
  221. UITouch *touch = [[event allTouches] anyObject];
  222. CGPoint touchLocation = [touch locationInView:self];
  223. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, false);
  224. }
  225. @end
  226. @interface AppDelegate : UIResponder<UIApplicationDelegate>
  227. {
  228. UIWindow* m_window;
  229. View* m_view;
  230. }
  231. @property (nonatomic, retain) UIWindow* m_window;
  232. @property (nonatomic, retain) View* m_view;
  233. @end
  234. @implementation AppDelegate
  235. @synthesize m_window;
  236. @synthesize m_view;
  237. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  238. {
  239. BX_UNUSED(application, launchOptions);
  240. CGRect rect = [ [UIScreen mainScreen] bounds];
  241. m_window = [ [UIWindow alloc] initWithFrame: rect];
  242. m_view = [ [View alloc] initWithFrame: rect];
  243. [m_window addSubview: m_view];
  244. UIViewController *viewController = [[ViewController alloc] init];
  245. viewController.view = m_view;
  246. [m_window setRootViewController:viewController];
  247. [m_window makeKeyAndVisible];
  248. [m_window makeKeyAndVisible];
  249. //float scaleFactor = [[UIScreen mainScreen] scale]; // should use this, but ui is too small on ipad retina
  250. float scaleFactor = 1.0f;
  251. [m_view setContentScaleFactor: scaleFactor ];
  252. s_ctx = new Context((uint32_t)(scaleFactor*rect.size.width), (uint32_t)(scaleFactor*rect.size.height));
  253. return YES;
  254. }
  255. - (void)applicationWillResignActive:(UIApplication *)application
  256. {
  257. BX_UNUSED(application);
  258. s_ctx->m_eventQueue.postSuspendEvent(s_defaultWindow, Suspend::WillSuspend);
  259. [m_view stop];
  260. }
  261. - (void)applicationDidEnterBackground:(UIApplication *)application
  262. {
  263. BX_UNUSED(application);
  264. s_ctx->m_eventQueue.postSuspendEvent(s_defaultWindow, Suspend::DidSuspend);
  265. }
  266. - (void)applicationWillEnterForeground:(UIApplication *)application
  267. {
  268. BX_UNUSED(application);
  269. s_ctx->m_eventQueue.postSuspendEvent(s_defaultWindow, Suspend::WillResume);
  270. }
  271. - (void)applicationDidBecomeActive:(UIApplication *)application
  272. {
  273. BX_UNUSED(application);
  274. s_ctx->m_eventQueue.postSuspendEvent(s_defaultWindow, Suspend::DidResume);
  275. [m_view start];
  276. }
  277. - (void)applicationWillTerminate:(UIApplication *)application
  278. {
  279. BX_UNUSED(application);
  280. [m_view stop];
  281. }
  282. - (void)dealloc
  283. {
  284. [m_window release];
  285. [m_view release];
  286. [super dealloc];
  287. }
  288. @end
  289. int main(int _argc, char* _argv[])
  290. {
  291. NSAutoreleasePool* pool = [ [NSAutoreleasePool alloc] init];
  292. int exitCode = UIApplicationMain(_argc, _argv, @"UIApplication", NSStringFromClass([AppDelegate class]) );
  293. [pool release];
  294. return exitCode;
  295. }
  296. #endif // BX_PLATFORM_IOS