aboutsummaryrefslogtreecommitdiff
path: root/demos/platform
diff options
context:
space:
mode:
Diffstat (limited to 'demos/platform')
-rw-r--r--demos/platform/graphics/Base.cpp37
-rw-r--r--demos/platform/graphics/Base.h25
-rw-r--r--demos/platform/graphics/CMakeLists.txt1
-rw-r--r--demos/platform/graphics/DrawCircle.cpp26
-rw-r--r--demos/platform/graphics/SvgPath.cpp25
5 files changed, 75 insertions, 39 deletions
diff --git a/demos/platform/graphics/Base.cpp b/demos/platform/graphics/Base.cpp
new file mode 100644
index 00000000..be39f7e0
--- /dev/null
+++ b/demos/platform/graphics/Base.cpp
@@ -0,0 +1,37 @@
+#include "Base.h"
+#include <cru/base/io/CFileStream.h>
+#include <cru/platform/bootstrap/GraphicsBootstrap.h>
+#include <cru/platform/graphics/Factory.h>
+#include <cru/platform/graphics/Image.h>
+#include <cru/platform/graphics/ImageFactory.h>
+
+CruPlatformGraphicsDemo::CruPlatformGraphicsDemo(std::string file_name,
+ int width, int height)
+ : file_name_(std::move(file_name)) {
+ factory_.reset(cru::platform::bootstrap::CreateGraphicsFactory());
+ image_ = factory_->GetImageFactory()->CreateBitmap(width, height);
+ painter_ = image_->CreatePainter();
+}
+
+CruPlatformGraphicsDemo::~CruPlatformGraphicsDemo() {
+ painter_->EndDraw();
+
+ cru::io::CFileStream file_stream(file_name_.c_str(), "wb");
+
+ factory_->GetImageFactory()->EncodeToStream(
+ image_.get(), &file_stream, cru::platform::graphics::ImageFormat::Png,
+ 1.0f);
+}
+
+cru::platform::graphics::IGraphicsFactory*
+CruPlatformGraphicsDemo::GetFactory() {
+ return factory_.get();
+}
+
+cru::platform::graphics::IImage* CruPlatformGraphicsDemo::GetImage() {
+ return image_.get();
+}
+
+cru::platform::graphics::IPainter* CruPlatformGraphicsDemo::GetPainter() {
+ return painter_.get();
+}
diff --git a/demos/platform/graphics/Base.h b/demos/platform/graphics/Base.h
new file mode 100644
index 00000000..6f4f4929
--- /dev/null
+++ b/demos/platform/graphics/Base.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <cru/base/Base.h>
+#include <cru/platform/graphics/Factory.h>
+#include <cru/platform/graphics/Image.h>
+#include <cru/platform/graphics/Painter.h>
+
+#include <memory>
+#include <string>
+
+class CruPlatformGraphicsDemo : public cru::Object2 {
+ public:
+ CruPlatformGraphicsDemo(std::string file_name, int width, int height);
+ ~CruPlatformGraphicsDemo() override;
+
+ cru::platform::graphics::IGraphicsFactory* GetFactory();
+ cru::platform::graphics::IImage* GetImage();
+ cru::platform::graphics::IPainter* GetPainter();
+
+ private:
+ std::string file_name_;
+ std::unique_ptr<cru::platform::graphics::IGraphicsFactory> factory_;
+ std::unique_ptr<cru::platform::graphics::IImage> image_;
+ std::unique_ptr<cru::platform::graphics::IPainter> painter_;
+};
diff --git a/demos/platform/graphics/CMakeLists.txt b/demos/platform/graphics/CMakeLists.txt
index e2a96c09..ff5f64a5 100644
--- a/demos/platform/graphics/CMakeLists.txt
+++ b/demos/platform/graphics/CMakeLists.txt
@@ -1,5 +1,6 @@
add_library(CruDemoPlatformGraphicsBase INTERFACE)
target_link_libraries(CruDemoPlatformGraphicsBase INTERFACE CruPlatformGraphicsBootstrap)
+target_sources(CruDemoPlatformGraphicsBase INTERFACE Base.cpp)
add_executable(CruDemoPlatformGraphicsDrawCircle DrawCircle.cpp)
target_link_libraries(CruDemoPlatformGraphicsDrawCircle PRIVATE CruDemoPlatformGraphicsBase)
diff --git a/demos/platform/graphics/DrawCircle.cpp b/demos/platform/graphics/DrawCircle.cpp
index d3246527..76855e0a 100644
--- a/demos/platform/graphics/DrawCircle.cpp
+++ b/demos/platform/graphics/DrawCircle.cpp
@@ -1,31 +1,17 @@
-#include "cru/base/io/CFileStream.h"
+#include "Base.h"
#include "cru/platform/Color.h"
-#include "cru/platform/bootstrap/GraphicsBootstrap.h"
#include "cru/platform/graphics/Factory.h"
-#include "cru/platform/graphics/ImageFactory.h"
#include "cru/platform/graphics/Painter.h"
#include <memory>
int main() {
- std::unique_ptr<cru::platform::graphics::IGraphicsFactory> graphics_factory(
- cru::platform::bootstrap::CreateGraphicsFactory());
+ CruPlatformGraphicsDemo demo("draw-circle-demo.png", 500, 500);
- auto image = graphics_factory->GetImageFactory()->CreateBitmap(500, 500);
-
- {
- auto brush =
- graphics_factory->CreateSolidColorBrush(cru::platform::colors::skyblue);
- auto painter = image->CreatePainter();
- painter->FillEllipse(cru::platform::Rect{200, 200, 100, 100}, brush.get());
- painter->EndDraw();
- }
-
- cru::io::CFileStream file_stream("draw-circle-demo.png", "wb");
-
- graphics_factory->GetImageFactory()->EncodeToStream(
- image.get(), &file_stream, cru::platform::graphics::ImageFormat::Png,
- 1.0f);
+ auto brush =
+ demo.GetFactory()->CreateSolidColorBrush(cru::platform::colors::skyblue);
+ demo.GetPainter()->FillEllipse(cru::platform::Rect{200, 200, 100, 100},
+ brush.get());
return 0;
}
diff --git a/demos/platform/graphics/SvgPath.cpp b/demos/platform/graphics/SvgPath.cpp
index 332456a8..d0827878 100644
--- a/demos/platform/graphics/SvgPath.cpp
+++ b/demos/platform/graphics/SvgPath.cpp
@@ -1,22 +1,19 @@
-#include "cru/base/io/CFileStream.h"
+#include "Base.h"
#include "cru/platform/Color.h"
#include "cru/platform/Matrix.h"
-#include "cru/platform/bootstrap/GraphicsBootstrap.h"
#include "cru/platform/graphics/Factory.h"
-#include "cru/platform/graphics/ImageFactory.h"
#include "cru/platform/graphics/Painter.h"
#include <memory>
int main() {
- std::unique_ptr<cru::platform::graphics::IGraphicsFactory> graphics_factory(
- cru::platform::bootstrap::CreateGraphicsFactory());
+ CruPlatformGraphicsDemo demo("svg-path-demo.png", 160, 160);
auto brush =
- graphics_factory->CreateSolidColorBrush(cru::platform::colors::black);
+ demo.GetFactory()->CreateSolidColorBrush(cru::platform::colors::black);
- auto geometry_builder = graphics_factory->CreateGeometryBuilder();
+ auto geometry_builder = demo.GetFactory()->CreateGeometryBuilder();
geometry_builder->ParseAndApplySvgPathData(
uR"(
M8.5 5.5a.5.5 0 0 0-1 0v3.362l-1.429 2.38a.5.5 0 1 0 .858.515l1.5-2.5A.5.5 0 0 0 8.5 9V5.5z
@@ -24,18 +21,8 @@ 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
)");
auto geometry = geometry_builder->Build();
- auto image = graphics_factory->GetImageFactory()->CreateBitmap(160, 160);
- auto painter = image->CreatePainter();
-
- painter->ConcatTransform(cru::platform::Matrix::Scale(10, 10));
- painter->FillGeometry(geometry.get(), brush.get());
- painter->EndDraw();
-
- cru::io::CFileStream file_stream("./svg-path-demo.png", "wb");
-
- graphics_factory->GetImageFactory()->EncodeToStream(
- image.get(), &file_stream, cru::platform::graphics::ImageFormat::Png,
- 1.0f);
+ demo.GetPainter()->ConcatTransform(cru::platform::Matrix::Scale(10, 10));
+ demo.GetPainter()->FillGeometry(geometry.get(), brush.get());
return 0;
}