entry_ios.mm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright 2011-2025 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. bgfx::NativeWindowHandleType::Enum getNativeWindowHandleType()
  129. {
  130. return bgfx::NativeWindowHandleType::Default;
  131. }
  132. } // namespace entry
  133. using namespace entry;
  134. @interface ViewController : UIViewController
  135. @end
  136. @implementation ViewController
  137. - (BOOL)prefersStatusBarHidden
  138. {
  139. return YES;
  140. }
  141. @end
  142. @interface View : UIView
  143. {
  144. CADisplayLink* m_displayLink;
  145. }
  146. @end
  147. @implementation View
  148. + (Class)layerClass
  149. {
  150. #ifdef HAS_METAL_SDK
  151. static id<MTLDevice> device = NULL;
  152. Class metalClass = NSClassFromString(@"CAMetalLayer"); //is metal runtime sdk available
  153. if ( metalClass != nil)
  154. {
  155. device = MTLCreateSystemDefaultDevice(); // is metal supported on this device (is there a better way to do this - without creating device ?)
  156. if (NULL != device)
  157. {
  158. [device retain];
  159. return metalClass;
  160. }
  161. }
  162. #endif
  163. return [CAEAGLLayer class];
  164. }
  165. - (id)initWithFrame:(CGRect)rect
  166. {
  167. self = [super initWithFrame:rect];
  168. if (nil == self)
  169. {
  170. return nil;
  171. }
  172. return self;
  173. }
  174. - (void)layoutSubviews
  175. {
  176. uint32_t frameW = (uint32_t)(self.contentScaleFactor * self.frame.size.width);
  177. uint32_t frameH = (uint32_t)(self.contentScaleFactor * self.frame.size.height);
  178. s_ctx->m_eventQueue.postSizeEvent(s_defaultWindow, frameW, frameH);
  179. }
  180. - (void)start
  181. {
  182. if (nil == m_displayLink)
  183. {
  184. m_displayLink = [self.window.screen displayLinkWithTarget:self selector:@selector(renderFrame)];
  185. //[m_displayLink setFrameInterval:1];
  186. //[m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  187. //[m_displayLink addToRunLoop:[NSRunLoop currentRunLoop]];
  188. [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  189. }
  190. }
  191. - (void)stop
  192. {
  193. if (nil != m_displayLink)
  194. {
  195. [m_displayLink invalidate];
  196. m_displayLink = nil;
  197. }
  198. }
  199. - (void)renderFrame
  200. {
  201. bgfx::renderFrame();
  202. }
  203. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  204. {
  205. BX_UNUSED(touches);
  206. UITouch *touch = [[event allTouches] anyObject];
  207. CGPoint touchLocation = [touch locationInView:self];
  208. touchLocation.x *= self.contentScaleFactor;
  209. touchLocation.y *= self.contentScaleFactor;
  210. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0);
  211. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, true);
  212. }
  213. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  214. {
  215. BX_UNUSED(touches);
  216. UITouch *touch = [[event allTouches] anyObject];
  217. CGPoint touchLocation = [touch locationInView:self];
  218. touchLocation.x *= self.contentScaleFactor;
  219. touchLocation.y *= self.contentScaleFactor;
  220. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, false);
  221. }
  222. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  223. {
  224. BX_UNUSED(touches);
  225. UITouch *touch = [[event allTouches] anyObject];
  226. CGPoint touchLocation = [touch locationInView:self];
  227. touchLocation.x *= self.contentScaleFactor;
  228. touchLocation.y *= self.contentScaleFactor;
  229. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0);
  230. }
  231. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  232. {
  233. BX_UNUSED(touches);
  234. UITouch *touch = [[event allTouches] anyObject];
  235. CGPoint touchLocation = [touch locationInView:self];
  236. touchLocation.x *= self.contentScaleFactor;
  237. touchLocation.y *= self.contentScaleFactor;
  238. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, false);
  239. }
  240. @end
  241. @interface AppDelegate : UIResponder<UIApplicationDelegate>
  242. {
  243. UIWindow* m_window;
  244. View* m_view;
  245. }
  246. @property (nonatomic, retain) UIWindow* m_window;
  247. @property (nonatomic, retain) View* m_view;
  248. @end
  249. @implementation AppDelegate
  250. @synthesize m_window;
  251. @synthesize m_view;
  252. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  253. {
  254. BX_UNUSED(application, launchOptions);
  255. CGRect rect = [ [UIScreen mainScreen] bounds];
  256. m_window = [ [UIWindow alloc] initWithFrame: rect];
  257. m_view = [ [View alloc] initWithFrame: rect];
  258. [m_window addSubview: m_view];
  259. UIViewController *viewController = [[ViewController alloc] init];
  260. viewController.view = m_view;
  261. [m_window setRootViewController:viewController];
  262. [m_window makeKeyAndVisible];
  263. [m_window makeKeyAndVisible];
  264. float scaleFactor = [[UIScreen mainScreen] scale];
  265. [m_view setContentScaleFactor: scaleFactor ];
  266. s_ctx = new Context((uint32_t)(scaleFactor*rect.size.width), (uint32_t)(scaleFactor*rect.size.height));
  267. s_ctx->m_window = m_view.layer;
  268. return YES;
  269. }
  270. - (void)applicationWillResignActive:(UIApplication *)application
  271. {
  272. BX_UNUSED(application);
  273. s_ctx->m_eventQueue.postSuspendEvent(s_defaultWindow, Suspend::WillSuspend);
  274. [m_view stop];
  275. }
  276. - (void)applicationDidEnterBackground:(UIApplication *)application
  277. {
  278. BX_UNUSED(application);
  279. s_ctx->m_eventQueue.postSuspendEvent(s_defaultWindow, Suspend::DidSuspend);
  280. }
  281. - (void)applicationWillEnterForeground:(UIApplication *)application
  282. {
  283. BX_UNUSED(application);
  284. s_ctx->m_eventQueue.postSuspendEvent(s_defaultWindow, Suspend::WillResume);
  285. }
  286. - (void)applicationDidBecomeActive:(UIApplication *)application
  287. {
  288. BX_UNUSED(application);
  289. s_ctx->m_eventQueue.postSuspendEvent(s_defaultWindow, Suspend::DidResume);
  290. [m_view start];
  291. }
  292. - (void)applicationWillTerminate:(UIApplication *)application
  293. {
  294. BX_UNUSED(application);
  295. [m_view stop];
  296. }
  297. - (void)dealloc
  298. {
  299. [m_window release];
  300. [m_view release];
  301. [super dealloc];
  302. }
  303. @end
  304. int main(int _argc, const char* const* _argv)
  305. {
  306. NSAutoreleasePool* pool = [ [NSAutoreleasePool alloc] init];
  307. int exitCode = UIApplicationMain(_argc, (char**)_argv, @"UIApplication", NSStringFromClass([AppDelegate class]) );
  308. [pool release];
  309. return exitCode;
  310. }
  311. #endif // BX_PLATFORM_IOS