From d1d54e8b8f24432b1f078a4ffe0260a432f55334 Mon Sep 17 00:00:00 2001 From: Michal Szczecinski Date: Tue, 10 Nov 2020 04:13:48 +0100 Subject: [PATCH] capi examples: Added scene API C bindings. Scene CAPI allows to use scene functionaliy in C applications such as set opacity to few shapes at one time or implement transparent layers. @API Additions: ```c Tvg_Paint* tvg_scene_new(); Tvg_Result tvg_scene_reserve(Tvg_Paint* scene, uint32_t size); Tvg_Result tvg_scene_push(Tvg_Paint* scene, Tvg_Paint *paint); ``` Examples: ```c Tvg_Paint *scene = tvg_scene_new(); Tvg_Paint *shape1 = tvg_shape_new(); tvg_shape_append_rect(shape1, 10, 10, 100, 100, 0, 0); tvg_shape_set_fill_color(shape1, 255, 0, 0, 255); Tvg_Paint *shape2 = tvg_shape_new(); tvg_shape_append_rect(shape2, 120, 10, 100, 100, 0, 0); tvg_shape_set_fill_color(shape2, 255, 0, 0, 255); tvg_scene_push(scene, shape1); tvg_scene_push(scene, shape2); tvg_paint_set_opacity(scene, 100); tvg_canvas_push(canvas, scene); ``` Co-authored-by: Michal Szczecinski --- inc/thorvg_capi.h | 9 ++++++++- src/bindings/capi/tvgCapi.cpp | 17 +++++++++++++++++ src/examples/Capi.cpp | 16 ++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/inc/thorvg_capi.h b/inc/thorvg_capi.h index 9d5b1e9f..92e60c13 100644 --- a/inc/thorvg_capi.h +++ b/inc/thorvg_capi.h @@ -170,7 +170,6 @@ TVG_EXPORT Tvg_Result tvg_gradient_set_spread(Tvg_Gradient* grad, const Tvg_Stro TVG_EXPORT Tvg_Result tvg_gradient_get_spread(Tvg_Gradient* grad, Tvg_Stroke_Fill* spread); TVG_EXPORT Tvg_Result tvg_gradient_del(Tvg_Gradient* grad); - /************************************************************************/ /* Picture API */ /************************************************************************/ @@ -178,6 +177,14 @@ TVG_EXPORT Tvg_Paint* tvg_picture_new(); TVG_EXPORT Tvg_Result tvg_picture_load(Tvg_Paint* paint, const char* path); TVG_EXPORT Tvg_Result tvg_picture_get_viewbox(const Tvg_Paint* paint, float* x, float* y, float* w, float* h); +/************************************************************************/ +/* Scene API */ +/************************************************************************/ +TVG_EXPORT Tvg_Paint* tvg_scene_new(); +TVG_EXPORT Tvg_Result tvg_scene_reserve(Tvg_Paint* scene, uint32_t size); +TVG_EXPORT Tvg_Result tvg_scene_push(Tvg_Paint* scene, Tvg_Paint* paint); + + #ifdef __cplusplus } #endif diff --git a/src/bindings/capi/tvgCapi.cpp b/src/bindings/capi/tvgCapi.cpp index c19c81fc..24ada576 100644 --- a/src/bindings/capi/tvgCapi.cpp +++ b/src/bindings/capi/tvgCapi.cpp @@ -475,6 +475,23 @@ TVG_EXPORT Tvg_Result tvg_gradient_get_spread(Tvg_Gradient* grad, Tvg_Stroke_Fil return TVG_RESULT_SUCCESS; } +TVG_EXPORT Tvg_Paint* tvg_scene_new() +{ + return (Tvg_Paint*) Scene::gen().release(); +} + +TVG_EXPORT Tvg_Result tvg_scene_reserve(Tvg_Paint* scene, uint32_t size) +{ + if (!scene) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(scene)->reserve(size); +} + +TVG_EXPORT Tvg_Result tvg_scene_push(Tvg_Paint* scene, Tvg_Paint* paint) +{ + if (!scene || !paint) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(scene)->push(unique_ptr((Paint*)paint)); +} + #ifdef __cplusplus } diff --git a/src/examples/Capi.cpp b/src/examples/Capi.cpp index ef5c1149..675cd796 100644 --- a/src/examples/Capi.cpp +++ b/src/examples/Capi.cpp @@ -150,6 +150,22 @@ void testCapi() Tvg_Paint* dup = tvg_paint_duplicate(org); tvg_canvas_push(canvas, dup); + //Scene test + Tvg_Paint* scene = tvg_scene_new(); + + Tvg_Paint* scene_shape_1 = tvg_shape_new(); + tvg_shape_append_rect(scene_shape_1, 650, 410, 100, 50, 10, 10); + tvg_shape_set_fill_color(scene_shape_1, 0, 255, 0, 255); + + Tvg_Paint* scene_shape_2 = tvg_shape_new(); + tvg_shape_append_rect(scene_shape_2, 650, 470, 100, 50, 10, 10); + tvg_shape_set_fill_color(scene_shape_2, 0, 255, 0, 255); + + tvg_scene_push(scene, scene_shape_1); + tvg_scene_push(scene, scene_shape_2); + tvg_paint_set_opacity(scene, 100); + + tvg_canvas_push(canvas, scene); tvg_canvas_draw(canvas); tvg_canvas_sync(canvas);