entry_ios.mm 8.7 KB

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