Update Sound && SoundPlayer

This commit is contained in:
Nomango 2019-08-19 09:28:59 +08:00
parent bdedf76b5e
commit 7b8e4814a9
25 changed files with 181 additions and 154 deletions

View File

@ -4,14 +4,14 @@
<ClInclude Include="..\src\kiwano-audio\kiwano-audio.h" /> <ClInclude Include="..\src\kiwano-audio\kiwano-audio.h" />
<ClInclude Include="..\src\kiwano-audio\src\audio-modules.h" /> <ClInclude Include="..\src\kiwano-audio\src\audio-modules.h" />
<ClInclude Include="..\src\kiwano-audio\src\audio.h" /> <ClInclude Include="..\src\kiwano-audio\src\audio.h" />
<ClInclude Include="..\src\kiwano-audio\src\Player.h" /> <ClInclude Include="..\src\kiwano-audio\src\SoundPlayer.h" />
<ClInclude Include="..\src\kiwano-audio\src\Sound.h" /> <ClInclude Include="..\src\kiwano-audio\src\Sound.h" />
<ClInclude Include="..\src\kiwano-audio\src\Transcoder.h" /> <ClInclude Include="..\src\kiwano-audio\src\Transcoder.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\src\kiwano-audio\src\audio-modules.cpp" /> <ClCompile Include="..\src\kiwano-audio\src\audio-modules.cpp" />
<ClCompile Include="..\src\kiwano-audio\src\audio.cpp" /> <ClCompile Include="..\src\kiwano-audio\src\audio.cpp" />
<ClCompile Include="..\src\kiwano-audio\src\Player.cpp" /> <ClCompile Include="..\src\kiwano-audio\src\SoundPlayer.cpp" />
<ClCompile Include="..\src\kiwano-audio\src\Sound.cpp" /> <ClCompile Include="..\src\kiwano-audio\src\Sound.cpp" />
<ClCompile Include="..\src\kiwano-audio\src\Transcoder.cpp" /> <ClCompile Include="..\src\kiwano-audio\src\Transcoder.cpp" />
</ItemGroup> </ItemGroup>

View File

@ -8,15 +8,15 @@
<ClInclude Include="..\src\kiwano-audio\src\audio-modules.h"> <ClInclude Include="..\src\kiwano-audio\src\audio-modules.h">
<Filter>src</Filter> <Filter>src</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\kiwano-audio\src\Player.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano-audio\src\Sound.h"> <ClInclude Include="..\src\kiwano-audio\src\Sound.h">
<Filter>src</Filter> <Filter>src</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\kiwano-audio\src\Transcoder.h"> <ClInclude Include="..\src\kiwano-audio\src\Transcoder.h">
<Filter>src</Filter> <Filter>src</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\kiwano-audio\src\SoundPlayer.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\src\kiwano-audio\src\audio.cpp"> <ClCompile Include="..\src\kiwano-audio\src\audio.cpp">
@ -25,15 +25,15 @@
<ClCompile Include="..\src\kiwano-audio\src\audio-modules.cpp"> <ClCompile Include="..\src\kiwano-audio\src\audio-modules.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano-audio\src\Player.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano-audio\src\Sound.cpp"> <ClCompile Include="..\src\kiwano-audio\src\Sound.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano-audio\src\Transcoder.cpp"> <ClCompile Include="..\src\kiwano-audio\src\Transcoder.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano-audio\src\SoundPlayer.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="src"> <Filter Include="src">

View File

@ -22,4 +22,4 @@
#include "src/audio.h" #include "src/audio.h"
#include "src/Sound.h" #include "src/Sound.h"
#include "src/Player.h" #include "src/SoundPlayer.h"

View File

