2 gcc -lm -lz -O2 -Wall `pkg-config --libs --cflags cairo` -o broadway broadway.c demo.c
12 diff_surfaces (cairo_surface_t *surface,
13 cairo_surface_t *old_surface)
15 uint8_t *data, *old_data;
16 uint32_t *line, *old_line;
17 int w, h, stride, old_stride;
20 data = cairo_image_surface_get_data (surface);
21 old_data = cairo_image_surface_get_data (old_surface);
23 w = cairo_image_surface_get_width (surface);
24 h = cairo_image_surface_get_height (surface);
26 stride = cairo_image_surface_get_stride (surface);
27 old_stride = cairo_image_surface_get_stride (old_surface);
29 for (y = 0; y < h; y++)
31 line = (uint32_t *)data;
32 old_line = (uint32_t *)old_data;
34 for (x = 0; x < w; x++)
36 if ((*line & 0xffffff) == (*old_line & 0xffffff))
39 *old_line = *line | 0xff000000;
45 old_data += old_stride;
50 snippet(cairo_t *cr, int i)
55 cairo_rotate (cr, i * 0.002);
56 /* a custom shape that could be wrapped in a function */
57 double x0 = 25.6, /* parameters like cairo_rectangle */
61 radius = 102.4; /* and an approximate curvature radius */
67 if (rect_width/2<radius) {
68 if (rect_height/2<radius) {
69 cairo_move_to (cr, x0, (y0 + y1)/2);
70 cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
71 cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
72 cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
73 cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
75 cairo_move_to (cr, x0, y0 + radius);
76 cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
77 cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
78 cairo_line_to (cr, x1 , y1 - radius);
79 cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
80 cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
83 if (rect_height/2<radius) {
84 cairo_move_to (cr, x0, (y0 + y1)/2);
85 cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
86 cairo_line_to (cr, x1 - radius, y0);
87 cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
88 cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
89 cairo_line_to (cr, x0 + radius, y1);
90 cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
92 cairo_move_to (cr, x0, y0 + radius);
93 cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
94 cairo_line_to (cr, x1 - radius, y0);
95 cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
96 cairo_line_to (cr, x1 , y1 - radius);
97 cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
98 cairo_line_to (cr, x0 + radius, y1);
99 cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
102 cairo_close_path (cr);
104 cairo_set_source_rgb (cr, 0.5, 0.5, 1);
105 cairo_fill_preserve (cr);
106 cairo_set_source_rgba (cr, 0.5, 0, 0, 0.5);
107 cairo_set_line_width (cr, 10.0);
115 double radius = 100.0;
116 double angle1 = (45.0 + i * 5) * (M_PI/180.0); /* angles are specified */
117 double angle2 = (180.0 + i * 5) * (M_PI/180.0); /* in radians */
119 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
121 cairo_set_line_width (cr, 10.0);
122 cairo_arc (cr, xc, yc, radius, angle1, angle2);
125 /* draw helping lines */
126 cairo_set_source_rgba (cr, 1, 0.2, 0.2, 0.6);
127 cairo_set_line_width (cr, 6.0);
129 cairo_arc (cr, xc, yc, 10.0, 0, 2*M_PI);
132 cairo_arc (cr, xc, yc, radius, angle1, angle1);
133 cairo_line_to (cr, xc, yc);
134 cairo_arc (cr, xc, yc, radius, angle2, angle2);
135 cairo_line_to (cr, xc, yc);
141 demo2 (BroadwayOutput *output)
144 cairo_surface_t *surface, *old_surface;
145 BroadwayRect rects[2];
149 broadway_output_new_surface(output, 0, 100, 100, 800, 600);
151 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
153 old_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
156 cr = cairo_create (old_surface);
157 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
158 cairo_rectangle (cr, 0, 0, 800, 600);
162 for (i = 0; i < 100; i++)
164 cr = cairo_create (surface);
166 cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
168 cairo_rectangle (cr, 0, 0, 800, 600);
177 broadway_output_put_rgb (output, 0, 0, 0, 800, 600, 800*4,
178 cairo_image_surface_get_data(surface)
180 broadway_output_show_surface (output, 0);
184 diff_surfaces (surface,
186 broadway_output_put_rgba (output, 0, 0, 0, 800, 600, 800*4,
187 cairo_image_surface_get_data(old_surface));
189 broadway_output_move_surface (output, 0, 100 + i, 100 + i);
193 rects[0].width = 100;
194 rects[0].height = 100;
197 rects[1].width = 100;
198 rects[1].height = 100;
199 broadway_output_copy_rectangles (output,
204 broadway_output_flush (output);
206 cr = cairo_create (old_surface);
207 cairo_set_source_surface (cr, surface, 0, 0);
216 cairo_surface_destroy (surface);
217 broadway_output_destroy_surface(output, 0);
218 broadway_output_flush (output);
222 main (int argc, char *argv[])
224 BroadwayOutput *output;
226 output = broadway_output_new (STDOUT_FILENO, 1);