changeset 19:cf6d65398619

More animation demo
author Thinker K.F. Li <thinker@branda.to>
date Sat, 02 Aug 2008 15:38:54 +0800
parents 0f3baa488a62
children 74d3d5dc9aaa
files src/X_main.c src/paint.c src/paint.h src/redraw_man.c
diffstat 4 files changed, 87 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/X_main.c	Sat Aug 02 14:45:42 2008 +0800
+++ b/src/X_main.c	Sat Aug 02 15:38:54 2008 +0800
@@ -14,24 +14,35 @@
 
 void draw_path(cairo_t *cr, int w, int h) {
     redraw_man_t rdman;
-    shape_t *path;
-    coord_t *coord;
-    paint_t *fill;
+    shape_t *path1, *path2;
+    coord_t *coord1, *coord2;
+    paint_t *fill1, *fill2;
     int i;
 
     redraw_man_init(&rdman, cr);
-    coord = rdman.root_coord;
+    coord1 = rdman_coord_new(&rdman, rdman.root_coord);
+    coord2 = rdman_coord_new(&rdman, rdman.root_coord);
 
-    fill = paint_color_new(&rdman, 1, 1, 0);
-    path = sh_path_new("M 22,89.36218 C -34,-0.63782 39,-9.637817 82,12.36218 C 125,34.36218 142,136.36218 142,136.36218 C 100.66667,125.36218 74.26756,123.42795 22,89.36218 z ");
-    rdman_paint_fill(&rdman, fill, path);
-    coord->matrix[0] = 0.8;
-    coord->matrix[1] = 0;
-    coord->matrix[2] = 20;
-    coord->matrix[4] = 0.8;
-    coord->matrix[5] = 20;
-    rdman_coord_changed(&rdman, coord);
-    rdman_add_shape(&rdman, (shape_t *)path, coord);
+    fill1 = paint_color_new(&rdman, 1, 1, 0);
+    fill2 = paint_color_new(&rdman, 0, 1, 1);
+    path1 = sh_path_new("M 22,89.36218 C -34,-0.63782 39,-9.637817 82,12.36218 C 125,34.36218 142,136.36218 142,136.36218 C 100.66667,125.36218 74.26756,123.42795 22,89.36218 z ");
+    rdman_paint_fill(&rdman, fill1, path1);
+    coord1->matrix[0] = 0.8;
+    coord1->matrix[1] = 0;
+    coord1->matrix[2] = 20;
+    coord1->matrix[4] = 0.8;
+    coord1->matrix[5] = 20;
+    path2 = sh_path_new("M 22,89.36218 C -34,-0.63782 39,-9.637817 82,12.36218 C 125,34.36218 142,136.36218 142,136.36218 C 100.66667,125.36218 74.26756,123.42795 22,89.36218 z ");
+    rdman_paint_fill(&rdman, fill2, path2);
+    coord2->matrix[0] = -0.8;
+    coord2->matrix[1] = 0;
+    coord2->matrix[2] = 220;
+    coord2->matrix[4] = 0.8;
+    coord2->matrix[5] = 20;
+    rdman_coord_changed(&rdman, coord1);
+    rdman_coord_changed(&rdman, coord2);
+    rdman_add_shape(&rdman, (shape_t *)path1, coord1);
+    rdman_add_shape(&rdman, (shape_t *)path2, coord2);
 
     rdman_redraw_all(&rdman);
 
@@ -39,25 +50,35 @@
 
     for(i = 0; i < 50; i++) {
 	usleep(20000);
-	coord->matrix[2] += 1;
-	coord->matrix[5] += 1;
-	paint_color_set(fill, 1, 1, (i/25) & 0x1);
-	rdman_coord_changed(&rdman, coord);
+	coord1->matrix[2] += 1;
+	coord1->matrix[5] += 1;
+	coord2->matrix[2] -= 1;
+	coord2->matrix[5] += 1;
+	paint_color_set(fill1, 1, 1, (i/25) & 0x1);
+	paint_color_set(fill2, (i/25) & 0x1, 1, 1);
+	rdman_paint_changed(&rdman, fill1);
+	rdman_paint_changed(&rdman, fill2);
+	rdman_coord_changed(&rdman, coord1);
+	rdman_coord_changed(&rdman, coord2);
 	rdman_redraw_changed(&rdman);
 	XFlush(display);
     }
 
     for(i = 0; i < 5; i++) {
 	usleep(500000);
-	paint_color_set(fill, 1, i % 2, 0);
-	rdman_paint_changed(&rdman, fill);
+	paint_color_set(fill1, 1, i % 2, 0);
+	paint_color_set(fill2, 0, i % 2, 1);
+	rdman_paint_changed(&rdman, fill1);
+	rdman_paint_changed(&rdman, fill2);
 	rdman_redraw_changed(&rdman);
 	XFlush(display);
     }
 
-    fill->free(fill);
+    fill1->free(fill1);
+    fill2->free(fill2);
     redraw_man_destroy(&rdman);
-    sh_path_free(path);
+    sh_path_free(path1);
+    sh_path_free(path2);
 }
 
 void drawing(cairo_surface_t *surface, int w, int h) {
--- a/src/paint.c	Sat Aug 02 14:45:42 2008 +0800
+++ b/src/paint.c	Sat Aug 02 15:38:54 2008 +0800
@@ -34,9 +34,7 @@
     color->r = r;
     color->g = g;
     color->b = b;
-    color->paint.prepare = paint_color_prepare;
-    color->paint.free = paint_color_free;
-    STAILQ_INIT(color->paint.members);
+    paint_init(&color->paint, paint_color_prepare, paint_color_free);
     return (paint_t *)color;
 }
 
