examples: capi example ++

Animation is loaded from a memory instead from a file.
This commit is contained in:
Mira Grudzinska 2024-08-16 12:58:08 +02:00 committed by Hermet Park
parent 5448b1b9cd
commit 022f9fc897

View file

@ -201,16 +201,36 @@ void contents()
} }
//////6. Animation with a picture //////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 = tvg_animation_new(); animation = tvg_animation_new();
Tvg_Paint* pict_lottie = tvg_animation_get_picture(animation); Tvg_Paint* pict_lottie = tvg_animation_get_picture(animation);
if (tvg_picture_load(pict_lottie, EXAMPLE_DIR"/lottie/sample.json") != TVG_RESULT_SUCCESS) { if (tvg_picture_load_data(pict_lottie, data, data_size, nullptr, nullptr, false) != TVG_RESULT_SUCCESS) {
printf("Problem with loading an lottie file\n"); printf("Problem with loading a lottie file\n");
tvg_animation_del(animation); tvg_animation_del(animation);
} else { } else {
tvg_paint_scale(pict_lottie, 3.0f); tvg_paint_scale(pict_lottie, 3.0f);
tvg_canvas_push(canvas, pict_lottie); tvg_canvas_push(canvas, pict_lottie);
} }
free(data);
fclose(file);
//////7. Text //////7. Text
//load from a file //load from a file
if (tvg_font_load(EXAMPLE_DIR"/font/SentyCloud.ttf") != TVG_RESULT_SUCCESS) { if (tvg_font_load(EXAMPLE_DIR"/font/SentyCloud.ttf") != TVG_RESULT_SUCCESS) {
@ -224,12 +244,12 @@ void contents()
tvg_canvas_push(canvas, text); tvg_canvas_push(canvas, text);
} }
//load from a memory //load from a memory
FILE *file = fopen(EXAMPLE_DIR"/font/SentyCloud.ttf", "rb"); file = fopen(EXAMPLE_DIR"/font/SentyCloud.ttf", "rb");
if (file == NULL) return; if (file == NULL) return;
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
long data_size = ftell(file); data_size = ftell(file);
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
char* data = (char*)malloc(data_size); data = (char*)malloc(data_size);
if (!data) return; if (!data) return;
if (fread(data, 1, data_size, file) != (size_t)data_size) { if (fread(data, 1, data_size, file) != (size_t)data_size) {
free(data); free(data);
@ -316,10 +336,12 @@ int main(int argc, char **argv)
tvg_canvas_clear(canvas, false, true); tvg_canvas_clear(canvas, false, true);
//Update the animation //Update the animation
float duration, totalFrame; if (animation) {
tvg_animation_get_duration(animation, &duration); float duration, totalFrame;
tvg_animation_get_total_frame(animation, &totalFrame); tvg_animation_get_duration(animation, &duration);
tvg_animation_set_frame(animation, totalFrame * progress(elapsed, duration)); tvg_animation_get_total_frame(animation, &totalFrame);
tvg_animation_set_frame(animation, totalFrame * progress(elapsed, duration));
}
//Draw the canvas //Draw the canvas
tvg_canvas_update(canvas); tvg_canvas_update(canvas);