ActivityView.mm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // ActivityView.m
  3. // Wxl
  4. //
  5. // Created by wxl on 11-10-9.
  6. // Copyright 2011 hna. All rights reserved.
  7. //
  8. #import "ActivityView.h"
  9. #define DEGREES_TO_RADIANS(__ANGLE) ((__ANGLE) / 180.0 * M_PI)
  10. @interface ActivityView(PrivateMethods)
  11. - (BOOL)createFramebuffer;
  12. - (void)destroyFramebuffer;
  13. - (void)drawView;
  14. - (void)commitInit;
  15. @end
  16. @implementation ActivityView
  17. @synthesize hidesWhenStopped;
  18. #pragma mark ------
  19. #pragma mark Draw
  20. //创建正视图
  21. - (void)setupView
  22. {
  23. glViewport(0,0,backingWidth,backingHeight);
  24. glMatrixMode(GL_PROJECTION);
  25. glLoadIdentity();
  26. glOrthof(0, backingWidth, 0 , backingHeight, -1, 1);
  27. glMatrixMode(GL_MODELVIEW);
  28. glLoadIdentity();
  29. }
  30. - (void)updateColorWithIndex:(short)index;
  31. {
  32. /*
  33. 看到的11和12都是由于系统的UIActivityIndicatorView共有12条线
  34. */
  35. if(index<0 || index >11) return;
  36. for(short i=0;i<12;i++,index++)
  37. {
  38. index %= 12;
  39. //1color rgba
  40. allLineColors[index*8+0]=MIN(EndR,BeginR+(1.0/12.0)*i);
  41. allLineColors[index*8+1]=MIN(EndG,BeginG+(1.0/12.0)*i);
  42. allLineColors[index*8+2]=MIN(EndB,BeginB+(1.0/12.0)*i);
  43. allLineColors[index*8+3]=1.0;
  44. //2color rgba
  45. allLineColors[index*8+4]=MIN(EndR,BeginR+(1.0/12.0)*i);
  46. allLineColors[index*8+5]=MIN(EndG,BeginG+(1.0/12.0)*i);
  47. allLineColors[index*8+6]=MIN(EndB,BeginB+(1.0/12.0)*i);
  48. allLineColors[index*8+7]=1.0;
  49. }
  50. }
  51. //绘制
  52. - (void)drawView
  53. {
  54. //如果是后台 就不在展示弹出框了
  55. if (myDelegate.isBackgroundTask) {
  56. return;
  57. }
  58. [EAGLContext setCurrentContext:context];
  59. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  60. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  61. glLoadIdentity();
  62. glPushMatrix();
  63. glTranslatef(self.bounds.size.width/2.0,self.bounds.size.height/2.0,0.0);
  64. glColorPointer(4, GL_FLOAT,0,allLineColors);
  65. glVertexPointer(2,GL_FLOAT,0,allLineVertexs);
  66. glDrawArrays(GL_LINES,0,24);
  67. glPopMatrix();
  68. //dansonmark 先这样 看能不能捕获
  69. // Display the buffer
  70. @try {
  71. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  72. [context presentRenderbuffer:GL_RENDERBUFFER_OES];
  73. //5帧更新动画
  74. static short FrameCount=0;
  75. static short animCount=11;
  76. FrameCount++;
  77. if(FrameCount>=5)
  78. {
  79. FrameCount=0;
  80. animCount--;
  81. if(animCount<0)
  82. animCount=11;
  83. [self updateColorWithIndex:animCount];
  84. }
  85. } @catch (NSException *exception) {
  86. } @finally {
  87. }
  88. }
  89. #pragma mark ------
  90. #pragma mark LifeCycle
  91. + (Class) layerClass
  92. {
  93. return [CAEAGLLayer class];
  94. }
  95. - (void)commitInit
  96. {
  97. CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
  98. eaglLayer.opaque = NO; //层是透明的
  99. // In this application, we want to retain the EAGLDrawable contents after a call to presentRenderbuffer.
  100. eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  101. [NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
  102. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  103. if (!context || ![EAGLContext setCurrentContext:context])
  104. {
  105. //[self release];
  106. NSAssert(0,@"CAEAGLLayer context error!");
  107. }
  108. animationFrameInterval=1;
  109. depthRenderbuffer=1;
  110. hidesWhenStopped=FALSE;
  111. isAnimationRunning=FALSE;
  112. //初始化opengles
  113. glClearColor(0.0,0.0,0.0,0.0);
  114. glShadeModel(GL_SMOOTH);
  115. glEnable(GL_DEPTH_TEST);
  116. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  117. glEnable(GL_BLEND);
  118. glEnable(GL_LINE_SMOOTH);
  119. glEnableClientState(GL_VERTEX_ARRAY);
  120. glEnableClientState(GL_COLOR_ARRAY);
  121. glLineWidth(LINEWIDTH);
  122. //计算所有顶点
  123. allLineVertexs = new GLfloat[12*2*2]; //12条线 24个顶点
  124. allLineColors = new GLfloat[12*2*4]; //24个顶点的颜色
  125. for(int i=0;i<12;i++)
  126. {
  127. GLfloat x=cosf(DEGREES_TO_RADIANS(i*30));
  128. GLfloat y=sinf(DEGREES_TO_RADIANS(i*30));
  129. allLineVertexs[i*4+0]=INRADIUS*x;
  130. allLineVertexs[i*4+1]=INRADIUS*y;
  131. allLineVertexs[i*4+2]=OUTRADIUS*x;
  132. allLineVertexs[i*4+3]=OUTRADIUS*y;
  133. }
  134. [self updateColorWithIndex:0];
  135. }
  136. - (id)initWithFrame:(CGRect)frame
  137. {
  138. frame.size.width=mySize.width;
  139. frame.size.height=mySize.height;
  140. if(self = [super initWithFrame:frame])
  141. {
  142. [self commitInit];
  143. }
  144. return self;
  145. }
  146. - (BOOL)isAnimating
  147. {
  148. return isAnimationRunning;
  149. }
  150. -(void)startAnimating
  151. {
  152. displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView)];
  153. [displayLink setFrameInterval:animationFrameInterval];
  154. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  155. isAnimationRunning=TRUE;
  156. if(hidesWhenStopped)
  157. self.hidden=FALSE;
  158. }
  159. -(void)stopAnimating
  160. {
  161. [displayLink invalidate];
  162. displayLink=nil;
  163. isAnimationRunning=FALSE;
  164. if(hidesWhenStopped)
  165. self.hidden=TRUE;
  166. }
  167. -(void)layoutSubviews
  168. {
  169. [EAGLContext setCurrentContext:context];
  170. [self destroyFramebuffer];
  171. [self createFramebuffer];
  172. [self setupView];
  173. }
  174. - (BOOL)createFramebuffer
  175. {
  176. // Generate IDs for a framebuffer object and a color renderbuffer
  177. glGenFramebuffersOES(1, &viewFramebuffer);
  178. glGenRenderbuffersOES(1, &viewRenderbuffer);
  179. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  180. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  181. // This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
  182. // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
  183. [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
  184. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  185. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  186. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  187. if(depthRenderbuffer)
  188. {
  189. glGenRenderbuffersOES(1, &depthRenderbuffer);
  190. glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  191. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  192. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  193. }
  194. if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
  195. {
  196. NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  197. return NO;
  198. }
  199. return YES;
  200. }
  201. // Clean up any buffers we have allocated.
  202. - (void)destroyFramebuffer
  203. {
  204. glDeleteFramebuffersOES(1, &viewFramebuffer);
  205. viewFramebuffer = 0;
  206. glDeleteRenderbuffersOES(1, &viewRenderbuffer);
  207. viewRenderbuffer = 0;
  208. if(depthRenderbuffer)
  209. {
  210. glDeleteRenderbuffersOES(1, &depthRenderbuffer);
  211. depthRenderbuffer = 0;
  212. }
  213. }
  214. // Releases resources when they are not longer needed.
  215. - (void) dealloc
  216. {
  217. [self stopAnimating];
  218. if([EAGLContext currentContext] == context)
  219. {
  220. [EAGLContext setCurrentContext:nil];
  221. }
  222. delete allLineVertexs;
  223. delete allLineColors;
  224. // [context release];
  225. // [super dealloc];
  226. }
  227. @end