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