Add ImageCache, add ResourceCache, add Frame, rename Frames to FrameSequence

This commit is contained in:
Nomango 2019-08-13 14:44:37 +08:00
parent 645603775d
commit bd58cf1b49
47 changed files with 786 additions and 642 deletions

View File

@ -1,6 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup> <ItemGroup>
<ClInclude Include="..\src\kiwano\2d\action\Action.h" />
<ClInclude Include="..\src\kiwano\2d\action\ActionGroup.h" />
<ClInclude Include="..\src\kiwano\2d\action\ActionHelper.h" />
<ClInclude Include="..\src\kiwano\2d\action\ActionManager.h" />
<ClInclude Include="..\src\kiwano\2d\action\ActionTween.h" />
<ClInclude Include="..\src\kiwano\2d\action\Animation.h" />
<ClInclude Include="..\src\kiwano\2d\Frame.h" />
<ClInclude Include="..\src\kiwano\2d\GifSprite.h" /> <ClInclude Include="..\src\kiwano\2d\GifSprite.h" />
<ClInclude Include="..\src\kiwano\base\Director.h" /> <ClInclude Include="..\src\kiwano\base\Director.h" />
<ClInclude Include="..\src\kiwano\base\types.h" /> <ClInclude Include="..\src\kiwano\base\types.h" />
@ -8,20 +15,12 @@
<ClInclude Include="..\src\kiwano\config.h" /> <ClInclude Include="..\src\kiwano\config.h" />
<ClInclude Include="..\src\kiwano\macros.h" /> <ClInclude Include="..\src\kiwano\macros.h" />
<ClInclude Include="..\src\kiwano\2d\include-forwards.h" /> <ClInclude Include="..\src\kiwano\2d\include-forwards.h" />
<ClInclude Include="..\src\kiwano\2d\Action.h" />
<ClInclude Include="..\src\kiwano\2d\ActionGroup.h" />
<ClInclude Include="..\src\kiwano\2d\ActionHelper.h" />
<ClInclude Include="..\src\kiwano\2d\ActionManager.h" />
<ClInclude Include="..\src\kiwano\2d\ActionTween.h" />
<ClInclude Include="..\src\kiwano\2d\Animation.h" />
<ClInclude Include="..\src\kiwano\2d\Canvas.h" /> <ClInclude Include="..\src\kiwano\2d\Canvas.h" />
<ClInclude Include="..\src\kiwano\2d\Color.h" /> <ClInclude Include="..\src\kiwano\2d\Color.h" />
<ClInclude Include="..\src\kiwano\2d\DebugNode.h" /> <ClInclude Include="..\src\kiwano\2d\DebugNode.h" />
<ClInclude Include="..\src\kiwano\2d\Font.hpp" /> <ClInclude Include="..\src\kiwano\2d\Font.hpp" />
<ClInclude Include="..\src\kiwano\2d\Frames.h" /> <ClInclude Include="..\src\kiwano\2d\FrameSequence.h" />
<ClInclude Include="..\src\kiwano\2d\ShapeNode.h" /> <ClInclude Include="..\src\kiwano\2d\ShapeNode.h" />
<ClInclude Include="..\src\kiwano\2d\GifImage.h" />
<ClInclude Include="..\src\kiwano\2d\Image.h" />
<ClInclude Include="..\src\kiwano\2d\Layer.h" /> <ClInclude Include="..\src\kiwano\2d\Layer.h" />
<ClInclude Include="..\src\kiwano\2d\Actor.h" /> <ClInclude Include="..\src\kiwano\2d\Actor.h" />
<ClInclude Include="..\src\kiwano\2d\Stage.h" /> <ClInclude Include="..\src\kiwano\2d\Stage.h" />
@ -70,7 +69,10 @@
<ClInclude Include="..\src\kiwano\renderer\D3D10DeviceResources.h" /> <ClInclude Include="..\src\kiwano\renderer\D3D10DeviceResources.h" />
<ClInclude Include="..\src\kiwano\renderer\D3D11DeviceResources.h" /> <ClInclude Include="..\src\kiwano\renderer\D3D11DeviceResources.h" />
<ClInclude Include="..\src\kiwano\renderer\D3DDeviceResourcesBase.h" /> <ClInclude Include="..\src\kiwano\renderer\D3DDeviceResourcesBase.h" />
<ClInclude Include="..\src\kiwano\renderer\GifImage.h" />
<ClInclude Include="..\src\kiwano\renderer\helper.hpp" /> <ClInclude Include="..\src\kiwano\renderer\helper.hpp" />
<ClInclude Include="..\src\kiwano\renderer\Image.h" />
<ClInclude Include="..\src\kiwano\renderer\ImageCache.h" />
<ClInclude Include="..\src\kiwano\renderer\render.h" /> <ClInclude Include="..\src\kiwano\renderer\render.h" />
<ClInclude Include="..\src\kiwano\renderer\TextRenderer.h" /> <ClInclude Include="..\src\kiwano\renderer\TextRenderer.h" />
<ClInclude Include="..\src\kiwano\third-party\StackWalker\StackWalker.h" /> <ClInclude Include="..\src\kiwano\third-party\StackWalker\StackWalker.h" />
@ -80,22 +82,21 @@
<ClInclude Include="..\src\kiwano\utils\DataUtil.h" /> <ClInclude Include="..\src\kiwano\utils\DataUtil.h" />
<ClInclude Include="..\src\kiwano\utils\FileUtil.h" /> <ClInclude Include="..\src\kiwano\utils\FileUtil.h" />
<ClInclude Include="..\src\kiwano\utils\Path.h" /> <ClInclude Include="..\src\kiwano\utils\Path.h" />
<ClInclude Include="..\src\kiwano\utils\ResLoader.h" /> <ClInclude Include="..\src\kiwano\utils\ResourceCache.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\src\kiwano\2d\Action.cpp" /> <ClCompile Include="..\src\kiwano\2d\action\Action.cpp" />
<ClCompile Include="..\src\kiwano\2d\ActionGroup.cpp" /> <ClCompile Include="..\src\kiwano\2d\action\ActionGroup.cpp" />
<ClCompile Include="..\src\kiwano\2d\ActionManager.cpp" /> <ClCompile Include="..\src\kiwano\2d\action\ActionManager.cpp" />
<ClCompile Include="..\src\kiwano\2d\ActionTween.cpp" /> <ClCompile Include="..\src\kiwano\2d\action\ActionTween.cpp" />
<ClCompile Include="..\src\kiwano\2d\Animation.cpp" /> <ClCompile Include="..\src\kiwano\2d\action\Animation.cpp" />
<ClCompile Include="..\src\kiwano\2d\Canvas.cpp" /> <ClCompile Include="..\src\kiwano\2d\Canvas.cpp" />
<ClCompile Include="..\src\kiwano\2d\Color.cpp" /> <ClCompile Include="..\src\kiwano\2d\Color.cpp" />
<ClCompile Include="..\src\kiwano\2d\DebugNode.cpp" /> <ClCompile Include="..\src\kiwano\2d\DebugNode.cpp" />
<ClCompile Include="..\src\kiwano\2d\Frames.cpp" /> <ClCompile Include="..\src\kiwano\2d\Frame.cpp" />
<ClCompile Include="..\src\kiwano\2d\FrameSequence.cpp" />
<ClCompile Include="..\src\kiwano\2d\ShapeNode.cpp" /> <ClCompile Include="..\src\kiwano\2d\ShapeNode.cpp" />
<ClCompile Include="..\src\kiwano\2d\GifImage.cpp" />
<ClCompile Include="..\src\kiwano\2d\GifSprite.cpp" /> <ClCompile Include="..\src\kiwano\2d\GifSprite.cpp" />
<ClCompile Include="..\src\kiwano\2d\Image.cpp" />
<ClCompile Include="..\src\kiwano\2d\Layer.cpp" /> <ClCompile Include="..\src\kiwano\2d\Layer.cpp" />
<ClCompile Include="..\src\kiwano\2d\Actor.cpp" /> <ClCompile Include="..\src\kiwano\2d\Actor.cpp" />
<ClCompile Include="..\src\kiwano\2d\Stage.cpp" /> <ClCompile Include="..\src\kiwano\2d\Stage.cpp" />
@ -119,6 +120,9 @@
<ClCompile Include="..\src\kiwano\renderer\D2DDeviceResources.cpp" /> <ClCompile Include="..\src\kiwano\renderer\D2DDeviceResources.cpp" />
<ClCompile Include="..\src\kiwano\renderer\D3D10DeviceResources.cpp" /> <ClCompile Include="..\src\kiwano\renderer\D3D10DeviceResources.cpp" />
<ClCompile Include="..\src\kiwano\renderer\D3D11DeviceResources.cpp" /> <ClCompile Include="..\src\kiwano\renderer\D3D11DeviceResources.cpp" />
<ClCompile Include="..\src\kiwano\renderer\GifImage.cpp" />
<ClCompile Include="..\src\kiwano\renderer\Image.cpp" />
<ClCompile Include="..\src\kiwano\renderer\ImageCache.cpp" />
<ClCompile Include="..\src\kiwano\renderer\render.cpp" /> <ClCompile Include="..\src\kiwano\renderer\render.cpp" />
<ClCompile Include="..\src\kiwano\renderer\TextRenderer.cpp" /> <ClCompile Include="..\src\kiwano\renderer\TextRenderer.cpp" />
<ClCompile Include="..\src\kiwano\third-party\StackWalker\StackWalker.cpp" /> <ClCompile Include="..\src\kiwano\third-party\StackWalker\StackWalker.cpp" />
@ -128,7 +132,7 @@
<ClCompile Include="..\src\kiwano\utils\DataUtil.cpp" /> <ClCompile Include="..\src\kiwano\utils\DataUtil.cpp" />
<ClCompile Include="..\src\kiwano\utils\FileUtil.cpp" /> <ClCompile Include="..\src\kiwano\utils\FileUtil.cpp" />
<ClCompile Include="..\src\kiwano\utils\Path.cpp" /> <ClCompile Include="..\src\kiwano\utils\Path.cpp" />
<ClCompile Include="..\src\kiwano\utils\ResLoader.cpp" /> <ClCompile Include="..\src\kiwano\utils\ResourceCache.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">

View File

