最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

cocos2dx實(shí)現(xiàn)橡皮擦效果以及判斷是否擦除完畢

 更新時(shí)間:2018年12月20日 14:29:13   作者:Gemini_Dong  
這篇文章主要為大家詳細(xì)介紹了cocos2dx實(shí)現(xiàn)橡皮擦效果以及判斷是否擦除完畢,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了cocos2dx實(shí)現(xiàn)橡皮擦效果,以及判斷是否擦除完畢,供大家參考,具體內(nèi)容如下

首先修改HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
 
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
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();
 
 // a selector callback
 void menuCloseCallback(cocos2d::Ref* pSender);
 
 // implement the "static create()" method manually
 CREATE_FUNC(HelloWorld);
 void myUpdate(float dt);//不斷判斷是否全部擦除
 void onTouchesMoved(const std::vector<Touch*>& touches, Event* event);
 bool myIsDataClear(RenderTexture *pRenderTexture);//是否完全擦除
 bool myIsDataClearInRect(RenderTexture *pRenderTexture,int x,int y,int width ,int height);//某個(gè)區(qū)域是否完全擦除
 Sprite *sprFore;
 RenderTexture *renderTexture;
 Vector<Sprite*> _brushs;
};
 
#endif // __HELLOWORLD_SCENE_H__

然后修改HelloWorld.cpp文件

#include "HelloWorldScene.h"
 
USING_NS_CC;
 
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()
{
 
 if ( !Layer::init() )
 {
  return false;
 }
 
 Size visibleSize = Director::getInstance()->getVisibleSize();
 Vec2 origin = Director::getInstance()->getVisibleOrigin();
 
 auto closeItem = MenuItemImage::create(
           "CloseNormal.png",
           "CloseSelected.png",
           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
 
 closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
        origin.y + closeItem->getContentSize().height/2));
 
 auto menu = Menu::create(closeItem, NULL);
 menu->setPosition(Vec2::ZERO);
 this->addChild(menu, 1);
 
 
 
 
 auto label = Label::createWithTTF("Test Eraser", "fonts/Marker Felt.ttf", 24);
 
 
 label->setPosition(Vec2(origin.x + visibleSize.width/2,
       origin.y + visibleSize.height - label->getContentSize().height));
 
 this->addChild(label, 1);
 
 
 
 sprFore = Sprite::create("HelloWorld.png");
 sprFore->setPosition(Vec2(visibleSize / 2) + origin);
 sprFore->retain();
 renderTexture = RenderTexture::create(visibleSize.width, visibleSize.height, Texture2D::PixelFormat::RGBA8888);
 renderTexture->setContentSize(visibleSize);
 renderTexture->retain();
 this->addChild(renderTexture);
 renderTexture->setPosition(Vec2(visibleSize / 2) + origin);
 
 
 
 renderTexture->beginWithClear(0, 0, 0, 0);
 sprFore->visit();
 renderTexture->end();
 
 
 
 auto listener = EventListenerTouchAllAtOnce::create();
 listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
 
 //不斷判斷是否擦除完畢
 schedule(schedule_selector(HelloWorld::myUpdate), 0.5f);
 return true;
}
 
 
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
 MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
 return;
#endif
 
 Director::getInstance()->end();
 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
 exit(0);
#endif
}
 
