More descriptions for layer parameters
[synfig.git] / synfig-core / 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         origin(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         Layer::Vocab voc(get_param_vocab());
115         Layer::fill_static(voc);
116 }
117
118 void
119 Plant::branch(int n,int depth,float t, float stunt_growth, synfig::Point position,synfig::Vector vel)const
120 {
121         float next_split((1.0-t)/(splits-depth)+t/*+random_factor*random(40+depth,t*splits,0,0)/splits*/);
122         for(;t<next_split;t+=step)
123         {
124                 vel[0]+=gravity[0]*step;
125                 vel[1]+=gravity[1]*step;
126                 vel*=(1.0-(drag)*step);
127                 position[0]+=vel[0]*step;
128                 position[1]+=vel[1]*step;
129
130                 particle_list.push_back(Particle(position, gradient(t)));
131                 if (particle_list.size() % 1000000 == 0)
132                         synfig::info("constructed %d million particles...", particle_list.size()/1000000);
133
134                 bounding_rect.expand(position);
135         }
136
137         if(t>=1.0-stunt_growth)return;
138
139         synfig::Real sin_v=synfig::Angle::cos(split_angle).get();
140         synfig::Real cos_v=synfig::Angle::sin(split_angle).get();
141
142         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),
143                                                          vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 32+n+depth, t*splits, 0.0f, 0.0f));
144         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),
145                                                         -vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 33+n+depth, t*splits, 0.0f, 0.0f));
146
147         Plant::branch(n,depth+1,t,stunt_growth,position,velocity1);
148         Plant::branch(n,depth+1,t,stunt_growth,position,velocity2);
149 }
150
151 void
152 Plant::calc_bounding_rect()const
153 {
154         std::vector<synfig::BLinePoint>::const_iterator iter,next;
155
156         bounding_rect=Rect::zero();
157
158         // Bline must have at least 2 points in it
159         if(bline.size()<2)
160                 return;
161
162         next=bline.begin();
163
164         if(bline_loop)
165                 iter=--bline.end();
166         else
167                 iter=next++;
168
169         for(;next!=bline.end();iter=next++)
170         {
171                 bounding_rect.expand(iter->get_vertex());
172                 bounding_rect.expand(next->get_vertex());
173                 bounding_rect.expand(iter->get_vertex()+iter->get_tangent2()*0.3333333333333);
174                 bounding_rect.expand(next->get_vertex()-next->get_tangent1()*0.3333333333333);
175                 bounding_rect.expand(next->get_vertex()+next->get_tangent2()*velocity);
176         }
177         bounding_rect.expand_x(gravity[0]);
178         bounding_rect.expand_y(gravity[1]);
179         bounding_rect.expand_x(size);
180         bounding_rect.expand_y(size);
181 }
182
183 void
184 Plant::sync()const
185 {
186         Mutex::Lock lock(mutex);
187         if (!needs_sync_) return;
188         time_t start_time; time(&start_time);
189         particle_list.clear();
190
191         bounding_rect=Rect::zero();
192
193         // Bline must have at least 2 points in it
194         if(bline.size()<2)
195         {
196                 needs_sync_=false;
197                 return;
198         }
199
200         std::vector<synfig::BLinePoint>::const_iterator iter,next;
201
202         etl::hermite<Vector> curve;
203
204         Real step(abs(this->step));
205
206         int seg(0);
207
208         next=bline.begin();
209
210         if(bline_loop)  iter=--bline.end(); // iter is the last  bline in the list; next is the first  bline in the list
211         else                    iter=next++;            // iter is the first bline in the list; next is the second bline in the list
212
213         // loop through the bline; seg counts the blines as we do so; stop before iter is the last bline in the list
214         for(;next!=bline.end();iter=next++,seg++)
215         {
216                 float iterw=iter->get_width();  // the width value of the iter vertex
217                 float nextw=next->get_width();  // the width value of the next vertex
218                 float width;                                    // the width at an intermediate position
219                 curve.p1()=iter->get_vertex();
220                 curve.t1()=iter->get_tangent2();
221                 curve.p2()=next->get_vertex();
222                 curve.t2()=next->get_tangent1();
223                 curve.sync();
224                 etl::derivative<etl::hermite<Vector> > deriv(curve);
225
226                 Real f;
227
228                 int i=0, branch_count = 0, steps = round_to_int(1.0/step);
229                 if (steps < 1) steps = 1;
230                 for(f=0.0;f<1.0;f+=step,i++)
231                 {
232                         Point point(curve(f));
233
234                         particle_list.push_back(Particle(point, gradient(0)));
235                         if (particle_list.size() % 1000000 == 0)
236                                 synfig::info("constructed %d million particles...", particle_list.size()/1000000);
237
238                         bounding_rect.expand(point);
239
240                         Real stunt_growth(random_factor * (random(Random::SMOOTH_COSINE,i,f+seg,0.0f,0.0f)/2.0+0.5));
241                         stunt_growth*=stunt_growth;
242
243                         if((((i+1)*sprouts + steps/2) / steps) > branch_count) {
244                                 Vector branch_velocity(deriv(f).norm()*velocity + deriv(f).perp().norm()*perp_velocity);
245
246                                 if (isnan(branch_velocity[0]) || isnan(branch_velocity[1]))
247                                         continue;
248
249                                 branch_velocity[0] += random_factor * random(Random::SMOOTH_COSINE, 1, f*splits, 0.0f, 0.0f);
250                                 branch_velocity[1] += random_factor * random(Random::SMOOTH_COSINE, 2, f*splits, 0.0f, 0.0f);
251
252                                 if (use_width)
253                                 {
254                                         width = iterw+(nextw-iterw)*f; // calculate the width based on the current position
255
256                                         branch_velocity[0] *= width; // scale the velocity accordingly to the current width
257                                         branch_velocity[1] *= width;
258                                 }
259
260                                 branch_count++;
261                                 branch(i, 0, 0,          // time
262                                            stunt_growth, // stunt growth
263                                            point, branch_velocity);
264                         }
265                 }
266         }
267
268         time_t end_time; time(&end_time);
269         if (end_time-start_time > 4)
270                 synfig::info("Plant::sync() constructed %d particles in %d seconds\n",
271                                          particle_list.size(), int(end_time-start_time));
272         needs_sync_=false;
273 }
274
275 bool
276 Plant::set_param(const String & param, const ValueBase &value)
277 {
278         if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
279         {
280                 bline=value;
281                 bline_loop=value.get_loop();
282                 needs_sync_=true;
283                 set_param_static(param, value.get_static());
284                 return true;
285         }
286         if(param=="seed" && value.same_type_as(int()))
287         {
288                 random.set_seed(value.get(int()));
289                 needs_sync_=true;
290                 set_param_static(param, value.get_static());
291                 return true;
292         }
293         IMPORT(origin);
294         IMPORT_PLUS(split_angle,needs_sync_=true);
295         IMPORT_PLUS(gravity,needs_sync_=true);
296         IMPORT_PLUS(gradient,needs_sync_=true);
297         IMPORT_PLUS(velocity,needs_sync_=true);
298         IMPORT_PLUS(perp_velocity,needs_sync_=true);
299         IMPORT_PLUS(step,{
300                         needs_sync_ = true;
301                         if (step <= 0)
302                                 step=0.01; // user is probably clueless - give a good default
303                         else if (step < 0.00001)
304                                 step=0.00001; // 100K should be enough for anyone
305                         else if (step > 1)
306                                 step=1;
307                 });
308         IMPORT_PLUS(splits,{
309                         needs_sync_=true;
310                         if (splits < 1)
311                                 splits = 1;
312                 });
313         IMPORT_PLUS(sprouts,needs_sync_=true);
314         IMPORT_PLUS(random_factor,needs_sync_=true);
315         IMPORT_PLUS(drag,needs_sync_=true);
316         IMPORT(size);
317         IMPORT(size_as_alpha);
318         IMPORT(reverse);
319         IMPORT(use_width);
320
321         IMPORT_AS(origin,"offset");
322
323         return Layer_Composite::set_param(param,value);
324 }
325 /*
326 void
327 Plant::set_time(Context context, Time time)const
328 {
329         if(needs_sync==true)
330         {
331                 sync();
332                 needs_sync_=false;
333         }
334         //const_cast<Plant*>(this)->sync();
335         context.set_time(time);
336 }
337
338 void
339 Plant::set_time(Context context, Time time, Vector pos)const
340 {
341         if(needs_sync==true)
342         {
343                 sync();
344                 needs_sync_=false;
345         }
346         //const_cast<Plant*>(this)->sync();
347         context.set_time(time,pos);
348 }
349 */
350 ValueBase
351 Plant::get_param(const String& param)const
352 {
353         if(param=="seed")
354         {
355                 ValueBase ret(random.get_seed());
356                 ret.set_static(get_param_static(param));
357                 return ret;
358         }
359         EXPORT(bline);
360         EXPORT(origin);
361         EXPORT(split_angle);
362         EXPORT(gravity);
363         EXPORT(velocity);
364         EXPORT(perp_velocity);
365         EXPORT(step);
366         EXPORT(gradient);
367         EXPORT(splits);
368         EXPORT(sprouts);
369         EXPORT(random_factor);
370         EXPORT(drag);
371         EXPORT(size);
372         EXPORT(size_as_alpha);
373         EXPORT(reverse);
374         EXPORT(use_width);
375
376         EXPORT_NAME();
377
378         if(param=="Version" || param=="version" || param=="version__")
379                 return version;
380
381         return Layer_Composite::get_param(param);
382 }
383
384 Layer::Vocab
385 Plant::get_param_vocab()const
386 {
387         Layer::Vocab ret(Layer_Composite::get_param_vocab());
388
389         ret.push_back(ParamDesc("bline")
390                 .set_local_name(_("Vertices"))
391                 .set_description(_("A list of BLine Points"))
392                 .set_origin("origin")
393                 .set_hint("width")
394         );
395
396         ret.push_back(ParamDesc("origin")
397                 .set_local_name(_("Origin"))
398                 .set_description(_("Offset for the Vertices List"))
399         );
400
401         ret.push_back(ParamDesc("gradient")
402                 .set_local_name(_("Gradient"))
403                 .set_description(_("Gradient to be used for coloring the plant"))
404         );
405
406         ret.push_back(ParamDesc("split_angle")
407                 .set_local_name(_("Split Angle"))
408                 .set_description(_("Angle by which each split deviates from its parent"))
409         );
410
411         ret.push_back(ParamDesc("gravity")
412                 .set_local_name(_("Gravity"))
413                 .set_description(_("Direction in which the shoots tend to face"))
414                 .set_is_distance()
415         );
416
417         ret.push_back(ParamDesc("velocity")
418                 .set_local_name(_("Tangential Velocity"))
419                 .set_description(_("Amount to which shoots tend to grow along the tangent to the BLine"))
420         );
421
422         ret.push_back(ParamDesc("perp_velocity")
423                 .set_local_name(_("Perpendicular Velocity"))
424                 .set_description(_("Amount to which shoots tend to grow perpendicular to the tangent to the BLine"))
425         );
426
427         ret.push_back(ParamDesc("size")
428                 .set_local_name(_("Stem Size"))
429                 .set_description(_("Size of the stem"))
430                 .set_is_distance()
431         );
432
433         ret.push_back(ParamDesc("size_as_alpha")
434                 .set_local_name(_("Size As Alpha"))
435                 .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"))
436         );
437
438         ret.push_back(ParamDesc("reverse")
439                 .set_local_name(_("Reverse"))
440                 .set_description(_("If enabled, render the plant in the opposite direction"))
441         );
442
443         ret.push_back(ParamDesc("step")
444                 .set_local_name(_("Step"))
445                 .set_description(_("Measure of the distance between points when rendering"))
446         );
447
448         ret.push_back(ParamDesc("seed")
449                 .set_local_name(_("Seed"))
450                 .set_description(_("Used to seed the pseudo-random number generator"))
451         );
452
453         ret.push_back(ParamDesc("splits")
454                 .set_local_name(_("Splits"))
455                 .set_description(_("Maximum number of times that each sprout can sprout recursively"))
456         );
457
458         ret.push_back(ParamDesc("sprouts")
459                 .set_local_name(_("Sprouts"))
460                 .set_description(_("Number of places that growth occurs on each bline section"))
461         );
462
463         ret.push_back(ParamDesc("random_factor")
464                 .set_local_name(_("Random Factor"))
465                 .set_description(_("Used to scale down all random effects.  Set to zero to disable randomness"))
466         );
467
468         ret.push_back(ParamDesc("drag")
469                 .set_local_name(_("Drag"))
470                 .set_description(_("Drag slows the growth"))
471         );
472
473         ret.push_back(ParamDesc("use_width")
474                 .set_local_name(_("Use Width"))
475                 .set_description(_("Scale the velocity by the bline's width"))
476         );
477
478         return ret;
479 }
480
481 bool
482 Plant::set_version(const String &ver)
483 {
484         version = ver;
485
486         if (version == "0.1")
487                 use_width = false;
488
489         return true;
490 }
491
492 bool
493 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
494 {
495         bool ret(context.accelerated_render(surface,quality,renddesc,cb));
496         if(is_disabled() || !ret)
497                 return ret;
498
499         Surface dest_surface;
500         dest_surface.set_wh(surface->get_w(),surface->get_h());
501         dest_surface.clear();
502
503         const Point     tl(renddesc.get_tl()-origin);
504         const Point br(renddesc.get_br()-origin);
505
506         const int       w(renddesc.get_w());
507         const int       h(renddesc.get_h());
508
509         const int       surface_width(surface->get_w());
510         const int       surface_height(surface->get_h());
511
512         // Width and Height of a pixel
513         const Real pw = (br[0] - tl[0]) / w;
514         const Real ph = (br[1] - tl[1]) / h;
515
516         if (isinf(pw) || isinf(ph))
517                 return true;
518
519         if(needs_sync_==true)
520                 sync();
521
522         if (particle_list.begin() != particle_list.end())
523         {
524                 std::vector<Particle>::iterator iter;
525                 Particle *particle;
526
527                 float radius(size*sqrt(1.0f/(abs(pw)*abs(ph))));
528
529                 int x1,y1,x2,y2;
530
531                 if (reverse)    iter = particle_list.end();
532                 else                    iter = particle_list.begin();
533
534                 while (true)
535                 {
536                         if (reverse)    particle = &(*(iter-1));
537                         else                    particle = &(*iter);
538
539                         float scaled_radius(radius);
540                         Color color(particle->color);
541                         if(size_as_alpha)
542                         {
543                                 scaled_radius*=color.get_a();
544                                 color.set_a(1);
545                         }
546
547                         // previously, radius was multiplied by sqrt(step)*12 only if
548                         // the radius came out at less than 1 (pixel):
549                         //   if (radius<=1.0f) radius*=sqrt(step)*12.0f;
550                         // seems a little arbitrary - does it help?
551
552                         // calculate the box that this particle will be drawn as
553                         float x1f=(particle->point[0]-tl[0])/pw-(scaled_radius*0.5);
554                         float x2f=(particle->point[0]-tl[0])/pw+(scaled_radius*0.5);
555                         float y1f=(particle->point[1]-tl[1])/ph-(scaled_radius*0.5);
556                         float y2f=(particle->point[1]-tl[1])/ph+(scaled_radius*0.5);
557                         x1=ceil_to_int(x1f);
558                         x2=ceil_to_int(x2f)-1;
559                         y1=ceil_to_int(y1f);
560                         y2=ceil_to_int(y2f)-1;
561
562                         // if the box isn't entirely off the canvas, draw it
563                         if(x1<=surface_width && y1<=surface_height && x2>=0 && y2>=0)
564                         {
565                                 float x1e=x1-x1f, x2e=x2f-x2, y1e=y1-y1f, y2e=y2f-y2;
566                                 // printf("x1e %.4f x2e %.4f y1e %.4f y2e %.4f\n", x1e, x2e, y1e, y2e);
567
568                                 // adjust the box so it's entirely on the canvas
569                                 if(x1<=0) { x1=0; x1e=0; }
570                                 if(y1<=0) { y1=0; y1e=0; }
571                                 if(x2>=surface_width)  { x2=surface_width;  x2e=0; }
572                                 if(y2>=surface_height) { y2=surface_height; y2e=0; }
573
574                                 int w(x2-x1), h(y2-y1);
575
576                                 Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
577                                 if(w>0 && h>0)
578                                         dest_surface.fill(color,surface_pen,w,h);
579
580                                 /* the rectangle doesn't cross any vertical pixel boundaries so we don't
581                                  * need to draw any top or bottom edges
582                                  */
583                                 if(x2<x1)
584                                 {
585                                         // case 1 - a single pixel
586                                         if(y2<y1)
587                                         {
588                                                 surface_pen.move_to(x2,y2);
589                                                 surface_pen.set_alpha((x2f-x1f)*(y2f-y1f));
590                                                 surface_pen.put_value(color);
591                                         }
592                                         // case 2 - a single vertical column of pixels
593                                         else
594                                         {
595                                                 surface_pen.move_to(x2,y1-1);
596                                                 if (y1e!=0)     // maybe draw top pixel
597                                                 {
598                                                         surface_pen.set_alpha(y1e*(x2f-x1f));
599                                                         surface_pen.put_value(color);
600                                                 }
601                                                 surface_pen.inc_y();
602                                                 surface_pen.set_alpha(x2f-x1f);
603                                                 for(int i=y1; i<y2; i++) // maybe draw pixels between
604                                                 {
605                                                         surface_pen.put_value(color);
606                                                         surface_pen.inc_y();
607                                                 }
608                                                 if (y2e!=0)     // maybe draw bottom pixel
609                                                 {
610                                                         surface_pen.set_alpha(y2e*(x2f-x1f));
611                                                         surface_pen.put_value(color);
612                                                 }
613                                         }
614                                 }
615                                 else
616                                 {
617                                         // case 3 - a single horizontal row of pixels
618                                         if(y2<y1)
619                                         {
620                                                 surface_pen.move_to(x1-1,y2);
621                                                 if (x1e!=0)     // maybe draw left pixel
622                                                 {
623                                                         surface_pen.set_alpha(x1e*(y2f-y1f));
624                                                         surface_pen.put_value(color);
625                                                 }
626                                                 surface_pen.inc_x();
627                                                 surface_pen.set_alpha(y2f-y1f);
628                                                 for(int i=x1; i<x2; i++) // maybe draw pixels between
629                                                 {
630                                                         surface_pen.put_value(color);
631                                                         surface_pen.inc_x();
632                                                 }
633                                                 if (x2e!=0)     // maybe draw right pixel
634                                                 {
635                                                         surface_pen.set_alpha(x2e*(y2f-y1f));
636                                                         surface_pen.put_value(color);
637                                                 }
638                                         }
639                                         // case 4 - a proper block of pixels
640                                         else
641                                         {
642                                                 if (x1e!=0)     // maybe draw left edge
643                                                 {
644                                                         surface_pen.move_to(x1-1,y1-1);
645                                                         if (y1e!=0)     // maybe draw top left pixel
646                                                         {
647                                                                 surface_pen.set_alpha(x1e*y1e);
648                                                                 surface_pen.put_value(color);
649                                                         }
650                                                         surface_pen.inc_y();
651                                                         surface_pen.set_alpha(x1e);
652                                                         for(int i=y1; i<y2; i++) // maybe draw pixels along the left edge
653                                                         {
654                                                                 surface_pen.put_value(color);
655                                                                 surface_pen.inc_y();
656                                                         }
657                                                         if (y2e!=0)     // maybe draw bottom left pixel
658                                                         {
659                                                                 surface_pen.set_alpha(x1e*y2e);
660                                                                 surface_pen.put_value(color);
661                                                         }
662                                                         surface_pen.inc_x();
663                                                 }
664                                                 else
665                                                         surface_pen.move_to(x1,y2);
666
667                                                 if (y2e!=0)     // maybe draw bottom edge
668                                                 {
669                                                         surface_pen.set_alpha(y2e);
670                                                         for(int i=x1; i<x2; i++) // maybe draw pixels along the bottom edge
671                                                         {
672                                                                 surface_pen.put_value(color);
673                                                                 surface_pen.inc_x();
674                                                         }
675                                                         if (x2e!=0)     // maybe draw bottom right pixel
676                                                         {
677                                                                 surface_pen.set_alpha(x2e*y2e);
678                                                                 surface_pen.put_value(color);
679                                                         }
680                                                         surface_pen.dec_y();
681                                                 }
682                                                 else
683                                                         surface_pen.move_to(x2,y2-1);
684
685                                                 if (x2e!=0)     // maybe draw right edge
686                                                 {
687                                                         surface_pen.set_alpha(x2e);
688                                                         for(int i=y1; i<y2; i++) // maybe draw pixels along the right edge
689                                                         {
690                                                                 surface_pen.put_value(color);
691                                                                 surface_pen.dec_y();
692                                                         }
693                                                         if (y1e!=0)     // maybe draw top right pixel
694                                                         {
695                                                                 surface_pen.set_alpha(x2e*y1e);
696                                                                 surface_pen.put_value(color);
697                                                         }
698                                                         surface_pen.dec_x();
699                                                 }
700                                                 else
701                                                         surface_pen.move_to(x2-1,y1-1);
702
703                                                 if (y1e!=0)     // maybe draw top edge
704                                                 {
705                                                         surface_pen.set_alpha(y1e);
706                                                         for(int i=x1; i<x2; i++) // maybe draw pixels along the top edge
707                                                         {
708                                                                 surface_pen.put_value(color);
709                                                                 surface_pen.dec_x();
710                                                         }
711                                                 }
712                                         }
713                                 }
714                         }
715
716                         if (reverse)
717                         {
718                                 if (--iter == particle_list.begin())
719                                         break;
720                         }
721                         else
722                         {
723                                 if (++iter == particle_list.end())
724                                         break;
725                         }
726                 }
727         }
728
729         Surface::alpha_pen pen(surface->get_pen(0,0),get_amount(),get_blend_method());
730         dest_surface.blit_to(pen);
731
732         return true;
733 }
734
735 Rect
736 Plant::get_bounding_rect(Context context)const
737 {
738         if(needs_sync_==true)
739                 sync();
740
741         if(is_disabled())
742                 return Rect::zero();
743
744         if(Color::is_onto(get_blend_method()))
745                 return context.get_full_bounding_rect() & bounding_rect;
746
747         //if(get_blend_method()==Color::BLEND_BEHIND)
748         //      return context.get_full_bounding_rect() | bounding_rect;
749         return bounding_rect;
750 }