1

I'm a newbie in Cocos2d. I try to draw a simple line in Cocos2d:

-(void)draw
{
    [super draw];
    glColor4ub(0,0,0,255);
    glLineWidth(2);
    glColor4f(1.0, 1.0, 0.5, 1);
    ccDrawLine(ccp(0,100), ccp(320,150));

}

but that shows the warning:

HelloWorldScene.m:70:5: Implicit declaration of function 'glColor4ub' is invalid in C99 HelloWorldScene.m:72:5: Implicit declaration of function 'glColor4f' is invalid in C99 HelloWorldScene.m:73:5: Implicit declaration of function 'ccDrawLine' is invalid in C99

Arnaud
  • 7,259
  • 10
  • 50
  • 71
Sunary
  • 121
  • 1
  • 14
  • "Implicit declaration" warnings mean "I want an explicit declaration" (i.e. include the OpenGL header file - or in your case the Cocos2d header file). – trojanfoe Apr 03 '14 at 07:59
  • yes, I mean warning :). and what do I need implicit? – Sunary Apr 03 '14 at 08:09
  • 1
    this looks like OpenGL ES 1.1 code, that won't work in cocos2d v2.x and later (uses OpenGL ES 2.0). Also cocos2d has builtin drawing functions, search for ccDrawLine, ccDrawCircle, etc. or even better if available is CCDrawNode because it'll render drawn primitives more efficiently. – CodeSmile Apr 03 '14 at 08:19

2 Answers2

3

I will extend @abhineetprasad answer. At first - you should call drawing functions at the end of the visit function rather than in draw:

-(void) visit
{

    // call implementation of the super class to draw self and all children, in proper order
    [super visit];

    //custom draw code put below [super visit] will draw over the current node and all of its children
} 

than you can use functions like ccDrawLine or ccDrawColor4F to draw the line / set properly the color. So, for example:

-(void) visit
{
    // call implementation of the super class to draw self and all children, in proper order
    [super visit];

    //custom draw code put below [super visit] will draw over the current node and all of its children

    //set color to red
    ccDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);

    //draw the line
    ccDrawLine(ccp(0,0), ccp(320, 150));

 } 

In cocos2d-v3 CCDrawingPrimitives.h is already included via the "cocos2d.h" header file. I'am not sure if it is true in previous verions than v3.0 of Cocos2d.

pmpod
  • 379
  • 1
  • 5
2

As suggested by @LearnCocos2d , you could use the builtin drawing functions provided by cocos2d by calling them in the visit function.

#import "CCDrawingPrimitives.h"


-(void) visit{

    [super visit];
    ccDrawLine(ccp(0,100), ccp(320,150));
}
Abhineet Prasad
  • 1,271
  • 2
  • 11
  • 14