Cocos2d-X directly using OpenGL interface, cocos2d-xopengl

Source: Internet
Author: User

Cocos2d-X directly using OpenGL interface, cocos2d-xopengl

Cocos2d-X is based on OpenGL ES 2D game engine, so Cocos2d-X can directly use OpenGL Interface

First, create a Draw class for processing OpenGL interfaces.

Add the following code to Draw. h:

#ifndef _Draw_H_#define _Draw_H_#include "cocos2d.h"USING_NS_CC;class Draw : public CCLayer{public:    static CCScene* scene();    CREATE_FUNC(Draw);    bool init();    void draw();};#endif


Example 1: Draw a vertex Using OpenGL Interface

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2); // point: the default size is one pixel, and the value is white. // ccPointSize sets the size of the Point ccPointSize (25); // ccDrawColor4B sets the color of the Point ccDrawColor4B (255, 0, 0,255); // set the ccDrawPoint (center );}

Execution result:


Example 2: draw a line using OpenGL Interface

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // line: The default value is a pixel width, and the color ccDrawColor4B (255,255, 0,255) is white ); // glLineWidth is the function glLineWidth (5) for setting the line width; ccDrawLine (center, // The Position of the starting point ccpAdd (center, ccp (100,100) // The Position of the midpoint ); /* ccpAdd (center, ccp (100,100) indicates center. x + 100, center. y + 100 */}


Execution result:


Example 3: Use the OpenGL interface to draw multiple points

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // set the color of the vertex ccDrawColor4B (255, 0, 0,255); // defines the position of the saved vertex in an array CCPoint pts [] = {CCPoint (100,100 ), CCPoint (100,200), CCPoint (200,200), CCPoint (200,100)}; // painting point ccDrawPoints (pts, 4 );}

Execution result:


Example 4: use OpenGL to draw a circle with a center link

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // set the circle color ccDrawColor4B (192,192, 0,255);/* draw the circle parameter 1: center parameter 2: radius parameter 3: parameter 4: Number of line segments, parameter 5: whether to link the center */ccDrawCircle (center, 3.14, 180, 100, true );}

Execution result:


Example 5: use OpenGL to draw a circle without a center link

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // set the circle color ccDrawColor4B (192,192, 0,255);/* draw the circle parameter 1: center parameter 2: radius parameter 3: parameter 4: Number of line segments, parameter 5: whether to link the center */ccDrawCircle (center, 3.14, 180, 100, false );}


Execution result:


Example 6: use OpenGL to draw a closed polygon

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // set the color of the vertex ccDrawColor4B (192,192, 0,255); // defines the position of the saved vertex in an array CCPoint pts [] = {CCPoint (100,100 ), CCPoint (100,200), CCPoint (200,200), CCPoint (200,100), CCPoint (150, 50)}; // draw the enclosed polygon ccDrawPoly (pts, 5, true );}

Execution result:


Example 7: Use the OpenGL interface to draw a non-sealing Polygon

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // set the color of the vertex ccDrawColor4B (192,192, 0,255); // defines the position of the saved vertex in an array CCPoint pts [] = {CCPoint (100,100 ), CCPoint (100,200), CCPoint (200,200), CCPoint (200,100), CCPoint (150, 50)}; // draw the enclosed polygon ccDrawPoly (pts, 5, false );}

Execution result:


Example 8: use OpenGL to draw a solid Polygon

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // set the color of the vertex ccDrawColor4B (192,192, 0,255); // define the position of the saved vertex in the array CCPoint pts2 [] = {CCPoint (300,100 ), CCPoint (400,200), CCPoint (400,300)}; // draw a solid polygon ccDrawSolidPoly (pts2, 3, ccc4f (1, 0, 0, 1 ));}

Execution result:


Example 9: Use the OpenGL interface to implement the bezr Curve

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // set the color of the vertex ccDrawColor4B (192,192, 0,255); // define the position of the saved vertex in the array CCPoint pts2 [] = {CCPoint (300,100 ), CCPoint (400,200), CCPoint (400,300)}; // beestimated curve // Second-Order beestimated curve ccdrawquadbeestimated (pts2 [0], pts2 [1], pts2 [2], 10 );}


Execution result:



Example 10: Use the OpenGL interface to implement a second-order bezr Curve

Add the following code to Draw. cpp:

# Include "Draw. h "CCScene * Draw: scene () {CCScene * s = CCScene: create (); Draw * layer = Draw: create (); s-> addChild (layer); return s;} bool Draw: init () {CCLayer: init (); return true;} void Draw: draw () {// get the window size CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinates CCPoint center = ccp (winSize. width/2, winSize. height/2);/* center. x = winSize. width/2 center. y = winSize. height/2 * // define the position of the point where the array is saved. CCPoint pts [] = {CCPoint (100,100), CCPoint (100,200), CCPoint (200,200), CCPoint (200,100 )}; // define the position of the point where the array is saved. CCPoint pts2 [] = {CCPoint (300,100), CCPoint (400,200), CCPoint (400,300 )}; // bezr curve // Second-Order bezr curve ccdrawquadbezr (pts2 [0], pts2 [1], pts2 [2], 10); ccDrawColor4B (90, 90,255,255 ); ccdrawcubicbezr (pts [0], pts [1], pts2 [2], pts [3], 10 );}

Execution result:


Zookeeper

Example 11: use OpenGL to draw a polygon by clicking the mouse

