Add InterpolationMode

This commit is contained in:
Nomango 2019-08-21 16:33:41 +08:00
parent 7039d00e61
commit c626697ebf
33 changed files with 434 additions and 366 deletions

View File

@ -12,7 +12,6 @@
<ClInclude Include="..\src\kiwano\2d\Frame.h" />
<ClInclude Include="..\src\kiwano\2d\GifSprite.h" />
<ClInclude Include="..\src\kiwano\base\Director.h" />
<ClInclude Include="..\src\kiwano\base\types.h" />
<ClInclude Include="..\src\kiwano\core\basic_json.hpp" />
<ClInclude Include="..\src\kiwano\core\function.hpp" />
<ClInclude Include="..\src\kiwano\core\core.h" />
@ -70,8 +69,9 @@
<ClInclude Include="..\src\kiwano\renderer\FontCollection.h" />
<ClInclude Include="..\src\kiwano\renderer\Geometry.h" />
<ClInclude Include="..\src\kiwano\renderer\GifImage.h" />
<ClInclude Include="..\src\kiwano\renderer\Image.h" />
<ClInclude Include="..\src\kiwano\renderer\ImageCache.h" />
<ClInclude Include="..\src\kiwano\renderer\StrokeStyle.h" />
<ClInclude Include="..\src\kiwano\renderer\Texture.h" />
<ClInclude Include="..\src\kiwano\renderer\TextureCache.h" />
<ClInclude Include="..\src\kiwano\renderer\LayerArea.h" />
<ClInclude Include="..\src\kiwano\renderer\Renderer.h" />
<ClInclude Include="..\src\kiwano\renderer\RenderTarget.h" />
@ -132,8 +132,8 @@
<ClCompile Include="..\src\kiwano\renderer\FontCollection.cpp" />
<ClCompile Include="..\src\kiwano\renderer\Geometry.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\Texture.cpp" />
<ClCompile Include="..\src\kiwano\renderer\TextureCache.cpp" />
<ClCompile Include="..\src\kiwano\renderer\LayerArea.cpp" />
<ClCompile Include="..\src\kiwano\renderer\Renderer.cpp" />
<ClCompile Include="..\src\kiwano\renderer\RenderTarget.cpp" />

View File

@ -150,9 +150,6 @@
<ClInclude Include="..\src\kiwano\utils\FileUtil.h">
<Filter>utils</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\base\types.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\2d\GifSprite.h">
<Filter>2d</Filter>
</ClInclude>
@ -258,12 +255,6 @@
<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\renderer\ImageCache.h">
<Filter>renderer</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\renderer\LayerArea.h">
<Filter>renderer</Filter>
</ClInclude>
@ -306,6 +297,15 @@
<ClInclude Include="..\src\kiwano\2d\Transform.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\renderer\Texture.h">
<Filter>renderer</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\renderer\TextureCache.h">
<Filter>renderer</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\renderer\StrokeStyle.h">
<Filter>renderer</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\kiwano\ui\Button.cpp">
@ -443,12 +443,6 @@
<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\renderer\ImageCache.cpp">
<Filter>renderer</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\renderer\LayerArea.cpp">
<Filter>renderer</Filter>
</ClCompile>
@ -482,5 +476,11 @@
<ClCompile Include="..\src\kiwano\2d\Transform.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\renderer\Texture.cpp">
<Filter>renderer</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\renderer\TextureCache.cpp">
<Filter>renderer</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -247,20 +247,20 @@ namespace kiwano
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
return;
MouseCursor cursor = MouseCursor::Arrow;
CursorType cursor = CursorType::Arrow;
switch (ImGui::GetMouseCursor())
{
case ImGuiMouseCursor_Arrow: cursor = MouseCursor::Arrow; break;
case ImGuiMouseCursor_TextInput: cursor = MouseCursor::TextInput; break;
case ImGuiMouseCursor_ResizeAll: cursor = MouseCursor::SizeAll; break;
case ImGuiMouseCursor_ResizeEW: cursor = MouseCursor::SizeWE; break;
case ImGuiMouseCursor_ResizeNS: cursor = MouseCursor::SizeNS; break;
case ImGuiMouseCursor_ResizeNESW: cursor = MouseCursor::SizeNESW; break;
case ImGuiMouseCursor_ResizeNWSE: cursor = MouseCursor::SizeNWSE; break;
case ImGuiMouseCursor_Hand: cursor = MouseCursor::Hand; break;
case ImGuiMouseCursor_Arrow: cursor = CursorType::Arrow; break;
case ImGuiMouseCursor_TextInput: cursor = CursorType::TextInput; break;
case ImGuiMouseCursor_ResizeAll: cursor = CursorType::SizeAll; break;
case ImGuiMouseCursor_ResizeEW: cursor = CursorType::SizeWE; break;
case ImGuiMouseCursor_ResizeNS: cursor = CursorType::SizeNS; break;
case ImGuiMouseCursor_ResizeNESW: cursor = CursorType::SizeNESW; break;
case ImGuiMouseCursor_ResizeNWSE: cursor = CursorType::SizeNWSE; break;
case ImGuiMouseCursor_Hand: cursor = CursorType::Hand; break;
}
Window::GetInstance()->SetMouseCursor(cursor);
Window::GetInstance()->SetCursor(cursor);
}
void ImGuiModule::UpdateGamepads()
{

View File

@ -31,7 +31,7 @@ namespace kiwano
, stroke_color_(Color::White)
, stroke_style_(StrokeStyle::Miter)
{
Renderer::GetInstance()->CreateImageRenderTarget(rt_);
Renderer::GetInstance()->CreateTextureRenderTarget(rt_);
}
Canvas::~Canvas()
@ -53,12 +53,12 @@ namespace kiwano
{
UpdateCache();
if (image_cached_.IsValid())
if (texture_cached_.IsValid())
{
PrepareRender(rt);
Rect bitmap_rect(0.f, 0.f, image_cached_.GetWidth(), image_cached_.GetHeight());
rt->DrawImage(image_cached_, bitmap_rect, bitmap_rect);
Rect bitmap_rect(0.f, 0.f, texture_cached_.GetWidth(), texture_cached_.GetHeight());
rt->DrawTexture(texture_cached_, bitmap_rect, bitmap_rect);
}
}
@ -245,11 +245,11 @@ namespace kiwano
cache_expired_ = true;
}
void Canvas::DrawImage(Image const& image, const Rect* src_rect, const Rect* dest_rect)
void Canvas::DrawTexture(Texture const& texture, const Rect* src_rect, const Rect* dest_rect)
{
if (image.IsValid())
if (texture.IsValid())
{
rt_.DrawImage(image, src_rect, dest_rect);
rt_.DrawTexture(texture, src_rect, dest_rect);
cache_expired_ = true;
}
}
@ -326,17 +326,17 @@ namespace kiwano
cache_expired_ = true;
}
Image Canvas::ExportToImage() const
Texture Canvas::ExportToTexture() const
{
UpdateCache();
return image_cached_;
return texture_cached_;
}
void Canvas::UpdateCache() const
{
if (cache_expired_)
{
image_cached_ = rt_.GetOutput();
texture_cached_ = rt_.GetOutput();
cache_expired_ = false;
}
}

