#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) { }
时间: 2024-12-25 22:32:12