首页 > 代码库 > Cocos2d-x3.2 绘制线条
Cocos2d-x3.2 绘制线条
#include "cocos2d.h" USING_NS_CC; class GameScene : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); void menuCallback(Ref* pSender); CREATE_FUNC(GameScene); //重载draw方法 virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; protected: //自定义draw实现 void onDraw(const cocos2d::Mat4 &transform, bool transformUpdated); cocos2d::CustomCommand _customCommand; private: //注意不能用auto关键字 Size size; Sprite *sprite; };
// Created by Jacedy on 14-8-11. #include "GameScene.h" USING_NS_CC; cocos2d::Scene* GameScene::createScene() { auto scene = Scene::create(); //创建一个场景 auto layer = GameScene::create(); //创建一个图层 scene->addChild(layer); return scene; } //初始化当前的图层 bool GameScene::init() { if(!Layer::init()) //初始化父类 return false; //获取屏幕大小 size = Director::getInstance()->getVisibleSize(); //auto size = Director::getInstance()->getWinSize(); return true; } void GameScene::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) { _customCommand.init(_globalZOrder); _customCommand.func = CC_CALLBACK_0(GameScene::onDraw, this, transform, flags); renderer->addCommand(&_customCommand); } void GameScene::onDraw(const cocos2d::Mat4 &transform, bool transformUpdated) { //利用Stack缓存 Director *director = Director::getInstance(); //CCASSERT(nullptr != director, "Director is null when setting matrix stack"); director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); CHECK_GL_ERROR_DEBUG(); //画边框 DrawPrimitives::setDrawColor4B(255, 255, 255, 255); glLineWidth(10); Vec2 vertices[] = {Vec2(100, 100), Vec2(300, 100), Vec2(300, 300), Vec2(100, 300)}; DrawPrimitives::drawPoly(vertices, 4, true); CHECK_GL_ERROR_DEBUG(); //绘制停止,释放 director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); } void GameScene::menuCallback(Ref* pSender) { }
Cocos2d-x3.2 绘制线条
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。