@ -34,6 +34,9 @@
<Filter Include="third-party\tinyxml2"> <Filter Include="third-party\tinyxml2">
<UniqueIdentifier>{0cae76f7-7016-4a45-bb26-a130fbce8024}</UniqueIdentifier> <UniqueIdentifier>{0cae76f7-7016-4a45-bb26-a130fbce8024}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="2d\action">
<UniqueIdentifier>{9314f30d-5742-48b6-94e5-e3b4284106f6}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\src\kiwano\ui\Button.h"> <ClInclude Include="..\src\kiwano\ui\Button.h">
@ -42,24 +45,6 @@
<ClInclude Include="..\src\kiwano\ui\Menu.h"> <ClInclude Include="..\src\kiwano\ui\Menu.h">
<Filter>ui</Filter> <Filter>ui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\kiwano\2d\Action.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\ActionGroup.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\ActionHelper.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\ActionManager.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\ActionTween.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\Animation.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\Canvas.h"> <ClInclude Include="..\src\kiwano\2d\Canvas.h">
<Filter>2d</Filter> <Filter>2d</Filter>
</ClInclude> </ClInclude>
@ -72,12 +57,6 @@
<ClInclude Include="..\src\kiwano\2d\Font.hpp"> <ClInclude Include="..\src\kiwano\2d\Font.hpp">
<Filter>2d</Filter> <Filter>2d</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\kiwano\2d\Frames.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\Image.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\include-forwards.h"> <ClInclude Include="..\src\kiwano\2d\include-forwards.h">
<Filter>2d</Filter> <Filter>2d</Filter>
</ClInclude> </ClInclude>
@ -186,9 +165,6 @@
<ClInclude Include="..\src\kiwano\utils\Path.h"> <ClInclude Include="..\src\kiwano\utils\Path.h">
<Filter>utils</Filter> <Filter>utils</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\kiwano\utils\ResLoader.h">
<Filter>utils</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\config.h" /> <ClInclude Include="..\src\kiwano\config.h" />
<ClInclude Include="..\src\kiwano\macros.h" /> <ClInclude Include="..\src\kiwano\macros.h" />
<ClInclude Include="..\src\kiwano\math\helper.h"> <ClInclude Include="..\src\kiwano\math\helper.h">
@ -222,9 +198,6 @@
<ClInclude Include="..\src\kiwano\base\AsyncTask.h"> <ClInclude Include="..\src\kiwano\base\AsyncTask.h">
<Filter>base</Filter> <Filter>base</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\kiwano\2d\GifImage.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\third-party\StackWalker\StackWalker.h"> <ClInclude Include="..\src\kiwano\third-party\StackWalker\StackWalker.h">
<Filter>third-party\StackWalker</Filter> <Filter>third-party\StackWalker</Filter>
</ClInclude> </ClInclude>
@ -270,6 +243,42 @@
<ClInclude Include="..\src\kiwano\2d\Stage.h"> <ClInclude Include="..\src\kiwano\2d\Stage.h">
<Filter>2d</Filter> <Filter>2d</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\kiwano\2d\Frame.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\renderer\GifImage.h">
<Filter>renderer</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\renderer\Image.h">
<Filter>renderer</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\action\Action.h">
<Filter>2d\action</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\action\ActionGroup.h">
<Filter>2d\action</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\action\ActionHelper.h">
<Filter>2d\action</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\action\ActionTween.h">
<Filter>2d\action</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\action\ActionManager.h">
<Filter>2d\action</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\action\Animation.h">
<Filter>2d\action</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\renderer\ImageCache.h">
<Filter>renderer</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\utils\ResourceCache.h">
<Filter>utils</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\FrameSequence.h">
<Filter>2d</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\src\kiwano\ui\Button.cpp"> <ClCompile Include="..\src\kiwano\ui\Button.cpp">
@ -278,21 +287,6 @@
<ClCompile Include="..\src\kiwano\ui\Menu.cpp"> <ClCompile Include="..\src\kiwano\ui\Menu.cpp">
<Filter>ui</Filter> <Filter>ui</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano\2d\Action.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\ActionGroup.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\ActionManager.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\ActionTween.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\Animation.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\Canvas.cpp"> <ClCompile Include="..\src\kiwano\2d\Canvas.cpp">
<Filter>2d</Filter> <Filter>2d</Filter>
</ClCompile> </ClCompile>
@ -302,12 +296,6 @@
<ClCompile Include="..\src\kiwano\2d\DebugNode.cpp"> <ClCompile Include="..\src\kiwano\2d\DebugNode.cpp">
<Filter>2d</Filter> <Filter>2d</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano\2d\Frames.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\Image.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\Layer.cpp"> <ClCompile Include="..\src\kiwano\2d\Layer.cpp">
<Filter>2d</Filter> <Filter>2d</Filter>
</ClCompile> </ClCompile>
@ -359,9 +347,6 @@
<ClCompile Include="..\src\kiwano\utils\Path.cpp"> <ClCompile Include="..\src\kiwano\utils\Path.cpp">
<Filter>utils</Filter> <Filter>utils</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano\utils\ResLoader.cpp">
<Filter>utils</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\base\Input.cpp"> <ClCompile Include="..\src\kiwano\base\Input.cpp">
<Filter>base</Filter> <Filter>base</Filter>
</ClCompile> </ClCompile>
@ -383,9 +368,6 @@
<ClCompile Include="..\src\kiwano\base\AsyncTask.cpp"> <ClCompile Include="..\src\kiwano\base\AsyncTask.cpp">
<Filter>base</Filter> <Filter>base</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano\2d\GifImage.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\third-party\StackWalker\StackWalker.cpp"> <ClCompile Include="..\src\kiwano\third-party\StackWalker\StackWalker.cpp">
<Filter>third-party\StackWalker</Filter> <Filter>third-party\StackWalker</Filter>
</ClCompile> </ClCompile>
@ -410,5 +392,38 @@
<ClCompile Include="..\src\kiwano\2d\Stage.cpp"> <ClCompile Include="..\src\kiwano\2d\Stage.cpp">
<Filter>2d</Filter> <Filter>2d</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano\2d\Frame.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\renderer\GifImage.cpp">
<Filter>renderer</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\renderer\Image.cpp">
<Filter>renderer</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\action\Action.cpp">
<Filter>2d\action</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\action\ActionGroup.cpp">
<Filter>2d\action</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\action\ActionTween.cpp">
<Filter>2d\action</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\action\ActionManager.cpp">
<Filter>2d\action</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\action\Animation.cpp">
<Filter>2d\action</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\renderer\ImageCache.cpp">
<Filter>renderer</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\utils\ResourceCache.cpp">
<Filter>utils</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\2d\FrameSequence.cpp">
<Filter>2d</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -19,7 +19,6 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "Actor.h" #include "Actor.h"
#include "Action.h"
#include "Stage.h" #include "Stage.h"
#include "../base/logs.h" #include "../base/logs.h"
#include "../renderer/render.h" #include "../renderer/render.h"
@ -658,11 +657,11 @@ namespace kiwano
} }
void VisualNode::PrepareRender() void VisualActor::PrepareRender()
{ {
auto renderer = Renderer::Instance(); auto renderer = Renderer::Instance();
renderer->SetTransform(transform_matrix_); renderer->SetTransform(transform_matrix_);
renderer->SetOpacity(displayed_opacity_); renderer->SetOpacity(displayed_opacity_);
} }
} }

View File

@ -21,7 +21,7 @@
#pragma once #pragma once
#include "include-forwards.h" #include "include-forwards.h"
#include "Transform.hpp" #include "Transform.hpp"
#include "ActionManager.h" #include "action/ActionManager.h"
#include "../base/TimerManager.h" #include "../base/TimerManager.h"
#include "../base/EventDispatcher.h" #include "../base/EventDispatcher.h"
@ -77,7 +77,7 @@ namespace kiwano
// »ñÈ¡ y ×ø±ê // »ñÈ¡ y ×ø±ê
float GetPositionY() const { return transform_.position.y; } float GetPositionY() const { return transform_.position.y; }
// 获取横向缩放比例 // 获取缩放比例
Point GetScale() const { return transform_.scale; } Point GetScale() const { return transform_.scale; }
// »ñÈ¡ºáÏòËõ·Å±ÈÀý // »ñÈ¡ºáÏòËõ·Å±ÈÀý
@ -452,8 +452,9 @@ namespace kiwano
}; };
// 可视化角色 // 可视角色
class KGE_API VisualNode // 在渲染前处理二维旋转矩阵和透明度
class KGE_API VisualActor
: public Actor : public Actor
{ {
public: public:

View File

@ -19,7 +19,6 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "Canvas.h" #include "Canvas.h"
#include "Image.h"
#include "../base/logs.h" #include "../base/logs.h"
#include "../renderer/render.h" #include "../renderer/render.h"
@ -258,7 +257,7 @@ namespace kiwano
D2D1::RectF(0, 0, image->GetWidth(), image->GetHeight()), D2D1::RectF(0, 0, image->GetWidth(), image->GetHeight()),
opacity, opacity,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
DX::ConvertToRectF(image->GetCropRect()) D2D1::RectF(0, 0, image->GetWidth(), image->GetHeight())
); );
cache_expired_ = true; cache_expired_ = true;
} }
@ -463,8 +462,7 @@ namespace kiwano
ImagePtr Canvas::ExportToImage() const ImagePtr Canvas::ExportToImage() const
{ {
auto image = new Image(GetBitmap()); ImagePtr image = new Image(GetBitmap());
image->Crop(Rect(Point{}, this->GetSize()));
return image; return image;
} }

View File

