aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-09-12 00:42:36 +0800
committerYuqian Yang <crupest@crupest.life>2025-09-12 00:42:36 +0800
commit27f153af927c056af5cec70db5f011752e29b156 (patch)
treecdbe0a61f037cd1641f23f681d221a3206454411
parent7c135df9b8057e1de3ea6a73fb785d5622b519d4 (diff)
downloadcru-27f153af927c056af5cec70db5f011752e29b156.tar.gz
cru-27f153af927c056af5cec70db5f011752e29b156.tar.bz2
cru-27f153af927c056af5cec70db5f011752e29b156.zip
Organize bootstrap and graphics demos.
-rw-r--r--demos/CMakeLists.txt7
-rw-r--r--demos/Graphics/CMakeLists.txt8
-rw-r--r--demos/Graphics/DrawCircle.cpp (renamed from demos/Graphics/DrawCircle/DrawCircle.cpp)12
-rw-r--r--demos/Graphics/DrawCircle/CMakeLists.txt7
-rw-r--r--demos/Graphics/DrawCircle/CairoMain.cpp12
-rw-r--r--demos/Graphics/DrawCircle/DrawCircle.h7
-rw-r--r--demos/Graphics/DrawCircle/PlatformMain.cpp12
-rw-r--r--demos/Graphics/SvgPath.cpp41
-rw-r--r--demos/SvgPath/CMakeLists.txt12
-rw-r--r--demos/SvgPath/main.cpp48
-rw-r--r--include/cru/platform/bootstrap/Bootstrap.h4
-rw-r--r--include/cru/platform/bootstrap/GraphicsBootstrap.h17
-rw-r--r--src/platform/bootstrap/Bootstrap.cpp30
-rw-r--r--src/platform/bootstrap/CMakeLists.txt10
-rw-r--r--src/platform/bootstrap/GraphicsBootstrap.cpp25
15 files changed, 116 insertions, 136 deletions
diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt
index 30a63e72..c0a6e268 100644
--- a/demos/CMakeLists.txt
+++ b/demos/CMakeLists.txt
@@ -2,17 +2,15 @@ add_library(CruDemoBase INTERFACE)
target_link_libraries(CruDemoBase INTERFACE CruPlatformBootstrap)
-if(WIN32)
- add_subdirectory(Graphics/DrawCircle)
+add_subdirectory(Graphics)
+if(WIN32)
add_subdirectory(main)
add_subdirectory(ScrollView)
add_subdirectory(InputMethod)
add_subdirectory(SvgPath)
elseif(APPLE)
- add_subdirectory(Graphics/DrawCircle)
-
add_subdirectory(main)
add_subdirectory(ScrollView)
@@ -21,7 +19,6 @@ elseif(APPLE)
elseif(EMSCRIPTEN)
elseif(UNIX)
- add_subdirectory(Graphics/DrawCircle)
add_subdirectory(xcb)
endif()
diff --git a/demos/Graphics/CMakeLists.txt b/demos/Graphics/CMakeLists.txt
new file mode 100644
index 00000000..64982a19
--- /dev/null
+++ b/demos/Graphics/CMakeLists.txt
@@ -0,0 +1,8 @@
+add_library(CruDemoGraphicsBase INTERFACE)
+target_link_libraries(CruDemoGraphicsBase INTERFACE CruPlatformGraphicsBootstrap)
+
+add_executable(CruDemoGraphicsDrawCircle DrawCircle.cpp)
+target_link_libraries(CruDemoGraphicsDrawCircle PRIVATE CruDemoGraphicsBase)
+
+add_executable(CruDemoGraphicsSvgPath SvgPath.cpp)
+target_link_libraries(CruDemoGraphicsSvgPath PRIVATE CruDemoGraphicsBase)
diff --git a/demos/Graphics/DrawCircle/DrawCircle.cpp b/demos/Graphics/DrawCircle.cpp
index a28bc4e4..db63c7dc 100644
--- a/demos/Graphics/DrawCircle/DrawCircle.cpp
+++ b/demos/Graphics/DrawCircle.cpp
@@ -1,13 +1,15 @@
#include "cru/base/io/CFileStream.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>
-namespace cru::demos::graphics {
- void DrawCircle(platform::graphics::IGraphicsFactory* graphics_factory) {
+int main() {
+ std::unique_ptr<cru::platform::graphics::IGraphicsFactory> graphics_factory(
+ cru::platform::bootstrap::CreateGraphicsFactory());
auto image = graphics_factory->GetImageFactory()->CreateBitmap(500, 500);
@@ -19,11 +21,11 @@ namespace cru::demos::graphics {
painter->EndDraw();
}
- cru::io::CFileStream file_stream("./test_image.png", "w");
+ cru::io::CFileStream file_stream("draw-circle-demo.png", "w");
graphics_factory->GetImageFactory()->EncodeToStream(
image.get(), &file_stream, cru::platform::graphics::ImageFormat::Png,
1.0f);
- }
-}
+ return 0;
+}
diff --git a/demos/Graphics/DrawCircle/CMakeLists.txt b/demos/Graphics/DrawCircle/CMakeLists.txt
deleted file mode 100644
index 51531d4c..00000000
--- a/demos/Graphics/DrawCircle/CMakeLists.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-add_executable(CruDemoGraphicsDrawCircle DrawCircle.cpp PlatformMain.cpp)
-target_link_libraries(CruDemoGraphicsDrawCircle PRIVATE CruDemoBase)
-
-if(TARGET CruPlatformGraphicsCairo)
- add_executable(CruDemoGraphicsDrawCircleCairo DrawCircle.cpp CairoMain.cpp)
- target_link_libraries(CruDemoGraphicsDrawCircleCairo PRIVATE CruDemoBase CruPlatformGraphicsCairo)
-endif()
diff --git a/demos/Graphics/DrawCircle/CairoMain.cpp b/demos/Graphics/DrawCircle/CairoMain.cpp
deleted file mode 100644
index 35d6840b..00000000
--- a/demos/Graphics/DrawCircle/CairoMain.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "cru/platform/graphics/cairo/CairoGraphicsFactory.h"
-
-#include "DrawCircle.h"
-
-int main() {
- std::unique_ptr<cru::platform::graphics::IGraphicsFactory> graphics_factory(
- new cru::platform::graphics::cairo::CairoGraphicsFactory());
-
- cru::demos::graphics::DrawCircle(graphics_factory.get());
-
- return 0;
-}
diff --git a/demos/Graphics/DrawCircle/DrawCircle.h b/demos/Graphics/DrawCircle/DrawCircle.h
deleted file mode 100644
index 8e14dc84..00000000
--- a/demos/Graphics/DrawCircle/DrawCircle.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#pragma once
-#include "cru/platform/graphics/Factory.h"
-
-namespace cru::demos::graphics {
- void DrawCircle(platform::graphics::IGraphicsFactory* graphics_factory);
-}
-
diff --git a/demos/Graphics/DrawCircle/PlatformMain.cpp b/demos/Graphics/DrawCircle/PlatformMain.cpp
deleted file mode 100644
index 3da1ec89..00000000
--- a/demos/Graphics/DrawCircle/PlatformMain.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "cru/platform/bootstrap/Bootstrap.h"
-
-#include "DrawCircle.h"
-
-int main() {
- std::unique_ptr<cru::platform::graphics::IGraphicsFactory> graphics_factory(
- cru::platform::bootstrap::CreateGraphicsFactory());
-
- cru::demos::graphics::DrawCircle(graphics_factory.get());
-
- return 0;
-}
diff --git a/demos/Graphics/SvgPath.cpp b/demos/Graphics/SvgPath.cpp
new file mode 100644
index 00000000..181cb297
--- /dev/null
+++ b/demos/Graphics/SvgPath.cpp
@@ -0,0 +1,41 @@
+
+#include "cru/base/io/CFileStream.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());
+
+ auto brush =
+ graphics_factory->CreateSolidColorBrush(cru::platform::colors::black);
+
+ auto geometry_builder = graphics_factory->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();
+
+ auto image = graphics_factory->GetImageFactory()->CreateBitmap(1000, 1000);
+ auto painter = image->CreatePainter();
+
+ painter->PushState();
+ painter->FillGeometry(geometry.get(), brush.get());
+ painter->PopState();
+ painter->EndDraw();
+
+ cru::io::CFileStream file_stream("./svg-path-demo.png", "w");
+
+ graphics_factory->GetImageFactory()->EncodeToStream(
+ image.get(), &file_stream, cru::platform::graphics::ImageFormat::Png,
+ 1.0f);
+
+ return 0;
+}
diff --git a/demos/SvgPath/CMakeLists.txt b/demos/SvgPath/CMakeLists.txt
deleted file mode 100644
index 34120111..00000000
--- a/demos/SvgPath/CMakeLists.txt
+++ /dev/null
@@ -1,12 +0,0 @@
-add_executable(CruDemoSvgPath main.cpp)
-
-if(APPLE)
- set_target_properties(CruDemoSvgPath PROPERTIES
- MACOSX_BUNDLE TRUE
- MACOSX_BUNDLE_BUNDLE_NAME cru-demo-svg-path
- MACOSX_BUNDLE_GUI_IDENTIFIER life.crupest.demo-svg-path
- )
-endif()
-
-target_link_libraries(CruDemoSvgPath PRIVATE CruDemoBase)
-
diff --git a/demos/SvgPath/main.cpp b/demos/SvgPath/main.cpp
deleted file mode 100644
index 87c410e9..00000000
--- a/demos/SvgPath/main.cpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#include "cru/platform/Color.h"
-#include "cru/platform/bootstrap/Bootstrap.h"
-#include "cru/platform/graphics/Factory.h"
-#include "cru/platform/graphics/Font.h"
-#include "cru/platform/graphics/Painter.h"
-#include "cru/platform/gui/InputMethod.h"
-#include "cru/platform/gui/UiApplication.h"
-#include "cru/platform/gui/Window.h"
-
-int main() {
- using namespace cru;
- using namespace cru::platform;
- using namespace cru::platform::graphics;
- using namespace cru::platform::gui;
-
- IUiApplication* application = bootstrap::CreateUiApplication();
-
- auto graphics_factory = application->GetGraphicsFactory();
-
- auto window = application->CreateWindow();
-
- auto brush = graphics_factory->CreateSolidColorBrush(colors::black);
-
- auto create_geometry = [graphics_factory](StringView path_d) {
- auto geometry_builder = graphics_factory->CreateGeometryBuilder();
- geometry_builder->ParseAndApplySvgPathData(path_d);
- return geometry_builder->Build();
- };
-
- auto geometry = create_geometry(
- 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
- )");
-
- window->PaintEvent()->AddHandler([&](auto) {
- auto painter = window->BeginPaint();
- painter->PushState();
- painter->ConcatTransform(Matrix::Scale(10, 10));
- painter->FillGeometry(geometry.get(), brush.get());
- painter->PopState();
- painter->EndDraw();
- });
-
- window->SetVisibility(WindowVisibilityType::Show);
-
- return application->Run();
-}
diff --git a/include/cru/platform/bootstrap/Bootstrap.h b/include/cru/platform/bootstrap/Bootstrap.h
index 42b4cbb6..a8a2c16a 100644
--- a/include/cru/platform/bootstrap/Bootstrap.h
+++ b/include/cru/platform/bootstrap/Bootstrap.h
@@ -1,5 +1,4 @@
#pragma once
-#include "cru/platform/graphics/Factory.h"
#include "cru/platform/gui/UiApplication.h"
#ifdef CRU_IS_DLL
@@ -15,7 +14,4 @@
namespace cru::platform::bootstrap {
CRU_PLATFORM_BOOTSTRAP_API cru::platform::gui::IUiApplication*
CreateUiApplication();
-
-CRU_PLATFORM_BOOTSTRAP_API cru::platform::graphics::IGraphicsFactory*
-CreateGraphicsFactory();
} // namespace cru::platform::bootstrap
diff --git a/include/cru/platform/bootstrap/GraphicsBootstrap.h b/include/cru/platform/bootstrap/GraphicsBootstrap.h
new file mode 100644
index 00000000..6f29a62d
--- /dev/null
+++ b/include/cru/platform/bootstrap/GraphicsBootstrap.h
@@ -0,0 +1,17 @@
+#pragma once
+#include "cru/platform/graphics/Factory.h"
+
+#ifdef CRU_IS_DLL
+#ifdef CRU_PLATFORM_GRAPHICS_BOOTSTRAP_EXPORT_API
+#define CRU_PLATFORM_GRAPHICS_BOOTSTRAP_API __declspec(dllexport)
+#else
+#define CRU_PLATFORM_GRAPHICS_BOOTSTRAP_API __declspec(dllimport)
+#endif
+#else
+#define CRU_PLATFORM_GRAPHICS_BOOTSTRAP_API
+#endif
+
+namespace cru::platform::bootstrap {
+CRU_PLATFORM_GRAPHICS_BOOTSTRAP_API cru::platform::graphics::IGraphicsFactory*
+CreateGraphicsFactory();
+} // namespace cru::platform::bootstrap
diff --git a/src/platform/bootstrap/Bootstrap.cpp b/src/platform/bootstrap/Bootstrap.cpp
index 5c47e95c..e9183550 100644
--- a/src/platform/bootstrap/Bootstrap.cpp
+++ b/src/platform/bootstrap/Bootstrap.cpp
@@ -1,39 +1,21 @@
#include "cru/platform/bootstrap/Bootstrap.h"
+#include "cru/base/Base.h"
-#ifdef CRU_PLATFORM_WINDOWS
-#include "cru/platform/graphics/direct2d/Factory.h"
+#if defined(_WIN32)
#include "cru/platform/gui/win/UiApplication.h"
-#elif defined(CRU_PLATFORM_OSX)
-#include "cru/platform/graphics/quartz/Factory.h"
+#elif defined(__APPLE__)
#include "cru/platform/gui/osx/UiApplication.h"
-#elif defined(CRU_PLATFORM_EMSCRIPTEN)
-// TODO: Go fill this!
#else
-#include "cru/platform/graphics/cairo/CairoGraphicsFactory.h"
#endif
namespace cru::platform::bootstrap {
cru::platform::gui::IUiApplication* CreateUiApplication() {
-#ifdef CRU_PLATFORM_WINDOWS
+#if defined(_WIN32)
return new cru::platform::gui::win::WinUiApplication();
-#elif defined(CRU_PLATFORM_OSX)
+#elif defined(__APPLE__)
return new cru::platform::gui::osx::OsxUiApplication();
#else
- return nullptr;
+ NotImplemented();
#endif
}
-
-CRU_PLATFORM_BOOTSTRAP_API cru::platform::graphics::IGraphicsFactory*
-CreateGraphicsFactory() {
-#ifdef CRU_PLATFORM_WINDOWS
- return new cru::platform::graphics::direct2d::DirectGraphicsFactory();
-#elif defined(CRU_PLATFORM_OSX)
- return new cru::platform::graphics::quartz::QuartzGraphicsFactory();
-#elif defined(CRU_PLATFORM_EMSCRIPTEN)
- return nullptr; // TODO: Change this.
-#else
- return new cru::platform::graphics::cairo::CairoGraphicsFactory();
-#endif
-}
-
} // namespace cru::platform::bootstrap
diff --git a/src/platform/bootstrap/CMakeLists.txt b/src/platform/bootstrap/CMakeLists.txt
index 4092c677..f600a75c 100644
--- a/src/platform/bootstrap/CMakeLists.txt
+++ b/src/platform/bootstrap/CMakeLists.txt
@@ -1,14 +1,24 @@
+add_library(CruPlatformGraphicsBootstrap
+ GraphicsBootstrap.cpp
+)
+
add_library(CruPlatformBootstrap
Bootstrap.cpp
)
+target_link_libraries(CruPlatformBootstrap PUBLIC CruPlatformGraphicsBootstrap)
+
if(WIN32)
+ target_link_libraries(CruPlatformGraphicsBootstrap PUBLIC CruPlatformGraphicsDirect)
target_link_libraries(CruPlatformBootstrap PUBLIC CruPlatformGuiWin)
elseif(APPLE)
+ target_link_libraries(CruPlatformGraphicsBootstrap PUBLIC CruPlatformGraphicsQuartz)
target_link_libraries(CruPlatformBootstrap PUBLIC CruPlatformGuiOsx)
elseif(EMSCRIPTEN)
+ target_link_libraries(CruPlatformGraphicsBootstrap PUBLIC CruBase)
target_link_libraries(CruPlatformBootstrap PUBLIC CruBase) # TODO: Remember to change this.
else()
+ target_link_libraries(CruPlatformGraphicsBootstrap PUBLIC CruPlatformGraphicsCairo)
target_link_libraries(CruPlatformBootstrap PUBLIC CruPlatformGraphicsCairo)
endif()
diff --git a/src/platform/bootstrap/GraphicsBootstrap.cpp b/src/platform/bootstrap/GraphicsBootstrap.cpp
new file mode 100644
index 00000000..cce42af8
--- /dev/null
+++ b/src/platform/bootstrap/GraphicsBootstrap.cpp
@@ -0,0 +1,25 @@
+#include "cru/platform/bootstrap/GraphicsBootstrap.h"
+
+#if defined(_WIN32)
+#include "cru/platform/graphics/direct2d/Factory.h"
+#elif defined(__APPLE__)
+#include "cru/platform/graphics/quartz/Factory.h"
+#elif defined(__unix)
+#include "cru/platform/graphics/cairo/CairoGraphicsFactory.h"
+#else
+#endif
+
+namespace cru::platform::bootstrap {
+CRU_PLATFORM_GRAPHICS_BOOTSTRAP_API cru::platform::graphics::IGraphicsFactory*
+CreateGraphicsFactory() {
+#if defined(_WIN32)
+ return new cru::platform::graphics::direct2d::DirectGraphicsFactory();
+#elif defined(__APPLE__)
+ return new cru::platform::graphics::quartz::QuartzGraphicsFactory();
+#elif defined(__unix)
+ return new cru::platform::graphics::cairo::CairoGraphicsFactory();
+#else
+ NotImplemented();
+#endif
+}
+} // namespace cru::platform::bootstrap