@ -22,7 +22,6 @@
#include <kiwano/utils/FileUtil.h> #include <kiwano/utils/FileUtil.h>
#include "Sound.h" #include "Sound.h"
#include "audio.h" #include "audio.h"
#include "Transcoder.h"
namespace kiwano namespace kiwano
{ {
@ -32,12 +31,16 @@ namespace kiwano
Sound::Sound() Sound::Sound()
: opened_(false) : opened_(false)
, playing_(false) , playing_(false)
, size_(0)
, wave_data_(nullptr)
, voice_(nullptr) , voice_(nullptr)
{ {
} }
Sound::Sound(String const& file_path)
: Sound()
{
Load(file_path);
}
Sound::Sound(Resource const& res) Sound::Sound(Resource const& res)
: Sound() : Sound()
{ {
@ -51,21 +54,19 @@ namespace kiwano
bool Sound::Load(String const& file_path) bool Sound::Load(String const& file_path)
{ {
if (opened_)
{
Close();
}
#if defined(KGE_DEBUG)
if (!FileUtil::ExistsFile(file_path)) if (!FileUtil::ExistsFile(file_path))
{ {
KGE_WARNING_LOG(L"Media file '%s' not found", file_path.c_str()); KGE_WARNING_LOG(L"Media file '%s' not found", file_path.c_str());
return false; return false;
} }
#endif
if (opened_)
{
Close();
}
Transcoder transcoder; Transcoder transcoder;
HRESULT hr = transcoder.LoadMediaFile(file_path, &wave_data_, &size_); HRESULT hr = transcoder.LoadMediaFile(file_path);
if (FAILED(hr)) if (FAILED(hr))
{ {
@ -73,14 +74,11 @@ namespace kiwano
return false; return false;
} }
hr = Audio::GetInstance()->CreateVoice(&voice_, transcoder.GetWaveFormatEx()); hr = Audio::GetInstance()->CreateVoice(&voice_, transcoder.GetBuffer().format);
if (FAILED(hr)) if (FAILED(hr))
{ {
if (wave_data_) Close();
{
delete[] wave_data_;
wave_data_ = nullptr;
}
KGE_ERROR_LOG(L"Create source voice failed with HRESULT of %08X", hr); KGE_ERROR_LOG(L"Create source voice failed with HRESULT of %08X", hr);
return false; return false;
} }
@ -97,7 +95,7 @@ namespace kiwano
} }
Transcoder transcoder; Transcoder transcoder;
HRESULT hr = transcoder.LoadMediaResource(res, &wave_data_, &size_); HRESULT hr = transcoder.LoadMediaResource(res);
if (FAILED(hr)) if (FAILED(hr))
{ {
@ -105,14 +103,11 @@ namespace kiwano
return false; return false;
} }
hr = Audio::GetInstance()->CreateVoice(&voice_, transcoder.GetWaveFormatEx()); hr = Audio::GetInstance()->CreateVoice(&voice_, transcoder.GetBuffer().format);
if (FAILED(hr)) if (FAILED(hr))
{ {
if (wave_data_) Close();
{
delete[] wave_data_;
wave_data_ = nullptr;
}
KGE_ERROR_LOG(L"Create source voice failed with HRESULT of %08X", hr); KGE_ERROR_LOG(L"Create source voice failed with HRESULT of %08X", hr);
return false; return false;
} }
@ -140,10 +135,12 @@ namespace kiwano
// clamp loop count // clamp loop count
loop_count = (loop_count < 0) ? XAUDIO2_LOOP_INFINITE : std::min(loop_count, XAUDIO2_LOOP_INFINITE - 1); loop_count = (loop_count < 0) ? XAUDIO2_LOOP_INFINITE : std::min(loop_count, XAUDIO2_LOOP_INFINITE - 1);
auto wave_buffer = transcoder_.GetBuffer();
XAUDIO2_BUFFER buffer = { 0 }; XAUDIO2_BUFFER buffer = { 0 };
buffer.pAudioData = wave_data_; buffer.pAudioData = wave_buffer.data;
buffer.Flags = XAUDIO2_END_OF_STREAM; buffer.Flags = XAUDIO2_END_OF_STREAM;
buffer.AudioBytes = size_; buffer.AudioBytes = wave_buffer.size;
buffer.LoopCount = static_cast<UInt32>(loop_count); buffer.LoopCount = static_cast<UInt32>(loop_count);
HRESULT hr = voice_->SubmitSourceBuffer(&buffer); HRESULT hr = voice_->SubmitSourceBuffer(&buffer);
@ -202,11 +199,7 @@ namespace kiwano
voice_ = nullptr; voice_ = nullptr;
} }
if (wave_data_) transcoder_.ClearBuffer();
{
delete[] wave_data_;
wave_data_ = nullptr;
}
opened_ = false; opened_ = false;
playing_ = false; playing_ = false;

View File

@ -23,6 +23,7 @@
#include <kiwano/base/ObjectBase.h> #include <kiwano/base/ObjectBase.h>
#include <kiwano/base/Resource.h> #include <kiwano/base/Resource.h>
#include <xaudio2.h> #include <xaudio2.h>
#include "Transcoder.h"
namespace kiwano namespace kiwano
{ {
@ -86,11 +87,10 @@ namespace kiwano
); );
protected: protected:
bool opened_; bool opened_;
bool playing_; bool playing_;
UInt32 size_; Transcoder transcoder_;
BYTE* wave_data_; IXAudio2SourceVoice* voice_;
IXAudio2SourceVoice* voice_;
}; };
} }
} }

View File

