void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { // If there is already a background music source we stop it first if (s_backgroundSource != AL_NONE) stopBackgroundMusic(false); // Changing file path to full path std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { preloadBackgroundMusic(fullPath.c_str()); it = s_backgroundMusics.find(fullPath); } if (it != s_backgroundMusics.end()) { s_backgroundSource = it->second->source; alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); setBackgroundVolume(s_volume); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic:alSourcePlay"); } }
// on "init" you need to initialize your instance bool Registrarse::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); /// Crea el fondo de la pantalla del menu "Registrarse" del juego auto sprite = Sprite::create("Fondos/registrarse.jpg"); /// Posiciona el sprite (fondo de pantalla) para que quede centralizado en la pantalla sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); /// Hace visible el fondo de pantalla this->addChild(sprite, 0); createMenu(); /// Se utiliza para reproducir la musica cuando se cambia a esta pantalla (registrarse). auto sound = CocosDenshion::SimpleAudioEngine::getInstance(); sound->stopBackgroundMusic(); sound->playBackgroundMusic("Music/cancion4.mp3", true); return true; }
GUISoundManager::~GUISoundManager(void) { stopBackgroundMusic(); if (eventSounds.size() > 0) for (vector<EventSound*>::const_iterator it = eventSounds.begin(); it != eventSounds.end(); ++it) { delete *it; } }
void GUISoundManager::update(vector<string> eventCompList) { if (enabled) { if (fadeIN && fadeGain >= backgroundGain) { fadeIN = false; setBackgroundGain(backgroundGain); } if (fadeIN && play) { fadeGain += ( backgroundGain/GameEngine::FPS())/5.0f; if (backgroundSounds.size()>0) { for (unsigned int i = 0; i < backgroundSounds.size(); i++) { GameEvent eve(GameEvent::E_SET_SOUND_GAIN, &GameEventData(fadeGain), 0x0); GameEngine::sendEvent(backgroundSounds[i], &eve); } } } if (fadeOUT && fadeGain <= 0) { stopBackgroundMusic(); fadeOUT = false; for (unsigned int i = 0; i < eventCompList.size(); i++) { GUIEventData::sendToComp(new GUIEventData(O_GUI_SOUND_FADEDOUT,L"GUISoundManager"),eventCompList.at(i).c_str()); } } if (fadeOUT && play) { fadeGain -= ( backgroundGain/GameEngine::FPS())/2.0f; if (backgroundSounds.size()>0) { for (unsigned int i = 0; i < backgroundSounds.size(); i++) { GameEvent eve(GameEvent::E_SET_SOUND_GAIN, &GameEventData(fadeGain), 0x0); GameEngine::sendEvent(backgroundSounds[i], &eve); } } } if (backgroundSounds.size()>0) { bool playing = false; GameEvent isPlaying(GameEvent::E_GET_SOUND_ISPLAYING, &GameEventData(&playing), 0x0); GameEngine::sendEvent(backgroundSounds[backgroundTitle], &isPlaying); if (!playing && play) playNextBackgroundMusic(); } } }
void GUISoundManager::playNextBackgroundMusic() { if (enabled && backgroundSounds.size()>0) { stopBackgroundMusic(); if (backgroundTitle < (signed)backgroundSounds.size()-1) backgroundTitle++; else backgroundTitle = 0; playBackgroundMusic(); } }
// on "init" you need to initialize your instance bool LevelSelectScene::init() { // 1. super init first if (!Layer::init()) { // if there is a mistake then we terminate the program, it couldnt launch return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); // add a label shows "Menu Inicio" // create and initialize a label auto label1 = LabelTTF::create("Seleccionar nivel", "Book Antigua", 50); auto label2 = LabelTTF::create("Nivel 1", "Book Antigua", 20); auto label3 = LabelTTF::create("Nivel 2", "Book Antigua", 20); auto label4 = LabelTTF::create("Nivel3", "Book Antigua", 20); // position the label on the center of the screen label1->setPosition(Point(origin.x + visibleSize.width * 0.5f, origin.y + visibleSize.height * .64f)); label2->setPosition(Point(visibleSize.width / 4, origin.y + visibleSize.height * 0.18f)); label3->setPosition(Point(2 * visibleSize.width / 4, origin.y + visibleSize.height * 0.18f)); label4->setPosition(Point(3 * visibleSize.width / 4, origin.y + visibleSize.height * 0.18f)); // add the label as a child to this layer this->addChild(label1, 1); this->addChild(label2, 1); this->addChild(label3, 1); this->addChild(label4, 1); // Crear el fondo del menu del juego auto sprite = Sprite::create("GameMenu/0000.jpg"); // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); createMenu(); // Reproducir la musica de la seleccion del nivel: quiza sea mejor dejar la misma del menu // principal y cambiarla cuando se haya iniciado el nivel. Se deja aqui por propositos // ilustrativos auto sound = CocosDenshion::SimpleAudioEngine::getInstance(); sound->stopBackgroundMusic(); sound->playBackgroundMusic("Music/LevelSelect.mp3", true); return true; }
void SimpleAudioEngine::rewindBackgroundMusic() { stopBackgroundMusic(); if (s_pBackPlayer) { if (PLAYER_STATE_PLAYING != s_pBackPlayer->GetState()) { result r = s_pBackPlayer->Play(); if (IsFailed(r)) { AppLog("ERROR: %s", GetErrorMessage(r)); } } } }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (0 != strcmp(s_currentBackgroundStr.c_str(), pszFilePath)) { stopBackgroundMusic(true); } else { if (s_playStatus == PAUSED) resumeBackgroundMusic(); else rewindBackgroundMusic(); } if (!s_isBackgroundInitialized) preloadBackgroundMusic(pszFilePath); if (bLoop) { // set it up to loop strm_dict_t *dictionary = strm_dict_new(); s_repeatDictionary = strm_dict_set(dictionary, "repeat", "all"); if (mmr_input_parameters(s_mmrContext, s_repeatDictionary) != 0) { mmrerror(s_mmrContext, "input parameters (loop)"); return; } } if (s_hasMMRError || !s_mmrContext) return; if (mmr_play(s_mmrContext) < 0) { mmrerror(s_mmrContext, "mmr_play"); s_hasMMRError = true; } if (!s_hasMMRError) s_playStatus = PLAYING; }
// We initialize our instance bool OptionsScene::init() { // 1. super init first if (!Layer::init()) { // if there is a mistake then we terminate the program, it couldnt launch return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); // add a label shows "Creditos" create and initialize labels auto labelTitulo = LabelTTF::create("Opciones", "Tahoma", 32); // position the label on the upper center of the screen labelTitulo->setPosition(Point(origin.x + visibleSize.width * 0.5f, origin.y + visibleSize.height * .88f)); // add the label as a child to this layer this->addChild(labelTitulo, 1); // We add the devs labels this->showNames(origin, visibleSize); // Creates the background of the game menu. auto sprite = Sprite::create("GameMenu/0000.jpg"); // sprites are important, those are the images // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); createMenu(); // We bring to live this scene. // Reproducir la musica de la seleccion del nivel: quiza sea mejor dejar la misma del menu // principal y cambiarla cuando se haya iniciado el nivel. Se deja aqui por propositos // ilustrativos auto sound = CocosDenshion::SimpleAudioEngine::getInstance(); sound->stopBackgroundMusic(); // this steps are to change tracks. sound->playBackgroundMusic("Music/Options.mp3", true); // We use a piece of music we already have and reproduce it for the options scene. return true; // we managed to bring frankestein alive, I mean, the game. }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (s_backgroundSource != AL_NONE) stopBackgroundMusic(false); // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { preloadBackgroundMusic(fullPath.c_str()); it = s_backgroundMusics.find(fullPath); } if (it != s_backgroundMusics.end()) { s_backgroundSource = it->second->source; alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic"); } }
void ComAudio::onExit() { stopBackgroundMusic(true); stopAllEffects(); }
Scene* Chapter13::createScene() { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // some upfront items that we need // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto dirs = Director::getInstance(); Size visibleSize = dirs->getVisibleSize(); Vec2 origin = dirs->getVisibleOrigin(); Size playingSize = Size(visibleSize.width, visibleSize.height - (visibleSize.height/8)); // actual playing size to work with // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a scene // 'scene' is an autorelease object // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto scene = Scene::create(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold non-sprites. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto nodeItems = Node::create(); nodeItems->setName("nodeItems"); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold menu // create a menu // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto menuNode = Node::create(); menuNode->setName("menuNode"); int index = 2; auto menuItem1 = MenuItemFont::create("Play Music"); menuItem1->setFontNameObj("fonts/Marker Felt.ttf"); menuItem1->setFontSizeObj(32); menuItem1->setName("menuItem1"); menuItem1->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem1->setCallback([&](cocos2d::Ref *sender) { auto dirs = Director::getInstance()->getRunningScene(); auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->playBackgroundMusic("music.mp3"); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem1"); cnode->setVisible(false); cnode = bnode->getChildByName("menuItem2"); cnode->setVisible(true); }); auto menuItem2 = MenuItemFont::create("Stop Music"); menuItem2->setFontNameObj("fonts/Marker Felt.ttf"); menuItem2->setFontSizeObj(32); menuItem2->setName("menuItem2"); menuItem2->setVisible(false); menuItem2->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem2->setCallback([&](cocos2d::Ref *sender) { auto dirs = Director::getInstance()->getRunningScene(); auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->stopBackgroundMusic("music.mp3"); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem2"); cnode->setVisible(false); cnode = bnode->getChildByName("menuItem1"); cnode->setVisible(true); }); auto menuItem3 = MenuItemFont::create("Play Effect"); menuItem3->setFontNameObj("fonts/Marker Felt.ttf"); menuItem3->setFontSizeObj(32); menuItem3->setName("menuItem3"); menuItem3->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (++index) * 40)); menuItem3->setCallback([&](cocos2d::Ref *sender) { auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->playEffect("alert.mp3"); }); auto menu = Menu::create(menuItem1, menuItem2, menuItem3, NULL); menu->setName("menu"); menuNode->addChild(menu, 1); menu->setPosition(Vec2::ZERO); scene->addChild(menuNode, 2); // return the scene return scene; }
void SettingsScene::turnOnOffMusic(cocos2d::Ref* pSender) { auto sound = CocosDenshion::SimpleAudioEngine::getInstance(); sound->stopBackgroundMusic(); }