T2DViewController.cpp 7.7 KB

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