@ -22,15 +22,18 @@
#include "Actor.h" #include "Actor.h"
#include "Font.hpp" #include "Font.hpp"
#include "TextStyle.hpp" #include "TextStyle.hpp"
#include "../renderer/Image.h"
#include "../renderer/TextRenderer.h" #include "../renderer/TextRenderer.h"
#undef DrawText #ifdef DrawText
# undef DrawText
#endif
namespace kiwano namespace kiwano
{ {
// »­²¼ // »­²¼
class KGE_API Canvas class KGE_API Canvas
: public VisualNode : public VisualActor
{ {
public: public:
Canvas(); Canvas();

View File

@ -24,7 +24,7 @@
namespace kiwano namespace kiwano
{ {
class KGE_API DebugNode class KGE_API DebugNode
: public VisualNode : public VisualActor
{ {
public: public:
DebugNode(); DebugNode();

73
src/kiwano/2d/Frame.cpp Normal file
View File

@ -0,0 +1,73 @@
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Frame.h"
#include "../renderer/ImageCache.h"
namespace kiwano
{
Frame::Frame()
{
}
Frame::Frame(Resource const& res)
{
Load(res);
}
Frame::Frame(ImagePtr image)
: image_(image)
{
}
bool Frame::Load(Resource const& res)
{
ImagePtr image = ImageCache::Instance()->AddImage(res);
if (image && image->IsValid())
{
SetImage(image);
return true;
}
return false;
}
void Frame::Crop(Rect const& crop_rect)
{
if (image_)
{
auto bitmap_size = image_->GetSize();
crop_rect_.origin.x = std::min(std::max(crop_rect.origin.x, 0.f), bitmap_size.x);
crop_rect_.origin.y = std::min(std::max(crop_rect.origin.y, 0.f), bitmap_size.y);
crop_rect_.size.x = std::min(std::max(crop_rect.size.x, 0.f), bitmap_size.x - crop_rect.origin.x);
crop_rect_.size.y = std::min(std::max(crop_rect.size.y, 0.f), bitmap_size.y - crop_rect.origin.y);
}
}
void Frame::SetImage(ImagePtr image)
{
image_ = image;
if (image_)
{
crop_rect_.origin.x = crop_rect_.origin.y = 0;
crop_rect_.size.x = image_->GetWidth();
crop_rect_.size.y = image_->GetHeight();
}
}
}

75
src/kiwano/2d/Frame.h Normal file
View File

@ -0,0 +1,75 @@
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include "../renderer/Image.h"
namespace kiwano
{
// ֡ͼÏñ
class KGE_API Frame
: public Object
{
public:
Frame();
explicit Frame(
Resource const& res
);
explicit Frame(
ImagePtr image
);
bool Load(
Resource const& res
);
// ²Ã¼ô¾ØÐÎ
void Crop(
Rect const& crop_rect /* ²Ã¼ô¾ØÐÎ */
);
// »ñÈ¡¿í¶È
float GetWidth() const { return crop_rect_.size.x; }
// »ñÈ¡¸ß¶È
float GetHeight() const { return crop_rect_.size.y; }
// »ñÈ¡´óС
Size GetSize() const { return crop_rect_.size; }
// »ñÈ¡²Ã¼ôλÖÃ
Point GetCropPoint() const { return crop_rect_.origin; }
// »ñÈ¡²Ã¼ô¾ØÐÎ
inline Rect const& GetCropRect() const { return crop_rect_; }
// »ñȡλͼ
inline ImagePtr GetImage() const { return image_; }
// ÉèÖÃλͼ
void SetImage(ImagePtr image);
protected:
ImagePtr image_;
Rect crop_rect_;
};
}

View File

@ -18,28 +18,28 @@
// 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 "Frames.h" #include "FrameSequence.h"
#include "Image.h" #include "Frame.h"
#include "../base/logs.h" #include "../base/logs.h"
namespace kiwano namespace kiwano
{ {
Frames::Frames() FrameSequence::FrameSequence()
{ {
} }
Frames::Frames(Array<ImagePtr> const& frames) FrameSequence::FrameSequence(Array<FramePtr> const& frames)
{ {
this->Add(frames); this->AddFrames(frames);
} }
Frames::~Frames() FrameSequence::~FrameSequence()
{ {
} }
void Frames::Add(ImagePtr frame) void FrameSequence::AddFrame(FramePtr frame)
{ {
KGE_ASSERT(frame && "Frames::Add failed, NULL pointer exception"); KGE_ASSERT(frame && "FrameSequence::Add failed, NULL pointer exception");
if (frame) if (frame)
{ {
@ -47,7 +47,7 @@ namespace kiwano
} }
} }
void Frames::Add(Array<ImagePtr> const& frames) void FrameSequence::AddFrames(Array<FramePtr> const& frames)
{ {
if (frames_.empty()) if (frames_.empty())
frames_ = frames; frames_ = frames;
@ -55,40 +55,43 @@ namespace kiwano
{ {
frames_.reserve(frames_.size() + frames.size()); frames_.reserve(frames_.size() + frames.size());
for (const auto& image : frames) for (const auto& image : frames)
Add(image); AddFrame(image);
} }
} }
Array<ImagePtr> const& Frames::GetFrames() const FramePtr FrameSequence::GetFrame(size_t index) const
{
KGE_ASSERT(index < frames_.size());
return frames_[index];
}
Array<FramePtr> const& FrameSequence::GetFrames() const
{ {
return frames_; return frames_;
} }
FramesPtr Frames::Clone() const FrameSequencePtr FrameSequence::Clone() const
{ {
auto animation = new (std::nothrow) Frames; auto frame_seq = new (std::nothrow) FrameSequence;
if (animation) if (frame_seq)
{ {
for (const auto& frame : frames_) frame_seq->AddFrames(frames_);
{
animation->Add(frame);
}
} }
return animation; return frame_seq;
} }
FramesPtr Frames::Reverse() const FrameSequencePtr FrameSequence::Reverse() const
{ {
auto animation = new (std::nothrow) Frames; auto frame_seq = new (std::nothrow) FrameSequence;
if (!frames_.empty()) if (!frames_.empty())
{ {
for (auto iter = frames_.crbegin(), crend = frames_.crend(); iter != crend; ++iter) for (auto iter = frames_.crbegin(), crend = frames_.crend(); iter != crend; ++iter)
{ {
if (*iter) if (*iter)
animation->Add(*iter); frame_seq->AddFrame(*iter);
} }
} }
return animation; return frame_seq;
} }
} }

View File

@ -24,38 +24,41 @@
namespace kiwano namespace kiwano
{ {
// 序列帧 // 序列帧
class KGE_API Frames class KGE_API FrameSequence
: public Object : public Object
{ {
public: public:
Frames(); FrameSequence();
explicit Frames( explicit FrameSequence(
Array<ImagePtr> const& frames /* ÐòÁÐÖ¡ */ Array<FramePtr> const& frames /* 帧序列 */
); );
virtual ~Frames(); virtual ~FrameSequence();
// 添加关键帧 // 添加关键帧
void Add( void AddFrame(
ImagePtr frame FramePtr frame
); );
// 添加多个关键帧 // 添加多个关键帧
void Add( void AddFrames(
Array<ImagePtr> const& frames Array<FramePtr> const& frames
); );
// 获取关键帧 // 获取关键帧
Array<ImagePtr> const& GetFrames() const; FramePtr GetFrame(size_t index) const;
// 获取关键帧
Array<FramePtr> const& GetFrames() const;
// 获取帧动画的拷贝对象 // 获取帧动画的拷贝对象
FramesPtr Clone() const; FrameSequencePtr Clone() const;
// 获取帧动画的倒转 // 获取帧动画的倒转
FramesPtr Reverse() const; FrameSequencePtr Reverse() const;
protected: protected:
Array<ImagePtr> frames_; Array<FramePtr> frames_;
}; };
} }

View File

@ -19,7 +19,6 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "GifSprite.h" #include "GifSprite.h"
#include "GifImage.h"
#include "../base/logs.h" #include "../base/logs.h"
#include "../platform/modules.h" #include "../platform/modules.h"
@ -87,7 +86,7 @@ namespace kiwano
void GifSprite::Update(Duration dt) void GifSprite::Update(Duration dt)
{ {
VisualNode::Update(dt); VisualActor::Update(dt);
if (image_ && animating_) if (image_ && animating_)
{ {

View File

@ -22,11 +22,12 @@
#include "Actor.h" #include "Actor.h"
#include "../base/Resource.h" #include "../base/Resource.h"
#include "../renderer/render.h" #include "../renderer/render.h"
#include "../renderer/GifImage.h"
namespace kiwano namespace kiwano
{ {
class KGE_API GifSprite class KGE_API GifSprite
: public VisualNode : public VisualActor
{ {
public: public:
using LoopDoneCallback = Closure<void(int)>; using LoopDoneCallback = Closure<void(int)>;

View File

@ -1,184 +0,0 @@
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Image.h"
#include "../base/logs.h"
#include "../platform/modules.h"
#include "../utils/FileUtil.h"
namespace kiwano
{
Image::Image()
: bitmap_(nullptr)
, crop_rect_()
{
}
Image::Image(Resource const& res)
: Image()
{
this->Load(res);
}
Image::Image(Resource const& res, Rect const& crop_rect)
: Image()
{
this->Load(res);
this->Crop(crop_rect);
}
Image::Image(ComPtr<ID2D1Bitmap> const & bitmap)
: Image()
{
SetBitmap(bitmap);
}
Image::~Image()
{
}
bool Image::Load(Resource const& res)
{
HRESULT hr = S_OK;
ComPtr<ID2D1Bitmap> bitmap;
if (res.IsFileType())
{
#if defined(KGE_DEBUG)
if (!FileUtil::ExistsFile(res.GetFileName()))
{
KGE_WARNING_LOG(L"Image file '%s' not found!", res.GetFileName().c_str());
return false;
}
#endif
hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromFile(bitmap, res.GetFileName());
}
else
{
hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromResource(bitmap, res);
}
if (FAILED(hr))
{
KGE_ERROR_LOG(L"Load image file failed with HRESULT of %08X", hr);
return false;
}
SetBitmap(bitmap);
return true;
}
bool Image::IsValid() const
{
return !!bitmap_;
}
void Image::Crop(Rect const& crop_rect)
{
if (bitmap_)
{
auto bitmap_size = bitmap_->GetSize();
crop_rect_.origin.x = std::min(std::max(crop_rect.origin.x, 0.f), bitmap_size.width);
crop_rect_.origin.y = std::min(std::max(crop_rect.origin.y, 0.f), bitmap_size.height);
crop_rect_.size.x = std::min(std::max(crop_rect.size.x, 0.f), bitmap_size.width - crop_rect.origin.x);
crop_rect_.size.y = std::min(std::max(crop_rect.size.y, 0.f), bitmap_size.height - crop_rect.origin.y);
}
}
float Image::GetWidth() const
{
return crop_rect_.size.x;
}
float Image::GetHeight() const
{
return crop_rect_.size.y;
}
Size Image::GetSize() const
{
return crop_rect_.size;
}
float Image::GetSourceWidth() const
{
if (bitmap_)
{
return bitmap_->GetSize().width;
}
return 0;
}
float Image::GetSourceHeight() const
{
if (bitmap_)
{
return bitmap_->GetSize().height;
}
return 0;
}
Size Image::GetSourceSize() const
{
if (bitmap_)
{
auto bitmap_size = bitmap_->GetSize();
return Size{ bitmap_size.width, bitmap_size.height };
}
return Size{};
}
float Image::GetCropX() const
{
return crop_rect_.origin.x;
}
float Image::GetCropY() const
{
return crop_rect_.origin.y;
}
Point Image::GetCropPos() const
{
return crop_rect_.origin;
}
Rect Image::GetCropRect() const
{
return crop_rect_;
}
ComPtr<ID2D1Bitmap> const& Image::GetBitmap() const
{
return bitmap_;
}
void Image::SetBitmap(ComPtr<ID2D1Bitmap> const & bitmap)
{
if (bitmap)
{
bitmap_ = bitmap;
crop_rect_.origin.x = crop_rect_.origin.y = 0;
crop_rect_.size.x = bitmap_->GetSize().width;
crop_rect_.size.y = bitmap_->GetSize().height;
}
}
}

View File

@ -26,7 +26,7 @@ namespace kiwano
{ {
// śţÎŹÍźĐνÇÉŤ // śţÎŹÍźĐνÇÉŤ
class KGE_API ShapeNode class KGE_API ShapeNode
: public VisualNode : public VisualActor
{ {
public: public:
ShapeNode(); ShapeNode();

View File

@ -24,74 +24,70 @@
namespace kiwano namespace kiwano
{ {
Sprite::Sprite() Sprite::Sprite()
: image_(nullptr) : frame_(nullptr)
{ {
} }
Sprite::Sprite(ImagePtr image)
: image_(nullptr)
{
Load(image);
}
Sprite::Sprite(Resource const& res) Sprite::Sprite(Resource const& res)
: image_(nullptr) : frame_(nullptr)
{ {
Load(res); Load(res);
} }
Sprite::Sprite(Resource const& res, const Rect& crop_rect) Sprite::Sprite(Resource const& res, const Rect& crop_rect)
: image_(nullptr) : frame_(nullptr)
{ {
Load(res); Load(res);
Crop(crop_rect); Crop(crop_rect);
} }
Sprite::Sprite(FramePtr frame)
: frame_(nullptr)
{
SetFrame(frame);
}
Sprite::~Sprite() Sprite::~Sprite()
{ {
} }
bool Sprite::Load(ImagePtr image)
{
if (image && image_ != image)
{
image_ = image;
Actor::SetSize(image_->GetWidth(), image_->GetHeight());
return true;
}
return false;
}
bool Sprite::Load(Resource const& res) bool Sprite::Load(Resource const& res)
{ {
ImagePtr image = new (std::nothrow) Image; FramePtr frame = new (std::nothrow) Frame;
if (image->Load(res)) if (frame->Load(res))
{ {
return Load(image); SetFrame(frame);
return true;
} }
return false; return false;
} }
void Sprite::Crop(const Rect& crop_rect) void Sprite::Crop(const Rect& crop_rect)
{ {
image_->Crop(crop_rect); if (frame_)
Actor::SetSize( {
std::min(std::max(crop_rect.size.x, 0.f), image_->GetSourceWidth() - image_->GetCropX()), frame_->Crop(crop_rect);
std::min(std::max(crop_rect.size.y, 0.f), image_->GetSourceHeight() - image_->GetCropY()) SetSize(frame_->GetWidth(), frame_->GetHeight());
); }
} }
ImagePtr Sprite::GetImage() const void Sprite::SetFrame(FramePtr frame)
{ {
return image_; if (frame_ != frame)
{
frame_ = frame;
if (frame_)
{
SetSize(frame_->GetWidth(), frame_->GetHeight());
}
}
} }
void Sprite::OnRender() void Sprite::OnRender()
{ {
if (image_) if (frame_)
{ {
Renderer::Instance()->DrawImage(image_, GetBounds()); Renderer::Instance()->DrawBitmap(frame_->GetImage()->GetBitmap(), frame_->GetCropRect(), GetBounds());
} }
} }
} }

View File

@ -20,54 +20,52 @@
#pragma once #pragma once
#include "Actor.h" #include "Actor.h"
#include "Image.h" #include "Frame.h"
namespace kiwano namespace kiwano
{ {
// ¾«Áé // ¾«Áé
class KGE_API Sprite class KGE_API Sprite
: public VisualNode : public VisualActor
{ {
public: public:
Sprite(); Sprite();
explicit Sprite(
ImagePtr image
);
explicit Sprite( explicit Sprite(
Resource const& res Resource const& res
); );
explicit Sprite( explicit Sprite(
Resource const& res, Resource const& res,
const Rect& crop_rect /* 裁剪矩形 */ Rect const& crop_rect /* ²Ã¼ô¾ØÐÎ */
);
explicit Sprite(
FramePtr frame
); );
virtual ~Sprite(); virtual ~Sprite();
// 加载图片文件 // ¼ÓÔØÍ¼Ïñ×ÊÔ´
bool Load( bool Load(
Resource const& res Resource const& res
); );
// 加载图片 // ²Ã¼ô¾ØÐÎ
bool Load(
ImagePtr image
);
// 将图片裁剪为矩形
void Crop( void Crop(
const Rect& crop_rect /* 裁剪矩形 */ const Rect& crop_rect
); );
// 获取 Image 对象 // »ñȡ֡ͼÏñ
ImagePtr GetImage() const; inline FramePtr GetFrame() const { return frame_; }
// ÉèÖÃ֡ͼÏñ
void SetFrame(FramePtr frame);
// äÖȾ¾«Áé // äÖȾ¾«Áé
void OnRender() override; void OnRender() override;
protected: protected:
ImagePtr image_; FramePtr frame_;
}; };
} }

View File

@ -28,7 +28,7 @@ namespace kiwano
{ {
// 文本 // 文本
class KGE_API Text class KGE_API Text
: public VisualNode : public VisualActor
{ {
public: public:
Text(); Text();

View File

@ -19,7 +19,7 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "Action.h" #include "Action.h"
#include "Actor.h" #include "../Actor.h"
namespace kiwano namespace kiwano
{ {

View File

@ -19,7 +19,7 @@
// THE SOFTWARE. // THE SOFTWARE.
#pragma once #pragma once
#include "include-forwards.h" #include "../include-forwards.h"
namespace kiwano namespace kiwano
{ {

View File

@ -19,8 +19,8 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "ActionGroup.h" #include "ActionGroup.h"
#include "Actor.h" #include "../Actor.h"
#include "../base/logs.h" #include "../../base/logs.h"
namespace kiwano namespace kiwano
{ {
@ -145,4 +145,4 @@ namespace kiwano
return group; return group;
} }
} }

View File

@ -19,8 +19,8 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "ActionManager.h" #include "ActionManager.h"
#include "Actor.h" #include "../Actor.h"
#include "../base/logs.h" #include "../../base/logs.h"
namespace kiwano namespace kiwano
{ {

View File

@ -19,8 +19,7 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "ActionTween.h" #include "ActionTween.h"
#include "include-forwards.h" #include "../Actor.h"
#include "Actor.h"
namespace kiwano namespace kiwano
{ {

View File

@ -20,8 +20,8 @@
#pragma once #pragma once
#include "Action.h" #include "Action.h"
#include "../base/logs.h" #include "../../base/logs.h"
#include "../renderer/render.h" // ID2D1PathGeometry, ID2D1GeometrySink #include "../../renderer/render.h" // ID2D1PathGeometry, ID2D1GeometrySink
namespace kiwano namespace kiwano
{ {

View File

@ -19,50 +19,49 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "Animation.h" #include "Animation.h"
#include "Frames.h" #include "../FrameSequence.h"
#include "Image.h" #include "../Sprite.h"
#include "Sprite.h"
namespace kiwano namespace kiwano
{ {
Animation::Animation() Animation::Animation()
: frames_(nullptr) : frame_seq_(nullptr)
{ {
} }
Animation::Animation(Duration duration, FramesPtr animation, EaseFunc func) Animation::Animation(Duration duration, FrameSequencePtr frame_seq, EaseFunc func)
: ActionTween(duration, func) : ActionTween(duration, func)
, frames_(nullptr) , frame_seq_(nullptr)
{ {
this->SetFrames(animation); this->SetFrameSequence(frame_seq);
} }
Animation::~Animation() Animation::~Animation()
{ {
} }
FramesPtr Animation::GetFrames() const FrameSequencePtr Animation::GetFrameSequence() const
{ {
return frames_; return frame_seq_;
} }
void Animation::SetFrames(FramesPtr frames) void Animation::SetFrameSequence(FrameSequencePtr frames)
{ {
frames_ = frames; frame_seq_ = frames;
} }
void Animation::Init(ActorPtr target) void Animation::Init(ActorPtr target)
{ {
if (!frames_ || frames_->GetFrames().empty()) if (!frame_seq_ || frame_seq_->GetFrames().empty())
{ {
Done(); Done();
return; return;
} }
auto sprite_target = dynamic_cast<Sprite*>(target.Get()); auto sprite_target = dynamic_cast<Sprite*>(target.Get());
if (sprite_target && frames_) if (sprite_target && frame_seq_)
{ {
sprite_target->Load(frames_->GetFrames()[0]); sprite_target->SetFrame(frame_seq_->GetFrames()[0]);
} }
} }
@ -72,27 +71,27 @@ namespace kiwano
KGE_ASSERT(sprite_target && "Animation only supports Sprites"); KGE_ASSERT(sprite_target && "Animation only supports Sprites");
const auto& frames = frames_->GetFrames(); const auto& frames = frame_seq_->GetFrames();
auto size = frames.size(); auto size = frames.size();
auto index = std::min(static_cast<size_t>(math::Floor(size * percent)), size - 1); auto index = std::min(static_cast<size_t>(math::Floor(size * percent)), size - 1);
sprite_target->Load(frames[index]); sprite_target->SetFrame(frames[index]);
} }
ActionPtr Animation::Clone() const ActionPtr Animation::Clone() const
{ {
if (frames_) if (frame_seq_)
{ {
return new (std::nothrow) Animation(dur_, frames_, ease_func_); return new (std::nothrow) Animation(dur_, frame_seq_, ease_func_);
} }
return nullptr; return nullptr;
} }
ActionPtr Animation::Reverse() const ActionPtr Animation::Reverse() const
{ {
if (frames_) if (frame_seq_)
{ {
FramesPtr frames = frames_->Reverse(); FrameSequencePtr frames = frame_seq_->Reverse();
if (frames) if (frames)
{ {
return new (std::nothrow) Animation(dur_, frames, ease_func_); return new (std::nothrow) Animation(dur_, frames, ease_func_);
@ -101,4 +100,4 @@ namespace kiwano
return nullptr; return nullptr;
} }
} }

View File

@ -32,18 +32,18 @@ namespace kiwano
Animation( Animation(
Duration duration, /* 动画时长 */ Duration duration, /* 动画时长 */
FramesPtr frames, /* ÐòÁÐÖ¡ */ FrameSequencePtr frame_seq, /* ÐòÁÐÖ¡ */
EaseFunc func = nullptr /* 速度变化 */ EaseFunc func = nullptr /* 速度变化 */
); );
virtual ~Animation(); virtual ~Animation();
// 获取动画 // 获取动画
FramesPtr GetFrames() const; FrameSequencePtr GetFrameSequence() const;
// 设置动画 // 设置动画
void SetFrames( void SetFrameSequence(
FramesPtr frames FrameSequencePtr frames
); );
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
@ -58,6 +58,6 @@ namespace kiwano
void UpdateTween(ActorPtr target, float percent) override; void UpdateTween(ActorPtr target, float percent) override;
protected: protected:
FramesPtr frames_; FrameSequencePtr frame_seq_;
}; };
} }

View File

@ -34,9 +34,8 @@
namespace kiwano namespace kiwano
{ {
KGE_DECLARE_SMART_PTR(Image); KGE_DECLARE_SMART_PTR(Frame);
KGE_DECLARE_SMART_PTR(GifImage); KGE_DECLARE_SMART_PTR(FrameSequence);
KGE_DECLARE_SMART_PTR(Frames);
KGE_DECLARE_SMART_PTR(Actor); KGE_DECLARE_SMART_PTR(Actor);
KGE_DECLARE_SMART_PTR(Stage); KGE_DECLARE_SMART_PTR(Stage);

View File

@ -62,7 +62,7 @@ namespace kiwano
size_t Resource::GetHashCode() const size_t Resource::GetHashCode() const
{ {
if (type_ == Type::File) if (type_ == Type::File)
return std::hash<String>{}(GetFileName()); return GetFileName().hash();
return std::hash<LPCWSTR>{}(bin_name_); return std::hash<LPCWSTR>{}(bin_name_);
} }
@ -133,4 +133,4 @@ namespace kiwano
} }
return true; return true;
} }
} }

View File

@ -18,8 +18,8 @@
// 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.
// //
// Website: https://www.kiwano.cn // Website: https://www.kiwanoengine.com
// Source Code: https://github.com/kiwano/kiwano // Source Code: https://github.com/KiwanoEngine/Kiwano
// //
@ -60,14 +60,15 @@
// base // base
// //
#include "renderer/render.h"
#include "renderer/Image.h"
#include "renderer/GifImage.h"
#include "base/time.h" #include "base/time.h"
#include "base/window.h" #include "base/window.h"
#include "base/input.h" #include "base/input.h"
#include "base/Director.h" #include "base/Director.h"
#include "base/logs.h" #include "base/logs.h"
#include "renderer/render.h"
#include "platform/modules.h"
#include "platform/Application.h"
#include "base/Object.h" #include "base/Object.h"
#include "base/Event.hpp" #include "base/Event.hpp"
@ -83,15 +84,16 @@
#include "2d/Transform.hpp" #include "2d/Transform.hpp"
#include "2d/TextStyle.hpp" #include "2d/TextStyle.hpp"
#include "2d/Image.h" #include "2d/Frame.h"
#include "2d/GifImage.h" #include "2d/FrameSequence.h"
#include "2d/Frames.h"
#include "2d/Action.h" #include "2d/action/Action.h"
#include "2d/ActionGroup.h" #include "2d/action/ActionGroup.h"
#include "2d/ActionTween.h" #include "2d/action/ActionTween.h"
#include "2d/ActionHelper.h" #include "2d/action/Animation.h"
#include "2d/Animation.h" #include "2d/action/ActionHelper.h"
#include "2d/ActionManager.h" #include "2d/action/ActionManager.h"
#include "2d/Transition.h" #include "2d/Transition.h"
#include "2d/Actor.h" #include "2d/Actor.h"
@ -104,6 +106,8 @@
#include "2d/ShapeNode.h" #include "2d/ShapeNode.h"
#include "2d/DebugNode.h" #include "2d/DebugNode.h"
#include "platform/modules.h"
#include "platform/Application.h"
// //
// utils // utils
@ -112,7 +116,7 @@
#include "utils/Path.h" #include "utils/Path.h"
#include "utils/DataUtil.h" #include "utils/DataUtil.h"
#include "utils/FileUtil.h" #include "utils/FileUtil.h"
#include "utils/ResLoader.h" #include "utils/ResourceCache.h"
// //

View File

@ -33,7 +33,7 @@ namespace kiwano
value_type x; value_type x;
value_type y; value_type y;
Vec2T() : x(0.f), y(0.f) {} Vec2T() : x(value_type(0)), y(value_type(0)) {}
Vec2T(value_type x, value_type y) : x(x), y(y) {} Vec2T(value_type x, value_type y) : x(x), y(y) {}
@ -41,7 +41,7 @@ namespace kiwano
inline value_type Length() const inline value_type Length() const
{ {
return math::Sqrt(x * x + y * y); return static_cast<value_type>(math::Sqrt(static_cast<float>(x * x + y * y)));
} }
inline bool IsOrigin() const inline bool IsOrigin() const

View File

@ -19,9 +19,9 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "D2DDeviceResources.h" #include "D2DDeviceResources.h"
#include "../2d/Image.h" #include "ImageCache.h"
#include "../base/logs.h" #include "../base/logs.h"
#include "../platform/modules.h" #include "../utils/FileUtil.h"
#pragma comment(lib, "d2d1.lib") #pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib") #pragma comment(lib, "dwrite.lib")
@ -73,9 +73,7 @@ namespace kiwano
ID2D1StrokeStyle* GetStrokeStyle(StrokeStyle stroke) const override; ID2D1StrokeStyle* GetStrokeStyle(StrokeStyle stroke) const override;
void ClearImageCache() override; void DiscardResources() override;
void DiscardResources() override;
public: public:
unsigned long STDMETHODCALLTYPE AddRef(); unsigned long STDMETHODCALLTYPE AddRef();
@ -91,9 +89,6 @@ namespace kiwano
unsigned long ref_count_; unsigned long ref_count_;
float dpi_; float dpi_;
using BitmapMap = UnorderedMap<size_t, ComPtr<ID2D1Bitmap>>;
BitmapMap bitmap_cache_;
ComPtr<ID2D1StrokeStyle> d2d_miter_stroke_style_; ComPtr<ID2D1StrokeStyle> d2d_miter_stroke_style_;
ComPtr<ID2D1StrokeStyle> d2d_bevel_stroke_style_; ComPtr<ID2D1StrokeStyle> d2d_bevel_stroke_style_;
ComPtr<ID2D1StrokeStyle> d2d_round_stroke_style_; ComPtr<ID2D1StrokeStyle> d2d_round_stroke_style_;
@ -179,7 +174,7 @@ namespace kiwano
void D2DDeviceResources::DiscardResources() void D2DDeviceResources::DiscardResources()
{ {
ClearImageCache(); ImageCache::Instance()->Clear();
factory_.Reset(); factory_.Reset();
device_.Reset(); device_.Reset();
@ -328,11 +323,10 @@ namespace kiwano
if (!imaging_factory_ || !device_context_) if (!imaging_factory_ || !device_context_)
return E_UNEXPECTED; return E_UNEXPECTED;
size_t hash_code = std::hash<String>{}(file_path); if (!FileUtil::ExistsFile(file_path))
if (bitmap_cache_.find(hash_code) != bitmap_cache_.end())
{ {
bitmap = bitmap_cache_[hash_code]; KGE_WARNING_LOG(L"Image file '%s' not found!", file_path.c_str());
return S_OK; return E_FAIL;
} }
ComPtr<IWICBitmapDecoder> decoder; ComPtr<IWICBitmapDecoder> decoder;
@ -384,9 +378,7 @@ namespace kiwano
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
bitmap = bitmap_tmp; bitmap = bitmap_tmp;
bitmap_cache_.insert(std::make_pair(hash_code, bitmap));
} }
return hr; return hr;
} }
@ -395,13 +387,6 @@ namespace kiwano
if (!imaging_factory_ || !device_context_) if (!imaging_factory_ || !device_context_)
return E_UNEXPECTED; return E_UNEXPECTED;
size_t hash_code = res.GetHashCode();
if (bitmap_cache_.find(hash_code) != bitmap_cache_.end())
{
bitmap = bitmap_cache_[hash_code];
return S_OK;
}
ComPtr<IWICBitmapDecoder> decoder; ComPtr<IWICBitmapDecoder> decoder;
ComPtr<IWICBitmapFrameDecode> source; ComPtr<IWICBitmapFrameDecode> source;
ComPtr<IWICStream> stream; ComPtr<IWICStream> stream;
@ -471,9 +456,7 @@ namespace kiwano
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
bitmap = bitmap_tmp; bitmap = bitmap_tmp;
bitmap_cache_.insert(std::make_pair(hash_code, bitmap));
} }
return hr; return hr;
} }
@ -599,11 +582,6 @@ namespace kiwano
return hr; return hr;
} }
void D2DDeviceResources::ClearImageCache()
{
bitmap_cache_.clear();
}
ID2D1StrokeStyle* D2DDeviceResources::GetStrokeStyle(StrokeStyle stroke) const ID2D1StrokeStyle* D2DDeviceResources::GetStrokeStyle(StrokeStyle stroke) const
{ {
switch (stroke) switch (stroke)
@ -615,4 +593,4 @@ namespace kiwano
return nullptr; return nullptr;
} }
} }

View File

@ -70,8 +70,6 @@ namespace kiwano
_In_ ComPtr<ID2D1Bitmap1> const& target _In_ ComPtr<ID2D1Bitmap1> const& target
) = 0; ) = 0;
virtual void ClearImageCache() = 0;
virtual void DiscardResources() = 0; virtual void DiscardResources() = 0;
inline ID2D1Factory1* GetFactory() const { KGE_ASSERT(factory_); return factory_.Get(); } inline ID2D1Factory1* GetFactory() const { KGE_ASSERT(factory_); return factory_.Get(); }

View File

@ -20,7 +20,6 @@
#include "D3D10DeviceResources.h" #include "D3D10DeviceResources.h"
#include "../2d/Image.h"
#include "../base/logs.h" #include "../base/logs.h"
#pragma comment(lib, "d3d10_1.lib") #pragma comment(lib, "d3d10_1.lib")

View File

@ -20,7 +20,6 @@
#include "D3D11DeviceResources.h" #include "D3D11DeviceResources.h"
#include "../2d/Image.h"
#include "../base/logs.h" #include "../base/logs.h"
#include <versionhelpers.h> // IsWindows10OrGreater #include <versionhelpers.h> // IsWindows10OrGreater

View File

@ -20,7 +20,6 @@
#include "GifImage.h" #include "GifImage.h"
#include "../base/logs.h" #include "../base/logs.h"
#include "../platform/modules.h"
#include "../utils/FileUtil.h" #include "../utils/FileUtil.h"
namespace kiwano namespace kiwano

View File

@ -19,12 +19,13 @@
// THE SOFTWARE. // THE SOFTWARE.
#pragma once #pragma once
#include "include-forwards.h"
#include "../base/Resource.h" #include "../base/Resource.h"
#include "../renderer/render.h" #include "render.h"
namespace kiwano namespace kiwano
{ {
// GIF ͼÏñ
KGE_DECLARE_SMART_PTR(GifImage);
class KGE_API GifImage class KGE_API GifImage
: public Object : public Object
{ {

View File

@ -0,0 +1,113 @@
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Image.h"
#include "../base/logs.h"
#include "../platform/modules.h"
namespace kiwano
{
Image::Image()
: bitmap_(nullptr)
{
}
Image::Image(ComPtr<ID2D1Bitmap> const & bitmap)
: Image()
{
SetBitmap(bitmap);
}
Image::~Image()
{
}
bool Image::IsValid() const
{
return !!bitmap_;
}
float Image::GetWidth() const
{
if (bitmap_)
{
return bitmap_->GetSize().width;
}
return 0;
}
float Image::GetHeight() const
{
if (bitmap_)
{
return bitmap_->GetSize().height;
}
return 0;
}
Size Image::GetSize() const
{
if (bitmap_)
{
auto bitmap_size = bitmap_->GetSize();
return Size{ bitmap_size.width, bitmap_size.height };
}
return Size{};
}
UINT32 Image::GetWidthInPixels() const
{
if (bitmap_)
{
return bitmap_->GetPixelSize().width;
}
return 0;
}
UINT32 Image::GetHeightInPixels() const
{
if (bitmap_)
{
return bitmap_->GetPixelSize().height;
}
return 0;
}
math::Vec2T<UINT32> Image::GetSizeInPixels() const
{
if (bitmap_)
{
auto bitmap_size = bitmap_->GetPixelSize();
return math::Vec2T<UINT32>{ bitmap_size.width, bitmap_size.height };
}
return math::Vec2T<UINT32>{};
}
ComPtr<ID2D1Bitmap> Image::GetBitmap() const
{
return bitmap_;
}
void Image::SetBitmap(ComPtr<ID2D1Bitmap> bitmap)
{
bitmap_ = bitmap;
}
}

View File

@ -19,86 +19,53 @@
// THE SOFTWARE. // THE SOFTWARE.
#pragma once #pragma once
#include "include-forwards.h"
#include "../base/Resource.h" #include "../base/Resource.h"
#include "../renderer/render.h" #include "render.h" // ID2D1Bitmap
namespace kiwano namespace kiwano
{ {
// 图片 // 图像
KGE_DECLARE_SMART_PTR(Image);
class KGE_API Image class KGE_API Image
: public Object : public Object
{ {
public: public:
Image(); Image();
explicit Image(
Resource const& res
);
explicit Image(
Resource const& res,
Rect const& crop_rect /* 裁剪矩形 */
);
explicit Image( explicit Image(
ComPtr<ID2D1Bitmap> const& bitmap ComPtr<ID2D1Bitmap> const& bitmap
); );
virtual ~Image(); virtual ~Image();
// 加载图片资源
bool Load(
Resource const& res
);
// 资源是否有效 // 资源是否有效
bool IsValid() const; bool IsValid() const;
// 将图片裁剪为矩形 // 获取位图宽度
void Crop(
Rect const& crop_rect /* 裁剪矩形 */
);
// 获取宽度
float GetWidth() const; float GetWidth() const;
// 获取高度 // 获取位图高度
float GetHeight() const; float GetHeight() const;
// 获取大小 // 获取位图大小
Size GetSize() const; Size GetSize() const;
// 获取源图片宽度 // 获取位图像素宽度
float GetSourceWidth() const; UINT32 GetWidthInPixels() const;
// 获取源图片高度 // 获取位图像素高度
float GetSourceHeight() const; UINT32 GetHeightInPixels() const;
// 获取源图片大小 // 获取位图像素大小
Size GetSourceSize() const; math::Vec2T<UINT32> GetSizeInPixels() const;
// 获取裁剪位置 X 坐标 // 获取源位图
float GetCropX() const; ComPtr<ID2D1Bitmap> GetBitmap() const;
// 获取裁剪位置 Y 坐标 // 设置源位图
float GetCropY() const; void SetBitmap(ComPtr<ID2D1Bitmap> bitmap);
// 获取裁剪位置
Point GetCropPos() const;
// 获取裁剪矩形
Rect GetCropRect() const;
ComPtr<ID2D1Bitmap> const& GetBitmap() const;
protected: protected:
void SetBitmap(
ComPtr<ID2D1Bitmap> const& bitmap
);
protected:
Rect crop_rect_;
ComPtr<ID2D1Bitmap> bitmap_; ComPtr<ID2D1Bitmap> bitmap_;
}; };
} }

View File

@ -0,0 +1,83 @@
// Copyright (c) 2016-2019 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "ImageCache.h"
#include "../base/logs.h"
namespace kiwano
{
ImageCache::ImageCache()
{
}
ImageCache::~ImageCache()
{
}
ImagePtr ImageCache::AddImage(Resource const& res)
{
size_t hash_code = res.GetHashCode();
auto iter = image_cache_.find(hash_code);
if (iter != image_cache_.end())
{
return iter->second;
}
HRESULT hr = S_OK;
ComPtr<ID2D1Bitmap> bitmap;
if (res.IsFileType())
{
hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromFile(bitmap, res.GetFileName());
}
else
{
hr = Renderer::Instance()->GetD2DDeviceResources()->CreateBitmapFromResource(bitmap, res);
}
if (SUCCEEDED(hr))
{
ImagePtr ptr = new Image(bitmap);
image_cache_.insert(std::make_pair(hash_code, ptr));
}
else
{
KGE_ERROR_LOG(L"Load image file failed with HRESULT of %08X", hr);
}
return nullptr;
}
void ImageCache::RemoveImage(Resource const& res)
{
size_t hash_code = res.GetHashCode();
auto iter = image_cache_.find(hash_code);
if (iter != image_cache_.end())
{
image_cache_.erase(iter);
}
}
void ImageCache::Clear()
{
image_cache_.clear();
}
}

View File

@ -0,0 +1,48 @@
// Copyright (c) 2016-2019 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include "../common/Singleton.hpp"
#include "render.h"
namespace kiwano
{
class KGE_API ImageCache
: public Singleton<ImageCache>
{
KGE_DECLARE_SINGLETON(ImageCache);
public:
ImagePtr AddImage(Resource const& res);
void RemoveImage(Resource const& res);
void Clear();
protected:
ImageCache();
virtual ~ImageCache();
protected:
using ImageMap = UnorderedMap<size_t, ImagePtr>;
ImageMap image_cache_;
};
}

View File

@ -19,7 +19,6 @@
// THE SOFTWARE. // THE SOFTWARE.
#include "render.h" #include "render.h"
#include "../2d/Image.h"
#include "../base/logs.h" #include "../base/logs.h"
#include "../base/window.h" #include "../base/window.h"
@ -29,7 +28,7 @@ namespace kiwano
: hwnd_(nullptr) : hwnd_(nullptr)
, antialias_(true) , antialias_(true)
, vsync_(true) , vsync_(true)
, text_antialias_(TextAntialias::ClearType) , text_antialias_(TextAntialias::GrayScale)
, clear_color_(Color::Black) , clear_color_(Color::Black)
, opacity_(1.f) , opacity_(1.f)
, collecting_status_(false) , collecting_status_(false)
@ -292,27 +291,6 @@ namespace kiwano
return S_OK; return S_OK;
} }
HRESULT Renderer::DrawImage(ImagePtr image, Rect const& dest_rect)
{
if (!device_context_)
return E_UNEXPECTED;
if (!image->GetBitmap())
return S_OK;
device_context_->DrawBitmap(
image->GetBitmap().Get(),
DX::ConvertToRectF(dest_rect),
opacity_,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
DX::ConvertToRectF(image->GetCropRect())
);
if (collecting_status_)
++status_.primitives;
return S_OK;
}
HRESULT Renderer::DrawBitmap(ComPtr<ID2D1Bitmap> const & bitmap, Rect const& src_rect, Rect const& dest_rect) HRESULT Renderer::DrawBitmap(ComPtr<ID2D1Bitmap> const & bitmap, Rect const& src_rect, Rect const& dest_rect)
{ {
if (!device_context_) if (!device_context_)
@ -321,7 +299,6 @@ namespace kiwano
if (!bitmap) if (!bitmap)
return S_OK; return S_OK;
// Do not crop bitmap
device_context_->DrawBitmap( device_context_->DrawBitmap(
bitmap.Get(), bitmap.Get(),
DX::ConvertToRectF(dest_rect), DX::ConvertToRectF(dest_rect),

View File

@ -26,6 +26,7 @@
#include "../2d/Font.hpp" #include "../2d/Font.hpp"
#include "../2d/TextStyle.hpp" #include "../2d/TextStyle.hpp"
#include "helper.hpp" #include "helper.hpp"
#include "Image.h"
#include "TextRenderer.h" #include "TextRenderer.h"
#include "D2DDeviceResources.h" #include "D2DDeviceResources.h"
@ -79,11 +80,6 @@ namespace kiwano
Color const& fill_color Color const& fill_color
); );
HRESULT DrawImage(
ImagePtr image,
Rect const& dest_rect
);
HRESULT DrawBitmap( HRESULT DrawBitmap(
ComPtr<ID2D1Bitmap> const& bitmap, ComPtr<ID2D1Bitmap> const& bitmap,
Rect const& src_rect, Rect const& src_rect,

View File

@ -18,11 +18,11 @@
// 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 "ResLoader.h" #include "ResourceCache.h"
#include "../base/logs.h" #include "../base/logs.h"
#include "../2d/Image.h" #include "../2d/Frame.h"
#include "../2d/Frames.h" #include "../2d/FrameSequence.h"
#include "../2d/GifImage.h" #include "../renderer/GifImage.h"
#include "FileUtil.h" #include "FileUtil.h"
#include <fstream> #include <fstream>
@ -35,7 +35,7 @@ namespace kiwano
String path; String path;
}; };
bool LoadImagesFromData(ResLoader* loader, GlobalData* gdata, const String* id, const String* type, bool LoadImagesFromData(ResourceCache* loader, GlobalData* gdata, const String* id, const String* type,
const String* file, const Array<const wchar_t*>* files, int rows, int cols) const String* file, const Array<const wchar_t*>* files, int rows, int cols)
{ {
if (!gdata || !id) return false; if (!gdata || !id) return false;
@ -52,13 +52,13 @@ namespace kiwano
{ {
if (rows || cols) if (rows || cols)
{ {
// Image slices // Frame slices
return !!loader->AddFrames(*id, Resource(gdata->path + (*file)), std::max(cols, 1), std::max(rows, 1)); return !!loader->AddFrameSequence(*id, Resource(gdata->path + (*file)), std::max(cols, 1), std::max(rows, 1));
} }
else else
{ {
// Simple image // Simple image
return loader->AddImage(*id, Resource(gdata->path + (*file))); return loader->AddFrame(*id, Resource(gdata->path + (*file)));
} }
} }
} }
@ -66,22 +66,22 @@ namespace kiwano
// Frames // Frames
if (files) if (files)
{ {
Array<ImagePtr> images; Array<FramePtr> frames;
images.reserve(files->size()); frames.reserve(files->size());
for (const auto& file : (*files)) for (const auto& file : (*files))
{ {
ImagePtr image = new Image(gdata->path + (file)); FramePtr frame = new Frame;
if (image->IsValid()) if (frame->Load(gdata->path + (file)))
{ {
images.push_back(image); frames.push_back(frame);
} }
} }
return !!loader->AddFrames(*id, images); return !!loader->AddFrameSequence(*id, frames);
} }
return false; return false;
} }
bool LoadJsonData(ResLoader* loader, Json const& json_data) bool LoadJsonData(ResourceCache* loader, Json const& json_data)
{ {
GlobalData global_data; GlobalData global_data;
if (json_data.count(L"path")) if (json_data.count(L"path"))
@ -123,7 +123,7 @@ namespace kiwano
return true; return true;
} }
bool LoadXmlData(ResLoader* loader, tinyxml2::XMLElement* elem) bool LoadXmlData(ResourceCache* loader, tinyxml2::XMLElement* elem)
{ {
GlobalData global_data; GlobalData global_data;
if (auto path = elem->FirstChildElement(L"path")) if (auto path = elem->FirstChildElement(L"path"))
@ -170,18 +170,18 @@ namespace kiwano
namespace namespace
{ {
Map<String, Closure<bool(ResLoader*, Json const&)>> load_json_funcs = { Map<String, Closure<bool(ResourceCache*, Json const&)>> load_json_funcs = {
{ L"latest", __res_loader_01::LoadJsonData }, { L"latest", __res_loader_01::LoadJsonData },
{ L"0.1", __res_loader_01::LoadJsonData }, { L"0.1", __res_loader_01::LoadJsonData },
}; };
Map<String, Closure<bool(ResLoader*, tinyxml2::XMLElement*)>> load_xml_funcs = { Map<String, Closure<bool(ResourceCache*, tinyxml2::XMLElement*)>> load_xml_funcs = {
{ L"latest", __res_loader_01::LoadXmlData }, { L"latest", __res_loader_01::LoadXmlData },
{ L"0.1", __res_loader_01::LoadXmlData }, { L"0.1", __res_loader_01::LoadXmlData },
}; };
} }
bool ResLoader::LoadFromJsonFile(String const& file_path) bool ResourceCache::LoadFromJsonFile(String const& file_path)
{ {
Json json_data; Json json_data;
std::wifstream ifs; std::wifstream ifs;
@ -195,18 +195,18 @@ namespace kiwano
} }
catch (std::wifstream::failure& e) catch (std::wifstream::failure& e)
{ {
KGE_WARNING_LOG(L"ResLoader::LoadFromJsonFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str()); KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
return false; return false;
} }
catch (json_exception& e) catch (json_exception& e)
{ {
KGE_WARNING_LOG(L"ResLoader::LoadFromJsonFile failed: Cannot parse to JSON. (%s)", string_to_wide(e.what()).c_str()); KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot parse to JSON. (%s)", string_to_wide(e.what()).c_str());
return false; return false;
} }
return LoadFromJson(json_data); return LoadFromJson(json_data);
} }
bool ResLoader::LoadFromJson(Json const& json_data) bool ResourceCache::LoadFromJson(Json const& json_data)
{ {
try try
{ {
@ -228,13 +228,13 @@ namespace kiwano
} }
catch (std::exception& e) catch (std::exception& e)
{ {
KGE_WARNING_LOG(L"ResLoader::LoadFromJson failed: JSON data is invalid. (%s)", string_to_wide(e.what()).c_str()); KGE_WARNING_LOG(L"ResourceCache::LoadFromJson failed: JSON data is invalid. (%s)", string_to_wide(e.what()).c_str());
return false; return false;
} }
return false; return false;
} }
bool ResLoader::LoadFromXmlFile(String const& file_path) bool ResourceCache::LoadFromXmlFile(String const& file_path)
{ {
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
@ -250,21 +250,21 @@ namespace kiwano
if (tinyxml2::XML_SUCCESS != doc.Parse(ss.str().c_str())) if (tinyxml2::XML_SUCCESS != doc.Parse(ss.str().c_str()))
{ {
KGE_WARNING_LOG(L"ResLoader::LoadFromXmlFile failed: %s (%s)", KGE_WARNING_LOG(L"ResourceCache::LoadFromXmlFile failed: %s (%s)",
tinyxml2::XMLDocument::ErrorIDToName(doc.ErrorID()), doc.ErrorStr()); tinyxml2::XMLDocument::ErrorIDToName(doc.ErrorID()), doc.ErrorStr());
return false; return false;
} }
} }
catch (std::wifstream::failure& e) catch (std::wifstream::failure& e)
{ {
KGE_WARNING_LOG(L"ResLoader::LoadFromXmlFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str()); KGE_WARNING_LOG(L"ResourceCache::LoadFromXmlFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str());
return false; return false;
} }
return LoadFromXml(&doc); return LoadFromXml(&doc);
} }
bool ResLoader::LoadFromXml(tinyxml2::XMLDocument* doc) bool ResourceCache::LoadFromXml(tinyxml2::XMLDocument* doc)
{ {
if (doc) if (doc)
{ {
@ -292,42 +292,42 @@ namespace kiwano
} }
catch (std::exception& e) catch (std::exception& e)
{ {
KGE_WARNING_LOG(L"ResLoader::LoadFromXml failed: %s", string_to_wide(e.what()).c_str()); KGE_WARNING_LOG(L"ResourceCache::LoadFromXml failed: %s", string_to_wide(e.what()).c_str());
return false; return false;
} }
} }
return false; return false;
} }
bool ResLoader::AddImage(String const& id, Resource const& image) bool ResourceCache::AddFrame(String const& id, Resource const& res)
{ {
ImagePtr ptr = new (std::nothrow) Image; FramePtr ptr = new (std::nothrow) Frame;
if (ptr) if (ptr)
{ {
if (ptr->Load(image)) if (ptr->Load(res))
{ {
return AddImage(id, ptr); return AddFrame(id, ptr);
} }
} }
return false; return false;
} }
bool ResLoader::AddImage(String const & id, ImagePtr image) bool ResourceCache::AddFrame(String const & id, FramePtr frame)
{ {
if (image) if (frame)
{ {
res_.insert(std::make_pair(id, image)); cache_.insert(std::make_pair(id, frame));
return true; return true;
} }
return false; return false;
} }
bool ResLoader::AddGifImage(String const& id, Resource const& image) bool ResourceCache::AddGifImage(String const& id, Resource const& res)
{ {
GifImagePtr ptr = new (std::nothrow) GifImage; GifImagePtr ptr = new (std::nothrow) GifImage;
if (ptr) if (ptr)
{ {
if (ptr->Load(image)) if (ptr->Load(res))
{ {
return AddGifImage(id, ptr); return AddGifImage(id, ptr);
} }
@ -335,27 +335,27 @@ namespace kiwano
return false; return false;
} }
bool ResLoader::AddGifImage(String const& id, GifImagePtr image) bool ResourceCache::AddGifImage(String const& id, GifImagePtr image)
{ {
if (image) if (image)
{ {
res_.insert(std::make_pair(id, image)); cache_.insert(std::make_pair(id, image));
return true; return true;
} }
return false; return false;
} }
size_t ResLoader::AddFrames(String const& id, Array<Resource> const& images) size_t ResourceCache::AddFrameSequence(String const& id, Array<Resource> const& images)
{ {
if (images.empty()) if (images.empty())
return 0; return 0;
Array<ImagePtr> image_arr; Array<FramePtr> image_arr;
image_arr.reserve(images.size()); image_arr.reserve(images.size());
for (const auto& image : images) for (const auto& image : images)
{ {
ImagePtr ptr = new (std::nothrow) Image; FramePtr ptr = new (std::nothrow) Frame;
if (ptr) if (ptr)
{ {
if (ptr->Load(image)) if (ptr->Load(image))
@ -367,43 +367,43 @@ namespace kiwano
if (!image_arr.empty()) if (!image_arr.empty())
{ {
FramesPtr frames = new (std::nothrow) Frames(image_arr); FrameSequencePtr frames = new (std::nothrow) FrameSequence(image_arr);
return AddFrames(id, frames); return AddFrameSequence(id, frames);
} }
return 0; return 0;
} }
size_t ResLoader::AddFrames(String const& id, Array<ImagePtr> const& images) size_t ResourceCache::AddFrameSequence(String const& id, Array<FramePtr> const& images)
{ {
if (images.empty()) if (images.empty())
return 0; return 0;
FramesPtr frames = new (std::nothrow) Frames(images); FrameSequencePtr frames = new (std::nothrow) FrameSequence(images);
return AddFrames(id, frames); return AddFrameSequence(id, frames);
} }
size_t ResLoader::AddFrames(String const & id, Resource const & image, int cols, int rows) size_t ResourceCache::AddFrameSequence(String const & id, Resource const & image, int cols, int rows)
{ {
if (cols <= 0 || rows <= 0) if (cols <= 0 || rows <= 0)
return 0; return 0;
ImagePtr raw = new (std::nothrow) Image; FramePtr raw = new (std::nothrow) Frame;
if (!raw || !raw->Load(image)) if (!raw || !raw->Load(image))
return false; return false;
float raw_width = raw->GetSourceWidth(); float raw_width = raw->GetWidth();
float raw_height = raw->GetSourceHeight(); float raw_height = raw->GetHeight();
float width = raw_width / cols; float width = raw_width / cols;
float height = raw_height / rows; float height = raw_height / rows;
Array<ImagePtr> image_arr; Array<FramePtr> image_arr;
image_arr.reserve(rows * cols); image_arr.reserve(rows * cols);
for (int i = 0; i < rows; i++) for (int i = 0; i < rows; i++)
{ {
for (int j = 0; j < cols; j++) for (int j = 0; j < cols; j++)
{ {
ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); FramePtr ptr = new (std::nothrow) Frame(raw->GetImage());
if (ptr) if (ptr)
{ {
ptr->Crop(Rect{ j * width, i * height, width, height }); ptr->Crop(Rect{ j * width, i * height, width, height });
@ -412,22 +412,22 @@ namespace kiwano
} }
} }
FramesPtr frames = new (std::nothrow) Frames(image_arr); FrameSequencePtr frames = new (std::nothrow) FrameSequence(image_arr);
return AddFrames(id, frames); return AddFrameSequence(id, frames);
} }
size_t ResLoader::AddFrames(String const & id, Resource const & image, Array<Rect> const & crop_rects) size_t ResourceCache::AddFrameSequence(String const & id, Resource const & image, Array<Rect> const & crop_rects)
{ {
ImagePtr raw = new (std::nothrow) Image; FramePtr raw = new (std::nothrow) Frame;
if (!raw || !raw->Load(image)) if (!raw || !raw->Load(image))
return 0; return 0;
Array<ImagePtr> image_arr; Array<FramePtr> image_arr;
image_arr.reserve(crop_rects.size()); image_arr.reserve(crop_rects.size());
for (const auto& rect : crop_rects) for (const auto& rect : crop_rects)
{ {
ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); FramePtr ptr = new (std::nothrow) Frame(raw->GetImage());
if (ptr) if (ptr)
{ {
ptr->Crop(rect); ptr->Crop(rect);
@ -435,62 +435,62 @@ namespace kiwano
} }
} }
FramesPtr frames = new (std::nothrow) Frames(image_arr); FrameSequencePtr frames = new (std::nothrow) FrameSequence(image_arr);
return AddFrames(id, frames); return AddFrameSequence(id, frames);
} }
size_t ResLoader::AddFrames(String const & id, FramesPtr frames) size_t ResourceCache::AddFrameSequence(String const & id, FrameSequencePtr frames)
{ {
if (frames) if (frames)
{ {
res_.insert(std::make_pair(id, frames)); cache_.insert(std::make_pair(id, frames));
return frames->GetFrames().size(); return frames->GetFrames().size();
} }
return 0; return 0;
} }
bool ResLoader::AddObj(String const& id, ObjectPtr obj) bool ResourceCache::AddObj(String const& id, ObjectPtr obj)
{ {
if (obj) if (obj)
{ {
res_.insert(std::make_pair(id, obj)); cache_.insert(std::make_pair(id, obj));
return true; return true;
} }
return false; return false;
} }
ImagePtr ResLoader::GetImage(String const & id) const FramePtr ResourceCache::GetFrame(String const & id) const
{ {
return Get<Image>(id); return Get<Frame>(id);
} }
GifImagePtr ResLoader::GetGifImage(String const& id) const GifImagePtr ResourceCache::GetGifImage(String const& id) const
{ {
return Get<GifImage>(id); return Get<GifImage>(id);
} }
FramesPtr ResLoader::GetFrames(String const & id) const FrameSequencePtr ResourceCache::GetFrameSequence(String const & id) const
{ {
return Get<Frames>(id); return Get<FrameSequence>(id);
} }
void ResLoader::Delete(String const & id) void ResourceCache::Delete(String const & id)
{ {
res_.erase(id); cache_.erase(id);
} }
void ResLoader::Destroy() void ResourceCache::Destroy()
{ {
res_.clear(); cache_.clear();
} }
ResLoader::ResLoader() ResourceCache::ResourceCache()
{ {
} }
ResLoader::~ResLoader() ResourceCache::~ResourceCache()
{ {
Destroy(); Destroy();
} }
} }

View File

@ -24,12 +24,13 @@
#include "../common/Json.hpp" #include "../common/Json.hpp"
#include "../base/Resource.h" #include "../base/Resource.h"
#include "../2d/include-forwards.h" #include "../2d/include-forwards.h"
#include "../renderer/GifImage.h"
#include "../third-party/tinyxml2/tinyxml2.h" #include "../third-party/tinyxml2/tinyxml2.h"
namespace kiwano namespace kiwano
{ {
// ×ÊÔ´¼ÓÔØÆ÷ // ×ÊÔ´»º´æ
class KGE_API ResLoader class KGE_API ResourceCache
{ {
public: public:
// 从 JSON 文件加载资源信息 // 从 JSON 文件加载资源信息
@ -45,45 +46,45 @@ namespace kiwano
bool LoadFromXml(tinyxml2::XMLDocument* doc); bool LoadFromXml(tinyxml2::XMLDocument* doc);
// 添加图片 // 添加图片
bool AddImage(String const& id, Resource const& image); bool AddFrame(String const& id, Resource const& res);
// 添加图片 // 添加图片
bool AddImage(String const& id, ImagePtr image); bool AddFrame(String const& id, FramePtr frame);
// 添加 GIF 图片 // 添加 GIF 图片
bool AddGifImage(String const& id, Resource const& image); bool AddGifImage(String const& id, Resource const& res);
// 添加 GIF 图片 // 添加 GIF 图片
bool AddGifImage(String const& id, GifImagePtr image); bool AddGifImage(String const& id, GifImagePtr image);
// Ìí¼ÓÖ¡¼¯ºÏ // Ìí¼ÓÐòÁÐÖ¡
size_t AddFrames(String const& id, Array<Resource> const& images); size_t AddFrameSequence(String const& id, Array<Resource> const& frames);
// Ìí¼ÓÖ¡¼¯ºÏ // Ìí¼ÓÐòÁÐÖ¡
size_t AddFrames(String const& id, Array<ImagePtr> const& images); size_t AddFrameSequence(String const& id, Array<FramePtr> const& frames);
// Ìí¼ÓÖ¡¼¯ºÏ // Ìí¼ÓÐòÁÐÖ¡
// 按行列数裁剪图片 // 按行列数裁剪图片
size_t AddFrames(String const& id, Resource const& image, int cols, int rows = 1); size_t AddFrameSequence(String const& id, Resource const& frame, int cols, int rows = 1);
// Ìí¼ÓÖ¡¼¯ºÏ // Ìí¼ÓÐòÁÐÖ¡
// 按指定裁剪矩形裁剪图片 // 按指定裁剪矩形裁剪图片
size_t AddFrames(String const& id, Resource const& image, Array<Rect> const& crop_rects); size_t AddFrameSequence(String const& id, Resource const& frame, Array<Rect> const& crop_rects);
// Ìí¼ÓÖ¡¼¯ºÏ // Ìí¼ÓÐòÁÐÖ¡
size_t AddFrames(String const& id, FramesPtr frames); size_t AddFrameSequence(String const& id, FrameSequencePtr frames);
// 添加对象 // 添加对象
bool AddObj(String const& id, ObjectPtr obj); bool AddObj(String const& id, ObjectPtr obj);
// 获取图片资源 // 获取图片资源
ImagePtr GetImage(String const& id) const; FramePtr GetFrame(String const& id) const;
// 获取 GIF 图片资源 // 获取 GIF 图片资源
GifImagePtr GetGifImage(String const& id) const; GifImagePtr GetGifImage(String const& id) const;
// 获取序列帧 // 获取序列帧
FramesPtr GetFrames(String const& id) const; FrameSequencePtr GetFrameSequence(String const& id) const;
// 删除指定资源 // 删除指定资源
void Delete(String const& id); void Delete(String const& id);
@ -94,18 +95,18 @@ namespace kiwano
template<typename _Ty> template<typename _Ty>
_Ty* Get(String const& id) const _Ty* Get(String const& id) const
{ {
auto iter = res_.find(id); auto iter = cache_.find(id);
if (iter == res_.end()) if (iter == cache_.end())
return nullptr; return nullptr;
return dynamic_cast<_Ty*>((*iter).second.Get()); return dynamic_cast<_Ty*>((*iter).second.Get());
} }
public: public:
ResLoader(); ResourceCache();
virtual ~ResLoader(); virtual ~ResourceCache();
protected: protected:
UnorderedMap<String, ObjectPtr> res_; UnorderedMap<String, ObjectPtr> cache_;
}; };
} }