@ -18,23 +18,23 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#include "Player.h" #include "SoundPlayer.h"
namespace kiwano namespace kiwano
{ {
namespace audio namespace audio
{ {
Player::Player() SoundPlayer::SoundPlayer()
: volume_(1.f) : volume_(1.f)
{ {
} }
Player::~Player() SoundPlayer::~SoundPlayer()
{ {
ClearCache(); ClearCache();
} }
UInt32 Player::Load(String const& file_path) UInt32 SoundPlayer::Load(String const& file_path)
{ {
UInt32 hash_code = file_path.hash(); UInt32 hash_code = file_path.hash();
if (sound_cache_.end() != sound_cache_.find(hash_code)) if (sound_cache_.end() != sound_cache_.find(hash_code))
@ -54,7 +54,7 @@ namespace kiwano
return false; return false;
} }
UInt32 Player::Load(Resource const& res) UInt32 SoundPlayer::Load(Resource const& res)
{ {
UInt32 hash_code = res.GetId(); UInt32 hash_code = res.GetId();
if (sound_cache_.end() != sound_cache_.find(hash_code)) if (sound_cache_.end() != sound_cache_.find(hash_code))
@ -74,35 +74,35 @@ namespace kiwano
return false; return false;
} }
void Player::Play(UInt32 id, Int32 loop_count) void SoundPlayer::Play(UInt32 id, Int32 loop_count)
{ {
auto iter = sound_cache_.find(id); auto iter = sound_cache_.find(id);
if (sound_cache_.end() != iter) if (sound_cache_.end() != iter)
iter->second->Play(loop_count); iter->second->Play(loop_count);
} }
void Player::Pause(UInt32 id) void SoundPlayer::Pause(UInt32 id)
{ {
auto iter = sound_cache_.find(id); auto iter = sound_cache_.find(id);
if (sound_cache_.end() != iter) if (sound_cache_.end() != iter)
iter->second->Pause(); iter->second->Pause();
} }
void Player::Resume(UInt32 id) void SoundPlayer::Resume(UInt32 id)
{ {
auto iter = sound_cache_.find(id); auto iter = sound_cache_.find(id);
if (sound_cache_.end() != iter) if (sound_cache_.end() != iter)
iter->second->Resume(); iter->second->Resume();
} }
void Player::Stop(UInt32 id) void SoundPlayer::Stop(UInt32 id)
{ {
auto iter = sound_cache_.find(id); auto iter = sound_cache_.find(id);
if (sound_cache_.end() != iter) if (sound_cache_.end() != iter)
iter->second->Stop(); iter->second->Stop();
} }
bool Player::IsPlaying(UInt32 id) bool SoundPlayer::IsPlaying(UInt32 id)
{ {
auto iter = sound_cache_.find(id); auto iter = sound_cache_.find(id);
if (sound_cache_.end() != iter) if (sound_cache_.end() != iter)
@ -110,12 +110,12 @@ namespace kiwano
return false; return false;
} }
Float32 Player::GetVolume() const Float32 SoundPlayer::GetVolume() const
{ {
return volume_; return volume_;
} }
void Player::SetVolume(Float32 volume) void SoundPlayer::SetVolume(Float32 volume)
{ {
volume_ = std::min(std::max(volume, -224.f), 224.f); volume_ = std::min(std::max(volume, -224.f), 224.f);
for (const auto& pair : sound_cache_) for (const auto& pair : sound_cache_)
@ -124,7 +124,7 @@ namespace kiwano
} }
} }
void Player::PauseAll() void SoundPlayer::PauseAll()
{ {
for (const auto& pair : sound_cache_) for (const auto& pair : sound_cache_)
{ {
@ -132,7 +132,7 @@ namespace kiwano
} }
} }
void Player::ResumeAll() void SoundPlayer::ResumeAll()
{ {
for (const auto& pair : sound_cache_) for (const auto& pair : sound_cache_)
{ {
@ -140,7 +140,7 @@ namespace kiwano
} }
} }
void Player::StopAll() void SoundPlayer::StopAll()
{ {
for (const auto& pair : sound_cache_) for (const auto& pair : sound_cache_)
{ {
@ -148,7 +148,7 @@ namespace kiwano
} }
} }
void Player::ClearCache() void SoundPlayer::ClearCache()
{ {
sound_cache_.clear(); sound_cache_.clear();
} }

View File

@ -27,23 +27,23 @@ namespace kiwano
{ {
namespace audio namespace audio
{ {
KGE_DECLARE_SMART_PTR(Player); KGE_DECLARE_SMART_PTR(SoundPlayer);
// 稜있꺄렴포 // 稜있꺄렴포
class KGE_API Player class KGE_API SoundPlayer
: protected ObjectBase : protected ObjectBase
{ {
public: public:
Player(); SoundPlayer();
~Player(); ~SoundPlayer();
// 加载本地音频文件, 返回该资源标识符 // 加载本地音频文件, 返回该资源标识符
UInt32 Load( UInt32 Load(
String const& file_path String const& file_path
); );
// 加载音乐资源, 返回该资源标识符 // 加载音乐资源, 返回该资源标识符
UInt32 Load( UInt32 Load(
Resource const& res /* 稜있栗都 */ Resource const& res /* 稜있栗都 */
); );
@ -51,7 +51,7 @@ namespace kiwano
// 꺄렴稜있 // 꺄렴稜있
void Play( void Play(
UInt32 id, /* 깃街륜 */ UInt32 id, /* 깃街륜 */
Int32 loop_count = 0 /* 播放循环次数 (-1 为循环播放) */ Int32 loop_count = 0 /* 播放循环次数 (-1 为循环播放) */
); );
// 董界稜있 // 董界稜있

View File

@ -38,24 +38,39 @@ namespace kiwano
Transcoder::Transcoder() Transcoder::Transcoder()
: wave_format_(nullptr) : wave_format_(nullptr)
, wave_data_(nullptr)
, wave_size_(0)
{ {
} }
Transcoder::~Transcoder() Transcoder::~Transcoder()
{
ClearBuffer();
}
Transcoder::Buffer Transcoder::GetBuffer() const
{
return Buffer{ wave_data_, wave_size_, wave_format_ };
}
void Transcoder::ClearBuffer()
{ {
if (wave_format_) if (wave_format_)
{ {
::CoTaskMemFree(wave_format_); ::CoTaskMemFree(wave_format_);
wave_format_ = nullptr; wave_format_ = nullptr;
} }
if (wave_data_)
{
delete[] wave_data_;
wave_data_ = nullptr;
}
wave_size_ = 0;
} }
const WAVEFORMATEX* Transcoder::GetWaveFormatEx() const HRESULT Transcoder::LoadMediaFile(String const& file_path)
{
return wave_format_;
}
HRESULT Transcoder::LoadMediaFile(String const& file_path, BYTE** wave_data, UInt32* wave_data_size)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
@ -69,13 +84,13 @@ namespace kiwano
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
hr = ReadSource(reader.get(), wave_data, wave_data_size); hr = ReadSource(reader.get());
} }
return hr; return hr;
} }
HRESULT Transcoder::LoadMediaResource(Resource const& res, BYTE** wave_data, UInt32* wave_data_size) HRESULT Transcoder::LoadMediaResource(Resource const& res)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
@ -87,7 +102,7 @@ namespace kiwano
if (!data) { return E_FAIL; } if (!data) { return E_FAIL; }
stream = kiwano::modules::Shlwapi::Get().SHCreateMemStream( stream = kiwano::modules::Shlwapi::Get().SHCreateMemStream(
static_cast<const BYTE*>(data.buffer), static_cast<const Byte*>(data.buffer),
static_cast<UInt32>(data.size) static_cast<UInt32>(data.size)
); );
@ -113,13 +128,13 @@ namespace kiwano
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
hr = ReadSource(reader.get(), wave_data, wave_data_size); hr = ReadSource(reader.get());
} }
return hr; return hr;
} }
HRESULT Transcoder::ReadSource(IMFSourceReader* reader, BYTE** wave_data, UInt32* wave_data_size) HRESULT Transcoder::ReadSource(IMFSourceReader* reader)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
DWORD max_stream_size = 0; DWORD max_stream_size = 0;
@ -194,7 +209,7 @@ namespace kiwano
LONGLONG duration = prop.uhVal.QuadPart; LONGLONG duration = prop.uhVal.QuadPart;
max_stream_size = static_cast<DWORD>( max_stream_size = static_cast<DWORD>(
(duration * wave_format_->nAvgBytesPerSec) / 10000000 + 1 (duration * wave_format_->nAvgBytesPerSec) / 10000000 + 1
); );
PropVariantClear(&prop); PropVariantClear(&prop);
} }
@ -203,7 +218,7 @@ namespace kiwano
{ {
DWORD flags = 0; DWORD flags = 0;
DWORD position = 0; DWORD position = 0;
BYTE* data = new (std::nothrow) BYTE[max_stream_size]; Byte* data = new (std::nothrow) Byte[max_stream_size];
ComPtr<IMFSample> sample; ComPtr<IMFSample> sample;
ComPtr<IMFMediaBuffer> buffer; ComPtr<IMFMediaBuffer> buffer;
@ -236,7 +251,7 @@ namespace kiwano
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
BYTE* audio_data = nullptr; Byte* audio_data = nullptr;
DWORD sample_buffer_length = 0; DWORD sample_buffer_length = 0;
hr = buffer->Lock( hr = buffer->Lock(
@ -245,7 +260,7 @@ namespace kiwano
&sample_buffer_length &sample_buffer_length
); );
if (SUCCEEDED(hr)) if (SUCCEEDED(hr) && sample_buffer_length <= max_stream_size)
{ {
for (DWORD i = 0; i < sample_buffer_length; i++) for (DWORD i = 0; i < sample_buffer_length; i++)
{ {
@ -263,8 +278,8 @@ namespace kiwano
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
*wave_data = data; wave_data_ = data;
*wave_data_size = position; wave_size_ = position;
} }
} }
} }

