T2DViewController.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "platformAndroid/platformGL.h"
  24. #include "platformAndroid/AndroidWindow.h"
  25. #include "platformAndroid/platformAndroid.h"
  26. #include "graphics/dgl.h"
  27. extern AndroidPlatState platState;
  28. #define USE_DEPTH_BUFFER 0
  29. extern bool retinaEnabled;
  30. extern void ConvertToRetina (CGPoint *p);
  31. extern bool _AndroidTorqueFatalError;
  32. extern int _AndroidRunTorqueMain( id appID, UIView *Window, T2DViewController *Controller );
  33. //-Mat we should update the accelereometer once per frame
  34. extern U32 AccelerometerUpdateMS;
  35. extern void _AndroidGameInnerLoop();
  36. //TODO: android
  37. /*
  38. @implementation T2DViewController
  39. @synthesize context = _context;
  40. @synthesize connectionData = _connectionData;
  41. @synthesize connection = _connection;
  42. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  43. {
  44. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  45. if (self) {
  46. // Custom initialization
  47. }
  48. return self;
  49. }
  50. - (void)didReceiveMemoryWarning
  51. {
  52. // Releases the view if it doesn't have a superview.
  53. [super didReceiveMemoryWarning];
  54. // Release any cached data, images, etc that aren't in use.
  55. }
  56. #pragma mark - View lifecycle
  57. -(void)swapBuffers {
  58. if( isLayedOut == true ) {
  59. [self.context presentRenderbuffer:GL_RENDERBUFFER_OES];
  60. }
  61. }
  62. - (BOOL)createFramebuffer {
  63. glGenFramebuffersOES(1, &viewFramebuffer);
  64. glGenRenderbuffersOES(1, &viewRenderbuffer);
  65. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  66. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  67. [self.context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.view.layer];
  68. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  69. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  70. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  71. if (USE_DEPTH_BUFFER) {
  72. glGenRenderbuffersOES(1, &depthRenderbuffer);
  73. glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  74. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  75. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  76. }
  77. if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  78. NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  79. return NO;
  80. }
  81. return YES;
  82. }
  83. - (void)destroyFramebuffer {
  84. glDeleteFramebuffersOES(1, &viewFramebuffer);
  85. viewFramebuffer = 0;
  86. glDeleteRenderbuffersOES(1, &viewRenderbuffer);
  87. viewRenderbuffer = 0;
  88. if(depthRenderbuffer) {
  89. glDeleteRenderbuffersOES(1, &depthRenderbuffer);
  90. depthRenderbuffer = 0;
  91. }
  92. }
  93. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  94. - (void)viewDidLoad
  95. {
  96. [super viewDidLoad];
  97. self.context = [[EAGLContext alloc]
  98. initWithAPI:kEAGLRenderingAPIOpenGLES1];
  99. if (!self.context) {
  100. NSLog(@"Failed to create ES context");
  101. }
  102. T2DView *view = (T2DView *) self.view;
  103. view.context = self.context;
  104. if( AccelerometerUpdateMS <= 0 ) {
  105. //Luma: This variable needs to be store MS value, not Seconds value
  106. AccelerometerUpdateMS = 33; // 33 ms
  107. }
  108. //Luma: Do division by 1000.0f here to get the seconds value that the UIAccelerometer needs
  109. //[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(AccelerometerUpdateMS / 1000.0f)];//this value is in seconds
  110. //[[UIAccelerometer sharedAccelerometer] setDelegate:self];
  111. [EAGLContext setCurrentContext:self.context];
  112. [self createFramebuffer];
  113. view.isLayedOut = true;
  114. //by default, we are in portrait(upright) mode
  115. view.currentAngle = (M_PI / 2.0);
  116. platState.multipleTouchesEnabled = true;
  117. [self.view setMultipleTouchEnabled:YES];
  118. retinaEnabled = false;
  119. if([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
  120. retinaEnabled = true;
  121. UIApplication * application = [UIApplication sharedApplication];
  122. id appDelegate = [application delegate];
  123. _AndroidTorqueFatalError = false;
  124. if(!_AndroidRunTorqueMain( appDelegate, self.view, self ))
  125. {
  126. _AndroidTorqueFatalError = true;
  127. return;
  128. };
  129. }
  130. - (void)viewDidUnload
  131. {
  132. [super viewDidUnload];
  133. // Release any retained subviews of the main view.
  134. if ([EAGLContext currentContext] == self.context) {
  135. [EAGLContext setCurrentContext:nil];
  136. }
  137. self.context = nil;
  138. }
  139. - (void)update
  140. {
  141. _AndroidGameInnerLoop();
  142. }
  143. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  144. {
  145. // Return YES for supported orientations
  146. switch (interfaceOrientation)
  147. {
  148. case UIDeviceOrientationPortraitUpsideDown:
  149. return mOrientationPortraitUpsideDownSupported;
  150. break;
  151. case UIDeviceOrientationPortrait:
  152. return mOrientationPortraitSupported;
  153. break;
  154. case UIDeviceOrientationLandscapeRight:
  155. return mOrientationLandscapeRightSupported;
  156. break;
  157. case UIDeviceOrientationLandscapeLeft:
  158. return mOrientationLandscapeLeftSupported;
  159. break;
  160. default:
  161. break;
  162. }
  163. return NO;
  164. }
  165. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  166. {
  167. return UIInterfaceOrientationLandscapeRight;
  168. }
  169. void supportLandscape( bool enable)
  170. {
  171. platState.viewController->mOrientationLandscapeLeftSupported = enable;
  172. platState.viewController->mOrientationLandscapeRightSupported = enable;
  173. }
  174. ConsoleFunction(supportLandscape, void, 2, 2, "supportLandscape( bool ) "
  175. "enable or disable Landscape")
  176. {
  177. bool enable = true;
  178. if( argc > 1 )
  179. enable = dAtob( argv[1] );
  180. supportLandscape(enable);
  181. }
  182. void supportPortrait( bool enable )
  183. {
  184. platState.viewController->mOrientationPortraitSupported = enable;
  185. platState.viewController->mOrientationPortraitUpsideDownSupported = enable;
  186. }
  187. ConsoleFunction(supportPortrait, void, 2, 2, "supportPortrait( bool ) "
  188. "enable or disable portrait")
  189. {
  190. bool enable = true;
  191. if( argc > 1 )
  192. enable = dAtob( argv[1] );
  193. supportPortrait(enable);
  194. }
  195. - (void)viewDidLayoutSubviews
  196. {
  197. [self destroyFramebuffer];
  198. [self createFramebuffer];
  199. }
  200. @end
  201. */