Update Music Wrapper

This commit is contained in:
Kirigaya Kazuto 2017-06-23 13:17:55 +08:00
parent 4319ee521b
commit 0ea8797093
3 changed files with 40 additions and 39 deletions

View File

@ -1,20 +1,36 @@
#include "Music.h" #include "Music.h"
#include "begin_code.h" #include "begin_code.h"
void Music::_set(Mix_Music* p)//private //private
void Music::_set(Mix_Music* p)
{ {
_music.reset(p,Mix_FreeMusic); _music.reset(p,Mix_FreeMusic);
} }
//private
void Music::_clear()//private void Music::_clear()
{ {
_music.reset(); _music.reset();
} }
//private
Mix_Music* Music::_get()//private Mix_Music* Music::_get() const
{ {
return _music.get(); return _music.get();
} }
Music::Music(const std::string& Filename)
{
_set(Mix_LoadMUS(Filename.c_str()));
}
bool Music::isReady() const
{
return (_get()!=nullptr);
}
void Music::release()
{
_clear();
}
//static //static
int MusicPlayer::GetDecoderNum() int MusicPlayer::GetDecoderNum()
{ {
@ -27,20 +43,6 @@ std::string MusicPlayer::GetDecoderName(int index)
return std::string(Mix_GetMusicDecoder(index)); return std::string(Mix_GetMusicDecoder(index));
} }
Music MusicPlayer::loadMusic(const std::string& Filename) throw(ErrorViewer)
{
Mix_Music* temp = Mix_LoadMUS(Filename.c_str());
if (temp == nullptr)
{
ErrorViewer e;
e.fetch();
throw e;
}
Music m;
m._set(temp);
return m;
}
int MusicPlayer::play(Music music, int loops) int MusicPlayer::play(Music music, int loops)
{ {
m = music; m = music;
@ -77,17 +79,17 @@ int MusicPlayer::fadeOut(int ms)
return Mix_FadeOutMusic(ms); return Mix_FadeOutMusic(ms);
} }
bool MusicPlayer::isPlaying() bool MusicPlayer::isPlaying() const
{ {
return (Mix_PlayingMusic() == 1); return (Mix_PlayingMusic() == 1);
} }
bool MusicPlayer::isPaused() bool MusicPlayer::isPaused() const
{ {
return (Mix_PausedMusic() == 1); return (Mix_PausedMusic() == 1);
} }
int MusicPlayer::isFading() int MusicPlayer::isFading() const
{ {
switch (Mix_FadingMusic()) switch (Mix_FadingMusic())
{ {
@ -102,10 +104,9 @@ int MusicPlayer::isFading()
} }
} }
//static int MusicPlayer::setPosition(double second)
int MusicPlayer::SetMusicPosition(double position)
{ {
return Mix_SetMusicPosition(position); return Mix_SetMusicPosition(second);
} }
#include "end_code.h" #include "end_code.h"

View File

@ -4,45 +4,43 @@
#include <string> #include <string>
#include "ErrorViewer.h" #include "ErrorViewer.h"
#include "Audio.h" #include "Audio.h"
#include "__Noncopyable.h"
#include "begin_code.h" #include "begin_code.h"
/// Forward Declaration /// Forward Declaration
class Music class Music
{ {
public: public:
protected:
Music()=default; Music()=default;
Music(const std::string& Filename);
bool isReady() const;
void release();
private: private:
std::shared_ptr<Mix_Music> _music; std::shared_ptr<Mix_Music> _music;
void _set(Mix_Music*); void _set(Mix_Music*);
void _clear(); void _clear();
Mix_Music* _get(); Mix_Music* _get() const;
friend class MusicPlayer; friend class MusicPlayer;
}; };
class MusicPlayer : public AudioPlayer class MusicPlayer : public AudioPlayer, public NonCopyable
{ {
public: public:
static int GetDecoderNum(); static int GetDecoderNum();
static std::string GetDecoderName(int index); static std::string GetDecoderName(int index);
Music loadMusic(const std::string& Filename) throw (ErrorViewer); /// Play Music. Loop: -1:Infinite, >0:Exact that time.
int play(Music music, int loops); int play(Music music, int loops);
void pause(); void pause();
void resume(); void resume();
void rewind(); void rewind();
int setPosition(double second);
int stop(); int stop();
int fadeIn(int loops, int ms); int fadeIn(int loops, int ms);
int fadeOut(int ms); int fadeOut(int ms);
bool isPlaying(); bool isPlaying() const;
bool isPaused(); bool isPaused() const;
int isFading(); int isFading() const;
/// Experimental
static int SetMusicPosition(double position);
private: private:
Music m; Music m;
}; };

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "begin_code.h"
class NonCopyable class NonCopyable
{ {
protected: protected:
@ -7,3 +8,4 @@ protected:
NonCopyable(const NonCopyable&) = delete; NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator = (const NonCopyable&) = delete; NonCopyable& operator = (const NonCopyable&) = delete;
}; };
#include "end_code.h"