--- a/src/paint.h	Sat Aug 02 14:45:42 2008 +0800
+++ b/src/paint.h	Sat Aug 02 15:38:54 2008 +0800
@@ -4,6 +4,7 @@
 #include <cairo.h>
 #include "mb_types.h"
 #include "redraw_man.h"
+#include "tools.h"
 
 typedef float co_comp_t;
 
@@ -11,5 +12,11 @@
 				co_comp_t r, co_comp_t g, co_comp_t b);
 extern void paint_color_set(paint_t *paint,
 			    co_comp_t r, co_comp_t g, co_comp_t b);
+#define paint_init(_paint, _prepare, _free)	\
+     do {					\
+	 (_paint)->prepare = _prepare;		\
+	 (_paint)->free = _free;		\
+	 STAILQ_INIT((_paint)->members);	\
+     } while(0)					\
 
 #endif /* __PAINT_H_ */
--- a/src/redraw_man.c	Sat Aug 02 14:45:42 2008 +0800
+++ b/src/redraw_man.c	Sat Aug 02 15:38:54 2008 +0800
@@ -464,17 +464,18 @@
     paint_t *fill;
 
     fill = shape->fill;
-    if(fill)
+    if(fill) {
 	fill->prepare(fill, rdman->cr);
-    switch(shape->sh_type) {
-    case SHT_PATH:
-	sh_path_draw(shape, rdman->cr);
-	break;
+	switch(shape->sh_type) {
+	case SHT_PATH:
+	    sh_path_draw(shape, rdman->cr);
+	    break;
 #ifdef UNITTEST
-    default:
-	sh_dummy_draw(shape, rdman->cr);
-	break;
+	default:
+	    sh_dummy_draw(shape, rdman->cr);
+	    break;
 #endif /* UNITTEST */
+	}
     }
 }
 
@@ -719,6 +720,7 @@
 #ifdef UNITTEST
 
 #include <CUnit/Basic.h>
+#include "paint.h"
 
 struct _sh_dummy {
     shape_t shape;
@@ -779,6 +781,26 @@
     dummy->draw_cnt++;
 }
 
+static void dummy_paint_prepare(paint_t *paint, cairo_t *cr) {
+}
+
+static void dummy_paint_free(paint_t *paint) {
+    if(paint)
+	free(paint);
+}
+
+paint_t *dummy_paint_new(redraw_man_t *rdman) {
+    paint_t *paint;
+
+    paint = (paint_t *)malloc(sizeof(paint_t));
+    if(paint == NULL)
+	return NULL;
+
+    paint_init(paint, dummy_paint_prepare, dummy_paint_free);
+
+    return paint;
+}
+
 void test_rdman_find_overlaid_shapes(void) {
     redraw_man_t rdman;
     geo_t geo;
@@ -834,6 +856,7 @@
     coord_t *coords[3];
     shape_t *shapes[3];
     sh_dummy_t **dummys;
+    paint_t *paint;
     redraw_man_t *rdman;
     redraw_man_t _rdman;
     int i;
@@ -842,8 +865,10 @@
 
     rdman = &_rdman;
     redraw_man_init(rdman, NULL);
+    paint = dummy_paint_new(rdman);
     for(i = 0; i < 3; i++) {
 	shapes[i] = sh_dummy_new(0, 0, 50, 50);
+	rdman_paint_fill(rdman, paint, shapes[i]);
 	coords[i] = rdman_coord_new(rdman, rdman->root_coord);
 	coords[i]->matrix[2] = 10 + i * 100;
 	coords[i]->matrix[5] = 10 + i * 100;
@@ -863,6 +888,9 @@
     CU_ASSERT(dummys[0]->draw_cnt == 1);
     CU_ASSERT(dummys[1]->draw_cnt == 2);
     CU_ASSERT(dummys[2]->draw_cnt == 2);
+
+    paint->free(paint);
+    redraw_man_destroy(rdman);
 }
 
 CU_pSuite get_redraw_man_suite(void) {