<cocos2D>画图
http://www.2cto.com/kf/201202/118555.html
想要在layer中画一个圆,找到的cocos2d中的一些画图的例子代码
- (void) draw{
CGSize s = [[CCDirector sharedDirector] winSize];
// draw a simple line
// The default state is:
// Line Width: 1
// color: 255,255,255,255 (white, non-transparent)
// Aliased
ccDrawLine( ccp(0, 0), ccp(s.width, s.height) );
// line: color, width, anti-aliased
glLineWidth( 5.0f );
glEnable(GL_LINE_SMOOTH);
glColor4ub(255,0,0,255);
ccDrawLine( ccp(0, s.height), ccp(s.width, 0) );
// TIP:
// Since nobody disabled GL_LINE_SMOOTH,
// it will be valid until somone disabled it.
// The followin examples will be drawn using anti-aliasing.
//
// Remember: OpenGL is a state-machine.
// draw big point in the center
glPointSize(64);
glColor4ub(0,0,255,128);
ccDrawPoint( ccp(s.width / 2, s.height / 2) );
// draw 4 small points
CGPoint points[] = { ccp(60,60), ccp(70,70), ccp(60,70), ccp(70,60) };
glPointSize(4);
glColor4ub(0,255,255,255);
ccDrawPoints( points, 4);
// draw a green circle with 10 segments
glLineWidth(16);
glColor4ub(0, 255, 0, 255);
ccDrawCircle( ccp(s.width/2, s.height/2), 100, 0, 10, NO);
// draw a green circle with 50 segments with line to center
glLineWidth(2);
glColor4ub(0, 255, 255, 255);
ccDrawCircle( ccp(s.width/2, s.height/2), 50, CC_DEGREES_TO_RADIANS(90), 50, YES);
// open yellow poly
glColor4ub(255, 255, 0, 255);
glLineWidth(10);
CGPoint vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) };
ccDrawPoly( vertices, 5, NO);
// closed purble poly
glColor4ub(255, 0, 255, 255);
glLineWidth(2);
CGPoint vertices2[] = { ccp(30,130), ccp(30,230), ccp(50,200) };
ccDrawPoly( vertices2, 3, YES);
// restore original values
glLineWidth(1);
glDisable(GL_LINE_SMOOTH);
glColor4ub(255,255,255,255);
glPointSize(1);
}
ccDrawLine()函数仅仅是划线,本身并不返回任何形式的对象,因此需要把它放在draw()函数中使用,如:
<pre class="brush:objc; toolbar: true; auto-links: false;">
-(void)draw
{
glColor4f(0.8, 1.0, 0.76, 1.0);
glLineWidth(6.0f);
ccDrawLine(from_point, to_point);
}
</pre>
但是由于draw()函数是一个按帧绘制的函数,在绘制当前帧的时候会把前面帧的内容刷新掉,所以直接在ccTouchesMoved方法中调用draw(),显示的只能是最后绘制的两个点之间的连线,想要显示全部的路径,就要在一帧之内把所有的点全部连起来(不一定是必须这样,但我只实验出这一种方法,希望有更好方法的兄弟指点下啊),所以要记录所有移动过的点,并在每一帧内循环绘制(如果考虑到要绘制多条线段,还要使用二维动态数组来记录所有的点),代码如下:
<pre class="brush:objc; toolbar: true; auto-links: false;">
//在头文件中声明一个全局动态数组,用于存放所有线段的位置信息;
NSMutableArray *lines;
......
//在实现文件中
-(void)ccTouchesBegan:(NSSet*)pTouch withEvent:(UIEvent *)withEvent
{
//每一次开始绘制一条新线段的时候,创建一个新数组,这个数组代表当前绘制的那条线段;
NSMutableArray *points=[[NSMutableArray alloc] init];
[lines addObject:points];
}
-(void)ccTouchesMoved:(NSSet *)pTouch withEvent:(UIEvent *)withEvent
{
UITouch *touch=[pTouch anyObject];
CGPoint touch_location=[touch locationInView:[touch view]];
//cocos2d的坐标和Quartz 2D的坐标y轴是上下颠倒的,所以需要将其转换过来
touch_location= [[CCDirector sharedDirector] convertToGL:touch_location];
//取出最后一个数组元素(也就是当前绘制的线段);
NSMutableArray *points=[lines objectAtIndex:[lines count]-1];
//把要绘制的点加入到绘制队列中;
[points addObject:[NSValue valueWithCGPoint:touch_location]];
//NSLog(@"loaction is (%f,%f)",current_location.x,current_location.y);
[self draw];
}
-(void)draw
{
glColor4f(1.0, 1.0, 1.0, 1.0);
glLineWidth(8.0f);
glEnable(GL_LINE_SMOOTH);
//使用一个二重循环来绘制所有的点;
for (NSMutableArray *points in lines) {
for (int i=1; i<[points count]; i++) {
NSValue *current_point_value=[points objectAtIndex:i];
CGPoint current_point=[current_point_value CGPointValue];
NSValue *previous_point_value=[points objectAtIndex:i-1];
CGPoint previous_point=[previous_point_value CGPointValue];
ccDrawLine(previous_point, current_point);
}
}
}
//最后把所有数组资源释放:
-(void)dealloc{
for (NSMutableArray *points in lines) {
[points removeAllObjects];
[points release];
}
[lines removeAllObjects];
[lines release];
[super dealloc];
}
</pre>