2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2019-03-31 01:37:06 +08:00
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
2019-10-11 21:55:29 +08:00
|
|
|
#include <kiwano/renderer/Texture.h>
|
|
|
|
|
#include <kiwano/renderer/Renderer.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-09-30 10:59:04 +08:00
|
|
|
InterpolationMode Texture::default_interpolation_mode_ = InterpolationMode::Linear;
|
2019-08-21 16:33:41 +08:00
|
|
|
|
|
|
|
|
Texture::Texture()
|
|
|
|
|
: interpolation_mode_(default_interpolation_mode_)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
Texture::~Texture()
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
bool Texture::Load(String const& file_path)
|
2019-08-18 17:49:13 +08:00
|
|
|
{
|
2020-01-17 11:24:24 +08:00
|
|
|
Renderer::Instance().CreateTexture(*this, file_path);
|
2019-08-18 17:49:13 +08:00
|
|
|
return IsValid();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
bool Texture::Load(Resource const& res)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2020-01-17 11:24:24 +08:00
|
|
|
Renderer::Instance().CreateTexture(*this, res);
|
2019-08-16 00:50:54 +08:00
|
|
|
return IsValid();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
bool Texture::IsValid() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-16 00:50:54 +08:00
|
|
|
return bitmap_ != nullptr;
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
float Texture::GetWidth() const
|
2019-08-13 14:44:37 +08:00
|
|
|
{
|
|
|
|
|
if (bitmap_)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
return bitmap_->GetSize().width;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2019-08-13 14:44:37 +08:00
|
|
|
return 0;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
float Texture::GetHeight() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
if (bitmap_)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
return bitmap_->GetSize().height;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2019-08-13 14:44:37 +08:00
|
|
|
return 0;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
Size Texture::GetSize() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
if (bitmap_)
|
|
|
|
|
{
|
|
|
|
|
auto bitmap_size = bitmap_->GetSize();
|
|
|
|
|
return Size{ bitmap_size.width, bitmap_size.height };
|
|
|
|
|
}
|
|
|
|
|
return Size{};
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-12 11:26:41 +08:00
|
|
|
uint32_t Texture::GetWidthInPixels() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
if (bitmap_)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
return bitmap_->GetPixelSize().width;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2019-08-13 14:44:37 +08:00
|
|
|
return 0;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-12 11:26:41 +08:00
|
|
|
uint32_t Texture::GetHeightInPixels() const
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
if (bitmap_)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-13 14:44:37 +08:00
|
|
|
return bitmap_->GetPixelSize().height;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2019-08-13 14:44:37 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 11:26:41 +08:00
|
|
|
math::Vec2T<uint32_t> Texture::GetSizeInPixels() const
|
2019-08-13 14:44:37 +08:00
|
|
|
{
|
|
|
|
|
if (bitmap_)
|
|
|
|
|
{
|
|
|
|
|
auto bitmap_size = bitmap_->GetPixelSize();
|
2019-10-12 11:26:41 +08:00
|
|
|
return math::Vec2T<uint32_t>{ bitmap_size.width, bitmap_size.height };
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|
2019-10-12 11:26:41 +08:00
|
|
|
return math::Vec2T<uint32_t>{};
|
2019-08-13 14:44:37 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
InterpolationMode Texture::GetBitmapInterpolationMode() const
|
|
|
|
|
{
|
|
|
|
|
return interpolation_mode_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
void Texture::CopyFrom(TexturePtr copy_from)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-26 14:15:25 +08:00
|
|
|
if (IsValid() && copy_from)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-26 14:15:25 +08:00
|
|
|
HRESULT hr = bitmap_->CopyFromBitmap(nullptr, copy_from->GetBitmap().get(), nullptr);
|
2019-08-16 00:50:54 +08:00
|
|
|
|
2019-12-30 14:24:29 +08:00
|
|
|
win32::ThrowIfFailed(hr);
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-26 14:15:25 +08:00
|
|
|
void Texture::CopyFrom(TexturePtr copy_from, Rect const& src_rect, Point const& dest_point)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
2019-12-26 14:15:25 +08:00
|
|
|
if (IsValid() && copy_from)
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
|
|
|
|
HRESULT hr = bitmap_->CopyFromBitmap(
|
2019-10-12 11:26:41 +08:00
|
|
|
&D2D1::Point2U(uint32_t(dest_point.x), uint32_t(dest_point.y)),
|
2019-12-26 14:15:25 +08:00
|
|
|
copy_from->GetBitmap().get(),
|
2019-08-16 00:50:54 +08:00
|
|
|
&D2D1::RectU(
|
2019-10-12 11:26:41 +08:00
|
|
|
uint32_t(src_rect.GetLeft()),
|
|
|
|
|
uint32_t(src_rect.GetTop()),
|
|
|
|
|
uint32_t(src_rect.GetRight()),
|
|
|
|
|
uint32_t(src_rect.GetBottom()))
|
2019-08-16 00:50:54 +08:00
|
|
|
);
|
|
|
|
|
|
2019-12-30 14:24:29 +08:00
|
|
|
win32::ThrowIfFailed(hr);
|
2019-08-16 00:50:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
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
|
2019-08-16 00:50:54 +08:00
|
|
|
{
|
|
|
|
|
if (bitmap_)
|
|
|
|
|
{
|
|
|
|
|
return bitmap_->GetPixelFormat();
|
|
|
|
|
}
|
|
|
|
|
return D2D1_PIXEL_FORMAT();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
ComPtr<ID2D1Bitmap> Texture::GetBitmap() const
|
2019-08-13 14:44:37 +08:00
|
|
|
{
|
|
|
|
|
return bitmap_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
void Texture::SetBitmap(ComPtr<ID2D1Bitmap> bitmap)
|
2019-08-13 14:44:37 +08:00
|
|
|
{
|
|
|
|
|
bitmap_ = bitmap;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:33:41 +08:00
|
|
|
void Texture::SetDefaultInterpolationMode(InterpolationMode mode)
|
|
|
|
|
{
|
2019-12-29 18:58:22 +08:00
|
|
|
default_interpolation_mode_ = mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InterpolationMode Texture::GetDefaultInterpolationMode()
|
|
|
|
|
{
|
|
|
|
|
return default_interpolation_mode_;
|
2019-08-21 16:33:41 +08:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|