View File

@ -95,9 +95,9 @@ namespace kiwano
Vec2 const& radius
);
// »­Í¼Æ¬
void DrawImage(
Image const& image,
// »­Í¼
void DrawTexture(
Texture const& texture,
const Rect* src_rect = nullptr,
const Rect* dest_rect = nullptr
);
@ -232,7 +232,7 @@ namespace kiwano
Float32 GetBrushOpacity() const;
// 导出为图片
Image ExportToImage() const;
Texture ExportToTexture() const;
void OnRender(RenderTarget* rt) override;
@ -247,9 +247,9 @@ namespace kiwano
TextStyle text_style_;
StrokeStyle stroke_style_;
GeometrySink geo_sink_;
ImageRenderTarget rt_;
TextureRenderTarget rt_;
mutable bool cache_expired_;
mutable Image image_cached_;
mutable Texture texture_cached_;
};
}

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#include "Frame.h"
#include "../renderer/ImageCache.h"
#include "../renderer/TextureCache.h"
namespace kiwano
{
@ -37,17 +37,17 @@ namespace kiwano
Load(res);
}
Frame::Frame(Image const& image)
Frame::Frame(Texture const& texture)
{
SetImage(image);
SetTexture(texture);
}
bool Frame::Load(String const& file_path)
{
Image image = ImageCache::GetInstance()->AddOrGetImage(file_path);
if (image.IsValid())
Texture texture = TextureCache::GetInstance()->AddOrGetTexture(file_path);
if (texture.IsValid())
{
SetImage(image);
SetTexture(texture);
return true;
}
return false;
@ -55,10 +55,10 @@ namespace kiwano
bool Frame::Load(Resource const& res)
{
Image image = ImageCache::GetInstance()->AddOrGetImage(res);
if (image.IsValid())
Texture texture = TextureCache::GetInstance()->AddOrGetTexture(res);
if (texture.IsValid())
{
SetImage(image);
SetTexture(texture);
return true;
}
return false;
@ -66,9 +66,9 @@ namespace kiwano
void Frame::SetCropRect(Rect const& crop_rect)
{
if (image_.IsValid())
if (texture_.IsValid())
{
auto bitmap_size = image_.GetSize();
auto bitmap_size = texture_.GetSize();
crop_rect_.left_top.x = std::min(std::max(crop_rect.left_top.x, 0.f), bitmap_size.x);
crop_rect_.left_top.y = std::min(std::max(crop_rect.left_top.y, 0.f), bitmap_size.y);
crop_rect_.right_bottom.x = std::min(std::max(crop_rect.right_bottom.x, 0.f), bitmap_size.x);
@ -76,14 +76,14 @@ namespace kiwano
}
}
void Frame::SetImage(Image const& image)
void Frame::SetTexture(Texture const& texture)
{
image_ = image;
if (image_.IsValid())
texture_ = texture;
if (texture_.IsValid())
{
crop_rect_.left_top.x = crop_rect_.left_top.y = 0;
crop_rect_.right_bottom.x = image_.GetWidth();
crop_rect_.right_bottom.y = image_.GetHeight();
crop_rect_.right_bottom.x = texture_.GetWidth();
crop_rect_.right_bottom.y = texture_.GetHeight();
}
}
}

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
#include "../renderer/Image.h"
#include "../renderer/Texture.h"
namespace kiwano
{
@ -39,7 +39,7 @@ namespace kiwano
);
explicit Frame(
Image const& image
Texture const& texture
);
bool Load(
@ -55,31 +55,31 @@ namespace kiwano
Rect const& crop_rect /* 裁剪矩形 */
);
// ÉèÖÃλͼ
void SetImage(
Image const& image
// ÉèÖÃÎÆÀí
void SetTexture(
Texture const& texture
);
// 获取宽度
Float32 GetWidth() const { return crop_rect_.GetWidth(); }
Float32 GetWidth() const { return crop_rect_.GetWidth(); }
// 获取高度
Float32 GetHeight() const { return crop_rect_.GetHeight(); }
Float32 GetHeight() const { return crop_rect_.GetHeight(); }
// 获取大小
Size GetSize() const { return crop_rect_.GetSize(); }
Size GetSize() const { return crop_rect_.GetSize(); }
// 获取裁剪位置
Point GetCropPoint() const { return crop_rect_.GetLeftTop(); }
Point GetCropPoint() const { return crop_rect_.GetLeftTop(); }
// 获取裁剪矩形
inline Rect const& GetCropRect() const { return crop_rect_; }
inline Rect const& GetCropRect() const { return crop_rect_; }
// »ñȡλͼ
inline Image const& GetImage() const { return image_; }
// »ñÈ¡ÎÆÀí
inline Texture const& GetTexture() const { return texture_; }
protected:
Image image_;
Texture texture_;
Rect crop_rect_;
};
}

View File

@ -54,8 +54,8 @@ namespace kiwano
else
{
frames_.reserve(frames_.size() + frames.size());
for (const auto& image : frames)
AddFrame(image);
for (const auto& texture : frames)
AddFrame(texture);
}
}

View File

