Magic_Game/src/kiwano/renderer/Texture.cpp

200 lines
4.2 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// 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.
2019-08-21 16:33:41 +08:00
#include "Texture.h"
2019-08-16 00:50:54 +08:00
#include "Renderer.h"
2019-09-30 10:59:04 +08:00
#include "../base/win32/helper.h"
2019-04-11 14:40:54 +08:00
namespace kiwano
{
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-08-21 16:33:41 +08:00
Texture::Texture(String const& file_path)
: Texture()
2019-08-18 17:49:13 +08:00
{
Load(file_path);
}
2019-08-21 16:33:41 +08:00
Texture::Texture(Resource const& res)
: Texture()
2019-08-16 00:50:54 +08:00
{
Load(res);
}
2019-08-21 16:33:41 +08:00
Texture::Texture(ComPtr<ID2D1Bitmap> const & bitmap)
: Texture()
{
SetBitmap(bitmap);
}
2019-08-21 16:33:41 +08:00
Texture::~Texture()
{
}
2019-08-21 16:33:41 +08:00
bool Texture::Load(String const& file_path)
2019-08-18 17:49:13 +08:00
{
2019-08-21 16:33:41 +08:00
Renderer::GetInstance()->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
{
2019-08-21 16:33:41 +08:00
Renderer::GetInstance()->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-08-16 00:50:54 +08:00
return bitmap_ != nullptr;
}
2019-09-29 22:23:13 +08:00
float Texture::GetWidth() const
{
if (bitmap_)
{
return bitmap_->GetSize().width;
}
return 0;
}
2019-09-29 22:23:13 +08:00
float Texture::GetHeight() const
{
if (bitmap_)
{
return bitmap_->GetSize().height;
}
return 0;
}
2019-08-21 16:33:41 +08:00
Size Texture::GetSize() const
{
if (bitmap_)
{
auto bitmap_size = bitmap_->GetSize();
return Size{ bitmap_size.width, bitmap_size.height };
}
return Size{};
}
2019-09-29 22:23:13 +08:00
std::uint32_t Texture::GetWidthInPixels() const
{
if (bitmap_)
{
return bitmap_->GetPixelSize().width;
}
return 0;
}
2019-09-29 22:23:13 +08:00
std::uint32_t Texture::GetHeightInPixels() const
{
if (bitmap_)
{
return bitmap_->GetPixelSize().height;
}
return 0;
}
2019-09-29 22:23:13 +08:00
math::Vec2T<std::uint32_t> Texture::GetSizeInPixels() const
{
if (bitmap_)
{
auto bitmap_size = bitmap_->GetPixelSize();
2019-09-29 22:23:13 +08:00
return math::Vec2T<std::uint32_t>{ bitmap_size.width, bitmap_size.height };
}
2019-09-29 22:23:13 +08:00
return math::Vec2T<std::uint32_t>{};
}
2019-08-21 16:33:41 +08:00
InterpolationMode Texture::GetBitmapInterpolationMode() const
{
return interpolation_mode_;
}
void Texture::CopyFrom(Texture const& copy_from)
2019-08-16 00:50:54 +08:00
{
if (IsValid() && copy_from.IsValid())
{
HRESULT hr = bitmap_->CopyFromBitmap(nullptr, copy_from.GetBitmap().get(), nullptr);
ThrowIfFailed(hr);
}
}
2019-08-21 16:33:41 +08:00
void Texture::CopyFrom(Texture const& copy_from, Rect const& src_rect, Point const& dest_point)
2019-08-16 00:50:54 +08:00
{
if (IsValid() && copy_from.IsValid())
{
HRESULT hr = bitmap_->CopyFromBitmap(
2019-09-29 22:23:13 +08:00
&D2D1::Point2U(std::uint32_t(dest_point.x), std::uint32_t(dest_point.y)),
2019-08-16 00:50:54 +08:00
copy_from.GetBitmap().get(),
&D2D1::RectU(
2019-09-29 22:23:13 +08:00
std::uint32_t(src_rect.GetLeft()),
std::uint32_t(src_rect.GetTop()),
std::uint32_t(src_rect.GetRight()),
std::uint32_t(src_rect.GetBottom()))
2019-08-16 00:50:54 +08:00
);
ThrowIfFailed(hr);
}
}
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
{
return bitmap_;
}
2019-08-21 16:33:41 +08:00
void Texture::SetBitmap(ComPtr<ID2D1Bitmap> bitmap)
{
bitmap_ = bitmap;
}
2019-08-21 16:33:41 +08:00
void Texture::SetDefaultInterpolationMode(InterpolationMode mode)
{
}
}