第一部分:基本图形绘制

cocos2dx封装了大量opengl函数,用于快速绘制基本图形,这些代码的例子在,tests\DrawPrimitivesTest目录下

注意,该方法是重载node的draw方法实现的,在智能机上,并不推荐直接绘制几何图形,因为大量的坐标编码会极大降低工作效率,应尽量使用Image。而且cocos2dx的渲染机制会造成前后遮挡问题,尤其是几何图形与图片等其他node混合绘制时。

void HelloWorld::draw() {
CCLayer::draw();
CCSize s = CCDirector::sharedDirector()->getWinSize();
// draw a simple line
// The default state is:
// Line Width: 1
// color: 255,255,255,255 (white, non-transparent)
// Anti-Aliased
glEnable(GL_LINE_SMOOTH);
ccDrawLine( CCPointMake(0, 0), CCPointMake(s.width, s.height) ); // line: color, width, aliased
// glLineWidth > 1 and GL_LINE_SMOOTH are not compatible
//注意:线宽>1 则不支持GL_LINE_SMOOTH
// GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone
glDisable(GL_LINE_SMOOTH);
glLineWidth( 5.0f );
/*glColor4ub(255,0,0,255);*/
glColor4f(1.0, 0.0, 0.0, 1.0);
ccDrawLine( CCPointMake(0, s.height), CCPointMake(s.width, 0) ); // TIP:
// If you are going to use always the same color or width, you don't
// need to call it before every draw
//
// Remember: OpenGL is a state-machine. // draw big point in the center
// 注意:cocos2dx绘制的是方块点
glPointSize(64);
/*glColor4ub(0,0,255,128);*/
glColor4f(0.0, 0.0, 1.0, 0.5);
ccDrawPoint( CCPointMake(s.width / 2, s.height / 2) ); // draw 4 small points
// 注意:cocos2dx绘制的是方块点
CCPoint points[] = { CCPointMake(60,60), CCPointMake(70,70), CCPointMake(60,70), CCPointMake(70,60) };
glPointSize(4);
/*glColor4ub(0,255,255,255);*/
glColor4f(0.0, 1.0, 1.0, 1.0);
ccDrawPoints( points, 4); // draw a green circle with 10 segments
glLineWidth(16);
/*glColor4ub(0, 255, 0, 255);*/
glColor4f(0.0, 1.0, 0.0, 1.0);
//参数依次是:中心点,半径,角度,分段数,是否连接中心点
ccDrawCircle( CCPointMake(s.width/2, s.height/2), 100, 0, 10, false); // draw a green circle with 50 segments with line to center
glLineWidth(2);
/*glColor4ub(0, 255, 255, 255);*/
glColor4f(0.0, 1.0, 1.0, 1.0);
ccDrawCircle( CCPointMake(s.width/2, s.height/2), 50, CC_DEGREES_TO_RADIANS(90), 50, true); // open yellow poly
/*glColor4ub(255, 255, 0, 255);*/
glColor4f(1.0, 1.0, 0.0, 1.0);
glLineWidth(10);
CCPoint vertices[] = { CCPointMake(0,0), CCPointMake(50,50), CCPointMake(100,50), CCPointMake(100,100), CCPointMake(50,100) };
//参数依次是:点数组,点数量,是否封闭
ccDrawPoly( vertices, 5, false); // closed purple poly
/*glColor4ub(255, 0, 255, 255);*/
glColor4f(1.0, 0.0, 1.0, 1.0);
glLineWidth(2);
CCPoint vertices2[] = { CCPointMake(30,130), CCPointMake(30,230), CCPointMake(50,200) };
ccDrawPoly( vertices2, 3, true); // draw quad bezier path,绘制有一个控制点的贝塞尔曲线
ccDrawQuadBezier(CCPointMake(0,s.height), CCPointMake(s.width/2,s.height/2), CCPointMake(s.width,s.height), 50); // draw cubic bezier path,绘制有两个控制点的贝塞尔曲线
ccDrawCubicBezier(CCPointMake(s.width/2, s.height/2), CCPointMake(s.width/2+30,s.height/2+50), CCPointMake(s.width/2+60,s.height/2-50),CCPointMake(s.width, s.height/2),100); // restore original values,恢复opengl的正常参数
glLineWidth(1);
/*glColor4ub(255,255,255,255);*/
glColor4f(1.0, 1.0, 1.0, 1.0);
glPointSize(1);
}

第二部分:字符串绘制

1. 锚点

cocos2dx的字符串绘制使用的是Label,cocos2dx并不直接支持在屏幕中绘制字符串(这是有道理的,因为我们不能直接把一个string做成一个节点,那样很难理解),如果要直接绘制的话,可以自己封装opengl函数(网上有很多例子,一般是用texture做)。为了便于字体对齐,我们在很多游戏引擎中,都使用对齐锚点的功能,如j2me的anchor参数接口。默认情况下,锚点在(0,0)处。为了说明锚点的设置对字体对齐的影响,我们使用如下代码:

bool HelloWorld::init() {
if ( !CCLayer::init() ) {
return false;
}
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 64);
pLabel->setAnchorPoint(ccp(0,0.5));//设置锚点,默认是在node的中心,即锚点为(0.5,0.5),显示为居中对齐
pLabel->setPosition(ccp(size.width / 2, size.height/2));
this->addChild(pLabel, 1); draw();
return true;
}
//重载CCLayer的draw函数,然后绘制十字线;
void HelloWorld::draw() {
CCLayer::draw();
CCSize s = CCDirector::sharedDirector()->getWinSize();
glEnable(GL_LINE_SMOOTH);
ccDrawLine( CCPointMake(0, s.height/2), CCPointMake(s.width, s.height/2) );
ccDrawLine( CCPointMake(s.width/2, 0), CCPointMake(s.width/2, s.height) );
}
 