@ -20,7 +20,7 @@
#include "GifSprite.h"
#include "../base/Logger.h"
#include "../renderer/ImageCache.h"
#include "../renderer/TextureCache.h"
#include "../renderer/Renderer.h"
namespace kiwano
@ -45,41 +45,41 @@ namespace kiwano
Load(res);
}
GifSprite::GifSprite(GifImage image)
GifSprite::GifSprite(GifImage texture)
{
Load(image);
Load(texture);
}
bool GifSprite::Load(String const& file_path)
{
GifImage image = ImageCache::GetInstance()->AddOrGetGifImage(file_path);
return Load(image);
GifImage texture = TextureCache::GetInstance()->AddOrGetGifImage(file_path);
return Load(texture);
}
bool GifSprite::Load(Resource const& res)
{
GifImage image = ImageCache::GetInstance()->AddOrGetGifImage(res);
return Load(image);
GifImage texture = TextureCache::GetInstance()->AddOrGetGifImage(res);
return Load(texture);
}
bool GifSprite::Load(GifImage image)
bool GifSprite::Load(GifImage texture)
{
if (image.IsValid())
if (texture.IsValid())
{
image_ = image;
texture_ = texture;
next_index_ = 0;
loop_count_ = 0;
disposal_type_ = DisposalType::None;
SetSize(Size{ static_cast<Float32>(image_.GetWidthInPixels()), static_cast<Float32>(image_.GetHeightInPixels()) });
SetSize(Size{ static_cast<Float32>(texture_.GetWidthInPixels()), static_cast<Float32>(texture_.GetHeightInPixels()) });
if (!frame_rt_.IsValid())
{
Renderer::GetInstance()->CreateImageRenderTarget(frame_rt_);
Renderer::GetInstance()->CreateTextureRenderTarget(frame_rt_);
}
if (image_.GetFramesCount() > 0)
if (texture_.GetFramesCount() > 0)
{
ComposeNextFrame();
}
@ -94,7 +94,7 @@ namespace kiwano
{
PrepareRender(rt);
rt->DrawImage(frame_);
rt->DrawTexture(frame_);
}
}
@ -102,7 +102,7 @@ namespace kiwano
{
Actor::Update(dt);
if (image_.IsValid() && animating_)
if (texture_.IsValid() && animating_)
{
frame_elapsed_ += dt;
if (frame_delay_ <= frame_elapsed_)
@ -132,7 +132,7 @@ namespace kiwano
OverlayNextFrame();
} while (frame_delay_.IsZero() && !IsLastFrame());
animating_ = (!EndOfAnimation() && image_.GetFramesCount() > 1);
animating_ = (!EndOfAnimation() && texture_.GetFramesCount() > 1);
}
}
@ -163,9 +163,9 @@ namespace kiwano
void GifSprite::OverlayNextFrame()
{
Image raw_image;
Texture raw_texture;
HRESULT hr = image_.GetRawFrame(next_index_, raw_image, frame_rect_, frame_delay_, disposal_type_);
HRESULT hr = texture_.GetRawFrame(next_index_, raw_texture, frame_rect_, frame_delay_, disposal_type_);
if (SUCCEEDED(hr))
{
@ -182,24 +182,24 @@ namespace kiwano
if (next_index_ == 0)
{
// ÖØÐ»æÖƱ³¾°
frame_rt_.Clear(image_.GetBackgroundColor());
frame_rt_.Clear(texture_.GetBackgroundColor());
loop_count_++;
}
frame_rt_.DrawImage(raw_image, nullptr, &frame_rect_);
frame_rt_.DrawTexture(raw_texture, nullptr, &frame_rect_);
frame_rt_.EndDraw();
}
if (SUCCEEDED(hr))
{
Image frame_to_render = frame_rt_.GetOutput();
Texture frame_to_render = frame_rt_.GetOutput();
hr = frame_to_render.IsValid() ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
frame_ = frame_to_render;
next_index_ = (++next_index_) % image_.GetFramesCount();
next_index_ = (++next_index_) % texture_.GetFramesCount();
}
}
@ -218,7 +218,7 @@ namespace kiwano
void GifSprite::SaveComposedFrame()
{
Image frame_to_be_saved = frame_rt_.GetOutput();
Texture frame_to_be_saved = frame_rt_.GetOutput();
HRESULT hr = frame_to_be_saved.IsValid() ? S_OK : E_FAIL;
@ -253,7 +253,7 @@ namespace kiwano
if (SUCCEEDED(hr))
{
Image frame_to_copy_to = frame_rt_.GetOutput();
Texture frame_to_copy_to = frame_rt_.GetOutput();
hr = frame_to_copy_to.IsValid() ? S_OK : E_FAIL;
@ -271,7 +271,7 @@ namespace kiwano
frame_rt_.BeginDraw();
frame_rt_.PushClipRect(frame_rect_);
frame_rt_.Clear(image_.GetBackgroundColor());
frame_rt_.Clear(texture_.GetBackgroundColor());
frame_rt_.PopClipRect();
return frame_rt_.EndDraw();

View File

@ -46,7 +46,7 @@ namespace kiwano
);
GifSprite(
GifImage image
GifImage texture
);
bool Load(
@ -58,7 +58,7 @@ namespace kiwano
);
bool Load(
GifImage image
GifImage texture
);
// 设置 GIF 动画循环次数
@ -108,10 +108,10 @@ namespace kiwano
DisposalType disposal_type_;
LoopDoneCallback loop_cb_;
DoneCallback done_cb_;
GifImage image_;
Image frame_;
GifImage texture_;
Texture frame_;
Rect frame_rect_;
Image saved_frame_;
ImageRenderTarget frame_rt_;
Texture saved_frame_;
TextureRenderTarget frame_rt_;
};
}

View File

@ -21,6 +21,7 @@
#pragma once
#include "Actor.h"
#include "../renderer/Geometry.h"
#include "../renderer/StrokeStyle.h"
namespace kiwano
{

View File

@ -108,7 +108,7 @@ namespace kiwano
{
PrepareRender(rt);
rt->DrawImage(frame_->GetImage(), &frame_->GetCropRect(), nullptr);
rt->DrawTexture(frame_->GetTexture(), &frame_->GetCropRect(), nullptr);
}
}
}

View File