void HelloWorld::myUpdate(float dt)
{
 if (myIsDataClear(renderTexture) == true)
 {
 
 log("image is clear !");
 }
 
 if (myIsDataClearInRect(renderTexture,300,200,50,50) == true)
 {
 
 log("image in rect is clear !");
 }
}
 
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
{
 auto touch = touches[0];
 auto start = touch->getLocation();
 auto end = touch->getPreviousLocation();
 
 // begin drawing to the render texture
 renderTexture->begin();
 
 // for extra points, we'll draw this smoothly from the last position and vary the sprite's
 // scale/rotation/offset
 float distance = start.getDistance(end);
 if (distance > 1)
 {
 int d = (int)distance;
 _brushs.clear();
 for (int i = 0; i < d; ++i)
 {
 //橡皮擦
 auto sprite = CCSprite::create("red.png");//主要根據(jù)圖片定義橡皮擦的形狀
 BlendFunc blendFunc;
 blendFunc.src = GL_ZERO;
 blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
 sprite->setBlendFunc(blendFunc);
 sprite->setScale(1.8f);
 renderTexture->addChild(sprite);
 _brushs.pushBack(sprite);
 }
 for (int i = 0; i < d; i++)
 {
 float difx = end.x - start.x;
 float dify = end.y - start.y;
 float delta = (float)i / distance;
 _brushs.at(i)->setPosition(Vec2(start.x + (difx * delta), start.y + (dify * delta)));
 _brushs.at(i)->visit();
 }
 }
 
 // finish drawing and return context back to the screen
 renderTexture->end();
 
}
 
bool HelloWorld::myIsDataClear(RenderTexture *pRenderTexture)
{
 bool m_bEraserOk = false;
 
 Image* image = new Image();
 image = pRenderTexture->newImage(true);
 
 int m = 3;
 if (image->hasAlpha())
 {
 m = 4;
 }
 
 unsigned char *data_ = image->getData();
 
 int x = 0, y = 0;
 /// 這里要提醒一點(diǎn),即Opengl下,其中心點(diǎn)坐標(biāo)在左上角
 for (x = 0; x < pRenderTexture->getContentSize().width; ++x)
 {
 for (y = 0; y < pRenderTexture->getContentSize().height; ++y)
 {
 //獲取每個(gè)點(diǎn)的像素點(diǎn)值
 unsigned char *pixel = data_ + (x + y * image->getWidth()) * m;
 
 // You can see/change pixels' RGBA value(0-255) here !
 unsigned int r = (unsigned int)*pixel;
 unsigned int g = (unsigned int)*(pixel + 1);
 unsigned int b = (unsigned int)*(pixel + 2);
 unsigned int a = (unsigned int)*(pixel + 3);
 
 if (r != 0 && g != 0 && b != 0 && a != 0)
 {
 m_bEraserOk = false;
 break;
 }
 }
 //如果改列 有一個(gè)點(diǎn)的像素點(diǎn)值不為零 跳出
 if (pRenderTexture->getContentSize().height != y)
 {
 break;
 }
 }
 
 //如果所有點(diǎn)的像素點(diǎn)值都為0 則擦除完畢
 if (x == pRenderTexture->getContentSize().width && y == pRenderTexture->getContentSize().height)
 {
 m_bEraserOk = true;
 }
 
 delete image;
 
 return m_bEraserOk;
}
 
bool HelloWorld::myIsDataClearInRect(RenderTexture *pRenderTexture, int x, int y, int width, int height)
{
 
 
 
 bool m_bEraserOk = false;
 
 Image* image = new Image();
 image = pRenderTexture->newImage(true);
 
 int m = 3;
 if (image->hasAlpha())
 {
 m = 4;
 }
 
 int i = 0, j = 0;
 unsigned char* mdata = (unsigned char*)image->getData();
 for (i = 0; i < width; ++i)
 {
 for (j = 0; j < height; ++j)
 {
 
 unsigned char *pixel = mdata + (i + x + (image->getHeight() - y - (height - j)) * image->getWidth()) * m;
 
 // You can see/change pixels' RGBA value(0-255) here !
 unsigned int r = (unsigned int)*pixel;
 unsigned int g = (unsigned int)*(pixel + 1);
 unsigned int b = (unsigned int)*(pixel + 2);
 unsigned int a = (unsigned int)*(pixel + 3);
 
 if (r != 0 && g != 0 && b != 0 && a != 0)
 {
 break;
 }
 
 }
 
 if (height != j)
 {
 break;
 }
 }
 
 if (i == width && j == height)
 {
 m_bEraserOk = true;
 }
 return m_bEraserOk;
}

