Cocos2d-x elementary school (1)-> drawing

Source: Internet
Author: User
Tags addchild

Reprinted! Please indicate the source when reprint: http://blog.csdn.net/aa4790139/article/details/8103608

Because I have always had a hard time playing games, I love playing games since I was a child. Then I went to college and chose a major as a game major ~ A variety of expensive... but I like it.

Then we made the development of j2's, and then switched to android game development. Especially those who have j2's, I believe they all started from the bottom layer. I have also developed two games using the andengine engine before, except that the underlying rendering is written in C ++, and the others are written in Java. Since then, I have been using Java for development, therefore, I am familiar with Java and naturally choose to use the andengine engine for development... since it was previously hand-drawn, it is often the case on the screen that, in the face of a variety of annoying things, adaptation is not very good, andengine adaptation is very good, another feature, the memory to be loaded is very limited, so the memory overflows frequently. However, you can use texturepacker to process images, which can reduce the memory usage. For Java developers, it is good to learn about the andengine engine.

Cocos2d-x will naturally be drawing graphics are encapsulated, but for the underlying graphics drawing understand a little or better... You will know more in the future...

Although cocos2dx comes with test and test, there are also details about the basic graph drawing, but now I want you to draw the basic graph from the beginning, will you?

(Note: You can name all of them by yourself)

Part 1: Creating a project

Step 1: Create a Win32 basedrawtest Project

Here is my project directory:

Red folder to the cocos2d-x directory copy to the project directory... debug. Win32 you do not have, need to compile before it will be generated.

(Supplement: I am doing this because I want to put the Win32 project under the win32project directory. If we port it to Android, The cocos2dx program is generated, it is in the cocos2dx Engine Directory and is stored separately for differentiation:

1. If you create a Win32 project under the cocos2dx engine, you do not need to include the following six directories. Because the cocos2dx project is generated using vs, these paths are set by default, for example:

2. If you want me to create a separate cocos2d-x-win32-projects folder to save the Win32 project of cocos2dx, we only need to copy to the box2d, cocos2dx, cocosdenshion, lib (the file inside, in fact, it is the Lib file copy in the debug folder under the cocos2dx engine, a new file), and then use vs to create a cocos2dx Win32 project under the cocos2d-x-win32-projects, including the following directory:

 

)

Step 2: Open bashdrawtest. sln and add the corresponding directory.

Right-click the project and select properties:

For example:

Select the VC ++ directory from the box, add the triangle lines behind the directory, Reference Directory, library directory..., edit, and add the corresponding directory:

Include directory and Reference Directory: such as file:

Please select your corresponding directory to include it, and then the library Directory, which is the Lib under your basedrawtest directory. Previously, you were asked to copy it to the project directory ..

Now, compile and generate, show helloworld successfully, if not show please view http://blog.csdn.net/aa4790139/article/details/8086635

Part 2: Drawing

Step 1: Create your own scenario and Layer

The following figure shows how to create a basescene scenario and a basedrawlayer layer.

Step 2: Compile the code for the four files

Old look, upload my code...

Basedrawscene. h

# Pragma once # include "cocos2d. H" # include "basedrawlayer. H" using namespace cocos2d; Class basedrawscene: Public ccscene {public :~ Basedrawscene (void); // Method 1: static ccscene * scene ();/* Method 2: static basedrawscene * scene () {basedrawscene * scene = basedrawscene :: create (); // Note: You must override the create method basedrawlayer * layer = basedrawlayer: Create (); scene-> addchild (layer); Return scene;} of the parent class ;} scene_create_func (basedrawscene );*/};

Basedrawscene. cpp

#include "BaseDrawScene.h"BaseDrawScene::~BaseDrawScene(){}CCScene* BaseDrawScene::scene(){CCScene * scene = CCScene::create();BaseDrawLayer* layer=BaseDrawLayer::create();scene->addChild(layer);return scene;}

Basedrawlayer. h

#include "cocos2d.h"using namespace cocos2d;class BaseDrawLayer:public CCLayer{public:BaseDrawLayer();~BaseDrawLayer(void);virtual void draw();LAYER_CREATE_FUNC(BaseDrawLayer);};

Basedrawlayer. cpp

# Include "basedrawlayer. H" basedrawlayer: basedrawlayer () {} basedrawlayer ::~ Basedrawlayer (void) {} void basedrawlayer: Draw () {// note: the OpenGL coordinate system starts with ccsize S = ccdirector ctor: shareddire() in the lower left corner () -> getwinsize (); // set the vertex size and color, and draw a vertex glpointsize (64); glcolor4f (0.0, 0.0, 1.0, 0.5); ccdrawpoint (CCP (S. width/2 + 10, S. height/2); // draw a line. The parameter is the coordinates of the start point and the end point, including gl_line_smooth, ccdrawline (CCP (), CCP (S. width, S. height); // set the line width and color, and then draw a circle gllinewidth (2); ccdrawcolor4b (0,255,255,255); // parameter description: Center Coordinate, radius, angle, number of segments. Whether the ccdrawcircle (CCP (S. width/2, S. height/2), 50, 0, 50, false); // draw a polygon ccdrawcolor4b (255,255, 0,255); gllinewidth (10 ); ccpoint vertices [] = {CCP (100,100), CCP (50, 50), CCP (50,100, 50), CCP (), CCP ()}; // parameter description: vertex set, points, whether these five points constitute closed ccdrawpoly (vertices, 5, false); // draw a solid polygon gllinewidth (1); ccpoint filledvertices [] = {CCP (0,120 ), CCP (50,120), CCP (50,170), CCP (25,200), CCP (0,170)}; ccdrawsolidpoly (filledvertices, 5, ccc4f (0.5f, 0.5f, 1, 1 )); // draw the triangle ccdrawcolor4b (255, 0,255,255); gllinewidth (2); ccpoint vertices2 [] = {CCP (30,130), CCP (30,230), CCP (50,200 )}; ccdrawpoly (vertices2, 3, true); // draw a beiser curve of a control point // parameter description: starting point, control point, end point, number of segments ccdrawquadbezr (CCP (0, S. height), CCP (S. width/2, S. height/2), CCP (S. width, S. height), 50); // draw two control points (S. width/2, S. height/2), CCP (S. width/2 + 30, S. height/2 + 50), CCP (S. width/2 + 60, S. height/2-50), CCP (S. width, S. height/2), 100); // draw the filled diamond ccpoint vertices3 [] = {CCP (60,160), CCP (70,190), CCP (100,190 ), CCP (90,160)}; ccdrawsolidpoly (vertices3, 4, ccc4f (255,255,255,255,); // restore the original gllinewidth (1) drawn by OpenGL; ccdrawcolor4b ); ccpointsize (1 );}

Step 3: Modify applicationdidfinishlaunching () method in appdelegate. cpp

Ccscene * pscene = basedrawscene: Scene ();

Pdirector-> runwithscene (pscene );

Step 4: run the .. error and the various. dll libraries are missing

Step 5: Go to the Debug. Win32 folder under the cocos2dx Engine Directory, copy the following. dll libraries to the Debug. Win32 file under your project directory, and then run. OK

 

The effect of drawing should come out... the data adjustment test during the drawing process, so you will be familiar with it! If the story is incorrect or incorrect, I hope you can point it out!

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.