cocos2d 绘制虚线
// // XLDashedLine.h // XLDashedLine // // Created by Richard Wei on 11-8-31. // Copyright 2011 Xinranmsn Labs. All rights reserved. // #import "cocos2d.h" @interface XLDashedLine : CCNode { CGPoint _pStart, _pEnd; CGPoint _pStartScaled, _pEndScaled; float _width, _scaledWidth; float _dashLength, _dashLengthScaled; ccColor4F _color; } @property (nonatomic, assign) CGPoint pStart, pEnd; @property (nonatomic, assign) float width; @property (nonatomic, assign) ccColor4F color; @property (nonatomic, assign) float dashLength; +(id) lineWithPStart:(CGPoint)pStart pEnd:(CGPoint)pEnd width:(float)width color:(ccColor4F)color dashLength:(float)dashLength; -(id) initWithPStart:(CGPoint)pStart pEnd:(CGPoint)pEnd width:(float)width color:(ccColor4F)color dashLength:(float)dashLength; @end
// // XLDashedLine.m // XLDashedLine // // Created by Richard Wei on 11-8-31. // Copyright 2011 Xinranmsn Labs. All rights reserved. // #import "XLDashedLine.h" #define ccp2ccpr(__point__) ccp(__point__.x * CC_CONTENT_SCALE_FACTOR(), __point__.y * CC_CONTENT_SCALE_FACTOR()) #define ccpr2ccp(__point__) ccp(__point__.x / CC_CONTENT_SCALE_FACTOR(), __point__.y / CC_CONTENT_SCALE_FACTOR()) #define ccpr(__x__, __y__) ccp(__x__ * CC_CONTENT_SCALE_FACTOR(), __y__ * CC_CONTENT_SCALE_FACTOR()) #define sclr2sclrr(__scl__) ((__scl__) * CC_CONTENT_SCALE_FACTOR()) static ccColor4F ccc4F(float _r, float _g, float _b, float _a) { ccColor4F retColor = {_r, _g, _b, _a}; return retColor; } /** 核心逻辑~ */ static inline void drawDashedLine(CGPoint origin, CGPoint destination, float dashLength) { float dx = destination.x - origin.x; float dy = destination.y - origin.y; float dist = sqrtf(dx * dx + dy * dy); float x = dx / dist * dashLength; float y = dy / dist * dashLength; CGPoint p2; glDisable(GL_TEXTURE_2D); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glEnable(GL_LINE_SMOOTH); for (float i = 0.0f; i <= dist / dashLength * .5; i++) { p2.x = origin.x + x; p2.y = origin.y + y; ccVertex2F vertices[2] = {{origin.x, origin.y}, {p2.x, p2.y}}; glVertexPointer(2, GL_FLOAT, 0, vertices); glDrawArrays(GL_LINES, 0, 2); origin.x += x * 2; origin.y += y * 2; } glDisable(GL_LINE_SMOOTH); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D); } @implementation XLDashedLine @synthesize pStart = _pStart; @synthesize pEnd = _pEnd; @synthesize width = _width; @synthesize color = _color; @synthesize dashLength = _dashLength; +(id) lineWithPStart:(CGPoint)pStart pEnd:(CGPoint)pEnd width:(float)width color:(ccColor4F)color dashLength:(float)dashLength { return [[[self alloc] initWithPStart:pStart pEnd:pEnd width:width color:color dashLength:dashLength] autorelease]; } -(id) init { if ((self = [super init])) { self.pStart = CGPointZero; self.pEnd = CGPointZero; self.width = .0f; self.color = ccc4F(.0f, .0f, .0f, .0f); self.dashLength = .0f; } return self; } -(id) initWithPStart:(CGPoint)pStart pEnd:(CGPoint)pEnd width:(float)width color:(ccColor4F)color dashLength:(float)dashLength { if ((self = [super init])) { self.pStart = pStart; self.pEnd = pEnd; self.width = width; self.color = color; self.dashLength = dashLength; } return self; } -(void) draw { glLineWidth(_scaledWidth); glColor4f(_color.r, _color.g, _color.b, _color.a); drawDashedLine(_pStartScaled, _pEndScaled, _dashLengthScaled); glLineWidth(1.0f); [super draw]; } -(void) setPStart:(CGPoint)pStart { _pStart = pStart; _pStartScaled = ccp2ccpr(pStart); } -(void) setPEnd:(CGPoint)pEnd { _pEnd = pEnd; _pEndScaled = ccp2ccpr(pEnd); } -(void) setWidth:(float)width { _width = width; _scaledWidth = sclr2sclrr(width); } -(void) setDashLength:(float)dashLength { _dashLength = dashLength; _dashLengthScaled = sclr2sclrr(dashLength); } @end