@ -20,6 +20,7 @@
#pragma once
#include "include-forwards.h"
#include "../renderer/StrokeStyle.h"
namespace kiwano
{

View File

@ -24,7 +24,6 @@
#include "../base/RefCounter.hpp"
#include "../base/SmartPtr.hpp"
#include "../base/ObjectBase.h"
#include "../base/types.h"
#include "../math/math.h"
#include "../renderer/Color.h"

View File

@ -46,7 +46,7 @@ namespace kiwano
, device_name_(nullptr)
, is_fullscreen_(false)
, resizable_(false)
, mouse_cursor_(MouseCursor::Arrow)
, mouse_cursor_(CursorType::Arrow)
{
}
@ -222,7 +222,7 @@ namespace kiwano
if (handle_)
{
HINSTANCE hinstance = GetModuleHandle(nullptr);
HICON icon = (HICON)::LoadImage(
HICON icon = (HICON)::LoadImageW(
hinstance,
MAKEINTRESOURCE(icon_resource),
IMAGE_ICON,
@ -295,7 +295,7 @@ namespace kiwano
}
}
void Window::SetMouseCursor(MouseCursor cursor)
void Window::SetCursor(CursorType cursor)
{
mouse_cursor_ = cursor;
}
@ -327,14 +327,14 @@ namespace kiwano
LPTSTR win32_cursor = IDC_ARROW;
switch (mouse_cursor_)
{
case MouseCursor::Arrow: win32_cursor = IDC_ARROW; break;
case MouseCursor::TextInput: win32_cursor = IDC_IBEAM; break;
case MouseCursor::SizeAll: win32_cursor = IDC_SIZEALL; break;
case MouseCursor::SizeWE: win32_cursor = IDC_SIZEWE; break;
case MouseCursor::SizeNS: win32_cursor = IDC_SIZENS; break;
case MouseCursor::SizeNESW: win32_cursor = IDC_SIZENESW; break;
case MouseCursor::SizeNWSE: win32_cursor = IDC_SIZENWSE; break;
case MouseCursor::Hand: win32_cursor = IDC_HAND; break;
case CursorType::Arrow: win32_cursor = IDC_ARROW; break;
case CursorType::TextInput: win32_cursor = IDC_IBEAM; break;
case CursorType::SizeAll: win32_cursor = IDC_SIZEALL; break;
case CursorType::SizeWE: win32_cursor = IDC_SIZEWE; break;
case CursorType::SizeNS: win32_cursor = IDC_SIZENS; break;
case CursorType::SizeNESW: win32_cursor = IDC_SIZENESW; break;
case CursorType::SizeNWSE: win32_cursor = IDC_SIZENWSE; break;
case CursorType::Hand: win32_cursor = IDC_HAND; break;
}
::SetCursor(::LoadCursorW(nullptr, win32_cursor));
}

View File

@ -22,10 +22,24 @@
#include "../macros.h"
#include "../core/core.h"
#include "../math/math.h"
#include "types.h"
namespace kiwano
{
// 鼠标指针类型
enum class CursorType
{
Arrow, /* 指针 */
TextInput, /* 文本 */
Hand, /* 手指 */
SizeAll,
SizeNESW,
SizeNS,
SizeNWSE,
SizeWE,
};
// 窗口
class KGE_API Window
: public Singleton<Window>
{
@ -56,8 +70,8 @@ namespace kiwano
// 设置全屏模式
void SetFullscreen(bool fullscreen, Int32 width, Int32 height);
// 譜崔報炎峺寞
void SetMouseCursor(MouseCursor cursor);
// 设置鼠标指针类型
void SetCursor(CursorType cursor);
public:
void Init(
@ -94,6 +108,6 @@ namespace kiwano
Int32 width_;
Int32 height_;
WCHAR* device_name_;
MouseCursor mouse_cursor_;
CursorType mouse_cursor_;
};
}

View File

@ -61,12 +61,12 @@
#include "renderer/Color.h"
#include "renderer/Font.h"
#include "renderer/Image.h"
#include "renderer/Texture.h"
#include "renderer/GifImage.h"
#include "renderer/TextLayout.h"
#include "renderer/Geometry.h"
#include "renderer/LayerArea.h"
#include "renderer/ImageCache.h"
#include "renderer/TextureCache.h"
#include "renderer/Renderer.h"

View File

@ -24,7 +24,7 @@
#include "../base/Logger.h"
#include "../base/input.h"
#include "../base/Director.h"
#include "../renderer/ImageCache.h"
#include "../renderer/TextureCache.h"
#include "../renderer/Renderer.h"
#include "../utils/ResourceCache.h"
#include <windowsx.h> // GET_X_LPARAM, GET_Y_LPARAM
@ -150,7 +150,7 @@ namespace kiwano
// Clear all resources
Director::GetInstance()->ClearStages();
ResourceCache::GetInstance()->Clear();
ImageCache::GetInstance()->Clear();
TextureCache::GetInstance()->Clear();
if (inited_)
{
@ -166,7 +166,7 @@ namespace kiwano
// Destroy all instances
Director::DestroyInstance();
ResourceCache::DestroyInstance();
ImageCache::DestroyInstance();
TextureCache::DestroyInstance();
Input::DestroyInstance();
Renderer::DestroyInstance();
Window::DestroyInstance();

View File

@ -255,7 +255,7 @@ namespace kiwano
return hr;
}
HRESULT GifImage::GetRawFrame(UInt32 frame_index, Image& raw_frame, Rect& frame_rect, Duration& delay, DisposalType& disposal_type)
HRESULT GifImage::GetRawFrame(UInt32 frame_index, Texture& raw_frame, Rect& frame_rect, Duration& delay, DisposalType& disposal_type)
{
ComPtr<IWICFormatConverter> converter;
ComPtr<IWICBitmapFrameDecode> wic_frame;

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
#include "Image.h"
#include "Texture.h"
namespace kiwano
{
@ -58,7 +58,7 @@ namespace kiwano
HRESULT GetRawFrame(
UInt32 frame_index,
Image& raw_frame,
Texture& raw_frame,
Rect& frame_rect,
Duration& delay,
DisposalType& disposal_type

View File

@ -30,9 +30,9 @@ namespace kiwano
RenderTarget::RenderTarget()
: opacity_(1.f)
, collecting_status_(false)
, antialias_(true)
, fast_global_transform_(true)
, text_antialias_(TextAntialias::GrayScale)
, antialias_(true)
, text_antialias_(TextAntialiasMode::GrayScale)
{
status_.primitives = 0;
}
@ -134,7 +134,7 @@ namespace kiwano
geometry.GetGeometry().get(),
default_brush_.get(),
stroke_width,
device_resources_->GetStrokeStyle(stroke)
GetStrokeStyle(stroke).get()
);
IncreasePrimitivesCount();
@ -180,7 +180,7 @@ namespace kiwano
DX::ConvertToPoint2F(point2),
default_brush_.get(),
stroke_width,
device_resources_->GetStrokeStyle(stroke)
GetStrokeStyle(stroke).get()
);
IncreasePrimitivesCount();
@ -205,7 +205,7 @@ namespace kiwano
DX::ConvertToRectF(rect),
default_brush_.get(),
stroke_width,
device_resources_->GetStrokeStyle(stroke)
GetStrokeStyle(stroke).get()
);
IncreasePrimitivesCount();
@ -254,7 +254,7 @@ namespace kiwano
),
default_brush_.get(),
stroke_width,
device_resources_->GetStrokeStyle(stroke)
GetStrokeStyle(stroke).get()
);
IncreasePrimitivesCount();
@ -307,7 +307,7 @@ namespace kiwano
),
default_brush_.get(),
stroke_width,
device_resources_->GetStrokeStyle(stroke)
GetStrokeStyle(stroke).get()
);
IncreasePrimitivesCount();
@ -340,12 +340,12 @@ namespace kiwano
ThrowIfFailed(hr);
}
void RenderTarget::DrawImage(Image const& image, Rect const& src_rect, Rect const& dest_rect) const
void RenderTarget::DrawTexture(Texture const& texture, Rect const& src_rect, Rect const& dest_rect) const
{
DrawImage(image, &src_rect, &dest_rect);
DrawTexture(texture, &src_rect, &dest_rect);
}
void RenderTarget::DrawImage(Image const& image, const Rect* src_rect, const Rect* dest_rect) const
void RenderTarget::DrawTexture(Texture const& texture, const Rect* src_rect, const Rect* dest_rect) const
{
HRESULT hr = S_OK;
if (!render_target_)
@ -353,13 +353,17 @@ namespace kiwano
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr) && image.IsValid())
if (SUCCEEDED(hr) && texture.IsValid())
{
auto mode = (texture.GetBitmapInterpolationMode() == InterpolationMode::Linear)
? D2D1_BITMAP_INTERPOLATION_MODE_LINEAR
: D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
render_target_->DrawBitmap(
image.GetBitmap().get(),
texture.GetBitmap().get(),
dest_rect ? &DX::ConvertToRectF(*dest_rect) : nullptr,
opacity_,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
mode,
src_rect ? &DX::ConvertToRectF(*src_rect) : nullptr
);
@ -385,7 +389,7 @@ namespace kiwano
layout.GetTextStyle().outline,
DX::ConvertToColorF(layout.GetTextStyle().outline_color),
layout.GetTextStyle().outline_width,
device_resources_->GetStrokeStyle(layout.GetTextStyle().outline_stroke)
GetStrokeStyle(layout.GetTextStyle().outline_stroke).get()
);
}
@ -541,6 +545,17 @@ namespace kiwano
return global_transform_;
}
ComPtr<ID2D1StrokeStyle> RenderTarget::GetStrokeStyle(StrokeStyle style) const
{
switch (style)
{
case StrokeStyle::Miter: return device_resources_->GetMiterStrokeStyle(); break;
case StrokeStyle::Bevel: return device_resources_->GetBevelStrokeStyle(); break;
case StrokeStyle::Round: return device_resources_->GetRoundStrokeStyle(); break;
}
return nullptr;
}
void RenderTarget::SetTransform(const Matrix3x2& matrix)
{
HRESULT hr = S_OK;
@ -623,7 +638,7 @@ namespace kiwano
ThrowIfFailed(hr);
}
void RenderTarget::SetTextAntialiasMode(TextAntialias mode)
void RenderTarget::SetTextAntialiasMode(TextAntialiasMode mode)
{
HRESULT hr = S_OK;
if (!render_target_)
@ -637,16 +652,16 @@ namespace kiwano
D2D1_TEXT_ANTIALIAS_MODE antialias_mode = D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
switch (text_antialias_)
{
case TextAntialias::Default:
case TextAntialiasMode::Default:
antialias_mode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT;
break;
case TextAntialias::ClearType:
case TextAntialiasMode::ClearType:
antialias_mode = D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
break;
case TextAntialias::GrayScale:
case TextAntialiasMode::GrayScale:
antialias_mode = D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE;
break;
case TextAntialias::None:
case TextAntialiasMode::None:
antialias_mode = D2D1_TEXT_ANTIALIAS_MODE_ALIASED;
break;
default:
@ -683,14 +698,14 @@ namespace kiwano
//
// ImageRenderTarget
// TextureRenderTarget
//
ImageRenderTarget::ImageRenderTarget()
TextureRenderTarget::TextureRenderTarget()
{
}
Image ImageRenderTarget::GetOutput() const
Texture TextureRenderTarget::GetOutput() const
{
HRESULT hr = E_FAIL;
@ -706,13 +721,13 @@ namespace kiwano
if (SUCCEEDED(hr))
{
return Image(bitmap);
return Texture(bitmap);
}
}
}
ThrowIfFailed(hr);
return Image();
return Texture();
}
}