First, create an ActiveDraw class for processing OpenGL interfaces.

Add the following code to ActiveDraw. h:

# Ifndef _ ActiveDraw_H __# define _ ActiveDraw_H __# include "cocos2d. h "USING_NS_CC; class ActiveDraw: public CCLayer {public: static CCScene * scene (); CREATE_FUNC (ActiveDraw); bool init (); // set the touch event bool ccTouchBegan (CCTouch *, CCEvent *); // Save the CCPoint _ pts [100]; // record the valid touch point int _ ptsCount; void draw () ;}; # endif


Add the following code to ActiveDraw. cpp:

# Include "ActiveDraw. h "CCScene * ActiveDraw: scene () {CCScene * s = CCScene: create (); s-> addChild (ActiveDraw: create (); return s ;} bool ActiveDraw: init () {CCLayer: init (); // process the touch event setTouchEnabled (true); setTouchMode (kCCTouchesOneByOne); _ ptsCount = 0; return true ;} bool ActiveDraw: ccTouchBegan (CCTouch * touch, CCEvent * ev) {// obtain the coordinates of the touch point CCPoint pt = touch-> getLocation (); // save the coordinates to the array. _ pts [_ ptsCount ++] = pt; return true;} void ActiveDraw: draw () {// obtain the window size. CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the position of the vertex. CCPoint center = ccp (winSize. width/2, winSize. height/2); // draw a polygon ccDrawPoly (_ pts, _ ptsCount, true );}


Execution result:


Example 11: use OpenGL to draw a polygon 2.0 by clicking the mouse

First, create an ActiveDraw class for processing OpenGL interfaces.

Add the following code to ActiveDraw. h:

# Ifndef _ ActiveDraw_H __# define _ ActiveDraw_H __# include "cocos2d. h "USING_NS_CC; class ActiveDraw: public CCLayer {public: static CCScene * scene (); CREATE_FUNC (ActiveDraw); bool init (); // set the touch event bool ccTouchBegan (CCTouch *, CCEvent *); // Save the CCPoint _ pts [100]; // record the valid touch point int _ ptsCount; // define the sprite * _ sprite; void draw () ;};# endif

Add the following code to ActiveDraw. cpp:

# Include "ActiveDraw. h "CCScene * ActiveDraw: scene () {CCScene * s = CCScene: create (); s-> addChild (ActiveDraw: create (); return s ;} bool ActiveDraw: init () {CCLayer: init (); // process the touch event setTouchEnabled (true); setTouchMode (kCCTouchesOneByOne); _ ptsCount = 0; // create genie _ sprite = CCSprite: create (); addChild (_ sprite); return true;} bool ActiveDraw: ccTouchBegan (CCTouch * touch, CCEvent * ev) {// obtain the coordinate CCPoint pt = touch-> getLocation (); _ pts [_ ptsCount ++] = pt; // obtain the window size. CCSize winSize = CCDirector:: sharedDirector ()-> getWinSize (); // set the sprite position (the sprite position is a random position) _ sprite-> setPosition (ccp (winSize. width * CCRANDOM_0_1 (), winSize. height * CCRANDOM_0_1 (); return true;} void ActiveDraw: draw () {// obtain the window size. CCSize winSize = CCDirector: shareddire () -> getWinSize (); // set the position of the vertex CCPoint center = ccp (winSize. width/2, winSize. height/2); // draw a polygon ccDrawPoly (_ pts, _ ptsCount, true); if (_ sprite) {// draw a circular ccDrawCircle (_ sprite-> getPosition (), 20, 0,100, true );}}

Execution result:

Bytes


Example 12: use OpenGL to draw a line segment

First, create a DrawNode class for processing OpenGL interfaces.

Add the following code to DrawNode. h:

#ifndef _DrawNode_H_#define _DrawNode_H_#include "cocos2d.h"USING_NS_CC;class DrawNode : public CCLayer{public:    static CCScene* scene();    CREATE_FUNC(DrawNode);    bool init();};#endif

Add the following code to DrawNode. cpp:
# Include "DrawNode. h "CCScene * DrawNode: scene () {CCScene * s = CCScene: create (); DrawNode * layer = DrawNode: create (); s-> addChild (layer); return s;} bool DrawNode: init () {// initialize the parent class CCLayer: init (); // create DrawNode CCDrawNode * node = CCDrawNode: create (); // obtain the window size. CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // set the coordinate CCPoint center = ccp (winSize. width/2, winSize. height/2); // set the node position node-> setPosition (center); // draw a line segment node-> drawSegment (ccp (0, 0 ), ccp (100,100), 2, ccc4f (1, 0, 0, 1); // Add node addChild (node); return true ;}


Execution result: Too Many Rows exist.
Functions of OpenGL ES and cocos2d-x on android Development

Opengl is relatively basic and cocos2d is professional, but its functions are not comprehensive. There is no need to learn games, and many android things need to be learned.

Cannot build cocos2d-x [for opengl version]

It is strongly recommended that you try another machine by using a friend's independent graphics card.
I have encountered a problem on the GM45 display netbook that the PC Edition of the Defender radish cannot run because of the unsupported video card.

However, I am using the dedicated explicit work, but I have never tested the development platform on the set explicit book. So ....

Go to the organization, go to the school, find a colleague, find a classmate, please eat, borrow a book, and pretend that the development environment won't have less meat.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.