entry_ios.mm 8.7 KB

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