Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / modules / mod_particle / plant.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file plant.cpp
3 **      \brief Implementation of the "Plant" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <synfig/angle.h>
34 #include "plant.h"
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
43
44 #include <ETL/calculus>
45 #include <ETL/bezier>
46 #include <ETL/hermite>
47 #include <vector>
48
49 #include <synfig/valuenode_bline.h>
50
51 #endif
52
53 using namespace etl;
54
55 /* === M A C R O S ========================================================= */
56
57 #define SAMPLES         300
58 #define ROUND_END_FACTOR        (4)
59 #define CUSP_THRESHOLD          (0.15)
60 #define NO_LOOP_COOKIE          synfig::Vector(84951305,7836658)
61 #define EPSILON                         (0.000000001)
62 #define CUSP_TANGENT_ADJUST     (0.025)
63
64 /* === G L O B A L S ======================================================= */
65
66 SYNFIG_LAYER_INIT(Plant);
67 SYNFIG_LAYER_SET_NAME(Plant,"plant");
68 SYNFIG_LAYER_SET_LOCAL_NAME(Plant,N_("Plant"));
69 SYNFIG_LAYER_SET_CATEGORY(Plant,N_("Other"));
70 SYNFIG_LAYER_SET_VERSION(Plant,"0.2");
71 SYNFIG_LAYER_SET_CVS_ID(Plant,"$Id$");
72
73 /* === P R O C E D U R E S ================================================= */
74
75 /* === M E T H O D S ======================================================= */
76
77
78 Plant::Plant():
79         offset(0,0),
80         split_angle(Angle::deg(10)),
81         gravity(0,-0.1),
82         velocity(0.3),
83         perp_velocity(0.0),
84         step(0.01),
85         sprouts(10),
86         version(version__),
87         use_width(true)
88 {
89         bounding_rect=Rect::zero();
90         random_factor=0.2;
91         random.set_seed(time(NULL));
92
93         bline.push_back(BLinePoint());
94         bline.push_back(BLinePoint());
95         bline.push_back(BLinePoint());
96         bline[0].set_vertex(Point(0,1));
97         bline[1].set_vertex(Point(0,-1));
98         bline[2].set_vertex(Point(1,0));
99         bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
100         bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
101         bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
102         bline[0].set_width(1.0f);
103         bline[1].set_width(1.0f);
104         bline[2].set_width(1.0f);
105         bline_loop=true;
106         mass=(0.5);
107         splits=5;
108         drag=0.1;
109         size=0.015;
110         needs_sync_=true;
111         sync();
112         size_as_alpha=false;
113         reverse=true;
114 }
115
116 void
117 Plant::branch(int n,int depth,float t, float stunt_growth, synfig::Point position,synfig::Vector vel)const
118 {
119         float next_split((1.0-t)/(splits-depth)+t/*+random_factor*random(40+depth,t*splits,0,0)/splits*/);
120         for(;t<next_split;t+=step)
121         {
122                 vel[0]+=gravity[0]*step;
123                 vel[1]+=gravity[1]*step;
124                 vel*=(1.0-(drag)*step);
125                 position[0]+=vel[0]*step;
126                 position[1]+=vel[1]*step;
127
128                 particle_list.push_back(Particle(position, gradient(t)));
129                 if (particle_list.size() % 1000000 == 0)
130                         synfig::info("constructed %d million particles...", particle_list.size()/1000000);
131
132                 bounding_rect.expand(position);
133         }
134
135         if(t>=1.0-stunt_growth)return;
136
137         synfig::Real sin_v=synfig::Angle::cos(split_angle).get();
138         synfig::Real cos_v=synfig::Angle::sin(split_angle).get();
139
140         synfig::Vector velocity1(vel[0]*sin_v - vel[1]*cos_v + random_factor*random(Random::SMOOTH_COSINE, 30+n+depth, t*splits, 0.0f, 0.0f),
141                                                          vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 32+n+depth, t*splits, 0.0f, 0.0f));
142         synfig::Vector velocity2(vel[0]*sin_v + vel[1]*cos_v + random_factor*random(Random::SMOOTH_COSINE, 31+n+depth, t*splits, 0.0f, 0.0f),
143                                                         -vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 33+n+depth, t*splits, 0.0f, 0.0f));
144
145         Plant::branch(n,depth+1,t,stunt_growth,position,velocity1);
146         Plant::branch(n,depth+1,t,stunt_growth,position,velocity2);
147 }
148
149 void
150 Plant::calc_bounding_rect()const
151 {
152         std::vector<synfig::BLinePoint>::const_iterator iter,next;
153
154         bounding_rect=Rect::zero();
155
156         // Bline must have at least 2 points in it
157         if(bline.size()<2)
158                 return;
159
160         next=bline.begin();
161
162         if(bline_loop)
163                 iter=--bline.end();
164         else
165                 iter=next++;
166
167         for(;next!=bline.end();iter=next++)
168         {
169                 bounding_rect.expand(iter->get_vertex());
170                 bounding_rect.expand(next->get_vertex());
171                 bounding_rect.expand(iter->get_vertex()+iter->get_tangent2()*0.3333333333333);
172                 bounding_rect.expand(next->get_vertex()-next->get_tangent1()*0.3333333333333);
173                 bounding_rect.expand(next->get_vertex()+next->get_tangent2()*velocity);
174         }
175         bounding_rect.expand_x(gravity[0]);
176         bounding_rect.expand_y(gravity[1]);
177         bounding_rect.expand_x(size);
178         bounding_rect.expand_y(size);
179 }
180
181 void
182 Plant::sync()const
183 {
184         Mutex::Lock lock(mutex);
185         if (!needs_sync_) return;
186         time_t start_time; time(&start_time);
187         particle_list.clear();
188
189         bounding_rect=Rect::zero();
190
191         // Bline must have at least 2 points in it
192         if(bline.size()<2)
193         {
194                 needs_sync_=false;
195                 return;
196         }
197
198         std::vector<synfig::BLinePoint>::const_iterator iter,next;
199
200         etl::hermite<Vector> curve;
201
202         Real step(abs(this->step));
203
204         int seg(0);
205
206         next=bline.begin();
207
208         if(bline_loop)  iter=--bline.end(); // iter is the last  bline in the list; next is the first  bline in the list
209         else                    iter=next++;            // iter is the first bline in the list; next is the second bline in the list
210
211         // loop through the bline; seg counts the blines as we do so; stop before iter is the last bline in the list
212         for(;next!=bline.end();iter=next++,seg++)
213         {
214                 float iterw=iter->get_width();  // the width value of the iter vertex
215                 float nextw=next->get_width();  // the width value of the next vertex
216                 float width;                                    // the width at an intermediate position
217                 curve.p1()=iter->get_vertex();
218                 curve.t1()=iter->get_tangent2();
219                 curve.p2()=next->get_vertex();
220                 curve.t2()=next->get_tangent1();
221                 curve.sync();
222                 etl::derivative<etl::hermite<Vector> > deriv(curve);
223
224                 Real f;
225
226                 int i=0, branch_count = 0, steps = round_to_int(1.0/step);
227                 if (steps < 1) steps = 1;
228                 for(f=0.0;f<1.0;f+=step,i++)
229                 {
230                         Point point(curve(f));
231
232                         particle_list.push_back(Particle(point, gradient(0)));
233                         if (particle_list.size() % 1000000 == 0)
234                                 synfig::info("constructed %d million particles...", particle_list.size()/1000000);
235
236                         bounding_rect.expand(point);
237
238                         Real stunt_growth(random_factor * (random(Random::SMOOTH_COSINE,i,f+seg,0.0f,0.0f)/2.0+0.5));
239                         stunt_growth*=stunt_growth;
240
241                         if((((i+1)*sprouts + steps/2) / steps) > branch_count) {
242                                 Vector branch_velocity(deriv(f).norm()*velocity + deriv(f).perp().norm()*perp_velocity);
243
244                                 branch_velocity[0] += random_factor * random(Random::SMOOTH_COSINE, 1, f*splits, 0.0f, 0.0f);
245                                 branch_velocity[1] += random_factor * random(Random::SMOOTH_COSINE, 2, f*splits, 0.0f, 0.0f);
246
247                                 if (use_width)
248                                 {
249                                         width = iterw+(nextw-iterw)*f; // calculate the width based on the current position
250
251                                         branch_velocity[0] *= width; // scale the velocity accordingly to the current width
252                                         branch_velocity[1] *= width;
253                                 }
254
255                                 branch_count++;
256                                 branch(i, 0, 0,          // time
257                                            stunt_growth, // stunt growth
258                                            point, branch_velocity);
259                         }
260                 }
261         }
262
263         time_t end_time; time(&end_time);
264         if (end_time-start_time > 4)
265                 synfig::info("Plant::sync() constructed %d particles in %d seconds\n",
266                                          particle_list.size(), int(end_time-start_time));
267         needs_sync_=false;
268 }
269
270 bool
271 Plant::set_param(const String & param, const ValueBase &value)
272 {
273         if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
274         {
275                 bline=value;
276                 bline_loop=value.get_loop();
277                 needs_sync_=true;
278
279                 return true;
280         }
281         if(param=="seed" && value.same_type_as(int()))
282         {
283                 random.set_seed(value.get(int()));
284                 needs_sync_=true;
285                 return true;
286         }
287         IMPORT(offset);
288         IMPORT_PLUS(split_angle,needs_sync_=true);
289         IMPORT_PLUS(gravity,needs_sync_=true);
290         IMPORT_PLUS(gradient,needs_sync_=true);
291         IMPORT_PLUS(velocity,needs_sync_=true);
292         IMPORT_PLUS(perp_velocity,needs_sync_=true);
293         IMPORT_PLUS(step,{
294                         needs_sync_ = true;
295                         if (step <= 0)
296                                 step=0.01; // user is probably clueless - give a good default
297                         else if (step < 0.00001)
298                                 step=0.00001; // 100K should be enough for anyone
299                         else if (step > 1)
300                                 step=1;
301                 });
302         IMPORT_PLUS(splits,{
303                         needs_sync_=true;
304                         if (splits < 1)
305                                 splits = 1;
306                 });
307         IMPORT_PLUS(sprouts,needs_sync_=true);
308         IMPORT_PLUS(random_factor,needs_sync_=true);
309         IMPORT_PLUS(drag,needs_sync_=true);
310         IMPORT(size);
311         IMPORT(size_as_alpha);
312         IMPORT(reverse);
313
314         return Layer_Composite::set_param(param,value);
315 }
316 /*
317 void
318 Plant::set_time(Context context, Time time)const
319 {
320         if(needs_sync==true)
321         {
322                 sync();
323                 needs_sync_=false;
324         }
325         //const_cast<Plant*>(this)->sync();
326         context.set_time(time);
327 }
328
329 void
330 Plant::set_time(Context context, Time time, Vector pos)const
331 {
332         if(needs_sync==true)
333         {
334                 sync();
335                 needs_sync_=false;
336         }
337         //const_cast<Plant*>(this)->sync();
338         context.set_time(time,pos);
339 }
340 */
341 ValueBase
342 Plant::get_param(const String& param)const
343 {
344         if(param=="seed")
345                 return random.get_seed();
346         EXPORT(bline);
347         EXPORT(offset);
348         EXPORT(split_angle);
349         EXPORT(gravity);
350         EXPORT(velocity);
351         EXPORT(perp_velocity);
352         EXPORT(step);
353         EXPORT(gradient);
354         EXPORT(splits);
355         EXPORT(sprouts);
356         EXPORT(random_factor);
357         EXPORT(drag);
358         EXPORT(size);
359
360         EXPORT(size_as_alpha);
361         EXPORT(reverse);
362
363         EXPORT_NAME();
364
365         if(param=="Version" || param=="version" || param=="version__")
366                 return version;
367
368         return Layer_Composite::get_param(param);
369 }
370
371 Layer::Vocab
372 Plant::get_param_vocab()const
373 {
374         Layer::Vocab ret(Layer_Composite::get_param_vocab());
375
376         ret.push_back(ParamDesc("bline")
377                 .set_local_name(_("Vertices"))
378                 .set_description(_("A list of BLine Points"))
379                 .set_origin("offset")
380                 //.set_scalar("width")
381         );
382
383         ret.push_back(ParamDesc("offset")
384                 .set_local_name(_("Offset"))
385         );
386
387         ret.push_back(ParamDesc("gradient")
388                 .set_local_name(_("Gradient"))
389                 .set_description(_("Gradient to be used for coloring the plant"))
390         );
391
392         ret.push_back(ParamDesc("split_angle")
393                 .set_local_name(_("Split Angle"))
394                 .set_description(_("Angle by which each split deviates from its parent"))
395         );
396
397         ret.push_back(ParamDesc("gravity")
398                 .set_local_name(_("Gravity"))
399                 .set_description(_("Direction in which the shoots tend to face"))
400                 .set_is_distance()
401         );
402
403         ret.push_back(ParamDesc("velocity")
404                 .set_local_name(_("Tangential Velocity"))
405                 .set_description(_("Amount to which shoots tend to grow along the tangent to the BLine"))
406         );
407
408         ret.push_back(ParamDesc("perp_velocity")
409                 .set_local_name(_("Perpendicular Velocity"))
410                 .set_description(_("Amount to which shoots tend to grow perpendicular to the tangent to the BLine"))
411         );
412
413         ret.push_back(ParamDesc("size")
414                 .set_local_name(_("Stem Size"))
415                 .set_description(_("Size of the stem"))
416                 .set_is_distance()
417         );
418
419         ret.push_back(ParamDesc("size_as_alpha")
420                 .set_local_name(_("Size As Alpha"))
421                 .set_description(_("If enabled, the alpha channel from the gradient is multiplied by the stem size, and an alpha of 1.0 is used when rendering"))
422         );
423
424         ret.push_back(ParamDesc("reverse")
425                 .set_local_name(_("Reverse"))
426                 .set_description(_("If enabled, render the plant in the opposite direction"))
427         );
428
429         ret.push_back(ParamDesc("step")
430                 .set_local_name(_("Step"))
431                 .set_description(_("Measure of the distance between points when rendering"))
432         );
433
434         ret.push_back(ParamDesc("seed")
435                 .set_local_name(_("Seed"))
436                 .set_description(_("Used to seed the pseudo-random number generator"))
437         );
438
439         ret.push_back(ParamDesc("splits")
440                 .set_local_name(_("Splits"))
441                 .set_description(_("Maximum number of times that each sprout can sprout recursively"))
442         );
443
444         ret.push_back(ParamDesc("sprouts")
445                 .set_local_name(_("Sprouts"))
446                 .set_description(_("Number of places that growth occurs on each bline section"))
447         );
448
449         ret.push_back(ParamDesc("random_factor")
450                 .set_local_name(_("Random Factor"))
451                 .set_description(_("Used to scale down all random effects.  Set to zero to disable randomness"))
452         );
453
454         ret.push_back(ParamDesc("drag")
455                 .set_local_name(_("Drag"))
456                 .set_description(_("Drag slows the growth"))
457         );
458
459         return ret;
460 }
461
462 bool
463 Plant::set_version(const String &ver)
464 {
465         version = ver;
466
467         if (version == "0.1")
468                 use_width = false;
469
470         return true;
471 }
472
473 bool
474 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
475 {
476         bool ret(context.accelerated_render(surface,quality,renddesc,cb));
477         if(is_disabled() || !ret)
478                 return ret;
479
480         Surface dest_surface;
481         dest_surface.set_wh(surface->get_w(),surface->get_h());
482         dest_surface.clear();
483
484         const Point     tl(renddesc.get_tl()-offset);
485         const Point br(renddesc.get_br()-offset);
486
487         const int       w(renddesc.get_w());
488         const int       h(renddesc.get_h());
489
490         const int       surface_width(surface->get_w());
491         const int       surface_height(surface->get_h());
492
493         // Width and Height of a pixel
494         const Real pw = (br[0] - tl[0]) / w;
495         const Real ph = (br[1] - tl[1]) / h;
496
497         if (isinf(pw) || isinf(ph))
498                 return true;
499
500         if(needs_sync_==true)
501                 sync();
502
503         if (particle_list.begin() != particle_list.end())
504         {
505                 std::vector<Particle>::iterator iter;
506                 Particle *particle;
507
508                 float radius(size*sqrt(1.0f/(abs(pw)*abs(ph))));
509
510                 int x1,y1,x2,y2;
511
512                 if (reverse)    iter = particle_list.end();
513                 else                    iter = particle_list.begin();
514
515                 while (true)
516                 {
517                         if (reverse)    particle = &(*(iter-1));
518                         else                    particle = &(*iter);
519
520                         float scaled_radius(radius);
521                         Color color(particle->color);
522                         if(size_as_alpha)
523                         {
524                                 scaled_radius*=color.get_a();
525                                 color.set_a(1);
526                         }
527
528                         // previously, radius was multiplied by sqrt(step)*12 only if
529                         // the radius came out at less than 1 (pixel):
530                         //   if (radius<=1.0f) radius*=sqrt(step)*12.0f;
531                         // seems a little arbitrary - does it help?
532
533                         // calculate the box that this particle will be drawn as
534                         float x1f=(particle->point[0]-tl[0])/pw-(scaled_radius*0.5);
535                         float x2f=(particle->point[0]-tl[0])/pw+(scaled_radius*0.5);
536                         float y1f=(particle->point[1]-tl[1])/ph-(scaled_radius*0.5);
537                         float y2f=(particle->point[1]-tl[1])/ph+(scaled_radius*0.5);
538                         x1=ceil_to_int(x1f);
539                         x2=ceil_to_int(x2f)-1;
540                         y1=ceil_to_int(y1f);
541                         y2=ceil_to_int(y2f)-1;
542
543                         // if the box isn't entirely off the canvas, draw it
544                         if(x1<=surface_width && y1<=surface_height && x2>=0 && y2>=0)
545                         {
546                                 float x1e=x1-x1f, x2e=x2f-x2, y1e=y1-y1f, y2e=y2f-y2;
547                                 // printf("x1e %.4f x2e %.4f y1e %.4f y2e %.4f\n", x1e, x2e, y1e, y2e);
548
549                                 // adjust the box so it's entirely on the canvas
550                                 if(x1<=0) { x1=0; x1e=0; }
551                                 if(y1<=0) { y1=0; y1e=0; }
552                                 if(x2>=surface_width)  { x2=surface_width;  x2e=0; }
553                                 if(y2>=surface_height) { y2=surface_height; y2e=0; }
554
555                                 int w(x2-x1), h(y2-y1);
556
557                                 Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
558                                 if(w>0 && h>0)
559                                         dest_surface.fill(color,surface_pen,w,h);
560
561                                 /* the rectangle doesn't cross any vertical pixel boundaries so we don't
562                                  * need to draw any top or bottom edges
563                                  */
564                                 if(x2<x1)
565                                 {
566                                         // case 1 - a single pixel
567                                         if(y2<y1)
568                                         {
569                                                 surface_pen.move_to(x2,y2);
570                                                 surface_pen.set_alpha((x2f-x1f)*(y2f-y1f));
571                                                 surface_pen.put_value(color);
572                                         }
573                                         // case 2 - a single vertical column of pixels
574                                         else
575                                         {
576                                                 surface_pen.move_to(x2,y1-1);
577                                                 if (y1e!=0)     // maybe draw top pixel
578                                                 {
579                                                         surface_pen.set_alpha(y1e*(x2f-x1f));
580                                                         surface_pen.put_value(color);
581                                                 }
582                                                 surface_pen.inc_y();
583                                                 surface_pen.set_alpha(x2f-x1f);
584                                                 for(int i=y1; i<y2; i++) // maybe draw pixels between
585                                                 {
586                                                         surface_pen.put_value(color);
587                                                         surface_pen.inc_y();
588                                                 }
589                                                 if (y2e!=0)     // maybe draw bottom pixel
590                                                 {
591                                                         surface_pen.set_alpha(y2e*(x2f-x1f));
592                                                         surface_pen.put_value(color);
593                                                 }
594                                         }
595                                 }
596                                 else
597                                 {
598                                         // case 3 - a single horizontal row of pixels
599                                         if(y2<y1)
600                                         {
601                                                 surface_pen.move_to(x1-1,y2);
602                                                 if (x1e!=0)     // maybe draw left pixel
603                                                 {
604                                                         surface_pen.set_alpha(x1e*(y2f-y1f));
605                                                         surface_pen.put_value(color);
606                                                 }
607                                                 surface_pen.inc_x();
608                                                 surface_pen.set_alpha(y2f-y1f);
609                                                 for(int i=x1; i<x2; i++) // maybe draw pixels between
610                                                 {
611                                                         surface_pen.put_value(color);
612                                                         surface_pen.inc_x();
613                                                 }
614                                                 if (x2e!=0)     // maybe draw right pixel
615                                                 {
616                                                         surface_pen.set_alpha(x2e*(y2f-y1f));
617                                                         surface_pen.put_value(color);
618                                                 }
619                                         }
620                                         // case 4 - a proper block of pixels
621                                         else
622                                         {
623                                                 if (x1e!=0)     // maybe draw left edge
624                                                 {
625                                                         surface_pen.move_to(x1-1,y1-1);
626                                                         if (y1e!=0)     // maybe draw top left pixel
627                                                         {
628                                                                 surface_pen.set_alpha(x1e*y1e);
629                                                                 surface_pen.put_value(color);
630                                                         }
631                                                         surface_pen.inc_y();
632                                                         surface_pen.set_alpha(x1e);
633                                                         for(int i=y1; i<y2; i++) // maybe draw pixels along the left edge
634                                                         {
635                                                                 surface_pen.put_value(color);
636                                                                 surface_pen.inc_y();
637                                                         }
638                                                         if (y2e!=0)     // maybe draw bottom left pixel
639                                                         {
640                                                                 surface_pen.set_alpha(x1e*y2e);
641                                                                 surface_pen.put_value(color);
642                                                         }
643                                                         surface_pen.inc_x();
644                                                 }
645                                                 else
646                                                         surface_pen.move_to(x1,y2);
647
648                                                 if (y2e!=0)     // maybe draw bottom edge
649                                                 {
650                                                         surface_pen.set_alpha(y2e);
651                                                         for(int i=x1; i<x2; i++) // maybe draw pixels along the bottom edge
652                                                         {
653                                                                 surface_pen.put_value(color);
654                                                                 surface_pen.inc_x();
655                                                         }
656                                                         if (x2e!=0)     // maybe draw bottom right pixel
657                                                         {
658                                                                 surface_pen.set_alpha(x2e*y2e);
659                                                                 surface_pen.put_value(color);
660                                                         }
661                                                         surface_pen.dec_y();
662                                                 }
663                                                 else
664                                                         surface_pen.move_to(x2,y2-1);
665
666                                                 if (x2e!=0)     // maybe draw right edge
667                                                 {
668                                                         surface_pen.set_alpha(x2e);
669                                                         for(int i=y1; i<y2; i++) // maybe draw pixels along the right edge
670                                                         {
671                                                                 surface_pen.put_value(color);
672                                                                 surface_pen.dec_y();
673                                                         }
674                                                         if (y1e!=0)     // maybe draw top right pixel
675                                                         {
676                                                                 surface_pen.set_alpha(x2e*y1e);
677                                                                 surface_pen.put_value(color);
678                                                         }
679                                                         surface_pen.dec_x();
680                                                 }
681                                                 else
682                                                         surface_pen.move_to(x2-1,y1-1);
683
684                                                 if (y1e!=0)     // maybe draw top edge
685                                                 {
686                                                         surface_pen.set_alpha(y1e);
687                                                         for(int i=x1; i<x2; i++) // maybe draw pixels along the top edge
688                                                         {
689                                                                 surface_pen.put_value(color);
690                                                                 surface_pen.dec_x();
691                                                         }
692                                                 }
693                                         }
694                                 }
695                         }
696
697                         if (reverse)
698                         {
699                                 if (--iter == particle_list.begin())
700                                         break;
701                         }
702                         else
703                         {
704                                 if (++iter == particle_list.end())
705                                         break;
706                         }
707                 }
708         }
709
710         Surface::alpha_pen pen(surface->get_pen(0,0),get_amount(),get_blend_method());
711         dest_surface.blit_to(pen);
712
713         return true;
714 }
715
716 Rect
717 Plant::get_bounding_rect(Context context)const
718 {
719         if(needs_sync_==true)
720                 sync();
721
722         if(is_disabled())
723                 return Rect::zero();
724
725         if(Color::is_onto(get_blend_method()))
726                 return context.get_full_bounding_rect() & bounding_rect;
727
728         //if(get_blend_method()==Color::BLEND_BEHIND)
729         //      return context.get_full_bounding_rect() | bounding_rect;
730         return bounding_rect;
731 }