View File

@ -31,31 +31,36 @@ namespace kiwano
class KGE_API Transcoder class KGE_API Transcoder
{ {
public: public:
struct Buffer
{
Byte* data;
UInt32 size;
const WAVEFORMATEX* format;
};
Transcoder(); Transcoder();
~Transcoder(); ~Transcoder();
const WAVEFORMATEX* GetWaveFormatEx() const; Buffer GetBuffer() const;
void ClearBuffer();
HRESULT LoadMediaFile( HRESULT LoadMediaFile(
String const& file_path, String const& file_path
BYTE** wave_data,
UInt32* wave_data_size
); );
HRESULT LoadMediaResource( HRESULT LoadMediaResource(
Resource const& res, Resource const& res
BYTE** wave_data,
UInt32* wave_data_size
); );
HRESULT ReadSource( HRESULT ReadSource(
IMFSourceReader* reader, IMFSourceReader* reader
BYTE** wave_data,
UInt32* wave_data_size
); );
private: private:
Byte* wave_data_;
UInt32 wave_size_;
WAVEFORMATEX* wave_format_; WAVEFORMATEX* wave_format_;
}; };
} }

View File

@ -76,9 +76,17 @@ namespace kiwano
HRESULT Audio::CreateVoice(IXAudio2SourceVoice** voice, const WAVEFORMATEX* wfx) HRESULT Audio::CreateVoice(IXAudio2SourceVoice** voice, const WAVEFORMATEX* wfx)
{ {
KGE_ASSERT(x_audio2_ && "Audio engine hasn't been initialized!");
if (voice == nullptr) if (voice == nullptr)
{ {
return E_UNEXPECTED; return E_INVALIDARG;
}
if (*voice)
{
(*voice)->DestroyVoice();
(*voice) = nullptr;
} }
HRESULT hr = x_audio2_->CreateSourceVoice(voice, wfx, 0, XAUDIO2_DEFAULT_FREQ_RATIO); HRESULT hr = x_audio2_->CreateSourceVoice(voice, wfx, 0, XAUDIO2_DEFAULT_FREQ_RATIO);
@ -87,11 +95,15 @@ namespace kiwano
void Audio::Open() void Audio::Open()
{ {
KGE_ASSERT(x_audio2_ && "Audio engine hasn't been initialized!");
x_audio2_->StartEngine(); x_audio2_->StartEngine();
} }
void Audio::Close() void Audio::Close()
{ {
KGE_ASSERT(x_audio2_ && "Audio engine hasn't been initialized!");
x_audio2_->StopEngine(); x_audio2_->StopEngine();
} }
} }

