Cocos2dx3.5 opengl方式製作屏幕寫字板

通過重寫draw函數,來實現根據手勢劃線的功能,代碼一看就能明白

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
USING_NS_CC;
struct Line
{
    Vec2 p1;
    Vec2 p2;
};

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);

public:
    virtual bool onTouchBegan(Touch *touch, Event *unused_event);
    virtual void onTouchMoved(Touch *touch, Event *unused_event);
    virtual void onTouchEnded(Touch *touch, Event *unused_event);
    virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags);

private:
    Vec2 preVec2;
    Vec2 curVec2;
    Line _line;
    std::vector<Line> lines; //通過一段段小的line來畫連續的線
};

#endif // __HELLOWORLD_SCENE_H__

實現

#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);


    /*auto drawNode = DrawNode::create();
    this->addChild(drawNode);
    drawNode->drawTriangle(Vec2(20, 250), Vec2(100, 300), Vec2(50, 200), Color4F(1, 1, 0, 1));*/
    return true;
}

bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event) {
    auto location = touch->getLocation();
    preVec2 = curVec2 = location;
    return true;
}

void HelloWorld::onTouchMoved(Touch *touch, Event *unused_event) {
    auto location = touch->getLocation();
    curVec2 = location;

    if ((curVec2 - preVec2).getLength() > 1) {
        _line.p1 = preVec2;
        _line.p2 = curVec2;
        lines.push_back(_line);
        preVec2 = curVec2;
    }
}

void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event) {
    auto location = touch->getLocation();
    curVec2 = location;

    if ((curVec2 - preVec2).getLength() > 25) {
        _line.p1 = preVec2;
        _line.p2 = curVec2;
        lines.push_back(_line);
        preVec2 = curVec2;
    }
}

void HelloWorld::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags) {
    DrawPrimitives::setDrawColor4B(0, 255, 255, 255); ///4B 紅 綠 藍 alpha通道 四個字節(32位 RGBA分別8位 )來表示一個象素包含的信息
    glLineWidth(5);
    auto iter = lines.begin();
    for (iter; iter != lines.end(); iter++) {
        DrawPrimitives::drawLine(iter->p1, iter->p2);
    }
    //DrawPrimitives::drawPoly()
    //ccDrawRect(Vec2(10, 10), Vec2(100, 100));

}

運行結果如下圖所示:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章