diff --git a/cocos2dx/actions/CCActionTween.cpp b/cocos2dx/actions/CCActionTween.cpp index 4f00a89975..b13a123299 100644 --- a/cocos2dx/actions/CCActionTween.cpp +++ b/cocos2dx/actions/CCActionTween.cpp @@ -45,9 +45,9 @@ bool CCActionTween::initWithDuration(ccTime aDuration, const char* key, float fr { if (CCActionInterval::initWithDuration(aDuration)) { - key_ = key; - to_ = to; - from_ = from; + m_strKey = key; + m_fTo = to; + m_fFrom = from; return true; } @@ -58,17 +58,17 @@ void CCActionTween::startWithTarget(CCNode *pTarget) { CCAssert(dynamic_cast(pTarget), "target must implement CCActionTweenDelegate"); CCActionInterval::startWithTarget(pTarget); - delta_ = to_ - from_; + m_fDelta = m_fTo - m_fFrom; } void CCActionTween::update(ccTime dt) { - dynamic_cast(m_pTarget)->updateTweenAction(to_ - delta_ * (1 - dt), key_.c_str()); + dynamic_cast(m_pTarget)->updateTweenAction(m_fTo - m_fDelta * (1 - dt), m_strKey.c_str()); } CCActionInterval* CCActionTween::reverse() { - return CCActionTween::actionWithDuration(m_fDuration, key_.c_str(), to_, from_); + return CCActionTween::actionWithDuration(m_fDuration, m_strKey.c_str(), m_fTo, m_fFrom); } diff --git a/cocos2dx/include/CCActionManager.h b/cocos2dx/include/CCActionManager.h index 8258c155b9..7fa78bf1af 100755 --- a/cocos2dx/include/CCActionManager.h +++ b/cocos2dx/include/CCActionManager.h @@ -53,7 +53,6 @@ class CC_DLL CCActionManager : public CCObject public: CCActionManager(void); ~CCActionManager(void); - bool init(void); // actions diff --git a/cocos2dx/include/CCActionTween.h b/cocos2dx/include/CCActionTween.h index 19015bcbcf..77940f8bd7 100644 --- a/cocos2dx/include/CCActionTween.h +++ b/cocos2dx/include/CCActionTween.h @@ -67,9 +67,9 @@ public: void update(ccTime dt); CCActionInterval* reverse(); - std::string key_; - float from_, to_; - float delta_; + std::string m_strKey; + float m_fFrom, m_fTo; + float m_fDelta; }; NS_CC_END diff --git a/tests/tests/ExtensionsTest/ControlExtensionTest/CCControlScene.cpp b/tests/tests/ExtensionsTest/ControlExtensionTest/CCControlScene.cpp index 89160a0934..2134974c0b 100644 --- a/tests/tests/ExtensionsTest/ControlExtensionTest/CCControlScene.cpp +++ b/tests/tests/ExtensionsTest/ControlExtensionTest/CCControlScene.cpp @@ -24,9 +24,8 @@ */ #include "CCControlScene.h" - #include "CCControlSceneManager.h" - +#include "../ExtensionsTest.h" CCControlScene::CCControlScene() : m_pSceneTitleLabel(NULL) @@ -46,6 +45,13 @@ bool CCControlScene::init() // Get the sceensize CCSize screensize = CCDirector::sharedDirector()->getWinSize(); + CCMenuItemFont* pBackItem = CCMenuItemFont::itemWithString("Back", this, + menu_selector(CCControlScene::toExtensionsMainLayer)); + pBackItem->setPosition(ccp(screensize.width - 50, 25)); + CCMenu* pBackMenu = CCMenu::menuWithItems(pBackItem, NULL); + pBackMenu->setPosition( CCPointZero ); + addChild(pBackMenu, 10); + // Add the generated background CCSprite *background = CCSprite::spriteWithFile("extensions/background.png"); background->setPosition(ccp(screensize.width / 2, screensize.height / 2)); @@ -74,11 +80,19 @@ bool CCControlScene::init() item3->setPosition(ccp(screensize.width / 2 + 100, 37)); addChild(menu ,1); + return true; } return false; } +void CCControlScene::toExtensionsMainLayer(CCObject* sender) +{ + ExtensionsTestScene* pScene = new ExtensionsTestScene(); + pScene->runThisTest(); + pScene->release(); +} + void CCControlScene::previousCallback(CCObject* sender) { CCDirector::sharedDirector()->replaceScene(CCControlSceneManager::sharedControlSceneManager()->previousControlScene()); diff --git a/tests/tests/ExtensionsTest/ControlExtensionTest/CCControlScene.h b/tests/tests/ExtensionsTest/ControlExtensionTest/CCControlScene.h index 43e7ed53a1..9e2719c1dc 100644 --- a/tests/tests/ExtensionsTest/ControlExtensionTest/CCControlScene.h +++ b/tests/tests/ExtensionsTest/ControlExtensionTest/CCControlScene.h @@ -59,6 +59,7 @@ public: ~CCControlScene(); bool init(); // Menu Callbacks + void toExtensionsMainLayer(cocos2d::CCObject* sender); void previousCallback(cocos2d::CCObject* sender); void restartCallback(cocos2d::CCObject* sender); void nextCallback(cocos2d::CCObject* sender); diff --git a/tests/tests/SchedulerTest/SchedulerTest.cpp b/tests/tests/SchedulerTest/SchedulerTest.cpp index 79663cd98d..dc34af8ff1 100644 --- a/tests/tests/SchedulerTest/SchedulerTest.cpp +++ b/tests/tests/SchedulerTest/SchedulerTest.cpp @@ -5,7 +5,7 @@ enum { kTagAnimationDance = 1, }; -#define MAX_TESTS 8 +#define MAX_TESTS 11 static int sceneIdx = -1; CCLayer* nextSchedulerTest(); @@ -19,20 +19,26 @@ CCLayer* createSchedulerTest(int nIndex) switch (nIndex) { case 0: - pLayer = new SchedulerAutoremove(); break; + pLayer = new SchedulerDelayAndRepeat(); break; case 1: - pLayer = new SchedulerPauseResume(); break; + pLayer = new SchedulerTimeScale(); break; case 2: - pLayer = new SchedulerUnscheduleAll(); break; + pLayer = new TwoSchedulers(); break; case 3: - pLayer = new SchedulerUnscheduleAllHard(); break; + pLayer = new SchedulerAutoremove(); break; case 4: - pLayer = new SchedulerSchedulesAndRemove(); break; + pLayer = new SchedulerPauseResume(); break; case 5: - pLayer = new SchedulerUpdate(); break; + pLayer = new SchedulerUnscheduleAll(); break; case 6: - pLayer = new SchedulerUpdateAndCustom(); break; + pLayer = new SchedulerUnscheduleAllHard(); break; case 7: + pLayer = new SchedulerSchedulesAndRemove(); break; + case 8: + pLayer = new SchedulerUpdate(); break; + case 9: + pLayer = new SchedulerUpdateAndCustom(); break; + case 10: pLayer = new SchedulerUpdateFromCustom(); break; default: break; @@ -591,6 +597,273 @@ void RescheduleSelector::schedUpdate(ccTime dt) } } +// SchedulerDelayAndRepeat + +void SchedulerDelayAndRepeat::onEnter() +{ + SchedulerTestLayer::onEnter(); + schedule(schedule_selector(SchedulerDelayAndRepeat::update), 0, 4 , 3.f); + CCLOG("update is scheduled should begin after 3 seconds"); +} + +std::string SchedulerDelayAndRepeat::title() +{ + return "Schedule with delay of 3 sec, repeat 4 times"; +} + +std::string SchedulerDelayAndRepeat::subtitle() +{ + return "After 5 x executed, method unscheduled. See console"; +} + +void SchedulerDelayAndRepeat::update(ccTime dt) +{ + CCLog("update called:%f", dt); +} + +// SchedulerTimeScale + +CCControlSlider* SchedulerTimeScale::sliderCtl() +{ + CCControlSlider * slider = CCControlSlider::sliderWithFiles("extensions/sliderTrack2.png","extensions/sliderProgress2.png" ,"extensions/sliderThumb.png"); + + slider->addTargetWithActionForControlEvents(this, menu_selector(SchedulerTimeScale::sliderAction), CCControlEventValueChanged); + + slider->setMinimumValue(-3.0f); + slider->setMaximumValue(3.0f); + slider->setValue(1.0f); + + return slider; +} + +void SchedulerTimeScale::sliderAction(CCObject* pSender) +{ + CCControlSlider* pSliderCtl = (CCControlSlider*)pSender; + ccTime scale; + scale = pSliderCtl->getValue(); + + CCDirector::sharedDirector()->getScheduler()->setTimeScale(scale); +} + +void SchedulerTimeScale::onEnter() +{ + SchedulerTestLayer::onEnter(); + + CCSize s = CCDirector::sharedDirector()->getWinSize(); + + // rotate and jump + CCActionInterval *jump1 = CCJumpBy::actionWithDuration(4, ccp(-s.width+80,0), 100, 4); + CCActionInterval *jump2 = jump1->reverse(); + CCActionInterval *rot1 = CCRotateBy::actionWithDuration(4, 360*2); + CCActionInterval *rot2 = rot1->reverse(); + + CCFiniteTimeAction* seq3_1 = CCSequence::actions(jump2, jump1, NULL); + CCFiniteTimeAction* seq3_2 = CCSequence::actions(rot1, rot2, NULL); + CCFiniteTimeAction* spawn = CCSpawn::actions(seq3_1, seq3_2, NULL); + CCRepeat* action = CCRepeat::actionWithAction(spawn, 50); + + CCRepeat* action2 = (CCRepeat*)action->copy()->autorelease(); + CCRepeat* action3 = (CCRepeat*)action->copy()->autorelease(); + + CCSprite *grossini = CCSprite::spriteWithFile("Images/grossini.png"); + CCSprite *tamara = CCSprite::spriteWithFile("Images/grossinis_sister1.png"); + CCSprite *kathia = CCSprite::spriteWithFile("Images/grossinis_sister2.png"); + + grossini->setPosition(ccp(40,80)); + tamara->setPosition(ccp(40,80)); + kathia->setPosition(ccp(40,80)); + + addChild(grossini); + addChild(tamara); + addChild(kathia); + + grossini->runAction(CCSpeed::actionWithAction(action, 0.5f)); + tamara->runAction(CCSpeed::actionWithAction(action2, 1.5f)); + kathia->runAction(CCSpeed::actionWithAction(action3, 1.0f)); + + CCParticleSystem *emitter = CCParticleFireworks::node(); + emitter->setTexture( CCTextureCache::sharedTextureCache()->addImage(s_stars1) ); + addChild(emitter); + + m_pSliderCtl = sliderCtl(); + m_pSliderCtl->setPosition(ccp(s.width / 2.0f, s.height / 3.0f)); + + addChild(m_pSliderCtl); +} + +void SchedulerTimeScale::onExit() +{ + // restore scale + CCDirector::sharedDirector()->getScheduler()->setTimeScale(1); + SchedulerTestLayer::onExit(); +} + +std::string SchedulerTimeScale::title() +{ + return "Scheduler timeScale Test"; +} + +std::string SchedulerTimeScale::subtitle() +{ + return "Fast-forward and rewind using scheduler.timeScale"; +} + +//TwoSchedulers + +CCControlSlider *TwoSchedulers::sliderCtl() +{ + // CGRect frame = CGRectMake(12.0f, 12.0f, 120.0f, 7.0f); + CCControlSlider *slider = CCControlSlider::sliderWithFiles("extensions/sliderTrack2.png","extensions/sliderProgress2.png" ,"extensions/sliderThumb.png"); + //[[UISlider alloc] initWithFrame:frame]; + slider->addTargetWithActionForControlEvents(this, menu_selector(TwoSchedulers::sliderAction), CCControlEventValueChanged); + + // in case the parent view draws with a custom color or gradient, use a transparent color + //slider.backgroundColor = [UIColor clearColor]; + + slider->setMinimumValue(0.0f); + slider->setMaximumValue(2.0f); + //slider.continuous = YES; + slider->setValue(1.0f); + + return slider; +} + +void TwoSchedulers::sliderAction(CCObject* sender) +{ + ccTime scale; + + CCControlSlider *slider = (CCControlSlider*) sender; + scale = slider->getValue(); + + if( sender == sliderCtl1 ) + sched1->setTimeScale(scale); + else + sched2->setTimeScale(scale); +} + +void TwoSchedulers::onEnter() +{ + SchedulerTestLayer::onEnter(); + + CCSize s = CCDirector::sharedDirector()->getWinSize(); + + // rotate and jump + CCActionInterval *jump1 = CCJumpBy::actionWithDuration(4, ccp(0,0), 100, 4); + CCActionInterval *jump2 = jump1->reverse(); + + CCFiniteTimeAction* seq = CCSequence::actions(jump2, jump1, NULL); + CCRepeatForever* action = CCRepeatForever::actionWithAction((CCActionInterval *)seq); + + // + // Center + // + CCSprite *grossini = CCSprite::spriteWithFile("Images/grossini.png"); + addChild(grossini); + grossini->setPosition(ccp(s.width/2,100)); + grossini->runAction((CCAction*)action->copy()->autorelease()); + + + + CCScheduler *defaultScheduler = CCDirector::sharedDirector()->getScheduler(); + + // + // Left: + // + + // Create a new scheduler, and link it to the main scheduler + sched1 = new CCScheduler(); + + defaultScheduler->scheduleUpdateForTarget(sched1, 0, false); + + // Create a new ActionManager, and link it to the new scheudler + actionManager1 = new CCActionManager(); + sched1->scheduleUpdateForTarget(actionManager1, 0, false); + + for( unsigned int i=0; i < 10; i++ ) + { + CCSprite *sprite = CCSprite::spriteWithFile("Images/grossinis_sister1.png"); + + // IMPORTANT: Set the actionManager running any action + sprite->setActionManager(actionManager1); + + addChild(sprite); + sprite->setPosition(ccp(30+15*i,100)); + + sprite->runAction((CCAction*)action->copy()->autorelease()); + } + + + // + // Right: + // + + // Create a new scheduler, and link it to the main scheduler + sched2 = new CCScheduler();; + defaultScheduler->scheduleUpdateForTarget(sched2, 0, false); + + // Create a new ActionManager, and link it to the new scheudler + actionManager2 = new CCActionManager(); + sched2->scheduleUpdateForTarget(actionManager2, 0, false); + + for( unsigned int i=0; i < 10; i++ ) { + CCSprite *sprite = CCSprite::spriteWithFile("Images/grossinis_sister2.png"); + + // IMPORTANT: Set the actionManager running any action + sprite->setActionManager(actionManager2); + + addChild(sprite); + sprite->setPosition(ccp(s.width-30-15*i,100)); + + sprite->runAction((CCAction*)action->copy()->autorelease()); + } + + sliderCtl1 = sliderCtl(); + addChild(sliderCtl1); + sliderCtl1->retain(); + sliderCtl1->setPosition(ccp(s.width / 4.0f, s.height - 20)); + + sliderCtl2 = sliderCtl(); + addChild(sliderCtl2); + sliderCtl2->retain(); + sliderCtl2->setPosition(ccp(s.width / 4.0f*3.0f, s.height-20)); +// #ifdef __CC_PLATFORM_IOS +// CGRect frame = [sliderCtl2 frame]; +// #elif defined(__CC_PLATFORM_MAC) +// NSRect frame = [sliderCtl2 frame]; +// #endif +// frame.origin.x += 300; +// [sliderCtl2 setFrame:frame]; + +} + + +TwoSchedulers::~TwoSchedulers() +{ + CCScheduler *defaultScheduler = CCDirector::sharedDirector()->getScheduler(); + defaultScheduler->unscheduleAllSelectorsForTarget(sched1); + defaultScheduler->unscheduleAllSelectorsForTarget(sched2); + + sliderCtl1->release(); + sliderCtl2->release(); + + sched1->release(); + sched2->release(); + + actionManager1->release(); + actionManager2->release(); +} + +std::string TwoSchedulers::title() +{ + return "Two custom schedulers"; +} + +std::string TwoSchedulers::subtitle() +{ + return "Three schedulers. 2 custom + 1 default. Two different time scales"; +} + + //------------------------------------------------------------------ // // SchedulerTestScene diff --git a/tests/tests/SchedulerTest/SchedulerTest.h b/tests/tests/SchedulerTest/SchedulerTest.h index 20886cffcd..beca701eea 100644 --- a/tests/tests/SchedulerTest/SchedulerTest.h +++ b/tests/tests/SchedulerTest/SchedulerTest.h @@ -154,6 +154,47 @@ private: int m_nTicks; }; +class SchedulerDelayAndRepeat : public SchedulerTestLayer +{ +public: + virtual void onEnter(); + virtual std::string title(); + virtual std::string subtitle(); + void update(ccTime dt); +}; + +class SchedulerTimeScale : public SchedulerTestLayer +{ +public: + void onEnter(); + void onExit(); + virtual std::string title(); + virtual std::string subtitle(); + CCControlSlider* sliderCtl(); + void sliderAction(CCObject* pSender); + CCControlSlider* m_pSliderCtl; +}; + + +class TwoSchedulers : public SchedulerTestLayer +{ +public: + virtual ~TwoSchedulers(); + virtual std::string title(); + virtual std::string subtitle(); + void onEnter(); + CCControlSlider* sliderCtl(); + void sliderAction(CCObject* sender); + CCScheduler *sched1; + CCScheduler *sched2; + CCActionManager *actionManager1; + CCActionManager *actionManager2; + + CCControlSlider *sliderCtl1; + CCControlSlider *sliderCtl2; +}; + + class SchedulerTestScene : public TestScene { public: diff --git a/tests/tests/ShaderTest/ShaderTest.cpp b/tests/tests/ShaderTest/ShaderTest.cpp index ad9d21f7fb..b0bf031940 100644 --- a/tests/tests/ShaderTest/ShaderTest.cpp +++ b/tests/tests/ShaderTest/ShaderTest.cpp @@ -4,7 +4,7 @@ static int sceneIdx = -1; -#define MAX_LAYER 7 +#define MAX_LAYER 8 static CCLayer* createShaderLayer(int nIndex) { @@ -17,6 +17,7 @@ static CCLayer* createShaderLayer(int nIndex) case 4: return new ShaderFlower(); case 5: return new ShaderPlasma(); case 6: return new ShaderBlur(); + case 7: return new ShaderRetroEffect(); } return NULL; @@ -576,6 +577,7 @@ CCControlSlider* ShaderBlur::createSliderCtl() slider->setAnchorPoint(ccp(0.5f, 1.0f)); slider->setMinimumValue(0.0f); // Sets the min value of range slider->setMaximumValue(3.0f); // Sets the max value of range + slider->setValue(1.0f); slider->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 3.0f)); // When the value of the slider will change, the given selector will be call @@ -589,20 +591,20 @@ bool ShaderBlur::init() { if( ShaderTestDemo::init() ) { - blurSprite = SpriteBlur::spriteWithFile("Images/grossini.png"); + m_pBlurSprite = SpriteBlur::spriteWithFile("Images/grossini.png"); CCSprite *sprite = CCSprite::spriteWithFile("Images/grossini.png"); CCSize s = CCDirector::sharedDirector()->getWinSize(); - blurSprite->setPosition(ccp(s.width/3, s.height/2)); + m_pBlurSprite->setPosition(ccp(s.width/3, s.height/2)); sprite->setPosition(ccp(2*s.width/3, s.height/2)); - addChild(blurSprite); + addChild(m_pBlurSprite); addChild(sprite); - sliderCtl = createSliderCtl(); + m_pSliderCtl = createSliderCtl(); - addChild(sliderCtl); + addChild(m_pSliderCtl); return true; } @@ -612,7 +614,84 @@ bool ShaderBlur::init() void ShaderBlur::sliderAction(CCObject* sender) { CCControlSlider* pSlider = (CCControlSlider*)sender; - blurSprite->setBlurSize(pSlider->getValue()); + m_pBlurSprite->setBlurSize(pSlider->getValue()); +} + +// ShaderRetroEffect + +ShaderRetroEffect::ShaderRetroEffect() +: m_pLabel(NULL) +, m_fAccum(0.0f) +{ + init(); +} + +bool ShaderRetroEffect::init() +{ + if( ShaderTestDemo::init() ) { + + GLchar * fragSource = (GLchar*) CCString::stringWithContentsOfFile(CCFileUtils::fullPathFromRelativePath("Shaders/example_HorizontalColor.fsh"))->getCString(); + CCGLProgram *p = new CCGLProgram(); + p->initWithVertexShaderByteArray(ccPositionTexture_vert, fragSource); + + p->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position); + p->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); + + p->link(); + p->updateUniforms(); + + + CCDirector *director = CCDirector::sharedDirector(); + CCSize s = director->getWinSize(); + + m_pLabel = CCLabelBMFont::labelWithString("RETRO EFFECT", "fonts/west_england-64.fnt"); + + m_pLabel->setShaderProgram(p); + + p->release(); + + + m_pLabel->setPosition(ccp(s.width/2,s.height/2)); + + addChild(m_pLabel); + + scheduleUpdate(); + return true; + } + + return false; +} + +void ShaderRetroEffect::update(ccTime dt) +{ + m_fAccum += dt; + + CCArray* pArray = m_pLabel->getChildren(); + + int i=0; + CCObject* pObj = NULL; + CCARRAY_FOREACH(pArray, pObj) + { + CCSprite *sprite = (CCSprite*)pObj; + i++; + CCPoint oldPosition = sprite->getPosition(); + sprite->setPosition(ccp( oldPosition.x, sinf( m_fAccum * 2 + i/2.0) * 20 )); + + // add fabs() to prevent negative scaling + float scaleY = ( sinf( m_fAccum * 2 + i/2.0 + 0.707) ); + + sprite->setScaleY(scaleY); + } +} + +std::string ShaderRetroEffect::title() +{ + return "Shader: Retro test"; +} + +std::string ShaderRetroEffect::subtitle() +{ + return "sin() effect with moving colors"; } ///--------------------------------------- diff --git a/tests/tests/ShaderTest/ShaderTest.h b/tests/tests/ShaderTest/ShaderTest.h index fdbb687612..ccaf6c07aa 100644 --- a/tests/tests/ShaderTest/ShaderTest.h +++ b/tests/tests/ShaderTest/ShaderTest.h @@ -91,11 +91,22 @@ public: CCControlSlider* createSliderCtl(); void sliderAction(CCObject* sender); protected: - SpriteBlur *blurSprite; - CCControlSlider* sliderCtl; + SpriteBlur* m_pBlurSprite; + CCControlSlider* m_pSliderCtl; }; -/// can not implement SpriteBlur, because we don't have slider now +class ShaderRetroEffect : public ShaderTestDemo +{ +public: + ShaderRetroEffect(); + virtual std::string title(); + virtual std::string subtitle(); + bool init(); + void update(ccTime dt); +protected: + CCLabelBMFont* m_pLabel; + ccTime m_fAccum; +}; class ShaderNode : public CCNode {