2012-04-19 14:35:52 +08:00
|
|
|
#include "CocosDenshionTest.h"
|
|
|
|
#include "cocos2d.h"
|
|
|
|
#include "SimpleAudioEngine.h"
|
|
|
|
|
|
|
|
// android effect only support ogg
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
#define EFFECT_FILE "effect2.ogg"
|
2013-03-29 16:41:15 +08:00
|
|
|
#elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
|
2012-04-19 14:35:52 +08:00
|
|
|
#define EFFECT_FILE "effect1.raw"
|
|
|
|
#else
|
|
|
|
#define EFFECT_FILE "effect1.wav"
|
|
|
|
#endif // CC_PLATFORM_ANDROID
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
#define MUSIC_FILE "music.mid"
|
2013-03-29 16:32:50 +08:00
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX )
|
2012-04-19 14:35:52 +08:00
|
|
|
#define MUSIC_FILE "background.ogg"
|
|
|
|
#else
|
|
|
|
#define MUSIC_FILE "background.mp3"
|
|
|
|
#endif // CC_PLATFORM_WIN32
|
|
|
|
|
2012-06-14 05:19:13 +08:00
|
|
|
USING_NS_CC;
|
2012-04-19 14:35:52 +08:00
|
|
|
using namespace CocosDenshion;
|
|
|
|
|
|
|
|
#define LINE_SPACE 40
|
|
|
|
|
|
|
|
CocosDenshionTest::CocosDenshionTest()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _itmeMenu(NULL),
|
2013-06-20 14:17:10 +08:00
|
|
|
_beginPos(PointZero),
|
2013-06-15 14:03:30 +08:00
|
|
|
_soundId(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
std::string testItems[] = {
|
|
|
|
"play background music",
|
|
|
|
"stop background music",
|
|
|
|
"pause background music",
|
|
|
|
"resume background music",
|
|
|
|
"rewind background music",
|
|
|
|
"is background music playing",
|
|
|
|
"play effect",
|
|
|
|
"play effect repeatly",
|
|
|
|
"stop effect",
|
|
|
|
"unload effect",
|
|
|
|
"add background music volume",
|
|
|
|
"sub background music volume",
|
|
|
|
"add effects volume",
|
|
|
|
"sub effects volume",
|
|
|
|
"pause effect",
|
|
|
|
"resume effect",
|
|
|
|
"pause all effects",
|
|
|
|
"resume all effects",
|
|
|
|
"stop all effects"
|
|
|
|
};
|
|
|
|
|
|
|
|
// add menu items for tests
|
2013-06-20 14:17:10 +08:00
|
|
|
_itmeMenu = Menu::create();
|
2012-10-23 17:48:50 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_testCount = sizeof(testItems) / sizeof(testItems[0]);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (int i = 0; i < _testCount; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
|
2013-06-20 14:17:10 +08:00
|
|
|
// LabelBMFont* label = LabelBMFont::create(testItems[i].c_str(), "fonts/arial16.fnt");
|
2012-04-19 14:35:52 +08:00
|
|
|
//#else
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF* label = LabelTTF::create(testItems[i].c_str(), "Arial", 24);
|
2012-04-19 14:35:52 +08:00
|
|
|
//#endif
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(CocosDenshionTest::menuCallback, this));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_itmeMenu->addChild(pMenuItem, i + 10000);
|
2012-10-23 17:48:50 +08:00
|
|
|
pMenuItem->setPosition( ccp( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) ));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_itmeMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (_testCount + 1) * LINE_SPACE));
|
2013-06-20 14:17:10 +08:00
|
|
|
_itmeMenu->setPosition(PointZero);
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_itmeMenu);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// preload background music and effect
|
2013-01-27 19:20:49 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE );
|
|
|
|
SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// set default volume
|
|
|
|
SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
|
|
|
|
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
CocosDenshionTest::~CocosDenshionTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CocosDenshionTest::onExit()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer::onExit();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
SimpleAudioEngine::sharedEngine()->end();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void CocosDenshionTest::menuCallback(Object * pSender)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// get the userdata, it's the index of the menu item clicked
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItem* pMenuItem = (MenuItem *)(pSender);
|
2012-04-19 14:35:52 +08:00
|
|
|
int nIdx = pMenuItem->getZOrder() - 10000;
|
|
|
|
|
|
|
|
switch(nIdx)
|
|
|
|
{
|
|
|
|
// play background music
|
|
|
|
case 0:
|
|
|
|
|
2013-01-27 19:20:49 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
// stop background music
|
|
|
|
case 1:
|
|
|
|
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
|
|
|
|
break;
|
|
|
|
// pause background music
|
|
|
|
case 2:
|
|
|
|
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
|
|
|
|
break;
|
|
|
|
// resume background music
|
|
|
|
case 3:
|
|
|
|
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
|
|
|
|
break;
|
|
|
|
// rewind background music
|
|
|
|
case 4:
|
|
|
|
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
|
|
|
|
break;
|
|
|
|
// is background music playing
|
|
|
|
case 5:
|
|
|
|
if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
|
|
|
|
{
|
|
|
|
CCLOG("background music is playing");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("background music is not playing");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// play effect
|
|
|
|
case 6:
|
2013-06-15 14:03:30 +08:00
|
|
|
_soundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
// play effect
|
|
|
|
case 7:
|
2013-06-15 14:03:30 +08:00
|
|
|
_soundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
// stop effect
|
|
|
|
case 8:
|
2013-06-15 14:03:30 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->stopEffect(_soundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
// unload effect
|
|
|
|
case 9:
|
2013-01-27 19:20:49 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
// add bakcground music volume
|
|
|
|
case 10:
|
|
|
|
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);
|
|
|
|
break;
|
|
|
|
// sub backgroud music volume
|
|
|
|
case 11:
|
|
|
|
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);
|
|
|
|
break;
|
|
|
|
// add effects volume
|
|
|
|
case 12:
|
|
|
|
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);
|
|
|
|
break;
|
|
|
|
// sub effects volume
|
|
|
|
case 13:
|
|
|
|
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);
|
|
|
|
break;
|
|
|
|
case 14:
|
2013-06-15 14:03:30 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->pauseEffect(_soundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
case 15:
|
2013-06-15 14:03:30 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->resumeEffect(_soundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
|
|
|
|
break;
|
|
|
|
case 17:
|
|
|
|
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
|
|
|
|
break;
|
|
|
|
case 18:
|
|
|
|
SimpleAudioEngine::sharedEngine()->stopAllEffects();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void CocosDenshionTest::ccTouchesBegan(Set *pTouches, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
SetIterator it = pTouches->begin();
|
|
|
|
Touch* touch = (Touch*)(*it);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_beginPos = touch->getLocation();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void CocosDenshionTest::ccTouchesMoved(Set *pTouches, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
SetIterator it = pTouches->begin();
|
|
|
|
Touch* touch = (Touch*)(*it);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Point touchLocation = touch->getLocation();
|
2013-06-15 14:03:30 +08:00
|
|
|
float nMoveY = touchLocation.y - _beginPos.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Point curPos = _itmeMenu->getPosition();
|
|
|
|
Point nextPos = ccp(curPos.x, curPos.y + nMoveY);
|
2012-10-23 17:48:50 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (nextPos.y < 0.0f)
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
_itmeMenu->setPosition(PointZero);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (nextPos.y > ((_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_itmeMenu->setPosition(ccp(0, ((_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_itmeMenu->setPosition(nextPos);
|
|
|
|
_beginPos = touchLocation;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CocosDenshionTestScene::runThisTest()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = new CocosDenshionTest();
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(pLayer);
|
|
|
|
pLayer->autorelease();
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|