View File

@ -432,7 +432,7 @@ namespace kiwano
bool update_pausing_; bool update_pausing_;
bool cascade_opacity_; bool cascade_opacity_;
bool show_border_; bool show_border_;
Int32 z_order_; Int32 z_order_;
Float32 opacity_; Float32 opacity_;
Float32 displayed_opacity_; Float32 displayed_opacity_;
Actor* parent_; Actor* parent_;
@ -444,9 +444,9 @@ namespace kiwano
UpdateCallback cb_update_; UpdateCallback cb_update_;
Transform transform_; Transform transform_;
bool is_fast_transform_; bool is_fast_transform_;
mutable bool dirty_transform_; mutable bool dirty_transform_;
mutable bool dirty_transform_inverse_; mutable bool dirty_transform_inverse_;
mutable Matrix3x2 transform_matrix_; mutable Matrix3x2 transform_matrix_;
mutable Matrix3x2 transform_matrix_inverse_; mutable Matrix3x2 transform_matrix_inverse_;
}; };

View File

@ -62,7 +62,7 @@ namespace kiwano
); );
// 设置 GIF 动画循环次数 // 设置 GIF 动画循环次数
inline void SetLoopCount(Int32 loops) { total_loop_count_ = loops; } inline void SetLoopCount(Int32 loops) { total_loop_count_ = loops; }
// 设置 GIF 动画每次循环结束回调函数 // 设置 GIF 动画每次循环结束回调函数
inline void SetLoopDoneCallback(LoopDoneCallback const& cb) { loop_cb_ = cb; } inline void SetLoopDoneCallback(LoopDoneCallback const& cb) { loop_cb_ = cb; }
@ -100,8 +100,8 @@ namespace kiwano
protected: protected:
bool animating_; bool animating_;
Int32 total_loop_count_; Int32 total_loop_count_;
Int32 loop_count_; Int32 loop_count_;
UInt32 next_index_; UInt32 next_index_;
Duration frame_delay_; Duration frame_delay_;
Duration frame_elapsed_; Duration frame_elapsed_;

View File

