entry_ios.mm 3.6 KB

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