entry_ios.mm 6.6 KB

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