capi: Added C wrappers for gradient func

Change-Id: If41dab9b06f6cec2831ea1361f30b50a193e99c4
This commit is contained in:
Mateusz Palkowski 2020-07-31 11:34:49 +02:00
parent f15aefa5dc
commit 7b9c7de1e5
3 changed files with 99 additions and 3 deletions

View file

@ -35,6 +35,7 @@ extern "C" {
typedef struct _Tvg_Canvas Tvg_Canvas; typedef struct _Tvg_Canvas Tvg_Canvas;
typedef struct _Tvg_Paint Tvg_Paint; typedef struct _Tvg_Paint Tvg_Paint;
typedef struct _Tvg_Gradient Tvg_Gradient;
#define TVG_ENGINE_SW (1 << 1) #define TVG_ENGINE_SW (1 << 1)
#define TVG_ENGINE_GL (1 << 2) #define TVG_ENGINE_GL (1 << 2)
@ -93,6 +94,11 @@ typedef struct
float e31, e32, e33; float e31, e32, e33;
} Tvg_Matrix; } Tvg_Matrix;
typedef struct
{
float offset;
uint8_t r, g, b, a;
} Tvg_Color_Stop;
/************************************************************************/ /************************************************************************/
/* Engine API */ /* Engine API */
@ -144,6 +150,20 @@ TVG_EXPORT Tvg_Result tvg_shape_scale(Tvg_Paint* paint, float factor);
TVG_EXPORT Tvg_Result tvg_shape_rotate(Tvg_Paint* paint, float degree); TVG_EXPORT Tvg_Result tvg_shape_rotate(Tvg_Paint* paint, float degree);
TVG_EXPORT Tvg_Result tvg_shape_translate(Tvg_Paint* paint, float x, float y); TVG_EXPORT Tvg_Result tvg_shape_translate(Tvg_Paint* paint, float x, float y);
TVG_EXPORT Tvg_Result tvg_shape_transform(Tvg_Paint* paint, const Tvg_Matrix* m); TVG_EXPORT Tvg_Result tvg_shape_transform(Tvg_Paint* paint, const Tvg_Matrix* m);
TVG_EXPORT Tvg_Result tvg_shape_linear_gradient_set(Tvg_Paint* paint, Tvg_Gradient *grad);
TVG_EXPORT Tvg_Result tvg_shape_radial_gradient_set(Tvg_Paint* paint, Tvg_Gradient *grad);
/************************************************************************/
/* Gradient API */
/************************************************************************/
TVG_EXPORT Tvg_Gradient* tvg_linear_gradient_new();
TVG_EXPORT Tvg_Gradient* tvg_radial_gradient_new();
TVG_EXPORT Tvg_Result tvg_gradient_del(Tvg_Gradient* grad);
TVG_EXPORT Tvg_Result tvg_linear_gradient_set(Tvg_Gradient* grad, float x1, float y1, float x2, float y2);
TVG_EXPORT Tvg_Result tvg_radial_gradient_set(Tvg_Gradient* grad, float cx, float cy, float radius);
TVG_EXPORT Tvg_Result tvg_gradient_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -35,6 +35,11 @@ struct _Tvg_Paint
//Dummy for Direct Casting //Dummy for Direct Casting
}; };
struct _Tvg_Gradient
{
//Dummy for Direct Casting
};
/************************************************************************/ /************************************************************************/
/* Engine API */ /* Engine API */
@ -246,6 +251,52 @@ TVG_EXPORT Tvg_Result tvg_shape_transform(Tvg_Paint* paint, const Tvg_Matrix* m)
return (Tvg_Result) reinterpret_cast<Shape*>(paint)->transform(*(reinterpret_cast<const Matrix*>(m))); return (Tvg_Result) reinterpret_cast<Shape*>(paint)->transform(*(reinterpret_cast<const Matrix*>(m)));
} }
TVG_EXPORT Tvg_Result tvg_shape_linear_gradient_set(Tvg_Paint* paint, Tvg_Gradient *grad)
{
return (Tvg_Result) reinterpret_cast<Shape*>(paint)->fill(unique_ptr<LinearGradient>((LinearGradient*)(grad)));
}
TVG_EXPORT Tvg_Result tvg_shape_radial_gradient_set(Tvg_Paint* paint, Tvg_Gradient *grad)
{
return (Tvg_Result) reinterpret_cast<Shape*>(paint)->fill(unique_ptr<RadialGradient>((RadialGradient*)(grad)));
}
/************************************************************************/
/* Gradient API */
/************************************************************************/
TVG_EXPORT Tvg_Gradient* tvg_linear_gradient_new()
{
return (Tvg_Gradient*)LinearGradient::gen().release();
}
TVG_EXPORT Tvg_Gradient* tvg_radial_gradient_new()
{
return (Tvg_Gradient*)RadialGradient::gen().release();
}
TVG_EXPORT Tvg_Result tvg_gradient_del(Tvg_Gradient* grad)
{
delete(grad);
return TVG_RESULT_SUCCESS;
}
TVG_EXPORT Tvg_Result tvg_linear_gradient_set(Tvg_Gradient* grad, float x1, float y1, float x2, float y2)
{
return (Tvg_Result) reinterpret_cast<LinearGradient*>(grad)->linear(x1, y1, x2, y2);
}
TVG_EXPORT Tvg_Result tvg_radial_gradient_set(Tvg_Gradient* grad, float cx, float cy, float radius)
{
return (Tvg_Result) reinterpret_cast<RadialGradient*>(grad)->radial(cx, cy, radius);
}
TVG_EXPORT Tvg_Result tvg_gradient_color_stops(Tvg_Gradient* grad, const Tvg_Color_Stop* color_stop, uint32_t cnt)
{
return (Tvg_Result) reinterpret_cast<Fill*>(grad)->colorStops(reinterpret_cast<const Fill::ColorStop*>(color_stop), cnt);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -20,11 +20,36 @@ void testCapi()
Tvg_Paint* shape = tvg_shape_new(); Tvg_Paint* shape = tvg_shape_new();
tvg_shape_append_rect(shape, 0, 0, 200, 200, 0, 0); tvg_shape_append_rect(shape, 0, 0, 200, 200, 0, 0);
tvg_shape_append_circle(shape, 200, 200, 100, 100);
tvg_shape_append_rect(shape, 100, 100, 300, 300, 100, 100); tvg_shape_append_rect(shape, 100, 100, 300, 300, 100, 100);
tvg_shape_append_circle(shape, 400, 400, 100, 100); Tvg_Gradient* grad = tvg_linear_gradient_new();
tvg_shape_append_circle(shape, 400, 500, 170, 100); tvg_linear_gradient_set(grad, 0, 0, 300, 300);
tvg_shape_fill_color(shape, 255, 255, 0, 255); Tvg_Color_Stop color_stops[4] =
{
{.offset=0.0, .r=0, .g=0, .b=0, .a=255},
{.offset=0.25, .r=255, .g=0, .b=0, .a=255},
{.offset=0.5, .r=0, .g=255, .b=0, .a=255},
{.offset=1.0, .r=0, .g=0, .b=255, .a=255}
};
Tvg_Paint *shape1 = tvg_shape_new();
tvg_shape_append_rect(shape1, 500, 500, 100, 100, 30, 30);
Tvg_Gradient* grad1 = tvg_radial_gradient_new();
tvg_radial_gradient_set(grad1, 550, 550, 50);
Tvg_Color_Stop color_stops1[3] =
{
{.offset=0.0, .r=0, .g=0, .b=0, .a=255},
{.offset=0.6, .r=255, .g=0, .b=0, .a=255},
{.offset=1.0, .r=0, .g=255, .b=255, .a=255}
};
tvg_gradient_color_stops(grad, color_stops, 4);
tvg_gradient_color_stops(grad1, color_stops1, 3);
tvg_shape_linear_gradient_set(shape, grad);
tvg_shape_radial_gradient_set(shape1, grad1);
tvg_canvas_push(canvas, shape); tvg_canvas_push(canvas, shape);
tvg_canvas_push(canvas, shape1);
tvg_canvas_draw(canvas); tvg_canvas_draw(canvas);
tvg_canvas_sync(canvas); tvg_canvas_sync(canvas);