T2DViewController.mm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import "T2DViewController.h"
  23. #import "platformiOS/platformGL.h"
  24. #include "platformiOS/iOSWindow.h"
  25. #include "platformiOS/platformiOS.h"
  26. #include "graphics/dgl.h"
  27. extern iOSPlatState platState;
  28. #define USE_DEPTH_BUFFER 0
  29. extern bool retinaEnabled;
  30. extern void ConvertToRetina (CGPoint *p);
  31. extern bool _iOSTorqueFatalError;
  32. extern int _iOSRunTorqueMain( id appID, UIView *Window, T2DViewController *Controller );
  33. //-Mat we should update the accelereometer once per frame
  34. extern U32 AccelerometerUpdateMS;
  35. extern void _iOSGameInnerLoop();
  36. @implementation T2DViewController
  37. @synthesize context = _context;
  38. @synthesize connectionData = _connectionData;
  39. @synthesize connection = _connection;
  40. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  41. {
  42. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  43. if (self) {
  44. // Custom initialization
  45. }
  46. return self;
  47. }
  48. - (void)didReceiveMemoryWarning
  49. {
  50. // Releases the view if it doesn't have a superview.
  51. [super didReceiveMemoryWarning];
  52. // Release any cached data, images, etc that aren't in use.
  53. }
  54. #pragma mark - View lifecycle
  55. -(void)swapBuffers {
  56. if( isLayedOut == true ) {
  57. [self.context presentRenderbuffer:GL_RENDERBUFFER_OES];
  58. }
  59. }
  60. - (BOOL)createFramebuffer {
  61. glGenFramebuffersOES(1, &viewFramebuffer);
  62. glGenRenderbuffersOES(1, &viewRenderbuffer);
  63. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  64. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  65. [self.context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.view.layer];
  66. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  67. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  68. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  69. if (USE_DEPTH_BUFFER) {
  70. glGenRenderbuffersOES(1, &depthRenderbuffer);
  71. glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  72. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  73. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  74. }
  75. if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  76. NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  77. return NO;
  78. }
  79. return YES;
  80. }
  81. - (void)destroyFramebuffer {
  82. glDeleteFramebuffersOES(1, &viewFramebuffer);
  83. viewFramebuffer = 0;
  84. glDeleteRenderbuffersOES(1, &viewRenderbuffer);
  85. viewRenderbuffer = 0;
  86. if(depthRenderbuffer) {
  87. glDeleteRenderbuffersOES(1, &depthRenderbuffer);
  88. depthRenderbuffer = 0;
  89. }
  90. }
  91. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  92. - (void)viewDidLoad
  93. {
  94. [super viewDidLoad];
  95. self.context = [[EAGLContext alloc]
  96. initWithAPI:kEAGLRenderingAPIOpenGLES1];
  97. if (!self.context) {
  98. NSLog(@"Failed to create ES context");
  99. }
  100. T2DView *view = (T2DView *) self.view;
  101. view.context = self.context;
  102. if( AccelerometerUpdateMS <= 0 ) {
  103. //Luma: This variable needs to be store MS value, not Seconds value
  104. AccelerometerUpdateMS = 33; // 33 ms
  105. }
  106. //Luma: Do division by 1000.0f here to get the seconds value that the UIAccelerometer needs
  107. //[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(AccelerometerUpdateMS / 1000.0f)];//this value is in seconds
  108. //[[UIAccelerometer sharedAccelerometer] setDelegate:self];
  109. [EAGLContext setCurrentContext:self.context];
  110. [self createFramebuffer];
  111. view.isLayedOut = true;
  112. //by default, we are in portrait(upright) mode
  113. view.currentAngle = (M_PI / 2.0);
  114. platState.multipleTouchesEnabled = true;
  115. [self.view setMultipleTouchEnabled:YES];
  116. retinaEnabled = false;
  117. if([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] > 1)
  118. retinaEnabled = true;
  119. UIApplication * application = [UIApplication sharedApplication];
  120. id appDelegate = [application delegate];
  121. _iOSTorqueFatalError = false;
  122. if(!_iOSRunTorqueMain( appDelegate, self.view, self ))
  123. {
  124. _iOSTorqueFatalError = true;
  125. return;
  126. };
  127. }
  128. - (void)viewDidUnload
  129. {
  130. [super viewDidUnload];
  131. // Release any retained subviews of the main view.
  132. if ([EAGLContext currentContext] == self.context) {
  133. [EAGLContext setCurrentContext:nil];
  134. }
  135. self.context = nil;
  136. }
  137. - (void)update
  138. {
  139. _iOSGameInnerLoop();
  140. }
  141. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  142. {
  143. // Return YES for supported orientations
  144. switch (interfaceOrientation)
  145. {
  146. case UIDeviceOrientationPortraitUpsideDown:
  147. return mOrientationPortraitUpsideDownSupported;
  148. break;
  149. case UIDeviceOrientationPortrait:
  150. return mOrientationPortraitSupported;
  151. break;
  152. case UIDeviceOrientationLandscapeRight:
  153. return mOrientationLandscapeRightSupported;
  154. break;
  155. case UIDeviceOrientationLandscapeLeft:
  156. return mOrientationLandscapeLeftSupported;
  157. break;
  158. default:
  159. break;
  160. }
  161. return NO;
  162. }
  163. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  164. {
  165. return UIInterfaceOrientationLandscapeRight;
  166. }
  167. void supportLandscape( bool enable)
  168. {
  169. platState.viewController->mOrientationLandscapeLeftSupported = enable;
  170. platState.viewController->mOrientationLandscapeRightSupported = enable;
  171. }
  172. ConsoleFunction(supportLandscape, void, 2, 2, "supportLandscape( bool ) "
  173. "enable or disable Landscape")
  174. {
  175. bool enable = true;
  176. if( argc > 1 )
  177. enable = dAtob( argv[1] );
  178. supportLandscape(enable);
  179. }
  180. void supportPortrait( bool enable )
  181. {
  182. platState.viewController->mOrientationPortraitSupported = enable;
  183. platState.viewController->mOrientationPortraitUpsideDownSupported = enable;
  184. }
  185. ConsoleFunction(supportPortrait, void, 2, 2, "supportPortrait( bool ) "
  186. "enable or disable portrait")
  187. {
  188. bool enable = true;
  189. if( argc > 1 )
  190. enable = dAtob( argv[1] );
  191. supportPortrait(enable);
  192. }
  193. - (void)viewDidLayoutSubviews
  194. {
  195. [self destroyFramebuffer];
  196. [self createFramebuffer];
  197. }
  198. @end