cocos2d-x draw & update

  今天看代码想到一个问题,引擎在什么时候通过什么方法调用的opengl画出的图形?于是就在cocos2d-x中找到了draw,之后又查了点资料。
  Like in any game engine there are 2 different methods to draw and update things.

  Draw
  Each node has a draw method. This method is called every frme. The purpose of this method is to draw and only draw the node, nothing more, nothing less. You should not update ivars in this mehod.

  CCNode has an empty draw method.
  So, if you want to have a custom node, probably you will also need to override the draw method. It is worth nothing that the following OpenGL states are preset before calling draw: 

View Code
 1 // draw
2
3 void CCSprite::draw(void)
4 {
5 CCNode::draw();
6
7 assert(! m_bUsesBatchNode);
8
9 // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
10 // Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
11 // Unneeded states: -
12 bool newBlend = m_sBlendFunc.src != CC_BLEND_SRC || m_sBlendFunc.dst != CC_BLEND_DST;
13 if (newBlend)
14 {
15 glBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
16 }
17
18 #define kQuadSize sizeof(m_sQuad.bl)
19 if (m_pobTexture)
20 {
21 glBindTexture(GL_TEXTURE_2D, m_pobTexture->getName());
22 }
23 else
24 {
25 glBindTexture(GL_TEXTURE_2D, 0);
26 }
27
28 long offset = (long)&m_sQuad;
29
30 // vertex
31 int diff = offsetof(ccV3F_C4B_T2F, vertices);
32 glVertexPointer(3, GL_FLOAT, kQuadSize, (void*)(offset + diff));
33
34 // color
35 diff = offsetof( ccV3F_C4B_T2F, colors);
36 glColorPointer(4, GL_UNSIGNED_BYTE, kQuadSize, (void*)(offset + diff));
37
38 // tex coords
39 diff = offsetof( ccV3F_C4B_T2F, texCoords);
40 glTexCoordPointer(2, GL_FLOAT, kQuadSize, (void*)(offset + diff));
41
42 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
43
44 if( newBlend )
45 {
46 glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
47 }
48
49 #if CC_SPRITE_DEBUG_DRAW == 1
50 // draw bounding box
51 CCSize s = m_tContentSize;
52 CCPoint vertices[4] = {
53 ccp(0,0), ccp(s.width,0),
54 ccp(s.width,s.height), ccp(0,s.height)
55 };
56 ccDrawPoly(vertices, 4, true);
57 #elif CC_SPRITE_DEBUG_DRAW == 2
58 // draw texture box
59 CCSize s = m_obRect.size;
60 CCPoint offsetPix = getOffsetPositionInPixels();
61 CCPoint vertices[4] = {
62 ccp(offsetPix.x,offsetPix.y), ccp(offsetPix.x+s.width,offsetPix.y),
63 ccp(offsetPix.x+s.width,offsetPix.y+s.height), ccp(offsetPix.x,offsetPix.y+s.height)
64 };
65 ccDrawPoly(vertices, 4, true);
66 #endif // CC_SPRITE_DEBUG_DRAW
67 }

 

  Update
  Each node, by default, has no updates. if you want to update the state of your node you should schedule a callback
  The recommended way to do it is by calling CCNode Scheduing, and not by using ccTime.CCNode has the following method to timer

View Code
 1         /** schedules the "update" method. It will use the order number 0. This method will be called every frame.
2 Scheduled methods with a lower order value will be called before the ones that have a higher order value.
3 Only one "update" method could be scheduled per node.
4
5 @since v0.99.3
6 */
7 void scheduleUpdate(void);
8
9 /** schedules the "update" selector with a custom priority. This selector will be called every frame.
10 Scheduled selectors with a lower priority will be called before the ones that have a higher value.
11 Only one "update" selector could be scheduled per node (You can't have 2 'update' selectors).
12
13 @since v0.99.3
14 */
15 void scheduleUpdateWithPriority(int priority);
16
17 /* unschedules the "update" method.
18
19 @since v0.99.3
20 */
21 void unscheduleUpdate(void);
22
23 /** schedules a selector.
24 The scheduled selector will be ticked every frame
25 */
26 void schedule(SEL_SCHEDULE selector);
27
28 /** schedules a custom selector with an interval time in seconds.
29 If time is 0 it will be ticked every frame.
30 If time is 0, it is recommended to use 'scheduleUpdate' instead.
31 If the selector is already scheduled, then the interval parameter
32 will be updated without scheduling it again.
33 */
34 void schedule(SEL_SCHEDULE selector, ccTime interval);
35
36 /** unschedules a custom selector.*/
37 void unschedule(SEL_SCHEDULE selector);
38
39 /** unschedule all scheduled selectors: custom selectors, and the 'update' selector.
40 Actions are not affected by this method.
41 @since v0.99.3
42 */
43 void unscheduleAllSelectors(void);

  scheduleUpdate and/or scheduleUpdateWithPriority are the recommended way to schedule a callback
  If they are called, the update method will be called every frame with the "delta time" as argument.
  Try to use scheduleUpdate over scheduleSelector(void schedule(SEL_SCHEDULE selector);) since it is a little bit faster and consumes a little less memory.(why? scheduleSelector can define a longer time for the timer to call)
   原文

  关于update和draw,update负择维护程序中类的状态,而draw是通过类的各种状态从父节点一直draw,绘制出表现的图像。

write by fgd

posted @ 2011-12-16 18:11  wen_dao_  阅读(3767)  评论(0编辑  收藏  举报