Add parameter static option for rest of layers.
[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         );
399
400         ret.push_back(ParamDesc("gradient")
401                 .set_local_name(_("Gradient"))
402                 .set_description(_("Gradient to be used for coloring the plant"))
403         );
404
405         ret.push_back(ParamDesc("split_angle")
406                 .set_local_name(_("Split Angle"))
407                 .set_description(_("Angle by which each split deviates from its parent"))
408         );
409
410         ret.push_back(ParamDesc("gravity")
411                 .set_local_name(_("Gravity"))
412                 .set_description(_("Direction in which the shoots tend to face"))
413                 .set_is_distance()
414         );
415
416         ret.push_back(ParamDesc("velocity")
417                 .set_local_name(_("Tangential Velocity"))
418                 .set_description(_("Amount to which shoots tend to grow along the tangent to the BLine"))
419         );
420
421         ret.push_back(ParamDesc("perp_velocity")
422                 .set_local_name(_("Perpendicular Velocity"))
423                 .set_description(_("Amount to which shoots tend to grow perpendicular to the tangent to the BLine"))
424         );
425
426         ret.push_back(ParamDesc("size")
427                 .set_local_name(_("Stem Size"))
428                 .set_description(_("Size of the stem"))
429                 .set_is_distance()
430         );
431
432         ret.push_back(ParamDesc("size_as_alpha")
433                 .set_local_name(_("Size As Alpha"))
434                 .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"))
435         );
436
437         ret.push_back(ParamDesc("reverse")
438                 .set_local_name(_("Reverse"))
439                 .set_description(_("If enabled, render the plant in the opposite direction"))
440         );
441
442         ret.push_back(ParamDesc("step")
443                 .set_local_name(_("Step"))
444                 .set_description(_("Measure of the distance between points when rendering"))
445         );
446
447         ret.push_back(ParamDesc("seed")
448                 .set_local_name(_("Seed"))
449                 .set_description(_("Used to seed the pseudo-random number generator"))
450         );
451
452         ret.push_back(ParamDesc("splits")
453                 .set_local_name(_("Splits"))
454                 .set_description(_("Maximum number of times that each sprout can sprout recursively"))
455         );
456
457         ret.push_back(ParamDesc("sprouts")
458                 .set_local_name(_("Sprouts"))
459                 .set_description(_("Number of places that growth occurs on each bline section"))
460         );
461
462         ret.push_back(ParamDesc("random_factor")
463                 .set_local_name(_("Random Factor"))
464                 .set_description(_("Used to scale down all random effects.  Set to zero to disable randomness"))
465         );
466
467         ret.push_back(ParamDesc("drag")
468                 .set_local_name(_("Drag"))
469                 .set_description(_("Drag slows the growth"))
470         );
471
472         ret.push_back(ParamDesc("use_width")
473                 .set_local_name(_("Use Width"))
474                 .set_description(_("Scale the velocity by the bline's width"))
475         );
476
477         return ret;
478 }
479
480 bool
481 Plant::set_version(const String &ver)
482 {
483         version = ver;
484
485         if (version == "0.1")
486                 use_width = false;
487
488         return true;
489 }
490
491 bool
492 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
493 {
494         bool ret(context.accelerated_render(surface,quality,renddesc,cb));
495         if(is_disabled() || !ret)
496                 return ret;
497
498         Surface dest_surface;
499         dest_surface.set_wh(surface->get_w(),surface->get_h());
500         dest_surface.clear();
501
502         const Point     tl(renddesc.get_tl()-origin);
503         const Point br(renddesc.get_br()-origin);
504
505         const int       w(renddesc.get_w());
506         const int       h(renddesc.get_h());
507
508         const int       surface_width(surface->get_w());
509         const int       surface_height(surface->get_h());
510
511         // Width and Height of a pixel
512         const Real pw = (br[0] - tl[0]) / w;
513         const Real ph = (br[1] - tl[1]) / h;
514
515         if (isinf(pw) || isinf(ph))
516                 return true;
517
518         if(needs_sync_==true)
519                 sync();
520
521         if (particle_list.begin() != particle_list.end())
522         {
523                 std::vector<Particle>::iterator iter;
524                 Particle *particle;
525
526                 float radius(size*sqrt(1.0f/(abs(pw)*abs(ph))));
527
528                 int x1,y1,x2,y2;
529
530                 if (reverse)    iter = particle_list.end();
531                 else                    iter = particle_list.begin();
532
533                 while (true)
534                 {
535                         if (reverse)    particle = &(*(iter-1));
536                         else                    particle = &(*iter);
537
538                         float scaled_radius(radius);
539                         Color color(particle->color);
540                         if(size_as_alpha)
541                         {
542                                 scaled_radius*=color.get_a();
543                                 color.set_a(1);
544                         }
545
546                         // previously, radius was multiplied by sqrt(step)*12 only if
547                         // the radius came out at less than 1 (pixel):
548                         //   if (radius<=1.0f) radius*=sqrt(step)*12.0f;
549                         // seems a little arbitrary - does it help?
550
551                         // calculate the box that this particle will be drawn as
552                         float x1f=(particle->point[0]-tl[0])/pw-(scaled_radius*0.5);
553                         float x2f=(particle->point[0]-tl[0])/pw+(scaled_radius*0.5);
554                         float y1f=(particle->point[1]-tl[1])/ph-(scaled_radius*0.5);
555                         float y2f=(particle->point[1]-tl[1])/ph+(scaled_radius*0.5);
556                         x1=ceil_to_int(x1f);
557                         x2=ceil_to_int(x2f)-1;
558                         y1=ceil_to_int(y1f);
559                         y2=ceil_to_int(y2f)-1;
560
561                         // if the box isn't entirely off the canvas, draw it
562                         if(x1<=surface_width && y1<=surface_height && x2>=0 && y2>=0)
563                         {
564                                 float x1e=x1-x1f, x2e=x2f-x2, y1e=y1-y1f, y2e=y2f-y2;
565                                 // printf("x1e %.4f x2e %.4f y1e %.4f y2e %.4f\n", x1e, x2e, y1e, y2e);
566
567                                 // adjust the box so it's entirely on the canvas
568                                 if(x1<=0) { x1=0; x1e=0; }
569                                 if(y1<=0) { y1=0; y1e=0; }
570                                 if(x2>=surface_width)  { x2=surface_width;  x2e=0; }
571                                 if(y2>=surface_height) { y2=surface_height; y2e=0; }
572
573                                 int w(x2-x1), h(y2-y1);
574
575                                 Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
576                                 if(w>0 && h>0)
577                                         dest_surface.fill(color,surface_pen,w,h);
578
579                                 /* the rectangle doesn't cross any vertical pixel boundaries so we don't
580                                  * need to draw any top or bottom edges
581                                  */
582                                 if(x2<x1)
583                                 {
584                                         // case 1 - a single pixel
585                                         if(y2<y1)
586                                         {
587                                                 surface_pen.move_to(x2,y2);
588                                                 surface_pen.set_alpha((x2f-x1f)*(y2f-y1f));
589                                                 surface_pen.put_value(color);
590                                         }
591                                         // case 2 - a single vertical column of pixels
592                                         else
593                                         {
594                                                 surface_pen.move_to(x2,y1-1);
595                                                 if (y1e!=0)     // maybe draw top pixel
596                                                 {
597                                                         surface_pen.set_alpha(y1e*(x2f-x1f));
598                                                         surface_pen.put_value(color);
599                                                 }
600                                                 surface_pen.inc_y();
601                                                 surface_pen.set_alpha(x2f-x1f);
602                                                 for(int i=y1; i<y2; i++) // maybe draw pixels between
603                                                 {
604                                                         surface_pen.put_value(color);
605                                                         surface_pen.inc_y();
606                                                 }
607                                                 if (y2e!=0)     // maybe draw bottom pixel
608                                                 {
609                                                         surface_pen.set_alpha(y2e*(x2f-x1f));
610                                                         surface_pen.put_value(color);
611                                                 }
612                                         }
613                                 }
614                                 else
615                                 {
616                                         // case 3 - a single horizontal row of pixels
617                                         if(y2<y1)
618                                         {
619                                                 surface_pen.move_to(x1-1,y2);
620                                                 if (x1e!=0)     // maybe draw left pixel
621                                                 {
622                                                         surface_pen.set_alpha(x1e*(y2f-y1f));
623                                                         surface_pen.put_value(color);
624                                                 }
625                                                 surface_pen.inc_x();
626                                                 surface_pen.set_alpha(y2f-y1f);
627                                                 for(int i=x1; i<x2; i++) // maybe draw pixels between
628                                                 {
629                                                         surface_pen.put_value(color);
630                                                         surface_pen.inc_x();
631                                                 }
632                                                 if (x2e!=0)     // maybe draw right pixel
633                                                 {
634                                                         surface_pen.set_alpha(x2e*(y2f-y1f));
635                                                         surface_pen.put_value(color);
636                                                 }
637                                         }
638                                         // case 4 - a proper block of pixels
639                                         else
640                                         {
641                                                 if (x1e!=0)     // maybe draw left edge
642                                                 {
643                                                         surface_pen.move_to(x1-1,y1-1);
644                                                         if (y1e!=0)     // maybe draw top left pixel
645                                                         {
646                                                                 surface_pen.set_alpha(x1e*y1e);
647                                                                 surface_pen.put_value(color);
648                                                         }
649                                                         surface_pen.inc_y();
650                                                         surface_pen.set_alpha(x1e);
651                                                         for(int i=y1; i<y2; i++) // maybe draw pixels along the left edge
652                                                         {
653                                                                 surface_pen.put_value(color);
654                                                                 surface_pen.inc_y();
655                                                         }
656                                                         if (y2e!=0)     // maybe draw bottom left pixel
657                                                         {
658                                                                 surface_pen.set_alpha(x1e*y2e);
659                                                                 surface_pen.put_value(color);
660                                                         }
661                                                         surface_pen.inc_x();
662                                                 }
663                                                 else
664                                                         surface_pen.move_to(x1,y2);
665
666                                                 if (y2e!=0)     // maybe draw bottom edge
667                                                 {
668                                                         surface_pen.set_alpha(y2e);
669                                                         for(int i=x1; i<x2; i++) // maybe draw pixels along the bottom edge
670                                                         {
671                                                                 surface_pen.put_value(color);
672                                                                 surface_pen.inc_x();
673                                                         }
674                                                         if (x2e!=0)     // maybe draw bottom right pixel
675                                                         {
676                                                                 surface_pen.set_alpha(x2e*y2e);
677                                                                 surface_pen.put_value(color);
678                                                         }
679                                                         surface_pen.dec_y();
680                                                 }
681                                                 else
682                                                         surface_pen.move_to(x2,y2-1);
683
684                                                 if (x2e!=0)     // maybe draw right edge
685                                                 {
686                                                         surface_pen.set_alpha(x2e);
687                                                         for(int i=y1; i<y2; i++) // maybe draw pixels along the right edge
688                                                         {
689                                                                 surface_pen.put_value(color);
690                                                                 surface_pen.dec_y();
691                                                         }
692                                                         if (y1e!=0)     // maybe draw top right pixel
693                                                         {
694                                                                 surface_pen.set_alpha(x2e*y1e);
695                                                                 surface_pen.put_value(color);
696                                                         }
697                                                         surface_pen.dec_x();
698                                                 }
699                                                 else
700                                                         surface_pen.move_to(x2-1,y1-1);
701
702                                                 if (y1e!=0)     // maybe draw top edge
703                                                 {
704                                                         surface_pen.set_alpha(y1e);
705                                                         for(int i=x1; i<x2; i++) // maybe draw pixels along the top edge
706                                                         {
707                                                                 surface_pen.put_value(color);
708                                                                 surface_pen.dec_x();
709                                                         }
710                                                 }
711                                         }
712                                 }
713                         }
714
715                         if (reverse)
716                         {
717                                 if (--iter == particle_list.begin())
718                                         break;
719                         }
720                         else
721                         {
722                                 if (++iter == particle_list.end())
723                                         break;
724                         }
725                 }
726         }
727
728         Surface::alpha_pen pen(surface->get_pen(0,0),get_amount(),get_blend_method());
729         dest_surface.blit_to(pen);
730
731         return true;
732 }
733
734 Rect
735 Plant::get_bounding_rect(Context context)const
736 {
737         if(needs_sync_==true)
738                 sync();
739
740         if(is_disabled())
741                 return Rect::zero();
742
743         if(Color::is_onto(get_blend_method()))
744                 return context.get_full_bounding_rect() & bounding_rect;
745
746         //if(get_blend_method()==Color::BLEND_BEHIND)
747         //      return context.get_full_bounding_rect() | bounding_rect;
748         return bounding_rect;
749 }