可以看到,cocos2dx默认锚点是node的中心。你自己可以设置不同的锚点,使文本具有不同的对齐方式。

2.显示中文字体


第三部分:绘制图片

cocos2dx中并没有直接绘制图片的概念,我们一般是使用CCSprite。核心代码如下:

CCSize size=CCDirector::sharedDirector()->getWinSize();
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
//pSprite->setFlipX(true); //可以手动设置图形旋转和镜像,而不是使用Action,因为有许多Action是个过程,而不是直接显示结果;
pSprite->setFlipY(true);
//pSprite->setRotation(90);
pSprite->setPosition(ccp(size.width/2, size.height/2));
this->addChild(pSprite, 0);

【转】cocos2d-x学习笔记03:绘制基本图元的更多相关文章

  1. WebGL学习笔记二——绘制基本图元

    webGL的基本图元点.线.三角形 gl.drawArrays(mode, first,count) first,代表从第几个点开始绘制即顶点的起始位置 count,代表绘制的点的数量. mode,代 ...

  2. 机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理、源码解析及测试

    机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理.源码解析及测试 关键字:决策树.python.源码解析.测试作者:米仓山下时间:2018-10-2 ...

  3. OpenCV 学习笔记03 findContours函数

    opencv-python   4.0.1 1 函数释义 词义:发现轮廓! 从二进制图像中查找轮廓(Finds contours in a binary image):轮廓是形状分析和物体检测和识别的 ...

  4. OpenCV 学习笔记03 边界框、最小矩形区域和最小闭圆的轮廓

    本节代码使用的opencv-python 4.0.1,numpy 1.15.4 + mkl 使用图片为 Mjolnir_Round_Car_Magnet_300x300.jpg 代码如下: impor ...

  5. C++ GUI Qt4学习笔记03

    C++ GUI Qt4学习笔记03   qtc++spreadsheet文档工具resources 本章介绍创建Spreadsheet应用程序的主窗口 1.子类化QMainWindow 通过子类化QM ...

  6. Unity3D学习笔记2——绘制一个带纹理的面

    目录 1. 概述 2. 详论 2.1. 网格(Mesh) 2.1.1. 顶点 2.1.2. 顶点索引 2.2. 材质(Material) 2.2.1. 创建材质 2.2.2. 使用材质 2.3. 光照 ...

  7. SaToken学习笔记-03

    SaToken学习笔记-03 如果排版有问题,请点击:传送门 核心思想 所谓权限验证,验证的核心就是一个账号是否拥有一个权限码 有,就让你通过.没有?那么禁止访问! 再往底了说,就是每个账号都会拥有一 ...

  8. Redis:学习笔记-03

    Redis:学习笔记-03 该部分内容,参考了 bilibili 上讲解 Redis 中,观看数最多的课程 Redis最新超详细版教程通俗易懂,来自 UP主 遇见狂神说 7. Redis配置文件 启动 ...

  9. OpenCV 学习笔记03 boundingRect、minAreaRect、minEnclosingCircle、boxPoints、int0、circle、rectangle函数的用法

    函数中的代码是部分代码,详细代码在最后 1 cv2.boundingRect 作用:矩形边框(boundingRect),用于计算图像一系列点的外部矩形边界. cv2.boundingRect(arr ...

  10. OGG学习笔记03

    OGG学习笔记03-单向复制简单故障处理 环境:参考:OGG学习笔记02-单向复制配置实例实验目的:了解OGG简单故障的基本处理思路. 1. 故障现象故障现象:启动OGG源端的extract进程,da ...

随机推荐

  1. BZOJ 1828: [Usaco2010 Mar]balloc 农场分配

    Description Input 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i ...

  2. UITableView.m:8042 crash 崩溃

     CRASH : /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:804 ...

  3. CF_Lucky Sum

    幸运数字的定义是这样:仅含4和7且不比n小的数为n的幸运数字. 输入范围l,r要求输出这个范围内的数字的幸运数字之和. 代码: #include<stdio.h> #define N 10 ...

  4. Android 使用LinearLayout.getChildAt(i)获取一个线性布局的view,并实现content中实现方法

    1.定义接口content的方法,如ok,cancle; 2.在View的处理类myview中实现content的方法. 3.通过contently.getChildAt(i)的方法获得View v; ...

  5. java自定义接口

    compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1:

  6. saxReader的列子

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.Attributes; import org.x ...

  7. Centos6.4 设置开机自动以某个非root用户启动脚本

    开机自动运行脚本,可以将脚本的执行命令放在 /etc/rc.d/rc.local 文件中,但是这样开机自动运行这个脚本的用户默认为root. 如果想以某个非root用户运行脚本,可以使用如下命令: s ...

  8. JavaScript---网络编程(6)-Dom和Bom模型概念讲解(节点获取,window对象)

    本节讲Dom和Bom模型概念讲解(节点获取,window对象). out.js: 写了2个输出到页面的函数. function println(param){ document.write(param ...

  9. JavaScript高级程序设计59.pdf

    dropEffect和effectAllowed 利用dataTransfer对象,传输数据.确定被拖动元素以及作为放置目标的元素能够接受什么操作,为此需要访问对象的两个属性dropEffect和ef ...

  10. Poj 1151-Atlantis 矩形切割

    题目:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...