繪圖API由相應的編程語言提供,cocos2d封裝了自己的繪圖方法,形成獨有的特性。
1 --[[ 2 cocos2d-x基本圖形的繪制 3 --]] 4 5 local function drawPrimitivesMainLayer() 6 local kItemTagBasic = 1000 7 local testCount = 2 8 local maxCases = testCount 9 local curCase = 0 10 local size = cc.Director:getInstance():getWinSize() 11 local curLayer = nil 12 13 local function orderCallbackMenu() 14 local function backCallback() 15 curCase = curCase - 1 16 if curCase < 0 then 17 curCase = curCase + maxCases 18 end 19 showCurrentTest() 20 end 21 22 local function restartCallback() 23 showCurrentTest() 24 end 25 26 local function nextCallback() 27 curCase = curCase + 1 28 curCase = curCase % maxCases 29 showCurrentTest() 30 end 31 32 local ordercallbackmenu = cc.Menu:create() 33 local size = cc.Director:getInstance():getWinSize() 34 local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) 35 item1:registerScriptTapHandler(backCallback) 36 ordercallbackmenu:addChild(item1,kItemTagBasic) 37 local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) 38 item2:registerScriptTapHandler(restartCallback) 39 ordercallbackmenu:addChild(item2,kItemTagBasic) 40 local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) 41 ordercallbackmenu:addChild(item3,kItemTagBasic) 42 item3:registerScriptTapHandler(nextCallback) 43 44 item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) 45 item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) 46 item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) 47 48 ordercallbackmenu:setPosition(cc.p(0, 0)) 49 50 return ordercallbackmenu 51 end 52 53 local function GetTitle() 54 if 0 == curCase then 55 return "Draw primitives" 56 elseif 1 == curCase then 57 return "Test DrawNode" 58 end 59 end 60 61 local function GetSubTitle() 62 if 0 == curCase then 63 return "Drawing Primitives by call gl funtions" 64 elseif 1 == curCase then 65 return "Testing DrawNode - batched draws. Concave polygons are BROKEN" 66 end 67 end 68 69 local function InitTitle(layer) 70 --Title 71 local lableTitle = cc.Label:createWithTTF(GetTitle(), s_arialPath, 40) 72 layer:addChild(lableTitle, 15) 73 lableTitle:setAnchorPoint(cc.p(0.5, 0.5)) 74 lableTitle:setPosition(cc.p(size.width / 2, size.height - 32)) 75 lableTitle:setColor(cc.c3b(255, 255, 40)) 76 --SubTitle 77 local subLabelTitle = cc.Label:createWithTTF(GetSubTitle(), s_thonburiPath, 16) 78 layer:addChild(subLabelTitle, 15) 79 subLabelTitle:setAnchorPoint(cc.p(0.5, 0.5)) 80 subLabelTitle:setPosition(cc.p(size.width / 2, size.height - 80)) 81 end 82 --[[ 83 兩種大的方法: 84 1、OpenGL圖形庫繪圖 85 2、繪圖節點繪圖 86 --]] 87 88 --基本繪圖API展示 89 local function createDrawPrimitivesEffect() 90 local layer = cc.Layer:create() 91 92 InitTitle(layer) 93 94 --創建圖形庫節點,並設置尺寸和錨點 95 local glNode = gl.glNodeCreate() 96 glNode:setContentSize(cc.size(size.width, size.height)) 97 glNode:setAnchorPoint(cc.p(0.5, 0.5)) 98 99 --自定義回調方法 100 local function primitivesDraw(transform, transformUpdated) 101 --不懂OpenGL的,就只能先記住 了 102 kmGLPushMatrix() 103 kmGLLoadMatrix(transform) 104 --指定兩個點,畫線 105 cc.DrawPrimitives.drawLine(VisibleRect:leftBottom(), VisibleRect:rightTop() ) 106 107 --指定兩個點,畫線。還可以設置顏色、透明度和線寬 108 gl.lineWidth( 5.0 )--線寬 109 cc.DrawPrimitives.drawColor4B(255,0,0,255)--顏色 110 cc.DrawPrimitives.drawLine( VisibleRect:leftTop(), VisibleRect:rightBottom() ) 111 112 --在某一個坐標上畫點 113 cc.DrawPrimitives.setPointSize(64) 114 cc.DrawPrimitives.drawColor4B(0, 0, 255, 128) 115 cc.DrawPrimitives.drawPoint(VisibleRect:center()) 116 117 --畫多個點 118 local points = {cc.p(60,60), cc.p(70,70), cc.p(60,70), cc.p(70,60) } 119 cc.DrawPrimitives.setPointSize(4) 120 cc.DrawPrimitives.drawColor4B(0,255,255,255) 121 --drawPoints()第二個參數表示從table中依次取多少個 122 cc.DrawPrimitives.drawPoints(points,4) 123 124 gl.lineWidth(5) 125 cc.DrawPrimitives.drawColor4B(0, 255, 0, 255) 126 --5個參數:圓心,半徑,逆時針旋轉的角度(弧度制),正幾邊形,是否顯示半徑 127 --也許你覺得旋轉的角度沒有意義,但當你明白這里的Circle是由正多邊形模擬的,就懂了 128 -- cc.DrawPrimitives.drawCircle( VisibleRect:center(), 110, math.pi, 100, true) 129 -- cc.DrawPrimitives.drawCircle( VisibleRect:center(), 150, 90, 100, true) 130 -- cc.DrawPrimitives.drawCircle( VisibleRect:center(), 135, 45, 100, true) 131 cc.DrawPrimitives.drawCircle( VisibleRect:center(), 120, 0, 100, false) 132 133 --畫圓,同上 134 gl.lineWidth(2) 135 cc.DrawPrimitives.drawColor4B(0, 255, 255, 255) 136 cc.DrawPrimitives.drawCircle( VisibleRect:center(), 50, math.pi / 2, 50, true) 137 138 --畫實圓,參數:圓心,半徑,旋轉角度(弧度,邊數,x軸縮放,y軸縮放 139 gl.lineWidth(2) 140 cc.DrawPrimitives.drawColor4B(255, 0, 255, 255) 141 cc.DrawPrimitives.drawSolidCircle( cc.p(VisibleRect:center().x + 140 ,VisibleRect:center().y), 40, math.rad(90), 10, 1.0, 1.0) 142 143 --任意空多邊形 144 gl.lineWidth(10) 145 cc.DrawPrimitives.drawColor4B(255, 255, 0, 255) 146 local yellowPoints = { cc.p(0,0), cc.p(50,50), cc.p(100,50), cc.p(100,100), cc.p(50,100)} 147 --3參數:頂點列表,點數,是否封閉(首尾相連) 148 cc.DrawPrimitives.drawPoly( yellowPoints, 5, true) 149 150 --任意實多邊形, 151 gl.lineWidth(1) 152 local filledVertices = { cc.p(0,120), cc.p(50,120), cc.p(50,170), cc.p(25,200), cc.p(0,170) } 153 --3參數:頂點列表,頂點數,顏色 154 cc.DrawPrimitives.drawSolidPoly(filledVertices, 5, cc.c4f(0.5, 0.5, 1, 1)) 155 156 --任意空多邊形,同上 157 gl.lineWidth(2) 158 cc.DrawPrimitives.drawColor4B(255, 255, 255, 255) 159 local closePoints= { cc.p(30,130), cc.p(30,230), cc.p(50,200) } 160 cc.DrawPrimitives.drawPoly( closePoints, 3, true) 161 162 --基本貝賽爾曲線,4參數:起點,控制點,終點,邊數。 163 --普及一下:計算機是用首尾連接的線段模擬曲線和圓,分段越多,就越圓滑,越象曲線。 164 cc.DrawPrimitives.drawQuadBezier(VisibleRect:leftTop(), VisibleRect:center(), VisibleRect:rightTop(), 50) 165 166 --多段貝賽爾曲線:起點,控制點...控制點,終點,邊數 167 cc.DrawPrimitives.drawCubicBezier(VisibleRect:center(), cc.p(VisibleRect:center().x + 30, VisibleRect:center().y + 50), cc.p(VisibleRect:center().x + 60,VisibleRect:center().y - 50), VisibleRect:right(), 100) 168 169 --實任意多邊形:頂點列表,邊數,顏色 170 local solidvertices = {cc.p(60,160), cc.p(70,190), cc.p(100,190), cc.p(90,160)} 171 cc.DrawPrimitives.drawSolidPoly( solidvertices, 4, cc.c4f(1, 1, 0, 1) ) 172 173 local array = { 174 cc.p(0, 0), 175 cc.p(size.width / 2 - 30, 0), 176 cc.p(size.width / 2 - 30, size.height - 80), 177 cc.p(0, size.height - 80), 178 cc.p(0, 0), 179 } 180 cc.DrawPrimitives.drawColor4B(255,0,0,255) 181 cc.DrawPrimitives.drawCatmullRom( array, 20) 182 --基樣線條:參照點列表,精度,邊數 183 cc.DrawPrimitives.drawCardinalSpline( array, 0,100) 184 185 gl.lineWidth(1) 186 cc.DrawPrimitives.drawColor4B(255,255,255,255) 187 cc.DrawPrimitives.setPointSize(1) 188 189 kmGLPopMatrix() 190 end 191 --給圖形庫節點注冊繪圖回調 192 glNode:registerScriptDrawHandler(primitivesDraw) 193 --將圖形庫節點(glNode)添加到Layer,並設置位置 194 layer:addChild(glNode,-10) 195 glNode:setPosition( size.width / 2, size.height / 2) 196 197 return layer 198 end 199 200 -- 201 local function createDrawNodeTest() 202 local layer = cc.Layer:create() 203 204 InitTitle(layer) 205 206 --cocos2d-x繪圖節點 207 --下面的繪圖,直接調用DrawNode節點的相關方法:drawDot 208 local draw = cc.DrawNode:create() 209 layer:addChild(draw, 10) 210 211 --Draw 10 circles 212 --畫十個同心圓點 213 for i=1, 10 do 214 --參數:位置,半徑,顏色和透明度 - 215 draw:drawDot(cc.p(size.width/2, size.height/2 + i*10), 10*(10-i), cc.c4f(math.random(0,1), math.random(0,1), math.random(0,1), 1)) 216 end 217 218 --Draw polygons多邊形:頂點列表,頂點數,填充顏色,邊的顏色 219 points = { cc.p(size.height/4, 0), cc.p(size.width, size.height / 5), cc.p(size.width / 3 * 2, size.height) } 220 -- draw:drawPolygon(points, table.getn(points), cc.c4f(1,0,0,0.5), 4, cc.c4f(0,0,1,1)) 221 222 local o = 80 223 local w = 20 224 local h = 50 225 local star1 = { cc.p( o + w, o - h), cc.p(o + w * 2, o), cc.p(o + w * 2 + h, o + w), cc.p(o + w * 2, o + w * 2) } 226 227 --draw:drawPolygon(star1, table.getn(star1), cc.c4f(0,1,0,0.5), 1, cc.c4f(0,0,1,1)) 228 229 o = 180 230 w = 20 231 h = 50 232 local star2 = { 233 cc.p(o, o), cc.p(o + w, o - h), cc.p(o + w * 2, o), --lower spike 234 cc.p(o + w * 2 + h, o + w ), cc.p(o + w * 2, o + w * 2), --right spike 235 cc.p(o + w, o + w * 2 + h), cc.p(o, o + w * 2), --top spike 236 cc.p(o - h, o + w), --left spike 237 }; 238 239 --draw:drawPolygon(star2, table.getn(star2), cc.c4f(1,0,0,0.5), 1, cc.c4f(0,0,1,1)) 240 241 --畫線段:起點,終點,線寬,顏色 242 draw:drawSegment(cc.p(20,size.height), cc.p(20,size.height/2), 10, cc.c4f(0, 1, 1, 0.5)) 243 244 draw:drawSegment(cc.p(10,size.height/2), cc.p(size.width, size.height/2), 40, cc.c4f(1, 0, 0, 0.5)) 245 246 return layer 247 end 248 249 --[[ 250 總結: 251 繪圖兩種方法及各自的步驟: 252 方法一: 253 1、創建圖形庫節點:local glNode = gl.glNodeCreate() 254 2、給該節點注冊封裝了繪圖方法的回調 glNode:registerScriptDrawHandler(primitivesDraw),然后你就可以在primitiesDraw()中自由發揮了 255 直接調用cc.DrawPrimitives.下的一系列方法。其中設置線寬的方法為:gl.lineWidth(int),顏色:cc.DrawPrimitives.drawColor4B(255,0,0,255) 256 3、將圖形庫節點添加到層中顯示 257 258 方法二: 259 1、創建cocos2d-x繪圖節點,local drawNode = cc.DrawNode:create() 260 2、添加到層,layer:addChild(drawNode) 261 3、直接調用繪圖節點下的方法繪圖,如:drawNode:drawSegment(cc.p()...cc.p(),int,cc.c4f()) 262 263 至於說這兩種方法的區別,等我有空慢慢研究吧。 264 --]] 265 266 267 --下面的是通用界面布局,不解釋 268 local function createLayerByCurCase(curCase) 269 if 0 == curCase then 270 return createDrawPrimitivesEffect() 271 elseif 1 == curCase then 272 return createDrawNodeTest() 273 end 274 end 275 276 function showCurrentTest() 277 local curScene = cc.Scene:create() 278 if nil ~= curScene then 279 curLayer = createLayerByCurCase(curCase) 280 if nil ~= curLayer then 281 curScene:addChild(curLayer) 282 curLayer:addChild(orderCallbackMenu(),15) 283 curScene:addChild(CreateBackMenuItem()) 284 cc.Director:getInstance():replaceScene(curScene) 285 end 286 end 287 end 288 289 curLayer = createLayerByCurCase(curCase) 290 curLayer:addChild(orderCallbackMenu(),15) 291 return curLayer 292 end 293 294 function DrawPrimitivesTest() 295 local scene = cc.Scene:create() 296 scene:addChild(drawPrimitivesMainLayer()) 297 scene:addChild(CreateBackMenuItem()) 298 return scene 299 end
