RickON
RickON

Reputation: 395

Adding rectangles to a CCScene - cocos2d

The game is up and running, but I wanted to crate a Layer of rectangles, for future development/maintanance. These rectangles would be enabled at compiling time. They will show to the developer the scene hotspots.

I've already tried creating a new CCLayer and changing its draw method to:

- (void)draw
{
    glEnable(GL_LINE_SMOOTH);
    glColor4ub(255, 255, 255, 255);
    glLineWidth(2);
    CGPoint vertices2[] = { ccp(79,299), ccp(134,299), ccp(134,229), ccp(79,229) };
    ccDrawPoly(vertices2, 4, YES);
}

But Xcode says "Use of Undeclared identifier GL_LINE_SMOOTH"

I don't want to create image sprites just for this matter.

Isn't there any way I can use something simple like "CGContextAddRect"?

Something really simple...

I went to many advanced and complicated methods here. This is not what I am looking for.

Thank you!

Upvotes: 1

Views: 459

Answers (2)

godel9
godel9

Reputation: 7390

You can use the CCLayerColor class to add a solid color rectangle in just three lines of code and without any custom subclasses:

CCLayerColor *rectangleNode = [CCLayerColor layerWithColor:color
                                                     width:width
                                                    height:height];
rectangleNode.position = position;
[self addChild:rectangleNode];

Upvotes: 1

FuzzyBunnySlippers
FuzzyBunnySlippers

Reputation: 3397

I don't believe GL_LINE_SMOOTH is supported in OpenGL ES 2.0 (I can't find it in my ES 2.0 programming guide from the Khronos group). It may only be supported in the desktop version.

I believe you can use these drawing methods to draw rectangles and other things easily.

Since this is for development only (per your question), I am not sure how important the anti-aliasing effect is.

void ccDrawPoint (CGPoint point);
void ccDrawPoints (const CGPoint *points, NSUInteger numberOfPoints);
void ccDrawLine (CGPoint origin, CGPoint destination);
void ccDrawRect (CGPoint origin, CGPoint destination);

void ccDrawPoly (const CGPoint *vertices, NSUInteger numOfVertices, BOOL closePolygon);
void ccDrawCircle (CGPoint center, float radius, float angle, NSUInteger segments, BOOL drawLineToCenter);

You can also do filled versions:

void ccDrawSolidRect (CGPoint origin, CGPoint destination, ccColor4F color);
void ccDrawSolidPoly (const CGPoint *poli, NSUInteger numberOfPoints, ccColor4F color);

Here is a reference, with some example code (see the one at the bottom for -visit).

And just to make sure I wasn't just dropping some random stuff in, I created a project (older version of coco2d, 1.01) and dropped in the following code (into the default HelloWorldLayer.m):

-(void) visit
{
   [super visit];

   ccDrawPoint(ccp(240, 160));
   ccDrawLine(ccp(100, 100), ccp(400, 300));
   ccDrawCircle(ccp(310, 150), 50, 1, 10, YES);
   ccDrawQuadBezier(ccp(50, 300), ccp(450, 250), ccp(400, 30), 15);
}

This is what it looked like:

enter image description here

Was this what you needed?

Upvotes: 2

Related Questions