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));

}

运行结果如下图所示:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值