@ -62,7 +62,7 @@ namespace kiwano
inline void SetDelay(Duration delay) { delay_ = delay; } inline void SetDelay(Duration delay) { delay_ = delay; }
// 设置循环次数 (-1 为永久循环) // 设置循环次数 (-1 为永久循环)
inline void SetLoops(Int32 loops) { loops_ = loops; } inline void SetLoops(Int32 loops) { loops_ = loops; }
// 动作结束时移除目标角色 // 动作结束时移除目标角色
inline void RemoveTargetWhenDone() { detach_target_ = true; } inline void RemoveTargetWhenDone() { detach_target_ = true; }
@ -89,7 +89,7 @@ namespace kiwano
inline bool IsRemoveable() const { return status_ == Status::Removeable; } inline bool IsRemoveable() const { return status_ == Status::Removeable; }
inline Int32 GetLoops() const { return loops_; } inline Int32 GetLoops() const { return loops_; }
inline Duration GetDelay() const { return delay_; } inline Duration GetDelay() const { return delay_; }
@ -114,8 +114,8 @@ namespace kiwano
Status status_; Status status_;
bool running_; bool running_;
bool detach_target_; bool detach_target_;
Int32 loops_; Int32 loops_;
Int32 loops_done_; Int32 loops_done_;
Duration delay_; Duration delay_;
Duration elapsed_; Duration elapsed_;
ActionCallback cb_done_; ActionCallback cb_done_;

View File

@ -64,7 +64,7 @@ namespace kiwano
inline TweenHelper& SetDuration(Duration dur) { base->SetDuration(dur); return (*this); } inline TweenHelper& SetDuration(Duration dur) { base->SetDuration(dur); return (*this); }
// 设置循环次数 // 设置循环次数
inline TweenHelper& SetLoops(Int32 loops) { base->SetLoops(loops); return (*this); } inline TweenHelper& SetLoops(Int32 loops) { base->SetLoops(loops); return (*this); }
// 设置缓动函数 // 设置缓动函数
inline TweenHelper& SetEaseFunc(EaseFunc ease) { base->SetEaseFunc(ease); return (*this); } inline TweenHelper& SetEaseFunc(EaseFunc ease) { base->SetEaseFunc(ease); return (*this); }
@ -118,7 +118,7 @@ namespace kiwano
Duration dur, Duration dur,
Point const& pos, /* 目的坐标 */ Point const& pos, /* 目的坐标 */
Float32 height, /* 跳跃高度 */ Float32 height, /* 跳跃高度 */
Int32 jumps = 1) /* 跳跃次数 */ Int32 jumps = 1) /* 跳跃次数 */
{ {
return TweenHelper(new kiwano::ActionJumpBy(dur, pos, height, jumps)); return TweenHelper(new kiwano::ActionJumpBy(dur, pos, height, jumps));
} }
@ -128,7 +128,7 @@ namespace kiwano
Duration dur, Duration dur,
Point const& pos, /* 目的坐标 */ Point const& pos, /* 目的坐标 */
Float32 height, /* 跳跃高度 */ Float32 height, /* 跳跃高度 */
Int32 jumps = 1) /* 跳跃次数 */ Int32 jumps = 1) /* 跳跃次数 */
{ {
return TweenHelper(new kiwano::ActionJumpTo(dur, pos, height, jumps)); return TweenHelper(new kiwano::ActionJumpTo(dur, pos, height, jumps));
} }

View File

@ -166,7 +166,7 @@ namespace kiwano
Duration duration, /* 持续时长 */ Duration duration, /* 持续时长 */
Point const& vec, /* 跳跃距离 */ Point const& vec, /* 跳跃距离 */
Float32 height, /* 跳跃高度 */ Float32 height, /* 跳跃高度 */
Int32 jumps = 1, /* 跳跃次数 */ Int32 jumps = 1, /* 跳跃次数 */
EaseFunc func = nullptr /* 速度变化 */ EaseFunc func = nullptr /* 速度变化 */
); );
@ -199,7 +199,7 @@ namespace kiwano
Duration duration, /* 持续时长 */ Duration duration, /* 持续时长 */
Point const& pos, /* 目的坐标 */ Point const& pos, /* 目的坐标 */
Float32 height, /* 跳跃高度 */ Float32 height, /* 跳跃高度 */
Int32 jumps = 1, /* 跳跃次数 */ Int32 jumps = 1, /* 跳跃次数 */
EaseFunc func = nullptr /* 速度变化 */ EaseFunc func = nullptr /* 速度变化 */
); );
@ -234,8 +234,8 @@ namespace kiwano
ActionScaleBy( ActionScaleBy(
Duration duration, /* 持续时长 */ Duration duration, /* 持续时长 */
Float32 scale_x, /* 横向缩放相对变化值 */ Float32 scale_x, /* 横向缩放相对变化值 */
Float32 scale_y, /* 纵向缩放相对变化值 */ Float32 scale_y, /* 纵向缩放相对变化值 */
EaseFunc func = nullptr /* 速度变化 */ EaseFunc func = nullptr /* 速度变化 */
); );
@ -271,8 +271,8 @@ namespace kiwano
ActionScaleTo( ActionScaleTo(
Duration duration, /* 持续时长 */ Duration duration, /* 持续时长 */
Float32 scale_x, /* 横向缩放目标值 */ Float32 scale_x, /* 横向缩放目标值 */
Float32 scale_y, /* 纵向缩放目标值 */ Float32 scale_y, /* 纵向缩放目标值 */
EaseFunc func = nullptr /* 速度变化 */ EaseFunc func = nullptr /* 速度变化 */
); );
@ -302,7 +302,7 @@ namespace kiwano
public: public:
ActionFadeTo( ActionFadeTo(
Duration duration, /* 持续时长 */ Duration duration, /* 持续时长 */
Float32 opacity, /* 目标值 */ Float32 opacity, /* 目标值 */
EaseFunc func = nullptr /* 速度变化 */ EaseFunc func = nullptr /* 速度变化 */
); );
@ -361,7 +361,7 @@ namespace kiwano
public: public:
ActionRotateBy( ActionRotateBy(
Duration duration, /* 持续时长 */ Duration duration, /* 持续时长 */
Float32 rotation, /* 相对变化值 */ Float32 rotation, /* 相对变化值 */
EaseFunc func = nullptr /* 速度变化 */ EaseFunc func = nullptr /* 速度变化 */
); );
@ -389,7 +389,7 @@ namespace kiwano
public: public:
ActionRotateTo( ActionRotateTo(
Duration duration, /* 持续时长 */ Duration duration, /* 持续时长 */
Float32 rotation, /* 目标值 */ Float32 rotation, /* 目标值 */
EaseFunc func = nullptr /* 速度变化 */ EaseFunc func = nullptr /* 速度变化 */
); );

