diff options
Diffstat (limited to 'demos/platform')
-rw-r--r-- | demos/platform/CMakeLists.txt | 2 | ||||
-rw-r--r-- | demos/platform/graphics/Base.cpp | 37 | ||||
-rw-r--r-- | demos/platform/graphics/Base.h | 25 | ||||
-rw-r--r-- | demos/platform/graphics/CMakeLists.txt | 12 | ||||
-rw-r--r-- | demos/platform/graphics/DrawCircle.cpp | 17 | ||||
-rw-r--r-- | demos/platform/graphics/DrawText.cpp | 27 | ||||
-rw-r--r-- | demos/platform/graphics/SvgPath.cpp | 28 | ||||
-rw-r--r-- | demos/platform/gui/CMakeLists.txt | 5 | ||||
-rw-r--r-- | demos/platform/gui/EmptyWindow.cpp | 25 |
9 files changed, 178 insertions, 0 deletions
diff --git a/demos/platform/CMakeLists.txt b/demos/platform/CMakeLists.txt new file mode 100644 index 00000000..36673acd --- /dev/null +++ b/demos/platform/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(graphics) +add_subdirectory(gui) 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 new file mode 100644 index 00000000..07758ee6 --- /dev/null +++ b/demos/platform/graphics/CMakeLists.txt @@ -0,0 +1,12 @@ +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) + +add_executable(CruDemoPlatformGraphicsDrawText DrawText.cpp) +target_link_libraries(CruDemoPlatformGraphicsDrawText PRIVATE CruDemoPlatformGraphicsBase) + +add_executable(CruDemoPlatformGraphicsSvgPath SvgPath.cpp) +target_link_libraries(CruDemoPlatformGraphicsSvgPath PRIVATE CruDemoPlatformGraphicsBase) diff --git a/demos/platform/graphics/DrawCircle.cpp b/demos/platform/graphics/DrawCircle.cpp new file mode 100644 index 00000000..76855e0a --- /dev/null +++ b/demos/platform/graphics/DrawCircle.cpp @@ -0,0 +1,17 @@ +#include "Base.h" +#include "cru/platform/Color.h" +#include "cru/platform/graphics/Factory.h" +#include "cru/platform/graphics/Painter.h" + +#include <memory> + +int main() { + CruPlatformGraphicsDemo demo("draw-circle-demo.png", 500, 500); + + 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/DrawText.cpp b/demos/platform/graphics/DrawText.cpp new file mode 100644 index 00000000..dfea18cd --- /dev/null +++ b/demos/platform/graphics/DrawText.cpp @@ -0,0 +1,27 @@ +#include "Base.h" +#include "cru/platform/Color.h" +#include "cru/platform/graphics/Factory.h" +#include "cru/platform/graphics/Font.h" +#include "cru/platform/graphics/Painter.h" + +#include <iostream> +#include <memory> + +int main() { + CruPlatformGraphicsDemo demo("draw-text-demo.png", 500, 200); + + auto brush = + demo.GetFactory()->CreateSolidColorBrush(cru::platform::colors::skyblue); + + std::shared_ptr<cru::platform::graphics::IFont> font( + demo.GetFactory()->CreateFont(u"", 24)); + auto text_layout = demo.GetFactory()->CreateTextLayout(font, u"Hello world!"); + demo.GetPainter()->DrawText({0, 0}, text_layout.get(), brush.get()); + + auto bounds = text_layout->GetTextBounds(); + std::cout << "Bounds of text:\n\tx: " << bounds.left + << "\n\ty: " << bounds.top << "\n\twidth: " << bounds.width + << "\n\theight: " << bounds.height << std::endl; + + return 0; +} diff --git a/demos/platform/graphics/SvgPath.cpp b/demos/platform/graphics/SvgPath.cpp new file mode 100644 index 00000000..d0827878 --- /dev/null +++ b/demos/platform/graphics/SvgPath.cpp @@ -0,0 +1,28 @@ + +#include "Base.h" +#include "cru/platform/Color.h" +#include "cru/platform/Matrix.h" +#include "cru/platform/graphics/Factory.h" +#include "cru/platform/graphics/Painter.h" + +#include <memory> + +int main() { + CruPlatformGraphicsDemo demo("svg-path-demo.png", 160, 160); + + auto brush = + demo.GetFactory()->CreateSolidColorBrush(cru::platform::colors::black); + + 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 +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 .707.708l.746-.746A6.97 6.97 0 0 0 8 16a6.97 6.97 0 0 0 3.422-.892l.746.746a.5.5 0 0 0 .707-.708l-.601-.602A7.001 7.001 0 0 0 9 2.07V1h.5a.5.5 0 0 0 0-1h-3zm1.038 3.018a6.093 6.093 0 0 1 .924 0 6 6 0 1 1-.924 0zM0 3.5c0 .753.333 1.429.86 1.887A8.035 8.035 0 0 1 4.387 1.86 2.5 2.5 0 0 0 0 3.5zM13.5 1c-.753 0-1.429.333-1.887.86a8.035 8.035 0 0 1 3.527 3.527A2.5 2.5 0 0 0 13.5 1z + )"); + auto geometry = geometry_builder->Build(); + + demo.GetPainter()->ConcatTransform(cru::platform::Matrix::Scale(10, 10)); + demo.GetPainter()->FillGeometry(geometry.get(), brush.get()); + + return 0; +} diff --git a/demos/platform/gui/CMakeLists.txt b/demos/platform/gui/CMakeLists.txt new file mode 100644 index 00000000..b179fbc6 --- /dev/null +++ b/demos/platform/gui/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(CruDemoPlatformGuiBase INTERFACE) +target_link_libraries(CruDemoPlatformGuiBase INTERFACE CruPlatformBootstrap) + +add_executable(CruDemoPlatformGuiEmptyWindow EmptyWindow.cpp) +target_link_libraries(CruDemoPlatformGuiEmptyWindow PRIVATE CruDemoPlatformGuiBase) diff --git a/demos/platform/gui/EmptyWindow.cpp b/demos/platform/gui/EmptyWindow.cpp new file mode 100644 index 00000000..299ddd17 --- /dev/null +++ b/demos/platform/gui/EmptyWindow.cpp @@ -0,0 +1,25 @@ +#include "cru/platform/bootstrap/Bootstrap.h" +#include "cru/platform/gui/Base.h" +#include "cru/platform/gui/UiApplication.h" +#include "cru/platform/gui/Window.h" + +using cru::platform::gui::INativeWindow; +using cru::platform::gui::IUiApplication; +using cru::platform::gui::WindowVisibilityType; + +int main() { + std::unique_ptr<IUiApplication> application( + cru::platform::bootstrap::CreateUiApplication()); + + application->SetQuitOnAllWindowClosed(true); + + std::unique_ptr<INativeWindow> window1(application->CreateWindow()); + window1->SetVisibility(WindowVisibilityType::Show); + window1->SetToForeground(); + + std::unique_ptr<INativeWindow> window2(application->CreateWindow()); + window2->SetVisibility(WindowVisibilityType::Show); + window2->SetToForeground(); + + return application->Run(); +} |