看下運(yùn)行效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++編程使用findfirst和findnext查找及遍歷文件實(shí)現(xiàn)示例

    C++編程使用findfirst和findnext查找及遍歷文件實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了C++編程如何使用findfirst和findnext查找及遍歷文件實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • c++中的4種類型轉(zhuǎn)化方式詳細(xì)解析

    c++中的4種類型轉(zhuǎn)化方式詳細(xì)解析

    const_cast該函數(shù)用于去除指針變量的常量屬性,將它轉(zhuǎn)換為一個(gè)對應(yīng)指針類型的普通變量。反過來,也可以將一個(gè)非常量的指針變量轉(zhuǎn)換為一個(gè)常指針變量
    2013-10-10
  • Matlab控制電腦攝像實(shí)現(xiàn)實(shí)時(shí)人臉檢測和識別詳解

    Matlab控制電腦攝像實(shí)現(xiàn)實(shí)時(shí)人臉檢測和識別詳解

    人臉識別過程主要由四個(gè)階段組成:人臉檢測、圖像預(yù)處理、面部特征提取和特征識別。這篇文章主要介紹了如何使用MATLAB控制筆記本電腦的攝像頭,并進(jìn)行實(shí)時(shí)人臉檢測和識別,需要的可以參考一下
    2022-10-10
  • C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作

    C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作

    這篇文章主要介紹了C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Qt 智能指針的具體使用

    Qt 智能指針的具體使用

    本文主要介紹了Qt 智能指針的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • 詳解C語言面向?qū)ο缶幊讨械姆庋b

    詳解C語言面向?qū)ο缶幊讨械姆庋b

    這篇文章主要為大家詳細(xì)介紹了C語言面向?qū)ο缶幊讨械姆庋b,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作

    關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作

    這篇文章主要介紹了關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作,關(guān)于數(shù)據(jù)結(jié)構(gòu)鏈表的操作一般涉及的就是增刪改查,下面將關(guān)于無空頭鏈表展開介紹,需要的朋友可以參考下
    2023-04-04
  • C++中繼承基類與派生類的區(qū)別

    C++中繼承基類與派生類的區(qū)別

    這篇文章主要介紹了C++中繼承基類與派生類的區(qū)別,面向?qū)ο蟪绦蛟O(shè)計(jì)中最重要的一個(gè)概念是繼承。繼承允許我們依據(jù)另一個(gè)類來定義一個(gè)類,這使得創(chuàng)建和維護(hù)一個(gè)應(yīng)用程序變得更容易,需要的朋友可以參考下
    2023-05-05
  • 詳解C++?轉(zhuǎn)換的非正式分類

    詳解C++?轉(zhuǎn)換的非正式分類

    C++?正式分類方法是直接按語法分類,分為:隱式轉(zhuǎn)換和顯示轉(zhuǎn)換。這篇文章主要介紹了C++?轉(zhuǎn)換的非正式分類,需要的朋友可以參考下
    2022-01-01
  • 基于Matlab實(shí)現(xiàn)繪制3D足球的示例代碼

    基于Matlab實(shí)現(xiàn)繪制3D足球的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Matlab實(shí)現(xiàn)繪制3D足球,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定幫助,需要的可以參考一下
    2022-11-11

最新評論

巴彦淖尔市| 阜阳市| 额济纳旗| 来安县| 常山县| 进贤县| 昌宁县| 牟定县| 满城县| 额敏县| 永仁县| 柘城县| 郁南县| 兴山县| 城口县| 乐至县| 浦江县| 江孜县| 新安县| 吴忠市| 普安县| 报价| 红桥区| 闵行区| 大同县| 中西区| 达州市| 石台县| 常德市| 阜宁县| 淄博市| 晋城| 兴城市| 湟中县| 福清市| 望城县| 花莲县| 蓝山县| 新晃| 三河市| 峡江县|