View File

@ -31,7 +31,7 @@ namespace kiwano
} }
Resource::Resource(UInt32 id, LPCWSTR type) Resource::Resource(UInt32 id, const WChar* type)
: id_(id) : id_(id)
, type_(type) , type_(type)
{ {

View File

@ -49,8 +49,8 @@ namespace kiwano
Resource(); Resource();
Resource( Resource(
UInt32 id, /* ×ÊÔ´Ãû³Æ */ UInt32 id, /* ×ÊÔ´ ID */
LPCWSTR type /* ×ÊÔ´ÀàÐÍ */ const WChar* type /* ×ÊÔ´ÀàÐÍ */
); );
// 获取二进制数据 // 获取二进制数据
@ -58,11 +58,12 @@ namespace kiwano
inline UInt32 GetId() const { return id_; } inline UInt32 GetId() const { return id_; }
inline LPCWSTR GetType() const { return type_; } inline const WChar* GetType() const { return type_; }
private: private:
UInt32 id_; UInt32 id_;
LPCWSTR type_; const WChar* type_;
mutable Resource::Data data_; mutable Resource::Data data_;
}; };
} }

View File

@ -48,7 +48,7 @@ namespace kiwano
explicit Timer( explicit Timer(
Callback const& func, /* 执行函数 */ Callback const& func, /* 执行函数 */
Duration delay, /* 时间间隔(秒) */ Duration delay, /* 时间间隔(秒) */
Int32 times = -1, /* 执行次数(设 -1 为永久执行) */ Int32 times = -1, /* 执行次数(设 -1 为永久执行) */
String const& name = L"" /* 任务名称 */ String const& name = L"" /* 任务名称 */
); );
@ -68,8 +68,8 @@ namespace kiwano
protected: protected:
bool running_; bool running_;
Int32 run_times_; Int32 run_times_;
Int32 total_times_; Int32 total_times_;
Duration delay_; Duration delay_;
Duration delta_; Duration delta_;
Callback callback_; Callback callback_;

View File

