Added CCRGBAProtocol and CCBlendProtocol to CCSkeleton. Fixed issue #20.

This commit is contained in:
NathanSweet 2013-03-27 12:20:49 +01:00
parent e4e8089340
commit 41b748ef4d
5 changed files with 77 additions and 19 deletions

View File

@ -215,15 +215,6 @@
name = Frameworks; name = Frameworks;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
2FEE852B170030EC0013E4C9 /* spine */ = {
isa = PBXGroup;
children = (
2FEE8585170033120013E4C9 /* spine-cocos2dx */,
2FEE85841700330E0013E4C9 /* spine-cpp */,
);
name = spine;
sourceTree = "<group>";
};
2FEE85841700330E0013E4C9 /* spine-cpp */ = { 2FEE85841700330E0013E4C9 /* spine-cpp */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -339,7 +330,8 @@
BF23D4E2143315EB00657E08 /* Classes */ = { BF23D4E2143315EB00657E08 /* Classes */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
2FEE852B170030EC0013E4C9 /* spine */, 2FEE85841700330E0013E4C9 /* spine-cpp */,
2FEE8585170033120013E4C9 /* spine-cocos2dx */,
2FEE8522170030A20013E4C9 /* AppDelegate.cpp */, 2FEE8522170030A20013E4C9 /* AppDelegate.cpp */,
2FEE8523170030A20013E4C9 /* AppDelegate.h */, 2FEE8523170030A20013E4C9 /* AppDelegate.h */,
2FEE8524170030A20013E4C9 /* AppMacros.h */, 2FEE8524170030A20013E4C9 /* AppMacros.h */,

View File

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "1.0">
</Bucket>

View File

@ -15,7 +15,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
// create the application instance // create the application instance
AppDelegate app; AppDelegate app;
CCEGLView* eglView = CCEGLView::sharedOpenGLView(); CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setViewName("ExampleSpine"); eglView->setViewName("SpineExample");
eglView->setFrameSize(480, 320); eglView->setFrameSize(480, 320);
// So we need to invoke 'setFrameZoomFactor' (only valid on desktop(win32, mac, linux)) to make the window smaller. // So we need to invoke 'setFrameZoomFactor' (only valid on desktop(win32, mac, linux)) to make the window smaller.
// eglView->setFrameZoomFactor(0.4f); // eglView->setFrameZoomFactor(0.4f);

View File

@ -26,7 +26,7 @@
#ifndef SPINE_CCSKELETON_H_ #ifndef SPINE_CCSKELETON_H_
#define SPINE_CCSKELETON_H_ #define SPINE_CCSKELETON_H_
#include "base_nodes/CCNode.h" #include "cocos2d.h"
namespace spine { namespace spine {
@ -35,7 +35,7 @@ class Skeleton;
class AnimationState; class AnimationState;
class AnimationStateData; class AnimationStateData;
class CCSkeleton: public cocos2d::CCNode { class CCSkeleton: public cocos2d::CCNode, public cocos2d::CCRGBAProtocol, public cocos2d::CCBlendProtocol {
public: public:
Skeleton *skeleton; Skeleton *skeleton;
AnimationState *state; AnimationState *state;
@ -47,6 +47,17 @@ public:
virtual void update (float deltaTime); virtual void update (float deltaTime);
virtual void draw (); virtual void draw ();
// CCRGBAProtocol
CC_PROPERTY_PASS_BY_REF(cocos2d::ccColor3B, color, Color);
cocos2d::ccColor3B colorUnmodified;
CC_PROPERTY(GLubyte, opacity, Opacity);
bool opacityModifyRGB;
bool isOpacityModifyRGB ();
void setOpacityModifyRGB (bool isOpacityModifyRGB);
// CCBlendProtocol
CC_PROPERTY(cocos2d::ccBlendFunc, blendFunc, BlendFunc);
}; };
} /* namespace spine */ } /* namespace spine */

View File

@ -44,11 +44,20 @@ CCSkeleton* CCSkeleton::create (SkeletonData* skeletonData) {
} }
CCSkeleton::CCSkeleton (SkeletonData *skeletonData, AnimationStateData *stateData) : CCSkeleton::CCSkeleton (SkeletonData *skeletonData, AnimationStateData *stateData) :
opacity(255),
opacityModifyRGB(false),
debug(false) { debug(false) {
if (!skeletonData) throw std::invalid_argument("skeletonData cannot be null."); if (!skeletonData) throw std::invalid_argument("skeletonData cannot be null.");
skeleton = new Skeleton(skeletonData); skeleton = new Skeleton(skeletonData);
state = new AnimationState(stateData); state = new AnimationState(stateData);
color.r = 255;
color.g = 255;
color.b = 255;
colorUnmodified = color;
blendFunc.src = CC_BLEND_SRC;
blendFunc.dst = CC_BLEND_DST;
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor)); setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
scheduleUpdate(); scheduleUpdate();
} }
@ -67,7 +76,12 @@ void CCSkeleton::update (float deltaTime) {
void CCSkeleton::draw () { void CCSkeleton::draw () {
CC_NODE_DRAW_SETUP(); CC_NODE_DRAW_SETUP();
ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
ccGLBlendFunc(blendFunc.src, blendFunc.dst);
skeleton->r = color.r / (float)255;
skeleton->g = color.g / (float)255;
skeleton->b = color.b / (float)255;
skeleton->a = opacity / (float)255;
skeleton->draw(); skeleton->draw();
if (debug) { if (debug) {
@ -103,3 +117,49 @@ void CCSkeleton::draw () {
} }
} }
} }
// CCSkeleton - RGBA protocol
const ccColor3B& CCSkeleton::getColor () {
if (opacityModifyRGB) return colorUnmodified;
return color;
}
void CCSkeleton::setColor (const ccColor3B& color) {
this->color = colorUnmodified = color;
if (opacityModifyRGB) {
this->color.r = color.r * opacity / 255;
this->color.g = color.g * opacity / 255;
this->color.b = color.b * opacity / 255;
}
}
GLubyte CCSkeleton::getOpacity () {
return opacity;
}
void CCSkeleton::setOpacity (GLubyte opacity) {
this->opacity = opacity;
// Special opacity for premultiplied textures.
if (opacityModifyRGB) this->setColor(colorUnmodified);
}
void CCSkeleton::setOpacityModifyRGB (bool isOpacityModifyRGB) {
ccColor3B oldColor = this->getColor();
opacityModifyRGB = isOpacityModifyRGB;
this->setColor(oldColor);
}
bool CCSkeleton::isOpacityModifyRGB () {
return opacityModifyRGB;
}
// CCBlendProtocol
ccBlendFunc CCSkeleton::getBlendFunc () {
return blendFunc;
}
void CCSkeleton::setBlendFunc (ccBlendFunc blendFunc) {
this->blendFunc = blendFunc;
}