]> Pileus Git - ~andy/gtk/blob - gdk/broadway/broadway-demo.c
[broadway] Remove unnecessary backend-specific function
[~andy/gtk] / gdk / broadway / broadway-demo.c
1 /* Build with
2    gcc -lm -lz -O2 -Wall `pkg-config --libs --cflags cairo`  -o broadway broadway.c demo.c
3 */
4
5 #include "broadway.h"
6 #include <math.h>
7 #include <unistd.h>
8 #include <stdint.h>
9 #include <cairo.h>
10
11 static void
12 diff_surfaces (cairo_surface_t *surface,
13                cairo_surface_t *old_surface)
14 {
15   uint8_t *data, *old_data;
16   uint32_t *line, *old_line;
17   int w, h, stride, old_stride;
18   int x, y;
19
20   data = cairo_image_surface_get_data (surface);
21   old_data = cairo_image_surface_get_data (old_surface);
22
23   w = cairo_image_surface_get_width (surface);
24   h = cairo_image_surface_get_height (surface);
25
26   stride = cairo_image_surface_get_stride (surface);
27   old_stride = cairo_image_surface_get_stride (old_surface);
28
29   for (y = 0; y < h; y++)
30     {
31       line = (uint32_t *)data;
32       old_line = (uint32_t *)old_data;
33
34       for (x = 0; x < w; x++)
35         {
36           if ((*line & 0xffffff) == (*old_line & 0xffffff))
37             *old_line = 0;
38           else
39             *old_line = *line | 0xff000000;
40           line ++;
41           old_line ++;
42         }
43
44       data += stride;
45       old_data += old_stride;
46     }
47 }
48
49 static void
50 snippet(cairo_t *cr, int i)
51 {
52   if (1)
53     {
54       cairo_save(cr);
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 */
58         y0      = 25.6,
59         rect_width  = 204.8,
60         rect_height = 204.8,
61         radius = 102.4;   /* and an approximate curvature radius */
62
63       double x1,y1;
64
65       x1=x0+rect_width;
66       y1=y0+rect_height;
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);
74         } else {
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);
81         }
82       } else {
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);
91         } else {
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);
100         }
101       }
102       cairo_close_path (cr);
103
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);
108       cairo_stroke (cr);
109       cairo_restore(cr);
110     }
111   if (1)
112     {
113       double xc = 128.0;
114       double yc = 128.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           */
118
119       cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
120
121       cairo_set_line_width (cr, 10.0);
122       cairo_arc (cr, xc, yc, radius, angle1, angle2);
123       cairo_stroke (cr);
124
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);
128
129       cairo_arc (cr, xc, yc, 10.0, 0, 2*M_PI);
130       cairo_fill (cr);
131
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);
136       cairo_stroke (cr);
137     }
138 }
139
140 static void
141 demo2 (BroadwayOutput *output)
142 {
143   cairo_t *cr;
144   cairo_surface_t *surface, *old_surface;
145   BroadwayRect rects[2];
146   double da = 0;
147   int i;
148
149   broadway_output_new_surface(output,  0, 100, 100, 800, 600);
150
151   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
152                                         800, 600);
153   old_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
154                                             800, 600);
155
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);
159   cairo_fill (cr);
160   cairo_destroy (cr);
161
162   for (i = 0; i < 100; i++)
163     {
164       cr = cairo_create (surface);
165
166       cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
167
168       cairo_rectangle (cr, 0, 0, 800, 600);
169       cairo_fill (cr);
170
171       snippet(cr, i);
172
173       cairo_destroy (cr);
174
175       if (i == 0)
176         {
177           broadway_output_put_rgb (output, 0, 0, 0, 800, 600, 800*4,
178                                    cairo_image_surface_get_data(surface)
179                                    );
180           broadway_output_show_surface (output,  0);
181         }
182       else
183         {
184           diff_surfaces (surface,
185                          old_surface);
186           broadway_output_put_rgba (output, 0, 0, 0, 800, 600, 800*4,
187                                     cairo_image_surface_get_data(old_surface));
188         }
189       broadway_output_move_surface (output, 0, 100 + i, 100 + i);
190
191       rects[0].x = 500;
192       rects[0].y = 0;
193       rects[0].width = 100;
194       rects[0].height = 100;
195       rects[1].x = 600;
196       rects[1].y = 100;
197       rects[1].width = 100;
198       rects[1].height = 100;
199       broadway_output_copy_rectangles (output,
200                                        0,
201                                        rects, 2,
202                                        400, 0);
203
204       broadway_output_flush (output);
205
206       cr = cairo_create (old_surface);
207       cairo_set_source_surface (cr, surface, 0, 0);
208       cairo_paint (cr);
209       cairo_destroy (cr);
210
211       da += 10;
212       usleep (50 * 1000);
213   }
214
215
216   cairo_surface_destroy (surface);
217   broadway_output_destroy_surface(output,  0);
218   broadway_output_flush (output);
219 }
220
221 int
222 main (int argc, char *argv[])
223 {
224   BroadwayOutput *output;
225
226   output = broadway_output_new (STDOUT_FILENO);
227   demo2(output);
228
229   return 0;
230 }