之前一直做flash开发,像变灰,高亮,描边了这些效果非常容易实现,有现成的api直接调用即可,cocos2d-x并没有提供,所以遇到这种需求就得硬着都头寻求解决方案了,废话不多说,直接上代码:
CCColorUtil.cpp:
//
// CCColorUtil.cpp
// quickcocos2dx
//
// Created by Terran Tian on 13-11-19.
// Copyright (c) 2013年 qeeplay.com. All rights reserved.
//
#include "CCColorUtil.h"
#include "cocos2d.h"
using namespace cocos2d;
void CCColorUtil::addGray(CCSprite* sp)
{
do
{
CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureGray);
sp->setShaderProgram(pProgram);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->link();
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->updateUniforms();
CHECK_GL_ERROR_DEBUG();
} while (0);
}
void CCColorUtil::removeGray(CCSprite* sp)
{
do
{
CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor);
sp->setShaderProgram(pProgram);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->link();
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->updateUniforms();
CHECK_GL_ERROR_DEBUG();
} while (0);
}
CCColorUtil.h:
//
// CCColorUtil.h
// quickcocos2dx
//
// Created by Terran Tian on 13-11-19.
// Copyright (c) 2013年 qeeplay.com. All rights reserved.
//
#ifndef __quickcocos2dx__CCColorUtil__
#define __quickcocos2dx__CCColorUtil__
#include <iostream>
#endif /* defined(__quickcocos2dx__CCColorUtil__) */
#include "cocos2d.h"
using namespace cocos2d;
class CCColorUtil
{
public:
static void addGray(CCSprite* sp);
static void removeGray(CCSprite* sp);
};