entry_ios.mm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 2011-2013 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 bgfx
  14. {
  15. void renderFrame();
  16. }
  17. namespace entry
  18. {
  19. struct MainThreadEntry
  20. {
  21. int m_argc;
  22. char** m_argv;
  23. static int32_t threadFunc(void* _userData);
  24. };
  25. struct Context
  26. {
  27. Context()
  28. {
  29. const char* argv[1] = { "ios" };
  30. m_mte.m_argc = 1;
  31. m_mte.m_argv = const_cast<char**>(argv);
  32. m_eventQueue.postSizeEvent(768, 1024);
  33. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  34. }
  35. ~Context()
  36. {
  37. m_thread.shutdown();
  38. }
  39. MainThreadEntry m_mte;
  40. bx::Thread m_thread;
  41. EventQueue m_eventQueue;
  42. };
  43. static Context* s_ctx;
  44. int32_t MainThreadEntry::threadFunc(void* _userData)
  45. {
  46. MainThreadEntry* self = (MainThreadEntry*)_userData;
  47. int32_t result = main(self->m_argc, self->m_argv);
  48. return result;
  49. }
  50. const Event* poll()
  51. {
  52. return s_ctx->m_eventQueue.poll();
  53. }
  54. void release(const Event* _event)
  55. {
  56. s_ctx->m_eventQueue.release(_event);
  57. }
  58. void setWindowSize(uint32_t _width, uint32_t _height)
  59. {
  60. }
  61. void toggleWindowFrame()
  62. {
  63. }
  64. void setMouseLock(bool _lock)
  65. {
  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. }
  98. }
  99. - (void)stop
  100. {
  101. if (nil != m_displayLink)
  102. {
  103. [m_displayLink invalidate];
  104. m_displayLink = nil;
  105. }
  106. }
  107. - (void)renderFrame
  108. {
  109. bgfx::renderFrame();
  110. }
  111. @end
  112. @interface AppDelegate : UIResponder<UIApplicationDelegate>
  113. {
  114. UIWindow* m_window;
  115. View* m_view;
  116. }
  117. @property (nonatomic, retain) UIWindow* m_window;
  118. @property (nonatomic, retain) View* m_view;
  119. @end
  120. @implementation AppDelegate
  121. @synthesize m_window;
  122. @synthesize m_view;
  123. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  124. {
  125. CGRect rect = [ [UIScreen mainScreen] bounds];
  126. m_window = [ [UIWindow alloc] initWithFrame: rect];
  127. m_view = [ [View alloc] initWithFrame: rect];
  128. [m_window addSubview: m_view];
  129. [m_window makeKeyAndVisible];
  130. s_ctx = new Context;
  131. return YES;
  132. }
  133. - (void)applicationWillResignActive:(UIApplication *)application
  134. {
  135. [m_view stop];
  136. }
  137. - (void)applicationDidEnterBackground:(UIApplication *)application
  138. {
  139. }
  140. - (void)applicationWillEnterForeground:(UIApplication *)application
  141. {
  142. }
  143. - (void)applicationDidBecomeActive:(UIApplication *)application
  144. {
  145. [m_view start];
  146. }
  147. - (void)applicationWillTerminate:(UIApplication *)application
  148. {
  149. [m_view stop];
  150. }
  151. - (void)dealloc
  152. {
  153. [m_window release];
  154. [m_view release];
  155. [super dealloc];
  156. }
  157. @end
  158. int main(int _argc, char* _argv[])
  159. {
  160. NSAutoreleasePool* pool = [ [NSAutoreleasePool alloc] init];
  161. int exitCode = UIApplicationMain(_argc, _argv, @"UIApplication", NSStringFromClass([AppDelegate class]) );
  162. [pool release];
  163. return exitCode;
  164. }
  165. #endif // BX_PLATFORM_IOS