diff --git a/SDLWrapper/Music.cpp b/SDLWrapper/Music.cpp index 4e508a9..9bdae9b 100644 --- a/SDLWrapper/Music.cpp +++ b/SDLWrapper/Music.cpp @@ -1,20 +1,36 @@ #include "Music.h" #include "begin_code.h" -void Music::_set(Mix_Music* p)//private +//private +void Music::_set(Mix_Music* p) { _music.reset(p,Mix_FreeMusic); } - -void Music::_clear()//private +//private +void Music::_clear() { _music.reset(); } - -Mix_Music* Music::_get()//private +//private +Mix_Music* Music::_get() const { 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 int MusicPlayer::GetDecoderNum() { @@ -27,20 +43,6 @@ std::string MusicPlayer::GetDecoderName(int 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) { m = music; @@ -77,17 +79,17 @@ int MusicPlayer::fadeOut(int ms) return Mix_FadeOutMusic(ms); } -bool MusicPlayer::isPlaying() +bool MusicPlayer::isPlaying() const { return (Mix_PlayingMusic() == 1); } -bool MusicPlayer::isPaused() +bool MusicPlayer::isPaused() const { return (Mix_PausedMusic() == 1); } -int MusicPlayer::isFading() +int MusicPlayer::isFading() const { switch (Mix_FadingMusic()) { @@ -102,10 +104,9 @@ int MusicPlayer::isFading() } } -//static -int MusicPlayer::SetMusicPosition(double position) +int MusicPlayer::setPosition(double second) { - return Mix_SetMusicPosition(position); + return Mix_SetMusicPosition(second); } #include "end_code.h" diff --git a/SDLWrapper/Music.h b/SDLWrapper/Music.h index d7a3b51..7966886 100644 --- a/SDLWrapper/Music.h +++ b/SDLWrapper/Music.h @@ -4,45 +4,43 @@ #include #include "ErrorViewer.h" #include "Audio.h" +#include "__Noncopyable.h" #include "begin_code.h" /// Forward Declaration class Music { public: - -protected: - Music() = default; + Music()=default; + Music(const std::string& Filename); + bool isReady() const; + void release(); private: std::shared_ptr _music; void _set(Mix_Music*); void _clear(); - Mix_Music* _get(); + Mix_Music* _get() const; friend class MusicPlayer; }; -class MusicPlayer : public AudioPlayer +class MusicPlayer : public AudioPlayer, public NonCopyable { public: static int GetDecoderNum(); 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); void pause(); void resume(); void rewind(); + int setPosition(double second); int stop(); int fadeIn(int loops, int ms); int fadeOut(int ms); - bool isPlaying(); - bool isPaused(); - int isFading(); - - /// Experimental - static int SetMusicPosition(double position); - + bool isPlaying() const; + bool isPaused() const; + int isFading() const; private: Music m; }; diff --git a/SDLWrapper/__Noncopyable.h b/SDLWrapper/__Noncopyable.h index 22dd719..c2c820c 100644 --- a/SDLWrapper/__Noncopyable.h +++ b/SDLWrapper/__Noncopyable.h @@ -1,4 +1,5 @@ #pragma once +#include "begin_code.h" class NonCopyable { protected: @@ -7,3 +8,4 @@ protected: NonCopyable(const NonCopyable&) = delete; NonCopyable& operator = (const NonCopyable&) = delete; }; +#include "end_code.h"