@ -66,7 +66,7 @@ namespace kiwano
} }
} }
void Window::Init(String const& title, Int32 width, Int32 height, LPCWSTR icon, bool fullscreen, WNDPROC proc) void Window::Init(String const& title, Int32 width, Int32 height, UInt32 icon, bool fullscreen, WNDPROC proc)
{ {
HINSTANCE hinst = GetModuleHandleW(nullptr); HINSTANCE hinst = GetModuleHandleW(nullptr);
WNDCLASSEX wcex = { 0 }; WNDCLASSEX wcex = { 0 };
@ -84,7 +84,7 @@ namespace kiwano
if (icon) if (icon)
{ {
wcex.hIcon = (HICON)::LoadImageW(hinst, icon, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); wcex.hIcon = (HICON)::LoadImageW(hinst, MAKEINTRESOURCE(icon), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
} }
::RegisterClassExW(&wcex); ::RegisterClassExW(&wcex);
@ -216,14 +216,14 @@ namespace kiwano
return static_cast<Float32>(height_); return static_cast<Float32>(height_);
} }
void Window::SetIcon(LPCWSTR icon_resource) void Window::SetIcon(UInt32 icon_resource)
{ {
if (handle_) if (handle_)
{ {
HINSTANCE hinstance = GetModuleHandle(nullptr); HINSTANCE hinstance = GetModuleHandle(nullptr);
HICON icon = (HICON)::LoadImage( HICON icon = (HICON)::LoadImage(
hinstance, hinstance,
icon_resource, MAKEINTRESOURCE(icon_resource),
IMAGE_ICON, IMAGE_ICON,
0, 0,
0, 0,

View File

@ -48,7 +48,7 @@ namespace kiwano
void SetTitle(String const& title); void SetTitle(String const& title);
// 设置窗口图标 // 设置窗口图标
void SetIcon(LPCWSTR icon_resource); void SetIcon(UInt32 icon_resource);
// 重设窗口大小 // 重设窗口大小
void Resize(Int32 width, Int32 height); void Resize(Int32 width, Int32 height);
@ -62,9 +62,9 @@ namespace kiwano
public: public:
void Init( void Init(
String const& title, String const& title,
Int32 width, Int32 width,
Int32 height, Int32 height,
LPCWSTR icon, UInt32 icon,
bool fullscreen, bool fullscreen,
WNDPROC proc WNDPROC proc
); );
@ -85,11 +85,11 @@ namespace kiwano
~Window(); ~Window();
private: private:
HWND handle_; HWND handle_;
bool is_fullscreen_; bool is_fullscreen_;
Int32 width_; Int32 width_;
Int32 height_; Int32 height_;
WCHAR* device_name_; WCHAR* device_name_;
MouseCursor mouse_cursor_; MouseCursor mouse_cursor_;
}; };
} }

View File

@ -28,6 +28,7 @@ namespace kiwano
KGE_DEFINE_NUMERIC_TYPE(Char, signed char); KGE_DEFINE_NUMERIC_TYPE(Char, signed char);
KGE_DEFINE_NUMERIC_TYPE(UChar, unsigned char); KGE_DEFINE_NUMERIC_TYPE(UChar, unsigned char);
KGE_DEFINE_NUMERIC_TYPE(WChar, wchar_t); KGE_DEFINE_NUMERIC_TYPE(WChar, wchar_t);
KGE_DEFINE_NUMERIC_TYPE(Byte, UChar);
KGE_DEFINE_NUMERIC_TYPE(Int8, std::int8_t); KGE_DEFINE_NUMERIC_TYPE(Int8, std::int8_t);
KGE_DEFINE_NUMERIC_TYPE(Int16, std::int16_t); KGE_DEFINE_NUMERIC_TYPE(Int16, std::int16_t);

View File

@ -43,7 +43,7 @@ namespace kiwano
Queue<FunctionToPerform> functions_to_perform_; Queue<FunctionToPerform> functions_to_perform_;
} }
Options::Options(String const& title, Int32 width, Int32 height, LPCWSTR icon, Color clear_color, bool vsync, bool fullscreen, bool debug) Options::Options(String const& title, Int32 width, Int32 height, UInt32 icon, Color clear_color, bool vsync, bool fullscreen, bool debug)
: title(title) : title(title)
, width(width) , width(width)
, height(height) , height(height)

View File

@ -30,23 +30,23 @@ namespace kiwano
struct Options struct Options
{ {
String title; // 标题 String title; // 标题
Int32 width; // 宽度 Int32 width; // 宽度
Int32 height; // 高度 Int32 height; // 高度
LPCWSTR icon; // 图标 UInt32 icon; // 图标资源 ID
Color clear_color; // 清屏颜色 Color clear_color; // 清屏颜色
bool vsync; // 垂直同步 bool vsync; // 垂直同步
bool fullscreen; // 全屏模式 bool fullscreen; // 全屏模式
bool debug; // 调试模式 bool debug; // 调试模式
Options( Options(
String const& title = L"Kiwano Game", String const& title = L"Kiwano Game",
Int32 width = 640, Int32 width = 640,
Int32 height = 480, Int32 height = 480,
LPCWSTR icon = nullptr, UInt32 icon = 0,
Color clear_color = Color::Black, Color clear_color = Color::Black,
bool vsync = true, bool vsync = true,
bool fullscreen = false, bool fullscreen = false,
bool debug = false bool debug = false
); );
}; };

View File

@ -191,7 +191,7 @@ namespace kiwano
HRESULT GifImage::GetBackgroundColor(ComPtr<IWICMetadataQueryReader> metadata_reader) HRESULT GifImage::GetBackgroundColor(ComPtr<IWICMetadataQueryReader> metadata_reader)
{ {
BYTE bg_index = 0; UChar bg_index = 0;
WICColor bgcolors[256]; WICColor bgcolors[256];
UInt32 colors_copied = 0; UInt32 colors_copied = 0;
ComPtr<IWICPalette> wic_palette; ComPtr<IWICPalette> wic_palette;

View File

@ -948,7 +948,7 @@ namespace kiwano
if (fileOffset <= resourceSize_ && if (fileOffset <= resourceSize_ &&
fragmentSize <= resourceSize_ - fileOffset) fragmentSize <= resourceSize_ - fileOffset)
{ {
*fragmentStart = static_cast<BYTE const*>(resourcePtr_) + static_cast<UInt32>(fileOffset); *fragmentStart = static_cast<Byte const*>(resourcePtr_) + static_cast<UInt32>(fileOffset);
*fragmentContext = NULL; *fragmentContext = NULL;
return S_OK; return S_OK;
} }