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"
|
|
|
|
#elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
|
|
|
|
#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"
|
|
|
|
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_QNX)
|
|
|
|
#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()
|
|
|
|
: m_pItmeMenu(NULL),
|
|
|
|
m_tBeginPos(CCPointZero),
|
|
|
|
m_nSoundId(0)
|
|
|
|
{
|
|
|
|
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
|
2012-06-14 15:13:16 +08:00
|
|
|
m_pItmeMenu = CCMenu::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
m_nTestCount = sizeof(testItems) / sizeof(testItems[0]);
|
|
|
|
|
|
|
|
for (int i = 0; i < m_nTestCount; ++i)
|
|
|
|
{
|
|
|
|
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
|
2012-06-14 15:13:16 +08:00
|
|
|
// CCLabelBMFont* label = CCLabelBMFont::create(testItems[i].c_str(), "fonts/arial16.fnt");
|
2012-04-19 14:35:52 +08:00
|
|
|
//#else
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF* label = CCLabelTTF::create(testItems[i].c_str(), "Arial", 24);
|
2012-04-19 14:35:52 +08:00
|
|
|
//#endif
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(CocosDenshionTest::menuCallback));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
m_pItmeMenu->addChild(pMenuItem, i + 10000);
|
|
|
|
pMenuItem->setPosition( CCPointMake( s.width / 2, (s.height - (i + 1) * LINE_SPACE) ));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pItmeMenu->setContentSize(CCSizeMake(s.width, (m_nTestCount + 1) * LINE_SPACE));
|
|
|
|
m_pItmeMenu->setPosition(CCPointZero);
|
|
|
|
addChild(m_pItmeMenu);
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// preload background music and effect
|
2012-06-08 17:34:15 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE) );
|
|
|
|
SimpleAudioEngine::sharedEngine()->preloadEffect( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(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()
|
|
|
|
{
|
|
|
|
CCLayer::onExit();
|
|
|
|
|
|
|
|
SimpleAudioEngine::sharedEngine()->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CocosDenshionTest::menuCallback(CCObject * pSender)
|
|
|
|
{
|
|
|
|
// get the userdata, it's the index of the menu item clicked
|
|
|
|
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
|
|
|
|
int nIdx = pMenuItem->getZOrder() - 10000;
|
|
|
|
|
|
|
|
switch(nIdx)
|
|
|
|
{
|
|
|
|
// play background music
|
|
|
|
case 0:
|
|
|
|
|
2012-06-08 17:34:15 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str(), 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:
|
2012-06-08 17:34:15 +08:00
|
|
|
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
// play effect
|
|
|
|
case 7:
|
2012-06-08 17:34:15 +08:00
|
|
|
m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str(), true);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
// stop effect
|
|
|
|
case 8:
|
|
|
|
SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);
|
|
|
|
break;
|
|
|
|
// unload effect
|
|
|
|
case 9:
|
2012-06-08 17:34:15 +08:00
|
|
|
SimpleAudioEngine::sharedEngine()->unloadEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str());
|
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:
|
|
|
|
SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
|
|
|
|
break;
|
|
|
|
case 17:
|
|
|
|
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
|
|
|
|
break;
|
|
|
|
case 18:
|
|
|
|
SimpleAudioEngine::sharedEngine()->stopAllEffects();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CocosDenshionTest::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCSetIterator it = pTouches->begin();
|
|
|
|
CCTouch* touch = (CCTouch*)(*it);
|
|
|
|
|
2012-07-31 17:41:53 +08:00
|
|
|
m_tBeginPos = touch->getLocation();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CocosDenshionTest::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCSetIterator it = pTouches->begin();
|
|
|
|
CCTouch* touch = (CCTouch*)(*it);
|
|
|
|
|
2012-07-31 17:41:53 +08:00
|
|
|
CCPoint touchLocation = touch->getLocation();
|
2012-04-19 14:35:52 +08:00
|
|
|
float nMoveY = touchLocation.y - m_tBeginPos.y;
|
|
|
|
|
|
|
|
CCPoint curPos = m_pItmeMenu->getPosition();
|
|
|
|
CCPoint nextPos = ccp(curPos.x, curPos.y + nMoveY);
|
|
|
|
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
if (nextPos.y < 0.0f)
|
|
|
|
{
|
|
|
|
m_pItmeMenu->setPosition(CCPointZero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - winSize.height))
|
|
|
|
{
|
|
|
|
m_pItmeMenu->setPosition(ccp(0, ((m_nTestCount + 1)* LINE_SPACE - winSize.height)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pItmeMenu->setPosition(nextPos);
|
|
|
|
m_tBeginPos = touchLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CocosDenshionTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
CCLayer* pLayer = new CocosDenshionTest();
|
|
|
|
addChild(pLayer);
|
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
CCDirector::sharedDirector()->replaceScene(this);
|
|
|
|
}
|