View File

@ -20,7 +20,7 @@
#pragma once
#include "../base/time.h"
#include "Image.h"
#include "Texture.h"
#include "Geometry.h"
#include "TextLayout.h"
#include "LayerArea.h"
@ -28,6 +28,16 @@
namespace kiwano
{
// 文字抗锯齿模式
enum class TextAntialiasMode
{
Default, // 系统默认
ClearType, // ClearType 抗锯齿
GrayScale, // 灰度抗锯齿
None // 不启用抗锯齿
};
// 渲染目标
class KGE_API RenderTarget
: public noncopyable
@ -103,14 +113,14 @@ namespace kiwano
Color const& fill_color
) const;
void DrawImage(
Image const& image,
void DrawTexture(
Texture const& texture,
Rect const& src_rect,
Rect const& dest_rect
) const;
void DrawImage(
Image const& image,
void DrawTexture(
Texture const& texture,
const Rect* src_rect = nullptr,
const Rect* dest_rect = nullptr
) const;
@ -165,7 +175,7 @@ namespace kiwano
// 设置文字抗锯齿模式
void SetTextAntialiasMode(
TextAntialias mode
TextAntialiasMode mode
);
// 检查边界是否在视区内
@ -186,11 +196,13 @@ namespace kiwano
void IncreasePrimitivesCount() const;
inline Status const& GetStatus() const { return status_; }
inline Status const& GetStatus() const { return status_; }
inline ComPtr<ID2D1RenderTarget> GetRenderTarget() const { KGE_ASSERT(render_target_); return render_target_; }
inline ComPtr<ID2D1RenderTarget> GetRenderTarget() const { KGE_ASSERT(render_target_); return render_target_; }
inline ComPtr<ITextRenderer> GetTextRenderer() const { KGE_ASSERT(text_renderer_); return text_renderer_; }
inline ComPtr<ITextRenderer> GetTextRenderer() const { KGE_ASSERT(text_renderer_); return text_renderer_; }
ComPtr<ID2D1StrokeStyle> GetStrokeStyle(StrokeStyle style) const;
public:
RenderTarget();
@ -208,7 +220,7 @@ namespace kiwano
bool fast_global_transform_;
mutable bool collecting_status_;
mutable Status status_;
TextAntialias text_antialias_;
TextAntialiasMode text_antialias_;
ComPtr<ITextRenderer> text_renderer_;
ComPtr<ID2D1RenderTarget> render_target_;
ComPtr<ID2D1SolidColorBrush> default_brush_;
@ -219,12 +231,12 @@ namespace kiwano
// 位图渲染目标
class KGE_API ImageRenderTarget
class KGE_API TextureRenderTarget
: public RenderTarget
{
public:
ImageRenderTarget();
TextureRenderTarget();
Image GetOutput() const;
Texture GetOutput() const;
};
}

View File

@ -245,7 +245,7 @@ namespace kiwano
return hr;
}
void Renderer::CreateImage(Image& image, String const& file_path)
void Renderer::CreateTexture(Texture& texture, String const& file_path)
{
HRESULT hr = S_OK;
if (!d2d_res_)
@ -260,17 +260,17 @@ namespace kiwano
if (SUCCEEDED(hr))
{
image.SetBitmap(bitmap);
texture.SetBitmap(bitmap);
}
}
if (FAILED(hr))
{
KGE_WARNING_LOG(L"Load image failed with HRESULT of %08X!", hr);
KGE_WARNING_LOG(L"Load texture failed with HRESULT of %08X!", hr);
}
}
void Renderer::CreateImage(Image& image, Resource const& res)
void Renderer::CreateTexture(Texture& texture, Resource const& res)
{
HRESULT hr = S_OK;
if (!d2d_res_)
@ -285,17 +285,17 @@ namespace kiwano
if (SUCCEEDED(hr))
{
image.SetBitmap(bitmap);
texture.SetBitmap(bitmap);
}
}
if (FAILED(hr))
{
KGE_WARNING_LOG(L"Load image failed with HRESULT of %08X!", hr);
KGE_WARNING_LOG(L"Load texture failed with HRESULT of %08X!", hr);
}
}
void Renderer::CreateGifImage(GifImage& image, String const& file_path)
void Renderer::CreateGifImage(GifImage& texture, String const& file_path)
{
HRESULT hr = S_OK;
if (!d2d_res_)
@ -305,7 +305,7 @@ namespace kiwano
if (!FileUtil::ExistsFile(file_path))
{
KGE_WARNING_LOG(L"Gif image file '%s' not found!", file_path.c_str());
KGE_WARNING_LOG(L"Gif texture file '%s' not found!", file_path.c_str());
hr = E_FAIL;
}
@ -322,17 +322,17 @@ namespace kiwano
if (SUCCEEDED(hr))
{
image.SetDecoder(decoder);
texture.SetDecoder(decoder);
}
}
if (FAILED(hr))
{
KGE_WARNING_LOG(L"Load GIF image failed with HRESULT of %08X!", hr);
KGE_WARNING_LOG(L"Load GIF texture failed with HRESULT of %08X!", hr);
}
}
void Renderer::CreateGifImage(GifImage& image, Resource const& res)
void Renderer::CreateGifImage(GifImage& texture, Resource const& res)
{
HRESULT hr = S_OK;
if (!d2d_res_)
@ -369,14 +369,14 @@ namespace kiwano
if (SUCCEEDED(hr))
{
image.SetDecoder(decoder);
texture.SetDecoder(decoder);
}
}
}
if (FAILED(hr))
{
KGE_WARNING_LOG(L"Load GIF image failed with HRESULT of %08X!", hr);
KGE_WARNING_LOG(L"Load GIF texture failed with HRESULT of %08X!", hr);
}
}
@ -653,7 +653,7 @@ namespace kiwano
ThrowIfFailed(hr);
}
void Renderer::CreateImageRenderTarget(ImageRenderTarget& render_target)
void Renderer::CreateTextureRenderTarget(TextureRenderTarget& render_target)
{
HRESULT hr = S_OK;
if (!d2d_res_)

View File

@ -39,6 +39,20 @@ namespace kiwano
typedef ID3D11DeviceResources ID3DDeviceResources;
#endif
// 分辨率模式
// 分辨率模式决定了将画面渲染到视区上的方式
// Fixed (固定): 分辨率不随视区改变, 且画面始终与视区边界对齐(默认)
// Center (居中): 分辨率不随视区改变, 且画面始终在视区上居中
// Stretch (拉伸): 分辨率始终随视区等比例拉伸
// Adaptive (宽高自适应): 分辨率始终保持宽高比, 且尽可能的填充视区, 可能会出现黑色边界
enum class ResolutionMode
{
Fixed, /* 固定 */
Center, /* 居中 */
Stretch, /* 拉伸 */
Adaptive, /* 宽高自适应 */
};
class KGE_API Renderer
: public Singleton<Renderer>
, public Component
@ -57,24 +71,34 @@ namespace kiwano
bool enabled
);
// 设置画面分辨率
void SetResolution(
Size const& resolution
);
// 设置分辨率模式
void SetResolutionMode(
ResolutionMode mode
);
public:
void CreateImage(
Image& image,
void CreateTexture(
Texture& texture,
String const& file_path
);
void CreateImage(
Image& image,
void CreateTexture(
Texture& texture,
Resource const& res
);
void CreateGifImage(
GifImage& image,
GifImage& texture,
String const& file_path
);
void CreateGifImage(
GifImage& image,
GifImage& texture,
Resource const& res
);
@ -127,16 +151,8 @@ namespace kiwano
GeometrySink& sink
);
void CreateImageRenderTarget(
ImageRenderTarget& render_target
);
void SetResolution(
Size const& resolution
);
void SetResolutionMode(
ResolutionMode mode
void CreateTextureRenderTarget(
TextureRenderTarget& render_target
);
public:

View File

@ -23,41 +23,10 @@
namespace kiwano
{
// ÏßÌõÑùʽ
enum class StrokeStyle : Int32
enum class StrokeStyle
{
Miter = 0, /* бÇÐ */
Bevel = 1, /* б½Ç */
Round = 2 /* Ô²½Ç */
};
// 鼠标指针
enum class MouseCursor : Int32
{
Arrow, /* 指针 */
TextInput, /* 输入文本 */
Hand, /* 手指 */
SizeAll,
SizeNESW,
SizeNS,
SizeNWSE,
SizeWE,
};
// 文字抗锯齿属性
enum class TextAntialias
{
Default, // 系统默认
ClearType, // ClearType 抗锯齿
GrayScale, // 灰度抗锯齿
None // 不启用抗锯齿
};
// 分辨率模式
enum class ResolutionMode
{
Fixed, /* 固定 */
Center, /* 居中 */
Stretch, /* 拉伸 */
Adaptive, /* 宽高自适应 */
};
}

View File

@ -18,53 +18,59 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Image.h"
#include "Texture.h"
#include "Renderer.h"
#include "../base/Logger.h"
namespace kiwano
{
Image::Image()
InterpolationMode Texture::default_interpolation_mode_ = InterpolationMode::Linear;
Texture::Texture()
: interpolation_mode_(default_interpolation_mode_)
{
}
Image::Image(String const& file_path)
Texture::Texture(String const& file_path)
: Texture()
{
Load(file_path);
}
Image::Image(Resource const& res)
Texture::Texture(Resource const& res)
: Texture()
{
Load(res);
}
Image::Image(ComPtr<ID2D1Bitmap> const & bitmap)
Texture::Texture(ComPtr<ID2D1Bitmap> const & bitmap)
: Texture()
{
SetBitmap(bitmap);
}
Image::~Image()
Texture::~Texture()
{
}
bool Image::Load(String const& file_path)
bool Texture::Load(String const& file_path)
{
Renderer::GetInstance()->CreateImage(*this, file_path);
Renderer::GetInstance()->CreateTexture(*this, file_path);
return IsValid();
}
bool Image::Load(Resource const& res)
bool Texture::Load(Resource const& res)
{
Renderer::GetInstance()->CreateImage(*this, res);
Renderer::GetInstance()->CreateTexture(*this, res);
return IsValid();
}
bool Image::IsValid() const
bool Texture::IsValid() const
{
return bitmap_ != nullptr;
}
Float32 Image::GetWidth() const
Float32 Texture::GetWidth() const
{
if (bitmap_)
{
@ -73,7 +79,7 @@ namespace kiwano
return 0;
}
Float32 Image::GetHeight() const
Float32 Texture::GetHeight() const
{
if (bitmap_)
{
@ -82,7 +88,7 @@ namespace kiwano
return 0;
}
Size Image::GetSize() const
Size Texture::GetSize() const
{
if (bitmap_)
{
@ -92,7 +98,7 @@ namespace kiwano
return Size{};
}
UInt32 Image::GetWidthInPixels() const
UInt32 Texture::GetWidthInPixels() const
{
if (bitmap_)
{
@ -101,7 +107,7 @@ namespace kiwano
return 0;
}
UInt32 Image::GetHeightInPixels() const
UInt32 Texture::GetHeightInPixels() const
{
if (bitmap_)
{
@ -110,7 +116,7 @@ namespace kiwano
return 0;
}
math::Vec2T<UInt32> Image::GetSizeInPixels() const
math::Vec2T<UInt32> Texture::GetSizeInPixels() const
{
if (bitmap_)
{
@ -120,7 +126,12 @@ namespace kiwano
return math::Vec2T<UInt32>{};
}
void Image::CopyFrom(Image const& copy_from)
InterpolationMode Texture::GetBitmapInterpolationMode() const
{
return interpolation_mode_;
}
void Texture::CopyFrom(Texture const& copy_from)
{
if (IsValid() && copy_from.IsValid())
{
@ -130,7 +141,7 @@ namespace kiwano
}
}
void Image::CopyFrom(Image const& copy_from, Rect const& src_rect, Point const& dest_point)
void Texture::CopyFrom(Texture const& copy_from, Rect const& src_rect, Point const& dest_point)
{
if (IsValid() && copy_from.IsValid())
{
@ -148,7 +159,21 @@ namespace kiwano
}
}
D2D1_PIXEL_FORMAT Image::GetPixelFormat() const
void Texture::SetInterpolationMode(InterpolationMode mode)
{
interpolation_mode_ = mode;
switch (mode)
{
case InterpolationMode::Linear:
break;
case InterpolationMode::Nearest:
break;
default:
break;
}
}
D2D1_PIXEL_FORMAT Texture::GetPixelFormat() const
{
if (bitmap_)
{
@ -157,14 +182,18 @@ namespace kiwano
return D2D1_PIXEL_FORMAT();
}
ComPtr<ID2D1Bitmap> Image::GetBitmap() const
ComPtr<ID2D1Bitmap> Texture::GetBitmap() const
{
return bitmap_;
}
void Image::SetBitmap(ComPtr<ID2D1Bitmap> bitmap)
void Texture::SetBitmap(ComPtr<ID2D1Bitmap> bitmap)
{
bitmap_ = bitmap;
}
void Texture::SetDefaultInterpolationMode(InterpolationMode mode)
{
}
}

View File

@ -23,25 +23,36 @@
namespace kiwano
{
// 图像
class KGE_API Image
// 插值模式
// 插值模式指定了位图在缩放和旋转时像素颜色的计算方式
// Linear (双线性插值): 对周围四个像素进行两次线性插值计算, 在图像放大时可能会模糊(默认)
// Nearest (最邻近插值): 取最邻近的像素点的颜色值
enum class InterpolationMode
{
Linear, // 双线性插值
Nearest, // 最邻近插值
};
// 纹理
class KGE_API Texture
{
public:
Image();
Texture();
explicit Image(
explicit Texture(
String const& file_path
);
explicit Image(
explicit Texture(
Resource const& res
);
explicit Image(
explicit Texture(
ComPtr<ID2D1Bitmap> const& bitmap
);
virtual ~Image();
virtual ~Texture();
// 加载本地文件
bool Load(
@ -56,29 +67,38 @@ namespace kiwano
// 资源是否有效
bool IsValid() const;
// 获取位图宽度
// 获取宽度
Float32 GetWidth() const;
// 获取位图高度
// 获取高度
Float32 GetHeight() const;
// 获取位图大小
// 获取大小
Size GetSize() const;
// 获取位图像素宽度
// 获取像素宽度
UInt32 GetWidthInPixels() const;
// 获取位图像素高度
// 获取像素高度
UInt32 GetHeightInPixels() const;
// 获取位图像素大小
// 获取像素大小
math::Vec2T<UInt32> GetSizeInPixels() const;
// 拷贝位图内存
void CopyFrom(Image const& copy_from);
// 获取像素插值方式
InterpolationMode GetBitmapInterpolationMode() const;
// 拷贝位图内存
void CopyFrom(Image const& copy_from, Rect const& src_rect, Point const& dest_point);
void CopyFrom(Texture const& copy_from);
// 拷贝位图内存
void CopyFrom(Texture const& copy_from, Rect const& src_rect, Point const& dest_point);
// 设置像素插值方式
void SetInterpolationMode(InterpolationMode mode);
// 设置默认的像素插值方式
static void SetDefaultInterpolationMode(InterpolationMode mode);
public:
// 获取源位图
@ -92,5 +112,8 @@ namespace kiwano
protected:
ComPtr<ID2D1Bitmap> bitmap_;
InterpolationMode interpolation_mode_;
static InterpolationMode default_interpolation_mode_;
};
}

View File

@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "ImageCache.h"
#include "TextureCache.h"
#include "Renderer.h"
#include "../base/Logger.h"
@ -33,12 +33,12 @@ namespace kiwano
return iter->second;
}
_Ty image;
if (image.Load(path))
_Ty texture;
if (texture.Load(path))
{
cache.insert(std::make_pair(hash, image));
cache.insert(std::make_pair(hash, texture));
}
return image;
return texture;
}
template <typename _CacheTy>
@ -51,58 +51,58 @@ namespace kiwano
}
}
ImageCache::ImageCache()
TextureCache::TextureCache()
{
}
ImageCache::~ImageCache()
TextureCache::~TextureCache()
{
}
Image ImageCache::AddOrGetImage(String const& file_path)
Texture TextureCache::AddOrGetTexture(String const& file_path)
{
return CreateOrGetCache<Image>(image_cache_, file_path, file_path.hash());
return CreateOrGetCache<Texture>(texture_cache_, file_path, file_path.hash());
}
Image ImageCache::AddOrGetImage(Resource const& res)
Texture TextureCache::AddOrGetTexture(Resource const& res)
{
return CreateOrGetCache<Image>(image_cache_, res, res.GetId());
return CreateOrGetCache<Texture>(texture_cache_, res, res.GetId());
}
GifImage ImageCache::AddOrGetGifImage(String const& file_path)
GifImage TextureCache::AddOrGetGifImage(String const& file_path)
{
return CreateOrGetCache<GifImage>(gif_image_cache_, file_path, file_path.hash());
return CreateOrGetCache<GifImage>(gif_texture_cache_, file_path, file_path.hash());
}
GifImage ImageCache::AddOrGetGifImage(Resource const& res)
GifImage TextureCache::AddOrGetGifImage(Resource const& res)
{
return CreateOrGetCache<GifImage>(gif_image_cache_, res, res.GetId());
return CreateOrGetCache<GifImage>(gif_texture_cache_, res, res.GetId());
}
void ImageCache::RemoveImage(String const& file_path)
void TextureCache::RemoveTexture(String const& file_path)
{
RemoveCache(image_cache_, file_path.hash());
RemoveCache(texture_cache_, file_path.hash());
}
void ImageCache::RemoveImage(Resource const& res)
void TextureCache::RemoveTexture(Resource const& res)
{
RemoveCache(image_cache_, res.GetId());
RemoveCache(texture_cache_, res.GetId());
}
void ImageCache::RemoveGifImage(String const& file_path)
void TextureCache::RemoveGifImage(String const& file_path)
{
RemoveCache(gif_image_cache_, file_path.hash());
RemoveCache(gif_texture_cache_, file_path.hash());
}
void ImageCache::RemoveGifImage(Resource const& res)
void TextureCache::RemoveGifImage(Resource const& res)
{
RemoveCache(gif_image_cache_, res.GetId());
RemoveCache(gif_texture_cache_, res.GetId());
}
void ImageCache::Clear()
void TextureCache::Clear()
{
image_cache_.clear();
gif_image_cache_.clear();
texture_cache_.clear();
gif_texture_cache_.clear();
}
}

View File

@ -19,39 +19,39 @@
// THE SOFTWARE.
#pragma once
#include "Image.h"
#include "Texture.h"
#include "GifImage.h"
namespace kiwano
{
class KGE_API ImageCache
: public Singleton<ImageCache>
class KGE_API TextureCache
: public Singleton<TextureCache>
{
KGE_DECLARE_SINGLETON(ImageCache);
KGE_DECLARE_SINGLETON(TextureCache);
public:
Image AddOrGetImage(String const& file_path);
Image AddOrGetImage(Resource const& res);
Texture AddOrGetTexture(String const& file_path);
Texture AddOrGetTexture(Resource const& res);
GifImage AddOrGetGifImage(String const& file_path);
GifImage AddOrGetGifImage(Resource const& res);
void RemoveImage(String const& file_path);
void RemoveImage(Resource const& res);
void RemoveTexture(String const& file_path);
void RemoveTexture(Resource const& res);
void RemoveGifImage(String const& file_path);
void RemoveGifImage(Resource const& res);
void Clear();
protected:
ImageCache();
TextureCache();
virtual ~ImageCache();
virtual ~TextureCache();
protected:
using ImageMap = UnorderedMap<UInt32, Image>;
ImageMap image_cache_;
using TextureMap = UnorderedMap<UInt32, Texture>;
TextureMap texture_cache_;
using GifImageMap = UnorderedMap<UInt32, GifImage>;
GifImageMap gif_image_cache_;
GifImageMap gif_texture_cache_;
};
}

View File

@ -68,8 +68,6 @@ namespace kiwano
_In_ ComPtr<ID2D1Bitmap1> const& target
) override;
ID2D1StrokeStyle* GetStrokeStyle(StrokeStyle stroke) const override;
void DiscardResources() override;
public:
@ -85,10 +83,6 @@ namespace kiwano
protected:
unsigned long ref_count_;
Float32 dpi_;
ComPtr<ID2D1StrokeStyle> d2d_miter_stroke_style_;
ComPtr<ID2D1StrokeStyle> d2d_bevel_stroke_style_;
ComPtr<ID2D1StrokeStyle> d2d_round_stroke_style_;
};
@ -323,7 +317,7 @@ namespace kiwano
if (!FileUtil::ExistsFile(file_path))
{
KGE_WARNING_LOG(L"Image file '%s' not found!", file_path.c_str());
KGE_WARNING_LOG(L"Texture file '%s' not found!", file_path.c_str());
return E_FAIL;
}
@ -578,15 +572,4 @@ namespace kiwano
return hr;
}
ID2D1StrokeStyle* D2DDeviceResources::GetStrokeStyle(StrokeStyle stroke) const
{
switch (stroke)
{
case StrokeStyle::Miter: return d2d_miter_stroke_style_.get(); break;
case StrokeStyle::Bevel: return d2d_bevel_stroke_style_.get(); break;
case StrokeStyle::Round: return d2d_round_stroke_style_.get(); break;
}
return nullptr;
}
}

View File

@ -211,8 +211,6 @@ namespace kiwano
_In_ ComPtr<IDWriteTextFormat> const& text_format
) const = 0;
virtual ID2D1StrokeStyle* GetStrokeStyle(StrokeStyle stroke) const = 0;
virtual HRESULT SetD2DDevice(
_In_ ComPtr<ID2D1Device> const& device
) = 0;
@ -230,6 +228,10 @@ namespace kiwano
inline ID2D1DeviceContext* GetDeviceContext() const { KGE_ASSERT(device_context_); return device_context_.get(); }
inline ID2D1Bitmap1* GetTargetBitmap() const { KGE_ASSERT(target_bitmap_); return target_bitmap_.get(); }
inline ID2D1StrokeStyle* GetMiterStrokeStyle() const { KGE_ASSERT(d2d_miter_stroke_style_); return d2d_miter_stroke_style_.get(); }
inline ID2D1StrokeStyle* GetBevelStrokeStyle() const { KGE_ASSERT(d2d_bevel_stroke_style_); return d2d_bevel_stroke_style_.get(); }
inline ID2D1StrokeStyle* GetRoundStrokeStyle() const { KGE_ASSERT(d2d_round_stroke_style_); return d2d_round_stroke_style_.get(); }
protected:
ComPtr<ID2D1Factory1> factory_;
ComPtr<ID2D1Device> device_;
@ -238,6 +240,10 @@ namespace kiwano
ComPtr<IWICImagingFactory> imaging_factory_;
ComPtr<IDWriteFactory> dwrite_factory_;
ComPtr<ID2D1StrokeStyle> d2d_miter_stroke_style_;
ComPtr<ID2D1StrokeStyle> d2d_bevel_stroke_style_;
ComPtr<ID2D1StrokeStyle> d2d_round_stroke_style_;
};
}

View File

@ -112,7 +112,7 @@ namespace kiwano
if (evt.type == Event::MouseHover)
{
SetStatus(Status::Hover);
Window::GetInstance()->SetMouseCursor(MouseCursor::Hand);
Window::GetInstance()->SetCursor(CursorType::Hand);
if (mouse_over_callback_)
mouse_over_callback_();
@ -120,7 +120,7 @@ namespace kiwano
else if (evt.type == Event::MouseOut)
{
SetStatus(Status::Normal);
Window::GetInstance()->SetMouseCursor(MouseCursor::Arrow);
Window::GetInstance()->SetCursor(CursorType::Arrow);
if (mouse_out_callback_)
mouse_out_callback_();

View File

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