minor fixes

This commit is contained in:
Nomango 2019-08-21 16:54:51 +08:00
parent c626697ebf
commit 318eeff9c7
8 changed files with 49 additions and 42 deletions

View File

@ -51,8 +51,8 @@ namespace kiwano
if (status_ == Status::NotStarted) if (status_ == Status::NotStarted)
{ {
Init(target);
status_ = delay_.IsZero() ? Status::Started : Status::Delayed; status_ = delay_.IsZero() ? Status::Started : Status::Delayed;
Init(target);
} }
switch (status_) switch (status_)

View File

@ -62,7 +62,7 @@ namespace kiwano
{ {
if (!path_) if (!path_)
{ {
Complete(target); Done();
return; return;
} }

View File

@ -52,6 +52,7 @@ namespace kiwano
void Animation::Init(ActorPtr target) void Animation::Init(ActorPtr target)
{ {
KGE_ASSERT(frame_seq_ && "Animation::Init() failed: FrameSequence is NULL!");
if (!frame_seq_ || frame_seq_->GetFrames().empty()) if (!frame_seq_ || frame_seq_->GetFrames().empty())
{ {
Done(); Done();
@ -59,6 +60,8 @@ namespace kiwano
} }
auto sprite_target = dynamic_cast<Sprite*>(target.get()); auto sprite_target = dynamic_cast<Sprite*>(target.get());
KGE_ASSERT(sprite_target && "Animation only supports Sprites!");
if (sprite_target && frame_seq_) if (sprite_target && frame_seq_)
{ {
sprite_target->SetFrame(frame_seq_->GetFrames()[0]); sprite_target->SetFrame(frame_seq_->GetFrames()[0]);
@ -69,13 +72,14 @@ namespace kiwano
{ {
auto sprite_target = dynamic_cast<Sprite*>(target.get()); auto sprite_target = dynamic_cast<Sprite*>(target.get());
KGE_ASSERT(sprite_target && "Animation only supports Sprites"); if (sprite_target && frame_seq_)
{
const auto& frames = frame_seq_->GetFrames();
auto size = frames.size();
auto index = std::min(static_cast<UInt32>(math::Floor(size * percent)), size - 1);
const auto& frames = frame_seq_->GetFrames(); sprite_target->SetFrame(frames[index]);
auto size = frames.size(); }
auto index = std::min(static_cast<UInt32>(math::Floor(size * percent)), size - 1);
sprite_target->SetFrame(frames[index]);
} }
ActionPtr Animation::Clone() const ActionPtr Animation::Clone() const

View File

@ -19,3 +19,6 @@
//---- Define DirectX version. Defaults to using Direct3D11 //---- Define DirectX version. Defaults to using Direct3D11
//#define KGE_USE_DIRECTX10 //#define KGE_USE_DIRECTX10
//---- Define to enable DirectX debug layer
//#define KGE_ENABLE_DX_DEBUG

View File

@ -191,7 +191,7 @@ namespace kiwano
D2D1_FACTORY_OPTIONS options; D2D1_FACTORY_OPTIONS options;
ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS)); ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));
#ifdef KGE_DEBUG #if defined(KGE_DEBUG) && defined(KGE_ENABLE_DX_DEBUG)
options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION; options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
#endif #endif

View File

@ -217,7 +217,7 @@ namespace kiwano
// than the API default. It is required for compatibility with Direct2D. // than the API default. It is required for compatibility with Direct2D.
UInt32 creation_flags = D3D10_CREATE_DEVICE_BGRA_SUPPORT; UInt32 creation_flags = D3D10_CREATE_DEVICE_BGRA_SUPPORT;
#if defined(KGE_DEBUG) #if defined(KGE_DEBUG) && defined(KGE_ENABLE_DX_DEBUG)
if (DX::SdkLayersAvailable()) if (DX::SdkLayersAvailable())
{ {
creation_flags |= D3D10_CREATE_DEVICE_DEBUG; creation_flags |= D3D10_CREATE_DEVICE_DEBUG;

View File

@ -209,7 +209,7 @@ namespace kiwano
// than the API default. It is required for compatibility with Direct2D. // than the API default. It is required for compatibility with Direct2D.
UInt32 creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; UInt32 creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if defined(KGE_DEBUG) #if defined(KGE_DEBUG) && defined(KGE_ENABLE_DX_DEBUG)
if (DX::SdkLayersAvailable()) if (DX::SdkLayersAvailable())
{ {
creation_flags |= D3D11_CREATE_DEVICE_DEBUG; creation_flags |= D3D11_CREATE_DEVICE_DEBUG;

View File

@ -201,8 +201,8 @@ namespace kiwano
if (files.empty()) if (files.empty())
return 0; return 0;
Vector<FramePtr> texture_arr; Vector<FramePtr> frames;
texture_arr.reserve(files.size()); frames.reserve(files.size());
for (const auto& file : files) for (const auto& file : files)
{ {
@ -211,15 +211,15 @@ namespace kiwano
{ {
if (ptr->Load(file)) if (ptr->Load(file))
{ {
texture_arr.push_back(ptr); frames.push_back(ptr);
} }
} }
} }
if (!texture_arr.empty()) if (!frames.empty())
{ {
FrameSequencePtr frames = new (std::nothrow) FrameSequence(texture_arr); FrameSequencePtr fs = new (std::nothrow) FrameSequence(frames);
return AddFrameSequence(id, frames); return AddFrameSequence(id, fs);
} }
return 0; return 0;
} }
@ -238,8 +238,8 @@ namespace kiwano
Float32 width = raw_width / cols; Float32 width = raw_width / cols;
Float32 height = raw_height / rows; Float32 height = raw_height / rows;
Vector<FramePtr> texture_arr; Vector<FramePtr> frames;
texture_arr.reserve(rows * cols); frames.reserve(rows * cols);
for (Int32 i = 0; i < rows; i++) for (Int32 i = 0; i < rows; i++)
{ {
@ -249,13 +249,13 @@ namespace kiwano
if (ptr) if (ptr)
{ {
ptr->SetCropRect(Rect{ j * width, i * height, (j + 1) * width, (i + 1) * height }); ptr->SetCropRect(Rect{ j * width, i * height, (j + 1) * width, (i + 1) * height });
texture_arr.push_back(ptr); frames.push_back(ptr);
} }
} }
} }
FrameSequencePtr frames = new (std::nothrow) FrameSequence(texture_arr); FrameSequencePtr fs = new (std::nothrow) FrameSequence(frames);
return AddFrameSequence(id, frames); return AddFrameSequence(id, fs);
} }
UInt32 ResourceCache::AddFrameSequence(String const & id, FrameSequencePtr frames) UInt32 ResourceCache::AddFrameSequence(String const & id, FrameSequencePtr frames)
@ -325,7 +325,7 @@ namespace kiwano
} }
else else
{ {
// Simple texture // Simple image
return loader->AddFrame(*id, gdata->path + (*file)); return loader->AddFrame(*id, gdata->path + (*file));
} }
} }
@ -358,24 +358,24 @@ namespace kiwano
global_data.path = json_data[L"path"]; global_data.path = json_data[L"path"];
} }
if (json_data.count(L"textures")) if (json_data.count(L"images"))
{ {
for (const auto& texture : json_data[L"textures"]) for (const auto& image : json_data[L"images"])
{ {
const String* id = nullptr, * type = nullptr, * file = nullptr; const String* id = nullptr, * type = nullptr, * file = nullptr;
Int32 rows = 0, cols = 0; Int32 rows = 0, cols = 0;
if (texture.count(L"id")) id = &texture[L"id"].as_string(); if (image.count(L"id")) id = &image[L"id"].as_string();
if (texture.count(L"type")) type = &texture[L"type"].as_string(); if (image.count(L"type")) type = &image[L"type"].as_string();
if (texture.count(L"file")) file = &texture[L"file"].as_string(); if (image.count(L"file")) file = &image[L"file"].as_string();
if (texture.count(L"rows")) rows = texture[L"rows"].as_int(); if (image.count(L"rows")) rows = image[L"rows"].as_int();
if (texture.count(L"cols")) cols = texture[L"cols"].as_int(); if (image.count(L"cols")) cols = image[L"cols"].as_int();
if (texture.count(L"files")) if (image.count(L"files"))
{ {
Vector<const WChar*> files; Vector<const WChar*> files;
files.reserve(texture[L"files"].size()); files.reserve(image[L"files"].size());
for (const auto& file : texture[L"files"]) for (const auto& file : image[L"files"])
{ {
files.push_back(file.as_string().c_str()); files.push_back(file.as_string().c_str());
} }
@ -400,23 +400,23 @@ namespace kiwano
global_data.path = path->GetText(); global_data.path = path->GetText();
} }
if (auto textures = elem->FirstChildElement(L"textures")) if (auto images = elem->FirstChildElement(L"images"))
{ {
for (auto texture = textures->FirstChildElement(); texture; texture = texture->NextSiblingElement()) for (auto image = images->FirstChildElement(); image; image = image->NextSiblingElement())
{ {
String id, type, file; String id, type, file;
Int32 rows = 0, cols = 0; Int32 rows = 0, cols = 0;
if (auto attr = texture->Attribute(L"id")) id.assign(attr); // assign() copies attr content if (auto attr = image->Attribute(L"id")) id.assign(attr); // assign() copies attr content
if (auto attr = texture->Attribute(L"type")) type = attr; // operator=() just holds attr pointer if (auto attr = image->Attribute(L"type")) type = attr; // operator=() just holds attr pointer
if (auto attr = texture->Attribute(L"file")) file = attr; if (auto attr = image->Attribute(L"file")) file = attr;
if (auto attr = texture->IntAttribute(L"rows")) rows = attr; if (auto attr = image->IntAttribute(L"rows")) rows = attr;
if (auto attr = texture->IntAttribute(L"cols")) cols = attr; if (auto attr = image->IntAttribute(L"cols")) cols = attr;
if (file.empty() && !texture->NoChildren()) if (file.empty() && !image->NoChildren())
{ {
Vector<const WChar*> files_arr; Vector<const WChar*> files_arr;
for (auto file = texture->FirstChildElement(); file; file = file->NextSiblingElement()) for (auto file = image->FirstChildElement(); file; file = file->NextSiblingElement())
{ {
if (auto path = file->Attribute(L"path")) if (auto path = file->Attribute(L"path"))
{ {