example/capi: chores++

This commit is contained in:
Hermet Park 2025-03-07 00:59:52 +09:00
parent c854a7207f
commit 57dd66de1b

View file

@ -37,7 +37,8 @@ static Tvg_Animation* animation = NULL;
void contents()
{
//////1. Linear gradient shape with a linear gradient stroke
//Linear gradient shape with a linear gradient stroke
{
//Set a shape
Tvg_Paint* shape1 = tvg_shape_new();
tvg_shape_move_to(shape1, 25.0f, 25.0f);
@ -46,97 +47,87 @@ void contents()
tvg_shape_close(shape1);
//Prepare a gradient for the fill
Tvg_Gradient* grad1 = tvg_linear_gradient_new();
tvg_linear_gradient_set(grad1, 25.0f, 25.0f, 200.0f, 200.0f);
Tvg_Color_Stop color_stops1[4] =
{
{0.00f, 255, 0, 0, 155},
{0.33f, 0, 255, 0, 100},
{0.66f, 255, 0, 255, 100},
{1.00f, 0, 0, 255, 155}
};
tvg_gradient_set_color_stops(grad1, color_stops1, 4);
tvg_gradient_set_spread(grad1, TVG_STROKE_FILL_REFLECT);
Tvg_Gradient* grad = tvg_linear_gradient_new();
tvg_linear_gradient_set(grad, 25.0f, 25.0f, 200.0f, 200.0f);
Tvg_Color_Stop color_stops[4] = {{0.00f, 255, 0, 0, 155}, {0.33f, 0, 255, 0, 100}, {0.66f, 255, 0, 255, 100}, {1.00f, 0, 0, 255, 155}};
tvg_gradient_set_color_stops(grad, color_stops, 4);
tvg_gradient_set_spread(grad, TVG_STROKE_FILL_REFLECT);
//Prepare a gradient for the stroke
Tvg_Gradient* grad1_stroke = tvg_gradient_duplicate(grad1);
Tvg_Gradient* grad_stroke = tvg_gradient_duplicate(grad);
//Set a gradient fill
tvg_shape_set_gradient(shape1, grad1);
tvg_shape_set_gradient(shape1, grad);
//Set a gradient stroke
tvg_shape_set_stroke_width(shape1, 20.0f);
tvg_shape_set_stroke_gradient(shape1, grad1_stroke);
tvg_shape_set_stroke_gradient(shape1, grad_stroke);
tvg_shape_set_stroke_join(shape1, TVG_STROKE_JOIN_ROUND);
tvg_canvas_push(canvas, shape1);
}
//////2. Solid transformed shape
//Solid transformed shape
{
//Set a shape
const Tvg_Path_Command* cmds;
const Tvg_Point* pts;
uint32_t cmdCnt, ptsCnt;
tvg_shape_get_path(shape1, &cmds, &cmdCnt, &pts, &ptsCnt);
Tvg_Paint* shape2 = tvg_shape_new();
tvg_shape_append_path(shape2, cmds, cmdCnt, pts, ptsCnt);
tvg_shape_set_fill_color(shape2, 255, 255, 255, 128);
Tvg_Paint* shape = tvg_shape_new();
tvg_shape_move_to(shape, 25.0f, 25.0f);
tvg_shape_line_to(shape, 375.0f, 25.0f);
tvg_shape_cubic_to(shape, 500.0f, 100.0f, -500.0f, 200.0f, 375.0f, 375.0f);
tvg_shape_close(shape);
tvg_shape_set_fill_color(shape, 255, 255, 255, 128);
//Transform a shape
tvg_paint_scale(shape2, 0.3f);
tvg_paint_translate(shape2, 100.0f, 100.0f);
tvg_paint_scale(shape, 0.3f);
tvg_paint_translate(shape, 100.0f, 100.0f);
tvg_canvas_push(canvas, shape);
}
//Push shapes 1 and 2 into the canvas
tvg_canvas_push(canvas, shape1);
tvg_canvas_push(canvas, shape2);
//////3. Radial gradient shape with a radial dashed stroke
//Radial gradient shape with a radial dashed stroke
{
//Set a shape
Tvg_Paint* shape3 = tvg_shape_new();
tvg_shape_append_rect(shape3, 550.0f, 20.0f, 100.0f, 50.0f, 0.0f, 0.0f, true);
tvg_shape_append_circle(shape3, 600.0f, 150.0f, 100.0f, 50.0f, true);
tvg_shape_append_rect(shape3, 550.0f, 230.0f, 100.0f, 100.0f, 20.0f, 40.0f, true);
Tvg_Paint* shape = tvg_shape_new();
tvg_shape_append_rect(shape, 550.0f, 20.0f, 100.0f, 50.0f, 0.0f, 0.0f, true);
tvg_shape_append_circle(shape, 600.0f, 150.0f, 100.0f, 50.0f, true);
tvg_shape_append_rect(shape, 550.0f, 230.0f, 100.0f, 100.0f, 20.0f, 40.0f, true);
//Prepare a radial gradient for the fill
Tvg_Gradient* grad2 = tvg_radial_gradient_new();
tvg_radial_gradient_set(grad2, 600.0f, 180.0f, 50.0f, 640.0f, 180.0f, 0.0f);
Tvg_Color_Stop color_stops2[3] =
{
{0.0f, 255, 0, 255, 255},
{0.5f, 0, 0, 255, 255},
{1.0f, 50, 55, 155, 255}
};
tvg_gradient_set_color_stops(grad2, color_stops2, 3);
tvg_gradient_set_spread(grad2, TVG_STROKE_FILL_PAD);
Tvg_Gradient* grad = tvg_radial_gradient_new();
tvg_radial_gradient_set(grad, 600.0f, 180.0f, 50.0f, 640.0f, 180.0f, 0.0f);
Tvg_Color_Stop color_stops2[3] = {{0.0f, 255, 0, 255, 255}, {0.5f, 0, 0, 255, 255}, {1.0f, 50, 55, 155, 255}};
tvg_gradient_set_color_stops(grad, color_stops2, 3);
tvg_gradient_set_spread(grad, TVG_STROKE_FILL_PAD);
//Set a gradient fill
tvg_shape_set_gradient(shape3, grad2);
tvg_shape_set_gradient(shape, grad);
//Prepare a radial gradient for the stroke
uint32_t cnt;
const Tvg_Color_Stop* color_stops2_get;
tvg_gradient_get_color_stops(grad2, &color_stops2_get, &cnt);
tvg_gradient_get_color_stops(grad, &color_stops2_get, &cnt);
float cx, cy, r, fx, fy, fr;
tvg_radial_gradient_get(grad2, &cx, &cy, &r, &fx, &fy, &fr);
tvg_radial_gradient_get(grad, &cx, &cy, &r, &fx, &fy, &fr);
Tvg_Gradient* grad2_stroke = tvg_radial_gradient_new();
tvg_radial_gradient_set(grad2_stroke, cx, cy, r, fx, fy, fr);
tvg_gradient_set_color_stops(grad2_stroke, color_stops2_get, cnt);
tvg_gradient_set_spread(grad2_stroke, TVG_STROKE_FILL_REPEAT);
Tvg_Gradient* grad_stroke = tvg_radial_gradient_new();
tvg_radial_gradient_set(grad_stroke, cx, cy, r, fx, fy, fr);
tvg_gradient_set_color_stops(grad_stroke, color_stops2_get, cnt);
tvg_gradient_set_spread(grad_stroke, TVG_STROKE_FILL_REPEAT);
//Set a gradient stroke
tvg_shape_set_stroke_width(shape3, 30.0f);
tvg_shape_set_stroke_gradient(shape3, grad2_stroke);
tvg_shape_set_stroke_width(shape, 30.0f);
tvg_shape_set_stroke_gradient(shape, grad_stroke);
tvg_paint_set_opacity(shape3, 200);
tvg_paint_set_opacity(shape, 200);
//Push the shape into the canvas
tvg_canvas_push(canvas, shape3);
tvg_canvas_push(canvas, shape);
}
//////4. Scene
//Scene
{
//Set a scene
Tvg_Paint* scene = tvg_scene_new();
@ -173,8 +164,10 @@ void contents()
//Push the scene into the canvas
tvg_canvas_push(canvas, scene);
}
//////5. Masked picture
//Masked picture
{
//Set a scene
Tvg_Paint* pict = tvg_picture_new();
if (tvg_picture_load(pict, EXAMPLE_DIR"/svg/tiger.svg") != TVG_RESULT_SUCCESS) {
@ -196,43 +189,29 @@ void contents()
//Push the scene into the canvas
tvg_canvas_push(canvas, pict);
}
//////6. Animation with a picture
//instead loading from a memory, an animation can be loaded directly from a file using:
//tvg_picture_load(pict_lottie, EXAMPLE_DIR"/lottie/sample.json")
FILE *file = fopen(EXAMPLE_DIR"/lottie/sample.json", "r");
if (file == NULL) return;
fseek(file, 0, SEEK_END);
long data_size = ftell(file);
fseek(file, 0, SEEK_SET);
auto data = (char*)malloc(data_size + 1);
if (!data) return;
if (fread(data, 1, data_size, file) != (size_t)data_size) {
free(data);
fclose(file);
return;
}
//ensure null-termination in case tvg_picture_load_data is called with copy = false
data[data_size++] = '\0';
//Animation with a picture
{
animation = tvg_animation_new();
Tvg_Paint* pict_lottie = tvg_animation_get_picture(animation);
if (tvg_picture_load_data(pict_lottie, data, data_size, "lot", nullptr, false) != TVG_RESULT_SUCCESS) {
if (tvg_picture_load(pict_lottie, EXAMPLE_DIR"/lottie/sample.json") != TVG_RESULT_SUCCESS) {
printf("Problem with loading a lottie file\n");
tvg_animation_del(animation);
animation = NULL;
} else {
tvg_paint_scale(pict_lottie, 3.0f);
tvg_paint_scale(pict_lottie, 0.75f);
tvg_canvas_push(canvas, pict_lottie);
}
}
free(data);
fclose(file);
//////7. Text
//Text 1
{
//load from a file
if (tvg_font_load(EXAMPLE_DIR"/font/SentyCloud.ttf") != TVG_RESULT_SUCCESS) {
printf("Problem with loading the font from the file. Did you enable TTF Loader?\n");
} else {
}
Tvg_Paint *text = tvg_text_new();
tvg_text_set_font(text, "SentyCloud", 25.0f, "");
tvg_text_set_fill_color(text, 0, 0, 255);
@ -240,30 +219,28 @@ void contents()
tvg_paint_translate(text, 50.0f, 380.0f);
tvg_canvas_push(canvas, text);
}
//Text 2
{
//load from a memory
file = fopen(EXAMPLE_DIR"/font/SentyCloud.ttf", "rb");
if (file == NULL) return;
FILE* file = fopen(EXAMPLE_DIR"/font/Arial.ttf", "rb");
if (file) {
fseek(file, 0, SEEK_END);
data_size = ftell(file);
size_t data_size = ftell(file);
fseek(file, 0, SEEK_SET);
data = (char*)malloc(data_size);
if (!data) return;
if (fread(data, 1, data_size, file) != (size_t)data_size) {
free(data);
fclose(file);
return;
}
char* data = (char*)malloc(data_size);
if (fread(data, 1, data_size, file) == data_size) {
if (tvg_font_load_data("Arial", data, data_size, "ttf", true) != TVG_RESULT_SUCCESS) {
printf("Problem with loading the font file from a memory. Did you enable TTF Loader?\n");
} else {
//Radial gradient
}
}
free(data);
fclose(file);
}
Tvg_Gradient* grad = tvg_radial_gradient_new();
tvg_radial_gradient_set(grad, 200.0f, 200.0f, 20.0f, 200.0f, 200.0f, 0.0f);
Tvg_Color_Stop color_stops[2] =
{
{0.0f, 255, 0, 255, 255},
{1.0f, 0, 0, 255, 255}
};
Tvg_Color_Stop color_stops[2] = {{0.0f, 255, 0, 255, 255}, {1.0f, 0, 0, 255, 255}};
tvg_gradient_set_color_stops(grad, color_stops, 2);
tvg_gradient_set_spread(grad, TVG_STROKE_FILL_REFLECT);
@ -274,8 +251,6 @@ void contents()
tvg_paint_translate(text, 70.0f, 420.0f);
tvg_canvas_push(canvas, text);
}
free(data);
fclose(file);
}
@ -306,13 +281,19 @@ int main(int argc, char **argv)
contents();
//display the first frame
tvg_canvas_draw(canvas, true);
tvg_canvas_sync(canvas);
SDL_UpdateWindowSurface(window);
//app main loop!
SDL_Event event;
auto running = true;
auto ptime = SDL_GetTicks();
auto elapsed = 0;
while (running) {
//SDL Event handling
//SDL event handling
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT: {
@ -328,7 +309,7 @@ int main(int argc, char **argv)
}
}
//Update the animation
//update the animation
if (animation) {
float duration, totalFrame;
tvg_animation_get_duration(animation, &duration);
@ -336,7 +317,7 @@ int main(int argc, char **argv)
tvg_animation_set_frame(animation, totalFrame * progress(elapsed, duration));
}
//Draw the canvas
//draw the canvas
tvg_canvas_update(canvas);
tvg_canvas_draw(canvas, true);
tvg_canvas_sync(canvas);