aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/ci.yml4
-rw-r--r--demos/input_method/main.cpp2
-rw-r--r--demos/svg_path/main.cpp1
-rw-r--r--include/cru/osx/graphics/quartz/Image.h7
-rw-r--r--include/cru/osx/graphics/quartz/ImageFactory.h1
-rw-r--r--include/cru/platform/graphics/Image.h16
-rw-r--r--include/cru/platform/graphics/ImageFactory.h8
-rw-r--r--include/cru/platform/graphics/Painter.h4
-rw-r--r--include/cru/win/graphics/direct/Image.h2
-rw-r--r--include/cru/win/graphics/direct/ImageFactory.h2
-rw-r--r--include/cru/win/graphics/direct/Painter.h11
-rw-r--r--src/common/String.cpp7
-rw-r--r--src/common/io/Resource.cpp21
-rw-r--r--src/osx/graphics/quartz/Image.cpp31
-rw-r--r--src/osx/graphics/quartz/ImageFactory.cpp35
-rw-r--r--src/platform/Color.cpp3
-rw-r--r--src/platform/graphics/CMakeLists.txt1
-rw-r--r--src/platform/graphics/Image.cpp15
-rw-r--r--src/ui/render/ScrollBar.cpp1
-rw-r--r--src/ui/style/StyleRuleSet.cpp1
-rw-r--r--src/win/graphics/direct/Image.cpp7
-rw-r--r--src/win/graphics/direct/ImageFactory.cpp20
-rw-r--r--src/win/graphics/direct/Painter.cpp17
-rw-r--r--src/win/graphics/direct/WindowPainter.cpp11
-rw-r--r--src/win/gui/TimerManager.cpp1
-rw-r--r--tools/Use-VC.ps12
26 files changed, 200 insertions, 31 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 97b538ef..8d84ba18 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v2
- name: Restore Cache
- uses: actions/cache@v2
+ uses: actions/cache@v3
with:
path: ~/.cache/vcpkg/archives
key: vcpkg-${{ runner.os }}
@@ -48,7 +48,7 @@ jobs:
- uses: actions/checkout@v2
- name: Restore Cache
- uses: actions/cache@v2
+ uses: actions/cache@v3
with:
path: ~/AppData/Local/vcpkg/archives
key: vcpkg-${{ runner.os }}
diff --git a/demos/input_method/main.cpp b/demos/input_method/main.cpp
index 7d55f2db..abbbed2c 100644
--- a/demos/input_method/main.cpp
+++ b/demos/input_method/main.cpp
@@ -99,6 +99,8 @@ int main() {
cursor_lefttop.height},
brush.get());
}
+
+ painter->EndDraw();
});
window->KeyDownEvent()->AddHandler(
diff --git a/demos/svg_path/main.cpp b/demos/svg_path/main.cpp
index 970aa317..87c410e9 100644
--- a/demos/svg_path/main.cpp
+++ b/demos/svg_path/main.cpp
@@ -39,6 +39,7 @@ M6.5 0a.5.5 0 0 0 0 1H7v1.07a7.001 7.001 0 0 0-3.273 12.474l-.602.602a.5.5 0 0 0
painter->ConcatTransform(Matrix::Scale(10, 10));
painter->FillGeometry(geometry.get(), brush.get());
painter->PopState();
+ painter->EndDraw();
});
window->SetVisibility(WindowVisibilityType::Show);
diff --git a/include/cru/osx/graphics/quartz/Image.h b/include/cru/osx/graphics/quartz/Image.h
index 458f5db0..5b3aead9 100644
--- a/include/cru/osx/graphics/quartz/Image.h
+++ b/include/cru/osx/graphics/quartz/Image.h
@@ -9,7 +9,8 @@ namespace cru::platform::graphics::osx::quartz {
class QuartzImage : public OsxQuartzResource, public virtual IImage {
public:
QuartzImage(IGraphicsFactory* graphics_factory, IImageFactory* image_factory,
- CGImageRef image, bool auto_release);
+ CGImageRef image, bool auto_release,
+ unsigned char* buffer = nullptr);
CRU_DELETE_COPY(QuartzImage)
CRU_DELETE_MOVE(QuartzImage)
@@ -22,11 +23,15 @@ class QuartzImage : public OsxQuartzResource, public virtual IImage {
std::unique_ptr<IImage> CreateWithRect(const Rect& rect) override;
+ std::unique_ptr<IPainter> CreatePainter() override;
+
CGImageRef GetCGImage() const { return image_; }
private:
IImageFactory* image_factory_;
CGImageRef image_;
bool auto_release_ = false;
+
+ unsigned char* buffer_;
};
} // namespace cru::platform::graphics::osx::quartz
diff --git a/include/cru/osx/graphics/quartz/ImageFactory.h b/include/cru/osx/graphics/quartz/ImageFactory.h
index e9854738..19e9c8c1 100644
--- a/include/cru/osx/graphics/quartz/ImageFactory.h
+++ b/include/cru/osx/graphics/quartz/ImageFactory.h
@@ -15,5 +15,6 @@ class QuartzImageFactory : public OsxQuartzResource,
public:
std::unique_ptr<IImage> DecodeFromStream(io::Stream* stream) override;
+ std::unique_ptr<IImage> CreateBitmap(int width, int height) override;
};
} // namespace cru::platform::graphics::osx::quartz
diff --git a/include/cru/platform/graphics/Image.h b/include/cru/platform/graphics/Image.h
index 51e27678..de3b32d5 100644
--- a/include/cru/platform/graphics/Image.h
+++ b/include/cru/platform/graphics/Image.h
@@ -6,5 +6,21 @@ struct CRU_PLATFORM_GRAPHICS_API IImage : public virtual IGraphicsResource {
virtual float GetWidth() = 0;
virtual float GetHeight() = 0;
virtual std::unique_ptr<IImage> CreateWithRect(const Rect& rect) = 0;
+
+ /**
+ * \brief Create a painter for this image.
+ * \remarks Not all image could create a painter. If not this method will
+ * throw. Currently we only ensure images returned by
+ * IImageFactory::CreateBitmap or CloneToBitmap can create a painter.
+ * \todo Implement on Windows.
+ */
+ virtual std::unique_ptr<IPainter> CreatePainter() = 0;
+
+ /**
+ * \brief Create a bitmap image with the same pixels as this image's.
+ * \remarks This method can be used to create a bitmap image, so you can draw
+ * on the new bitmap, if the original image can't be directly painted.
+ */
+ virtual std::unique_ptr<IImage> CloneToBitmap();
};
} // namespace cru::platform::graphics
diff --git a/include/cru/platform/graphics/ImageFactory.h b/include/cru/platform/graphics/ImageFactory.h
index 2a7902b2..d2c2a468 100644
--- a/include/cru/platform/graphics/ImageFactory.h
+++ b/include/cru/platform/graphics/ImageFactory.h
@@ -7,5 +7,13 @@ namespace cru::platform::graphics {
struct CRU_PLATFORM_GRAPHICS_API IImageFactory
: public virtual IGraphicsResource {
virtual std::unique_ptr<IImage> DecodeFromStream(io::Stream* stream) = 0;
+
+ /**
+ * \brief Create an empty bitmap with given width and height.
+ * \remarks Implementation should ensure that the bitmap supports alpha
+ * channel. It had better be in 32-bit rgba format.
+ * \todo Implement on Windows.
+ */
+ virtual std::unique_ptr<IImage> CreateBitmap(int width, int height) = 0;
};
} // namespace cru::platform::graphics
diff --git a/include/cru/platform/graphics/Painter.h b/include/cru/platform/graphics/Painter.h
index 38ff8849..4268133e 100644
--- a/include/cru/platform/graphics/Painter.h
+++ b/include/cru/platform/graphics/Painter.h
@@ -3,6 +3,10 @@
namespace cru::platform::graphics {
+/**
+ * \brief Painter is a object to paint on something like window, bitmap and etc.
+ * \remarks Remember to call EndDraw() when you are done with painting.
+ */
struct CRU_PLATFORM_GRAPHICS_API IPainter : virtual IPlatformResource {
virtual Matrix GetTransform() = 0;
virtual void SetTransform(const Matrix& matrix) = 0;
diff --git a/include/cru/win/graphics/direct/Image.h b/include/cru/win/graphics/direct/Image.h
index 0b51f350..bcf2386b 100644
--- a/include/cru/win/graphics/direct/Image.h
+++ b/include/cru/win/graphics/direct/Image.h
@@ -20,6 +20,8 @@ class CRU_WIN_GRAPHICS_DIRECT_API Direct2DImage : public DirectGraphicsResource,
std::unique_ptr<IImage> CreateWithRect(const Rect& rect) override;
+ std::unique_ptr<IPainter> CreatePainter() override;
+
const Microsoft::WRL::ComPtr<ID2D1Bitmap1>& GetD2DBitmap() const {
return d2d_bitmap_;
}
diff --git a/include/cru/win/graphics/direct/ImageFactory.h b/include/cru/win/graphics/direct/ImageFactory.h
index 34d363b0..923e682f 100644
--- a/include/cru/win/graphics/direct/ImageFactory.h
+++ b/include/cru/win/graphics/direct/ImageFactory.h
@@ -19,6 +19,8 @@ class CRU_WIN_GRAPHICS_DIRECT_API WinImageFactory
public:
std::unique_ptr<IImage> DecodeFromStream(io::Stream* stream) override;
+ std::unique_ptr<IImage> CreateBitmap(int width, int height) override;
+
private:
DirectGraphicsFactory* graphics_factory_;
diff --git a/include/cru/win/graphics/direct/Painter.h b/include/cru/win/graphics/direct/Painter.h
index b1795395..da89ba5d 100644
--- a/include/cru/win/graphics/direct/Painter.h
+++ b/include/cru/win/graphics/direct/Painter.h
@@ -2,6 +2,7 @@
#include "ComResource.h"
#include "Resource.h"
+#include "cru/common/Base.h"
#include "cru/platform/graphics/Painter.h"
#include <vector>
@@ -11,13 +12,15 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DDeviceContextPainter
: public DirectResource,
public virtual IPainter,
public virtual IComResource<ID2D1DeviceContext1> {
+ CRU_DEFINE_CLASS_LOG_TAG(u"D2DDeviceContextPainter")
public:
- explicit D2DDeviceContextPainter(ID2D1DeviceContext1* device_context);
+ explicit D2DDeviceContextPainter(ID2D1DeviceContext1* device_context,
+ bool release = false);
CRU_DELETE_COPY(D2DDeviceContextPainter)
CRU_DELETE_MOVE(D2DDeviceContextPainter)
- ~D2DDeviceContextPainter() override = default;
+ ~D2DDeviceContextPainter() override;
public:
ID2D1DeviceContext1* GetComInterface() const override {
@@ -57,7 +60,7 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DDeviceContextPainter
void EndDraw() override final;
protected:
- virtual void DoEndDraw() = 0;
+ virtual void DoEndDraw() {}
private:
bool IsValid() { return is_drawing_; }
@@ -71,5 +74,7 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DDeviceContextPainter
drawing_state_stack_;
bool is_drawing_ = true;
+
+ bool release_;
};
} // namespace cru::platform::graphics::win::direct
diff --git a/src/common/String.cpp b/src/common/String.cpp
index bb31b987..cac807b0 100644
--- a/src/common/String.cpp
+++ b/src/common/String.cpp
@@ -1,14 +1,15 @@
#include "cru/common/String.h"
-#include <double-conversion/double-conversion.h>
-#include <double-conversion/string-to-double.h>
+
#include "cru/common/Exception.h"
#include "cru/common/StringToNumberConverter.h"
#include "cru/common/StringUtil.h"
-#include <cmath>
+#include <double-conversion/double-conversion.h>
+#include <double-conversion/string-to-double.h>
#include <gsl/gsl>
#include <algorithm>
+#include <cmath>
#include <cstring>
#include <functional>
#include <string_view>
diff --git a/src/common/io/Resource.cpp b/src/common/io/Resource.cpp
index 0ee7f753..b847e1cf 100644
--- a/src/common/io/Resource.cpp
+++ b/src/common/io/Resource.cpp
@@ -1,13 +1,19 @@
#include "cru/common/io/Resource.h"
#include "cru/common/Exception.h"
+#include "cru/common/log/Logger.h"
#if defined(CRU_PLATFORM_OSX)
#include <CoreFoundation/CoreFoundation.h>
#elif defined(CRU_PLATFORM_WINDOWS)
+#include <Windows.h>
#endif
+#include <filesystem>
+
namespace cru::io {
std::filesystem::path GetResourceDir() {
+ constexpr auto kLogTag = u"GetResourceDir";
+
#if defined(CRU_PLATFORM_OSX)
CFBundleRef main_bundle = CFBundleGetMainBundle();
CFURLRef bundle_url = CFBundleCopyBundleURL(main_bundle);
@@ -21,7 +27,20 @@ std::filesystem::path GetResourceDir() {
return bundle_path / "Contents/Resources";
#elif defined(CRU_PLATFORM_WINDOWS)
- throw Exception(u"Not implemented.");
+ wchar_t buffer[MAX_PATH];
+ DWORD size = ::GetModuleFileNameW(nullptr, buffer, MAX_PATH);
+ std::filesystem::path module_path(buffer, buffer + size);
+ auto p = module_path;
+ while (p.has_parent_path()) {
+ p = p.parent_path();
+ auto resource_dir_path = p / "assets";
+ if (std::filesystem::exists(resource_dir_path) &&
+ std::filesystem::is_directory(resource_dir_path)) {
+ return resource_dir_path;
+ }
+ }
+
+ throw Exception(u"Failed to find resource directory.");
#else
throw Exception(u"Not implemented.");
#endif
diff --git a/src/osx/graphics/quartz/Image.cpp b/src/osx/graphics/quartz/Image.cpp
index db10da76..feddad8c 100644
--- a/src/osx/graphics/quartz/Image.cpp
+++ b/src/osx/graphics/quartz/Image.cpp
@@ -1,14 +1,17 @@
#include "cru/osx/graphics/quartz/Image.h"
+#include "cru/common/Exception.h"
#include "cru/osx/graphics/quartz/Convert.h"
+#include "cru/osx/graphics/quartz/Painter.h"
namespace cru::platform::graphics::osx::quartz {
QuartzImage::QuartzImage(IGraphicsFactory* graphics_factory,
IImageFactory* image_factory, CGImageRef image,
- bool auto_release)
+ bool auto_release, unsigned char* buffer)
: OsxQuartzResource(graphics_factory),
image_factory_(image_factory),
image_(image),
- auto_release_(auto_release) {}
+ auto_release_(auto_release),
+ buffer_(buffer) {}
QuartzImage::~QuartzImage() {
if (auto_release_) {
@@ -26,4 +29,28 @@ std::unique_ptr<IImage> QuartzImage::CreateWithRect(const Rect& rect) {
return std::make_unique<QuartzImage>(GetGraphicsFactory(), image_factory_,
new_cg_image, true);
}
+
+std::unique_ptr<IPainter> QuartzImage::CreatePainter() {
+ if (!buffer_)
+ throw Exception(
+ u"Failed to create painter for image because failed to get its "
+ u"buffer.");
+
+ auto width = CGImageGetWidth(image_);
+ auto height = CGImageGetHeight(image_);
+ auto bits_per_component = CGImageGetBitsPerComponent(image_);
+ auto bytes_per_row = CGImageGetBytesPerRow(image_);
+ auto color_space = CGImageGetColorSpace(image_);
+ auto bitmap_info = CGImageGetBitmapInfo(image_);
+
+ auto cg_context =
+ CGBitmapContextCreate(buffer_, width, height, bits_per_component,
+ bytes_per_row, color_space, bitmap_info);
+
+ return std::make_unique<QuartzCGContextPainter>(
+ GetGraphicsFactory(), cg_context, true, Size(width, height),
+ [](QuartzCGContextPainter* painter) {
+
+ });
+}
} // namespace cru::platform::graphics::osx::quartz
diff --git a/src/osx/graphics/quartz/ImageFactory.cpp b/src/osx/graphics/quartz/ImageFactory.cpp
index 7796ef3a..a8c17719 100644
--- a/src/osx/graphics/quartz/ImageFactory.cpp
+++ b/src/osx/graphics/quartz/ImageFactory.cpp
@@ -1,4 +1,5 @@
#include "cru/osx/graphics/quartz/ImageFactory.h"
+#include "cru/common/Exception.h"
#include "cru/osx/graphics/quartz/Convert.h"
#include "cru/osx/graphics/quartz/Image.h"
#include "cru/platform/graphics/Image.h"
@@ -20,12 +21,36 @@ std::unique_ptr<IImage> QuartzImageFactory::DecodeFromStream(
CGImageRef cg_image =
CGImageSourceCreateImageAtIndex(image_source, 0, nullptr);
- QuartzImage* image =
- new QuartzImage(GetGraphicsFactory(), this, cg_image, true);
-
- CFRelease(cg_image);
+ CFRelease(image_source);
CGDataProviderRelease(data_provider);
- return std::unique_ptr<IImage>(image);
+ return std::unique_ptr<IImage>(
+ new QuartzImage(GetGraphicsFactory(), this, cg_image, true));
+}
+
+std::unique_ptr<IImage> QuartzImageFactory::CreateBitmap(int width,
+ int height) {
+ if (width <= 0) throw Exception(u"Image width should be greater than 0.");
+ if (height <= 0) throw Exception(u"Image height should be greater than 0.");
+
+ CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
+
+ const auto buffer_size = width * height * 4;
+ auto buffer = new unsigned char[buffer_size];
+
+ auto cg_data_provider = CGDataProviderCreateWithData(
+ nullptr, buffer, buffer_size,
+ [](void* info, const void* data, size_t size) {
+ delete[] static_cast<const unsigned char*>(data);
+ });
+
+ auto cg_image = CGImageCreate(width, height, 8, 32, 4 * width, color_space,
+ kCGImageAlphaLast, nullptr, nullptr, true,
+ kCGRenderingIntentDefault);
+
+ CGColorSpaceRelease(color_space);
+
+ return std::unique_ptr<IImage>(
+ new QuartzImage(GetGraphicsFactory(), this, cg_image, true, buffer));
}
} // namespace cru::platform::graphics::osx::quartz
diff --git a/src/platform/Color.cpp b/src/platform/Color.cpp
index dd1736ca..81709c6f 100644
--- a/src/platform/Color.cpp
+++ b/src/platform/Color.cpp
@@ -1,9 +1,10 @@
#include "cru/platform/Color.h"
+#include <gsl/gsl>
+
#include <algorithm>
#include <cmath>
#include <cstdint>
-#include <gsl/gsl>
#include <optional>
#include <stdexcept>
#include <string>
diff --git a/src/platform/graphics/CMakeLists.txt b/src/platform/graphics/CMakeLists.txt
index 1d983188..dc0a429e 100644
--- a/src/platform/graphics/CMakeLists.txt
+++ b/src/platform/graphics/CMakeLists.txt
@@ -1,6 +1,7 @@
add_library(cru_platform_graphics SHARED
ForDllExport.cpp
Geometry.cpp
+ Image.cpp
NullPainter.cpp
)
target_compile_definitions(cru_platform_graphics PRIVATE CRU_PLATFORM_GRAPHICS_EXPORT_API)
diff --git a/src/platform/graphics/Image.cpp b/src/platform/graphics/Image.cpp
new file mode 100644
index 00000000..2c787dd7
--- /dev/null
+++ b/src/platform/graphics/Image.cpp
@@ -0,0 +1,15 @@
+#include "cru/platform/graphics/Image.h"
+
+#include "cru/platform/graphics/Factory.h"
+#include "cru/platform/graphics/ImageFactory.h"
+#include "cru/platform/graphics/Painter.h"
+
+namespace cru::platform::graphics {
+std::unique_ptr<IImage> IImage::CloneToBitmap() {
+ auto image = GetGraphicsFactory()->GetImageFactory()->CreateBitmap(
+ GetWidth(), GetHeight());
+ auto painter = image->CreatePainter();
+ painter->DrawImage(Point{}, this);
+ return image;
+}
+} // namespace cru::platform::graphics
diff --git a/src/ui/render/ScrollBar.cpp b/src/ui/render/ScrollBar.cpp
index 710ca832..bcef74a2 100644
--- a/src/ui/render/ScrollBar.cpp
+++ b/src/ui/render/ScrollBar.cpp
@@ -19,7 +19,6 @@
#include <algorithm>
#include <cassert>
#include <chrono>
-#include <gsl/pointers>
#include <memory>
#include <optional>
#include <stdexcept>
diff --git a/src/ui/style/StyleRuleSet.cpp b/src/ui/style/StyleRuleSet.cpp
index 74aba5b1..4deaaefa 100644
--- a/src/ui/style/StyleRuleSet.cpp
+++ b/src/ui/style/StyleRuleSet.cpp
@@ -2,7 +2,6 @@
#include "cru/common/Event.h"
#include "cru/ui/controls/Control.h"
#include "cru/ui/model/IListChangeNotify.h"
-#include "gsl/gsl_assert"
#include <unordered_set>
diff --git a/src/win/graphics/direct/Image.cpp b/src/win/graphics/direct/Image.cpp
index 11f5e763..855098d7 100644
--- a/src/win/graphics/direct/Image.cpp
+++ b/src/win/graphics/direct/Image.cpp
@@ -3,6 +3,7 @@
#include "cru/win/graphics/direct/ConvertUtil.h"
#include "cru/win/graphics/direct/Exception.h"
#include "cru/win/graphics/direct/Factory.h"
+#include "cru/win/graphics/direct/Painter.h"
namespace cru::platform::graphics::win::direct {
Direct2DImage::Direct2DImage(DirectGraphicsFactory* graphics_factory,
@@ -29,4 +30,10 @@ std::unique_ptr<IImage> Direct2DImage::CreateWithRect(const Rect& rect) {
return std::make_unique<Direct2DImage>(GetDirectFactory(), std::move(bitmap));
}
+std::unique_ptr<IPainter> Direct2DImage::CreatePainter() {
+ auto device_context = GetDirectFactory()->CreateD2D1DeviceContext();
+ device_context->SetTarget(d2d_bitmap_.Get());
+ return std::make_unique<D2DDeviceContextPainter>(device_context.Detach(),
+ true);
+}
} // namespace cru::platform::graphics::win::direct
diff --git a/src/win/graphics/direct/ImageFactory.cpp b/src/win/graphics/direct/ImageFactory.cpp
index e7794aa5..49fc49e8 100644
--- a/src/win/graphics/direct/ImageFactory.cpp
+++ b/src/win/graphics/direct/ImageFactory.cpp
@@ -1,4 +1,5 @@
#include "cru/win/graphics/direct/ImageFactory.h"
+#include "cru/common/platform/win/Exception.h"
#include "cru/common/platform/win/StreamConvert.h"
#include "cru/win/graphics/direct/Exception.h"
#include "cru/win/graphics/direct/Factory.h"
@@ -42,4 +43,23 @@ std::unique_ptr<IImage> WinImageFactory::DecodeFromStream(io::Stream* stream) {
return std::make_unique<Direct2DImage>(graphics_factory_,
std::move(d2d_image));
}
+
+std::unique_ptr<IImage> WinImageFactory::CreateBitmap(int width, int height) {
+ if (width <= 0) throw Exception(u"Bitmap width must be greater than 0.");
+ if (height <= 0) throw Exception(u"Bitmap height must be greater than 0.");
+
+ Microsoft::WRL::ComPtr<ID2D1Bitmap> bitmap;
+
+ auto d2d_context = graphics_factory_->GetDefaultD2D1DeviceContext();
+ d2d_context->CreateBitmap(
+ D2D1::SizeU(width, height),
+ D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_R8G8B8A8_UINT,
+ D2D1_ALPHA_MODE_STRAIGHT)),
+ &bitmap);
+
+ Microsoft::WRL::ComPtr<ID2D1Bitmap1> bitmap1;
+ ThrowIfFailed(bitmap.As(&bitmap1), "Failed to convert bitmap to bitmap1.");
+
+ return std::make_unique<Direct2DImage>(graphics_factory_, std::move(bitmap1));
+}
} // namespace cru::platform::graphics::win::direct
diff --git a/src/win/graphics/direct/Painter.cpp b/src/win/graphics/direct/Painter.cpp
index 32386877..3e8bd5c1 100644
--- a/src/win/graphics/direct/Painter.cpp
+++ b/src/win/graphics/direct/Painter.cpp
@@ -1,5 +1,6 @@
#include "cru/win/graphics/direct/Painter.h"
+#include "cru/common/log/Logger.h"
#include "cru/platform/Check.h"
#include "cru/win/graphics/direct/Brush.h"
#include "cru/win/graphics/direct/ConvertUtil.h"
@@ -12,9 +13,22 @@
namespace cru::platform::graphics::win::direct {
D2DDeviceContextPainter::D2DDeviceContextPainter(
- ID2D1DeviceContext1* device_context) {
+ ID2D1DeviceContext1* device_context, bool release) {
Expects(device_context);
device_context_ = device_context;
+ release_ = release;
+ device_context->BeginDraw();
+}
+
+D2DDeviceContextPainter::~D2DDeviceContextPainter() {
+ if (is_drawing_) {
+ CRU_LOG_INFO(u"You may forget to call EndDraw before destroying painter.");
+ }
+
+ if (release_) {
+ device_context_->Release();
+ device_context_ = nullptr;
+ }
}
platform::Matrix D2DDeviceContextPainter::GetTransform() {
@@ -155,6 +169,7 @@ void D2DDeviceContextPainter::PopState() {
void D2DDeviceContextPainter::EndDraw() {
if (is_drawing_) {
is_drawing_ = false;
+ ThrowIfFailed(device_context_->EndDraw());
DoEndDraw();
}
}
diff --git a/src/win/graphics/direct/WindowPainter.cpp b/src/win/graphics/direct/WindowPainter.cpp
index 0f78119e..c0808887 100644
--- a/src/win/graphics/direct/WindowPainter.cpp
+++ b/src/win/graphics/direct/WindowPainter.cpp
@@ -7,14 +7,9 @@
namespace cru::platform::graphics::win::direct {
D2DWindowPainter::D2DWindowPainter(D2DWindowRenderTarget* render_target)
: D2DDeviceContextPainter(render_target->GetD2D1DeviceContext()),
- render_target_(render_target) {
- render_target_->GetD2D1DeviceContext()->BeginDraw();
-}
+ render_target_(render_target) {}
-D2DWindowPainter::~D2DWindowPainter() { EndDraw(); }
+D2DWindowPainter::~D2DWindowPainter() {}
-void D2DWindowPainter::DoEndDraw() {
- ThrowIfFailed(render_target_->GetD2D1DeviceContext()->EndDraw());
- render_target_->Present();
-}
+void D2DWindowPainter::DoEndDraw() { render_target_->Present(); }
} // namespace cru::platform::graphics::win::direct
diff --git a/src/win/gui/TimerManager.cpp b/src/win/gui/TimerManager.cpp
index ce8fead6..a09ee0bf 100644
--- a/src/win/gui/TimerManager.cpp
+++ b/src/win/gui/TimerManager.cpp
@@ -2,7 +2,6 @@
#include "cru/win/gui/Base.h"
#include "cru/win/gui/Exception.h"
-#include "gsl/gsl_util"
#include <functional>
#include <type_traits>
diff --git a/tools/Use-VC.ps1 b/tools/Use-VC.ps1
index fc039142..ad3dbcba 100644
--- a/tools/Use-VC.ps1
+++ b/tools/Use-VC.ps1
@@ -20,5 +20,5 @@ function Use-VC {
}
}
Pop-Location
- Write-Host "Visual Studio 2019 Command Prompt variables set." -ForegroundColor Yellow
+ Write-Host "Visual Studio Command Prompt variables set." -ForegroundColor Yellow
}