Mercurial > MadButterfly
comparison src/paint.c @ 23:56f592f56ff7
Fix bug and add linear gradient paint.
- fix the bug that forget to clear n_dirty_geos in rdman_redraw_all().
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 02 Aug 2008 23:10:42 +0800 |
parents | 83d24300a992 |
children | 59a295651480 |
comparison
equal
deleted
inserted
replaced
22:8fcf2d878ecd | 23:56f592f56ff7 |
---|---|
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include <cairo.h> | 3 #include <cairo.h> |
4 #include "paint.h" | 4 #include "paint.h" |
5 | 5 |
6 /*! \brief Solid color paint. | |
7 */ | |
6 typedef struct _paint_color { | 8 typedef struct _paint_color { |
7 paint_t paint; | 9 paint_t paint; |
8 co_comp_t r, g, b, a; | 10 co_comp_t r, g, b, a; |
9 redraw_man_t *rdman; | 11 redraw_man_t *rdman; |
10 } paint_color_t; | 12 } paint_color_t; |
48 color->r = r; | 50 color->r = r; |
49 color->g = g; | 51 color->g = g; |
50 color->b = b; | 52 color->b = b; |
51 color->a = a; | 53 color->a = a; |
52 } | 54 } |
55 | |
56 /*! \brief Linear gradient. | |
57 */ | |
58 typedef struct _paint_linear { | |
59 paint_t paint; | |
60 co_aix x1, y1; | |
61 co_aix x2, y2; | |
62 int n_stops; | |
63 grad_stop_t *stops; | |
64 int flags; | |
65 cairo_pattern_t *ptn; | |
66 } paint_linear_t; | |
67 | |
68 #define LIF_DIRTY 0x1 | |
69 | |
70 static void paint_linear_prepare(paint_t *paint, cairo_t *cr) { | |
71 paint_linear_t *linear = (paint_linear_t *)paint; | |
72 cairo_pattern_t *ptn; | |
73 grad_stop_t *stop; | |
74 int i; | |
75 | |
76 ptn = linear->ptn; | |
77 if(linear->flags & LIF_DIRTY) { | |
78 if(ptn) | |
79 cairo_pattern_destroy(ptn); | |
80 linear->flags &= ~LIF_DIRTY; | |
81 ptn = cairo_pattern_create_linear(linear->x1, linear->y1, | |
82 linear->x2, linear->y2); | |
83 for(i = 0; i < linear->n_stops; i++) { | |
84 stop = &linear->stops[i]; | |
85 cairo_pattern_add_color_stop_rgba(ptn, stop->offset, | |
86 stop->r, stop->g, stop->b, | |
87 stop->a); | |
88 } | |
89 linear->ptn = ptn; | |
90 } | |
91 | |
92 cairo_set_source(cr, ptn); | |
93 } | |
94 | |
95 static void paint_linear_free(paint_t *paint) { | |
96 paint_linear_t *linear = (paint_linear_t *)paint; | |
97 | |
98 if(linear->ptn) | |
99 cairo_pattern_destroy(linear->ptn); | |
100 free(paint); | |
101 } | |
102 | |
103 paint_t *paint_linear_new(redraw_man_t *rdman, | |
104 co_aix x1, co_aix y1, co_aix x2, co_aix y2) { | |
105 paint_linear_t *linear; | |
106 | |
107 linear = (paint_linear_t *)malloc(sizeof(paint_linear_t)); | |
108 if(linear == NULL) | |
109 return NULL; | |
110 | |
111 paint_init(&linear->paint, paint_linear_prepare, paint_linear_free); | |
112 | |
113 linear->x1 = x1; | |
114 linear->y1 = y1; | |
115 linear->x2 = x2; | |
116 linear->y2 = y2; | |
117 linear->n_stops = 0; | |
118 linear->stops = NULL; | |
119 linear->flags = LIF_DIRTY; | |
120 linear->ptn = NULL; | |
121 | |
122 return (paint_t *)linear; | |
123 } | |
124 | |
125 grad_stop_t *paint_linear_stops(paint_t *paint, | |
126 int n_stops, | |
127 grad_stop_t *stops) { | |
128 paint_linear_t *linear = (paint_linear_t *)paint; | |
129 grad_stop_t *old_stops; | |
130 | |
131 old_stops = linear->stops; | |
132 linear->n_stops = n_stops; | |
133 linear->stops = stops; | |
134 return old_stops; | |
135 } | |
136 |