2
0

entry_ios.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2011-2014 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. struct Context
  22. {
  23. Context()
  24. {
  25. const char* argv[1] = { "ios" };
  26. m_mte.m_argc = 1;
  27. m_mte.m_argv = const_cast<char**>(argv);
  28. m_eventQueue.postSizeEvent(768, 1024);
  29. // Prevent render thread creation.
  30. bgfx::renderFrame();
  31. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  32. }
  33. ~Context()
  34. {
  35. m_thread.shutdown();
  36. }
  37. MainThreadEntry m_mte;
  38. bx::Thread m_thread;
  39. EventQueue m_eventQueue;
  40. };
  41. static Context* s_ctx;
  42. int32_t MainThreadEntry::threadFunc(void* _userData)
  43. {
  44. MainThreadEntry* self = (MainThreadEntry*)_userData;
  45. int32_t result = main(self->m_argc, self->m_argv);
  46. return result;
  47. }
  48. const Event* poll()
  49. {
  50. return s_ctx->m_eventQueue.poll();
  51. }
  52. void release(const Event* _event)
  53. {
  54. s_ctx->m_eventQueue.release(_event);
  55. }
  56. void setWindowSize(uint32_t _width, uint32_t _height)
  57. {
  58. BX_UNUSED(_width, _height);
  59. }
  60. void toggleWindowFrame()
  61. {
  62. }
  63. void setMouseLock(bool _lock)
  64. {
  65. BX_UNUSED(_lock);
  66. }
  67. } // namespace entry
  68. using namespace entry;
  69. @interface View : UIView
  70. {
  71. CADisplayLink* m_displayLink;
  72. }
  73. @end
  74. @implementation View
  75. + (Class)layerClass
  76. {
  77. return [CAEAGLLayer class];
  78. }
  79. - (id)initWithFrame:(CGRect)rect
  80. {
  81. self = [super initWithFrame:rect];
  82. if (nil == self)
  83. {
  84. return nil;
  85. }
  86. CAEAGLLayer* layer = (CAEAGLLayer*)self.layer;
  87. bgfx::iosSetEaglLayer(layer);
  88. return self;
  89. }
  90. - (void)start
  91. {
  92. if (nil == m_displayLink)
  93. {
  94. m_displayLink = [self.window.screen displayLinkWithTarget:self selector:@selector(renderFrame)];
  95. //[m_displayLink setFrameInterval:1];
  96. //[m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  97. // [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop]];
  98. [m_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  99. }
  100. }
  101. - (void)stop
  102. {
  103. if (nil != m_displayLink)
  104. {
  105. [m_displayLink invalidate];
  106. m_displayLink = nil;
  107. }
  108. }
  109. - (void)renderFrame
  110. {
  111. bgfx::renderFrame();
  112. }
  113. @end
  114. @interface AppDelegate : UIResponder<UIApplicationDelegate>
  115. {
  116. UIWindow* m_window;
  117. View* m_view;
  118. }
  119. @property (nonatomic, retain) UIWindow* m_window;
  120. @property (nonatomic, retain) View* m_view;
  121. @end
  122. @implementation AppDelegate
  123. @synthesize m_window;
  124. @synthesize m_view;
  125. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  126. {
  127. BX_UNUSED(application, launchOptions);
  128. CGRect rect = [ [UIScreen mainScreen] bounds];
  129. m_window = [ [UIWindow alloc] initWithFrame: rect];
  130. m_view = [ [View alloc] initWithFrame: rect];
  131. [m_window addSubview: m_view];
  132. [m_window makeKeyAndVisible];
  133. s_ctx = new Context;
  134. return YES;
  135. }
  136. - (void)applicationWillResignActive:(UIApplication *)application
  137. {
  138. BX_UNUSED(application);
  139. [m_view stop];
  140. }
  141. - (void)applicationDidEnterBackground:(UIApplication *)application
  142. {
  143. BX_UNUSED(application);
  144. }
  145. - (void)applicationWillEnterForeground:(UIApplication *)application
  146. {
  147. BX_UNUSED(application);
  148. }
  149. - (void)applicationDidBecomeActive:(UIApplication *)application
  150. {
  151. BX_UNUSED(application);
  152. [m_view start];
  153. }
  154. - (void)applicationWillTerminate:(UIApplication *)application
  155. {
  156. BX_UNUSED(application);
  157. [m_view stop];
  158. }
  159. - (void)dealloc
  160. {
  161. [m_window release];
  162. [m_view release];
  163. [super dealloc];
  164. }
  165. @end
  166. int main(int _argc, char* _argv[])
  167. {
  168. NSAutoreleasePool* pool = [ [NSAutoreleasePool alloc] init];
  169. int exitCode = UIApplicationMain(_argc, _argv, @"UIApplication", NSStringFromClass([AppDelegate class]) );
  170. [pool release];
  171. return exitCode;
  172. }
  173. #endif // BX_PLATFORM_IOS