entry_ios.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 toggleFullscreen(WindowHandle _handle)
  102. {
  103. BX_UNUSED(_handle);
  104. }
  105. void setMouseLock(WindowHandle _handle, bool _lock)
  106. {
  107. BX_UNUSED(_handle, _lock);
  108. }
  109. } // namespace entry
  110. using namespace entry;
  111. @interface View : UIView
  112. {
  113. CADisplayLink* m_displayLink;
  114. }
  115. @end
  116. @implementation View
  117. + (Class)layerClass
  118. {
  119. return [CAEAGLLayer class];
  120. }
  121. - (id)initWithFrame:(CGRect)rect
  122. {
  123. self = [super initWithFrame:rect];
  124. if (nil == self)
  125. {
  126. return nil;
  127. }
  128. CAEAGLLayer* layer = (CAEAGLLayer*)self.layer;
  129. bgfx::iosSetEaglLayer(layer);
  130. return self;
  131. }
  132. - (void)start
  133. {
  134. if (nil == m_displayLink)
  135. {
  136. m_displayLink = [self.window.screen displayLinkWithTarget:self selector:@selector(renderFrame)];
  137. //[m_displayLink setFrameInterval:1];
  138. //[m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  139. // [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop]];
  140. [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  141. }
  142. }
  143. - (void)stop
  144. {
  145. if (nil != m_displayLink)
  146. {
  147. [m_displayLink invalidate];
  148. m_displayLink = nil;
  149. }
  150. }
  151. - (void)renderFrame
  152. {
  153. bgfx::renderFrame();
  154. }
  155. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  156. {
  157. BX_UNUSED(touches);
  158. UITouch *touch = [[event allTouches] anyObject];
  159. CGPoint touchLocation = [touch locationInView:self];
  160. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0);
  161. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, true);
  162. }
  163. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  164. {
  165. BX_UNUSED(touches);
  166. UITouch *touch = [[event allTouches] anyObject];
  167. CGPoint touchLocation = [touch locationInView:self];
  168. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, false);
  169. }
  170. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  171. {
  172. BX_UNUSED(touches);
  173. UITouch *touch = [[event allTouches] anyObject];
  174. CGPoint touchLocation = [touch locationInView:self];
  175. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0);
  176. }
  177. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  178. {
  179. BX_UNUSED(touches);
  180. UITouch *touch = [[event allTouches] anyObject];
  181. CGPoint touchLocation = [touch locationInView:self];
  182. s_ctx->m_eventQueue.postMouseEvent(s_defaultWindow, touchLocation.x, touchLocation.y, 0, MouseButton::Left, false);
  183. }
  184. @end
  185. @interface AppDelegate : UIResponder<UIApplicationDelegate>
  186. {
  187. UIWindow* m_window;
  188. View* m_view;
  189. }
  190. @property (nonatomic, retain) UIWindow* m_window;
  191. @property (nonatomic, retain) View* m_view;
  192. @end
  193. @implementation AppDelegate
  194. @synthesize m_window;
  195. @synthesize m_view;
  196. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  197. {
  198. BX_UNUSED(application, launchOptions);
  199. CGRect rect = [ [UIScreen mainScreen] bounds];
  200. m_window = [ [UIWindow alloc] initWithFrame: rect];
  201. m_view = [ [View alloc] initWithFrame: rect];
  202. [m_window addSubview: m_view];
  203. [m_window makeKeyAndVisible];
  204. //float scaleFactor = [[UIScreen mainScreen] scale]; // should use this, but ui is too small on ipad retina
  205. float scaleFactor = 1.0f;
  206. [m_view setContentScaleFactor: scaleFactor ];
  207. s_ctx = new Context((uint32_t)(scaleFactor*rect.size.width), (uint32_t)(scaleFactor*rect.size.height));
  208. return YES;
  209. }
  210. - (void)applicationWillResignActive:(UIApplication *)application
  211. {
  212. BX_UNUSED(application);
  213. [m_view stop];
  214. }
  215. - (void)applicationDidEnterBackground:(UIApplication *)application
  216. {
  217. BX_UNUSED(application);
  218. }
  219. - (void)applicationWillEnterForeground:(UIApplication *)application
  220. {
  221. BX_UNUSED(application);
  222. }
  223. - (void)applicationDidBecomeActive:(UIApplication *)application
  224. {
  225. BX_UNUSED(application);
  226. [m_view start];
  227. }
  228. - (void)applicationWillTerminate:(UIApplication *)application
  229. {
  230. BX_UNUSED(application);
  231. [m_view stop];
  232. }
  233. - (void)dealloc
  234. {
  235. [m_window release];
  236. [m_view release];
  237. [super dealloc];
  238. }
  239. @end
  240. int main(int _argc, char* _argv[])
  241. {
  242. NSAutoreleasePool* pool = [ [NSAutoreleasePool alloc] init];
  243. int exitCode = UIApplicationMain(_argc, _argv, @"UIApplication", NSStringFromClass([AppDelegate class]) );
  244. [pool release];
  245. return exitCode;
  246. }
  247. #endif // BX_PLATFORM_IOS