aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--demos/CMakeLists.txt4
-rw-r--r--demos/graphics/CMakeLists.txt3
-rw-r--r--demos/graphics/DrawCircle.cpp34
-rw-r--r--include/cru/common/io/FileStream.h2
-rw-r--r--src/osx/graphics/quartz/Image.cpp4
-rw-r--r--src/osx/graphics/quartz/ImageFactory.cpp10
6 files changed, 52 insertions, 5 deletions
diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt
index f2f781bd..f6dd20c3 100644
--- a/demos/CMakeLists.txt
+++ b/demos/CMakeLists.txt
@@ -3,12 +3,16 @@ add_library(cru_demo_base INTERFACE)
target_link_libraries(cru_demo_base INTERFACE cru_platform_bootstrap)
if(WIN32)
+ add_subdirectory(graphics)
+
add_subdirectory(main)
add_subdirectory(scroll_view)
add_subdirectory(input_method)
add_subdirectory(svg_path)
elseif(APPLE)
+ add_subdirectory(graphics)
+
add_subdirectory(main)
add_subdirectory(scroll_view)
diff --git a/demos/graphics/CMakeLists.txt b/demos/graphics/CMakeLists.txt
new file mode 100644
index 00000000..2ecd88bc
--- /dev/null
+++ b/demos/graphics/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_executable(cru_demo_graphics_draw_circle DrawCircle.cpp)
+
+target_link_libraries(cru_demo_graphics_draw_circle PRIVATE cru_demo_base)
diff --git a/demos/graphics/DrawCircle.cpp b/demos/graphics/DrawCircle.cpp
new file mode 100644
index 00000000..9f1eb253
--- /dev/null
+++ b/demos/graphics/DrawCircle.cpp
@@ -0,0 +1,34 @@
+#include "cru/common/io/FileStream.h"
+#include "cru/common/io/OpenFileFlag.h"
+#include "cru/platform/Color.h"
+#include "cru/platform/bootstrap/Bootstrap.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 image = graphics_factory->GetImageFactory()->CreateBitmap(500, 500);
+
+ {
+ auto brush =
+ graphics_factory->CreateSolidColorBrush(cru::platform::colors::black);
+ auto painter = image->CreatePainter();
+ painter->FillEllipse(cru::platform::Rect{200, 200, 100, 100}, brush.get());
+ painter->EndDraw();
+ }
+
+ cru::io::FileStream file_stream(
+ u"./test_image.png",
+ cru::io::OpenFileFlags::Write | cru::io::OpenFileFlags::Create);
+
+ graphics_factory->GetImageFactory()->EncodeToStream(
+ image.get(), &file_stream, cru::platform::graphics::ImageFormat::Png,
+ 1.0f);
+
+ return 0;
+}
diff --git a/include/cru/common/io/FileStream.h b/include/cru/common/io/FileStream.h
index 4f1499be..6f84526c 100644
--- a/include/cru/common/io/FileStream.h
+++ b/include/cru/common/io/FileStream.h
@@ -1,5 +1,7 @@
#pragma once
+#include "../PreConfig.h"
+
#ifdef CRU_PLATFORM_UNIX
#include "../platform/unix/UnixFileStream.h"
namespace cru::io {
diff --git a/src/osx/graphics/quartz/Image.cpp b/src/osx/graphics/quartz/Image.cpp
index feddad8c..28087000 100644
--- a/src/osx/graphics/quartz/Image.cpp
+++ b/src/osx/graphics/quartz/Image.cpp
@@ -11,7 +11,9 @@ QuartzImage::QuartzImage(IGraphicsFactory* graphics_factory,
image_factory_(image_factory),
image_(image),
auto_release_(auto_release),
- buffer_(buffer) {}
+ buffer_(buffer) {
+ Expects(image);
+}
QuartzImage::~QuartzImage() {
if (auto_release_) {
diff --git a/src/osx/graphics/quartz/ImageFactory.cpp b/src/osx/graphics/quartz/ImageFactory.cpp
index 2c6d4705..5ff262c3 100644
--- a/src/osx/graphics/quartz/ImageFactory.cpp
+++ b/src/osx/graphics/quartz/ImageFactory.cpp
@@ -87,7 +87,7 @@ std::unique_ptr<IImage> QuartzImageFactory::CreateBitmap(int width,
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
const auto buffer_size = width * height * 4;
- auto buffer = new unsigned char[buffer_size];
+ auto buffer = new unsigned char[buffer_size]{0};
auto cg_data_provider = CGDataProviderCreateWithData(
nullptr, buffer, buffer_size,
@@ -95,11 +95,13 @@ std::unique_ptr<IImage> QuartzImageFactory::CreateBitmap(int width,
delete[] static_cast<const unsigned char*>(data);
});
- auto cg_image = CGImageCreate(width, height, 8, 32, 4 * width, color_space,
- kCGImageAlphaLast, nullptr, nullptr, true,
- kCGRenderingIntentDefault);
+ auto cg_image =
+ CGImageCreate(width, height, 8, 32, 4 * width, color_space,
+ kCGImageAlphaPremultipliedLast, cg_data_provider, nullptr,
+ true, kCGRenderingIntentDefault);
CGColorSpaceRelease(color_space);
+ CGDataProviderRelease(cg_data_provider);
return std::unique_ptr<IImage>(
new QuartzImage(GetGraphicsFactory(), this, cg_image, true, buffer));