/* Prepares the first frame of animation */
static void draw_first_frame(void) {
    VMUINT8* img_data;
    VMUINT32 img_size;
    vm_graphic_color_argb_t color;

    /* draw background image */
    //img_data = vm_res_get_image(IMG_ID_BG, &img_size);
    //vm_graphic_draw_image_memory(g_frame_group[0], 0, 0, img_data, img_size, 0);

    /* draw a purple horizontal blue line from center of screen */
    color.a = 255;
    color.r = 0;
    color.g = 0;
    color.b = 255;
    vm_graphic_set_color(color);
    vm_graphic_draw_solid_rectangle(&g_temp_frame, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    color.r = 255;
    vm_graphic_set_color(color);
    vm_graphic_draw_line(&g_temp_frame, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2 + LINE_LENGTH - 1, SCREEN_HEIGHT / 2);

    /* create animation timer */
    g_timer_id = vm_timer_create_precise(100, timer_callback, NULL);
    theta = 0;

    /* explicitly trigger 1st frame */
    timer_callback(g_timer_id, NULL);
}
/* Update the rotating line, then update the display */
static void timer_callback(VM_TIMER_ID_PRECISE tid, void* user_data) {
    vm_graphic_point_t positions[2] = {0,};
    vm_graphic_color_argb_t color;

    /* clear the background with blue color key */
    color.a = 255;
    color.r = 0;
    color.g = 0;
    color.b = 255;
    vm_graphic_set_color(color);
    vm_graphic_draw_solid_rectangle(g_frame_group[1], 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    /* rotate the line image */
    matrix[0] = (VMFLOAT)cos(theta);
    matrix[1] = -(VMFLOAT)sin(theta);
    matrix[2] = -0.5 * SCREEN_WIDTH * (VMFLOAT)cos(theta) + 0.5 * SCREEN_HEIGHT * (VMFLOAT)sin(theta) + 0.5 * SCREEN_WIDTH;
    matrix[3] = (VMFLOAT)sin(theta);
    matrix[4] = (VMFLOAT)cos(theta);
    matrix[5] = -0.5 * SCREEN_WIDTH * (VMFLOAT)sin(theta) - 0.5 * SCREEN_HEIGHT * (VMFLOAT)cos(theta) + 0.5 * SCREEN_HEIGHT;
    matrix[6] = 0.0f;
    matrix[7] = 0.0f;
    matrix[8] = 1.0f;
    vm_graphic_linear_transform(
        g_frame_group[1],
        &g_temp_frame,
        matrix,
        0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
        0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
        VM_GRAPHIC_TRANSFORM_SOURCE_KEY_EDGE_FILTER_DULPLICATE,
        color,
        VM_GRAPHIC_TRANSFORM_SAMPLE_MODE_BILINEAR_WITH_EDGE_FILTER_DULPLICATE,
        VM_GRAPHIC_TRANSFORM_FLAG_NONE_BLOCKING);

    /* update the rotating angle */
    theta += (2 * PI) / FREQUENCY;
    if(theta > 2 * PI) {
        theta = 0;
    }
#if defined(__HDK_LINKIT_ASSIST_2502__)
    /* composite the rotated line image to background buffer and display it */
    vm_graphic_blt_frame(g_frame_group, positions, 2);
#endif
}
/* Set font and draw hello world text */
static void draw_hello(void) {
	VMWSTR string;
	vm_graphic_color_argb_t color;
	vm_graphic_frame_t frame;
	vm_graphic_frame_t* frame_group[1];
	VMWCHAR font_path[FONT_PATH_MAX_LENGTH + 1];
	VMWSTR font_paths_group[1];
	VM_RESULT result;
	VMUINT32 pool_size;
	VMUINT32 size;

	vm_graphic_point_t positions[1] = { 0, 0 };

	frame.buffer_length = SCREEN_WIDTH * SCREEN_HEIGHT * 2;
	frame.buffer = vm_malloc_dma(frame.buffer_length);
	if (frame.buffer == NULL) {
		return;
	}

	frame.color_format = VM_GRAPHIC_COLOR_FORMAT_16_BIT;
	frame.height = SCREEN_HEIGHT;
	frame.width = SCREEN_WIDTH;
	frame_group[0] = &frame;

	string = vm_res_get_string(STR_ID_HELLO, &size);
	color.a = 255;
	color.r = 243;
	color.g = 154;
	color.b = 30;
	vm_graphic_set_color(color);
	vm_graphic_draw_solid_rectangle(&frame, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

	result = vm_graphic_get_font_pool_size(EXTERNAL_FONT_SIZE, 1,
	FONT_CACHE_SIZE, &pool_size);
	if (VM_IS_SUCCEEDED(result)) {
		font_pool = vm_malloc(pool_size);
		if (NULL != font_pool) {
			result = vm_graphic_init_font_pool(font_pool, pool_size);
		} else {
			vm_log_info("allocate font pool memory failed");
			return;
		}
	}
	if (!(VM_IS_SUCCEEDED(result))) {
		vm_log_info("init font pool failed");
		return;
	}
	vm_chset_ascii_to_ucs2(font_path, (FONT_PATH_MAX_LENGTH + 1) * 2,
	EXTERNAL_FONT_PATH);
	font_paths_group[0] = font_path;
	vm_graphic_reset_font();
	result = vm_graphic_set_font(font_paths_group, 1);
	if (!(VM_IS_SUCCEEDED(result))) {
		vm_log_info("set font failed");
	}

	color.r = 255;
	color.g = 255;
	color.b = 255;
	vm_graphic_set_color(color);
	vm_graphic_set_font_size(VM_GRAPHIC_LARGE_FONT);
	vm_log_info("String: %d, %d, %d, %d, %d, %d", string[0], string[1],
			string[2], string[3], string[4], string[5]);
	vm_graphic_draw_text(&frame, 1, 1, string);
#if defined(__HDK_LINKIT_ASSIST_2502__)
	vm_graphic_blt_frame(frame_group, positions, 1);
#endif
	vm_free(frame.buffer);
	vm_free(font_pool);
	font_pool = NULL;
}