From 9c1feddb49c387e676c10d5bebedcd065fe48789 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Thu, 2 Feb 2017 14:23:22 +0800 Subject: [PATCH] [New] MusicManager Music SoundEffect added. MusicPlayer added. Sample Application Source Code Changed. --- App.cpp | 11 +++++-- InitManager.cpp | 4 ++- InitManager.h | 1 - MusicManager.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ MusicManager.h | 60 ++++++++++++++++++++++++++++++++++ mini_engine.h | 4 +++ 6 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 MusicManager.cpp create mode 100644 MusicManager.h diff --git a/App.cpp b/App.cpp index 33348ae..c9cd26d 100644 --- a/App.cpp +++ b/App.cpp @@ -1,5 +1,4 @@ #include "App.h" -#include "Game/Main.h" using namespace Engine; using namespace MiniEngine; @@ -17,7 +16,15 @@ namespace App mlog("Failed to open Font."); return; } - Game::Main(wnd,rnd,bigf); + rnd.clear(); + rnd.update(); + + MusicPlayer player; + Music m("res/music.mp3"); + player.add(m,-1); + player.play(); + + while(1) SDL_PollEvent(0); /* /// Sample Code of Brush diff --git a/InitManager.cpp b/InitManager.cpp index a31554c..2fc1e27 100644 --- a/InitManager.cpp +++ b/InitManager.cpp @@ -98,16 +98,18 @@ InitManager_Mix::InitManager_Mix() Global::ErrorQuit("Failed to Init Mixer."); } + /* if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024)<0) { Global::ErrorQuit("Failed to OpenAudio"); } Mix_AllocateChannels(32); + */ } InitManager_Mix::~InitManager_Mix() { - Mix_CloseAudio(); + //Mix_CloseAudio(); Mix_Quit(); } diff --git a/InitManager.h b/InitManager.h index 0ba88d6..43bed37 100644 --- a/InitManager.h +++ b/InitManager.h @@ -1,6 +1,5 @@ #pragma once #include "config.h" -#include class InitManager_SDL { diff --git a/MusicManager.cpp b/MusicManager.cpp new file mode 100644 index 0000000..3ceb088 --- /dev/null +++ b/MusicManager.cpp @@ -0,0 +1,84 @@ +#include "MusicManager.h" +#include +#include +using namespace std; + +struct Music::impl +{ + friend class Music; + shared_ptr sMusic; +}; +Music::Music() +{ + pimpl.reset(new impl); +} +Music::Music(const char* MusicFileName) : Music() +{ + load(MusicFileName); +} +int Music::load(const char* MusicFileName) +{ + Mix_Music* ptemp=Mix_LoadMUS(MusicFileName); + if(ptemp==nullptr) return -1; + pimpl->sMusic.reset(ptemp,Mix_FreeMusic); + return 0; +} +int Music::unload() +{ + if(pimpl->sMusic.get()) + { + pimpl->sMusic.reset(); + return 0; + } + else return -2; +} +bool Music::ready() +{ + return (pimpl->sMusic.get()!=nullptr); +} +Music::~Music() +{ + unload(); +} + + +struct MusicPlayer::impl +{ + vector> vec; +}; +MusicPlayer::MusicPlayer(int freq,Uint16 format,int soundChannel,int chunkSize) +{ + pimpl.reset(new impl); + if(pInstance) return; + Mix_OpenAudio(freq,format,soundChannel,chunkSize); + pInstance=this; +} +MusicPlayer::~MusicPlayer() +{ + if(pInstance) Mix_CloseAudio(); + pInstance=nullptr; +} +int MusicPlayer::play() +{ + return Mix_PlayMusic(pimpl->vec.front().first.pimpl->sMusic.get(),pimpl->vec.front().second); +} +int MusicPlayer::add(Music& music,int times) +{ + if(!music.ready()) return -1; + pimpl->vec.push_back(make_pair(music,times)); + return 0; +} +MusicPlayer* MusicPlayer::pInstance=nullptr; + + + +struct SoundEffect::impl +{ + friend class SoundEffect; + shared_ptr sChunk; +}; + +SoundEffect::SoundEffect() +{ + pimpl.reset(new impl); +} diff --git a/MusicManager.h b/MusicManager.h new file mode 100644 index 0000000..663c344 --- /dev/null +++ b/MusicManager.h @@ -0,0 +1,60 @@ +#pragma once +#include "config.h" +#include + +#define _MUSIC_MANAGER_IMPL \ + struct impl; \ + std::shared_ptr pimpl; + +/// Fwd Decl +class MusicPlayer; + +class Music +{ +public: + Music(); + Music(const char* MusicFileName); + int load(const char* MusicFileName); + int unload(); + bool ready(); + ~Music(); +private: + _MUSIC_MANAGER_IMPL + + friend class MusicPlayer; +}; + +class SoundEffect +{ +public: + SoundEffect(); + SoundEffect(const char* SoundEffectFileName); + int load(const char* SoundEffectFileName); + ~SoundEffect(); +private: + _MUSIC_MANAGER_IMPL +}; + +class MusicPlayer +{ +public: + MusicPlayer(int freq=MIX_DEFAULT_FREQUENCY,Uint16 format=MIX_DEFAULT_FORMAT,int soundChannel=2,int chunkSize=1024); + ~MusicPlayer(); + int play(); + int stop(); + int add(Music& music,int times); +private: + static MusicPlayer* pInstance; + _MUSIC_MANAGER_IMPL +}; + +class SoundEffectPlayer +{ +public: + SoundEffectPlayer(); + ~SoundEffectPlayer(); + int play(SoundEffect& soundeffect,int times); +private: + _MUSIC_MANAGER_IMPL; +}; + diff --git a/mini_engine.h b/mini_engine.h index c796884..8dea741 100644 --- a/mini_engine.h +++ b/mini_engine.h @@ -6,6 +6,10 @@ /// InitManager #include "InitManager.h" +/******************************************************************************/ +/// MusicManager +#include "MusicManager.h" + /******************************************************************************/ /// Widget #include "MiniEngine/Widget.h"