[New] MusicManager

Music SoundEffect added.
MusicPlayer added.
Sample Application Source Code Changed.
This commit is contained in:
Kirigaya Kazuto 2017-02-02 14:23:22 +08:00
parent 9e11919f44
commit 9c1feddb49
6 changed files with 160 additions and 4 deletions

11
App.cpp
View File

@ -1,5 +1,4 @@
#include "App.h" #include "App.h"
#include "Game/Main.h"
using namespace Engine; using namespace Engine;
using namespace MiniEngine; using namespace MiniEngine;
@ -17,7 +16,15 @@ namespace App
mlog("Failed to open Font."); mlog("Failed to open Font.");
return; 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 /// Sample Code of Brush

View File

@ -98,16 +98,18 @@ InitManager_Mix::InitManager_Mix()
Global::ErrorQuit("Failed to Init Mixer."); Global::ErrorQuit("Failed to Init Mixer.");
} }
/*
if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024)<0) if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024)<0)
{ {
Global::ErrorQuit("Failed to OpenAudio"); Global::ErrorQuit("Failed to OpenAudio");
} }
Mix_AllocateChannels(32); Mix_AllocateChannels(32);
*/
} }
InitManager_Mix::~InitManager_Mix() InitManager_Mix::~InitManager_Mix()
{ {
Mix_CloseAudio(); //Mix_CloseAudio();
Mix_Quit(); Mix_Quit();
} }

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#include "config.h" #include "config.h"
#include <vector>
class InitManager_SDL class InitManager_SDL
{ {

84
MusicManager.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "MusicManager.h"
#include <memory>
#include <vector>
using namespace std;
struct Music::impl
{
friend class Music;
shared_ptr<Mix_Music> 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<pair<Music,int>> 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<Mix_Chunk> sChunk;
};
SoundEffect::SoundEffect()
{
pimpl.reset(new impl);
}

60
MusicManager.h Normal file
View File

@ -0,0 +1,60 @@
#pragma once
#include "config.h"
#include <memory>
#define _MUSIC_MANAGER_IMPL \
struct impl; \
std::shared_ptr<impl> 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;
};

View File

@ -6,6 +6,10 @@
/// InitManager /// InitManager
#include "InitManager.h" #include "InitManager.h"
/******************************************************************************/
/// MusicManager
#include "MusicManager.h"
/******************************************************************************/ /******************************************************************************/
/// Widget /// Widget
#include "MiniEngine/Widget.h" #include "MiniEngine/Widget.h"