2
// AggPas 2.4 RM3 Demo application
3
// Note: Press F1 key on run to see more info about this demo
5
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
14
agg_platform_support ,
25
agg_rendering_buffer ,
27
agg_renderer_scanline ,
28
agg_renderer_primitives ,
29
agg_rasterizer_scanline_aa ,
34
agg_scanline_storage_aa ,
35
agg_scanline_storage_bin ,
36
agg_scanline_boolean_algebra ,
37
agg_render_scanlines ,
58
spiral = object(vertex_source )
73
constructor Construct(x ,y ,r1 ,r2 ,step : double; start_angle : double = 0 );
75
procedure rewind(path_id : unsigned ); virtual;
76
function vertex(x ,y : double_ptr ) : unsigned; virtual;
80
the_application = object(platform_support )
84
m_operation : rbox_ctrl;
89
constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
92
procedure render_scanline_boolean(ras1 ,ras2 : rasterizer_scanline_ptr );
93
function render_sbool (ras1 ,ras2 : rasterizer_scanline_ptr ) : unsigned;
95
procedure on_init; virtual;
96
procedure on_draw; virtual;
98
procedure on_mouse_move (x ,y : int; flags : unsigned ); virtual;
99
procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
101
procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
106
constructor spiral.Construct;
114
m_start_angle:=start_angle;
115
m_angle :=start_angle;
123
procedure spiral.rewind;
125
m_angle :=m_start_angle;
132
function spiral.vertex;
134
if m_curr_r > m_r2 then
136
result:=path_cmd_stop;
142
x^:=m_x + Cos(m_angle ) * m_curr_r;
143
y^:=m_y + Sin(m_angle ) * m_curr_r;
145
m_curr_r:=m_curr_r + m_dr;
146
m_angle :=m_angle + m_da;
152
result:=path_cmd_move_to;
156
result:=path_cmd_line_to;
161
function count_spans(ras : rasterizer_scanline_ptr; sl : scanline_ptr ) : unsigned;
168
if ras.rewind_scanlines then
170
sl.reset(ras._min_x ,ras._max_x );
172
if sl.is_embedded then
173
while ras.sweep_scanline_em(sl ) do
174
inc(n ,sl.num_spans )
176
while ras.sweep_scanline(sl ) do
177
inc(n ,sl.num_spans );
186
constructor the_application.Construct;
188
inherited Construct(format_ ,flip_y_ );
190
m_polygons.Construct (5.0 ,5.0 ,5.0 + 205.0 ,110.0 ,not flip_y_ );
191
m_fill_rule.Construct (200 ,5.0 ,200 + 105.0 ,50.0 ,not flip_y_ );
192
m_scanline_type.Construct(300 ,5.0 ,300 + 115.0 ,70.0 ,not flip_y_ );
193
m_operation.Construct (535.0 ,5.0 ,535.0 + 115.0 ,145.0 ,not flip_y_ );
195
m_operation.add_item ('None' );
196
m_operation.add_item ('OR' );
197
m_operation.add_item ('AND' );
198
m_operation.add_item ('XOR Linear' );
199
m_operation.add_item ('XOR Saddle' );
200
m_operation.add_item ('A-B' );
201
m_operation.add_item ('B-A' );
202
m_operation.cur_item_(2 );
204
add_ctrl(@m_operation );
206
m_operation.no_transform;
208
m_fill_rule.add_item ('Even-Odd' );
209
m_fill_rule.add_item ('Non Zero' );
210
m_fill_rule.cur_item_(1 );
212
add_ctrl(@m_fill_rule );
214
m_fill_rule.no_transform;
216
m_scanline_type.add_item ('scanline_p' );
217
m_scanline_type.add_item ('scanline_u' );
218
m_scanline_type.add_item ('scanline_bin' );
219
m_scanline_type.cur_item_(1 );
221
add_ctrl(@m_scanline_type );
223
m_scanline_type.no_transform;
225
m_polygons.add_item ('Two Simple Paths' );
226
m_polygons.add_item ('Closed Stroke' );
227
m_polygons.add_item ('Great Britain and Arrows' );
228
m_polygons.add_item ('Great Britain and Spiral' );
229
m_polygons.add_item ('Spiral and Glyph' );
230
m_polygons.cur_item_(3 );
232
add_ctrl(@m_polygons );
234
m_polygons.no_transform;
239
destructor the_application.Destruct;
244
m_fill_rule.Destruct;
245
m_scanline_type.Destruct;
246
m_operation.Destruct;
250
{ RENDER_SCANLINE_BOOLEAN }
251
procedure the_application.render_scanline_boolean;
256
buf : array[0..99 ] of char;
258
pixf : pixel_formats;
265
num_spans : unsigned;
269
rent : renderer_scanline_aa_solid;
270
renb : renderer_scanline_bin_solid;
284
txt_stroke : conv_stroke;
288
storage2 : scanline_storage_aa8;
292
storageb2 : scanline_storage_bin;
295
if m_operation._cur_item > 0 then
297
case m_operation._cur_item of
301
4 : op:=sbool_xor_saddle;
302
5 : op:=sbool_a_minus_b;
303
6 : op:=sbool_b_minus_a;
307
pixfmt_bgr24(pixf ,rbuf_window );
308
rb.Construct(@pixf );
316
case m_scanline_type._cur_item of
319
renp.Construct(@rb );
324
// The intermediate storage is used only to test the perfoprmance,
325
// the short variant can be as follows:
326
// ------------------------
327
// rgba.ConstrDbl (0.5 ,0.0 ,0 ,0.5 );
328
// renp.color (@rgba );
329
// sbool_combine_shapes_aa(op ,ras1 ,ras2 ,@slp1 ,@slp2 ,@slp ,@renp );
335
render_scanlines(ras1 ,@slp ,@storage1 );
336
render_scanlines(ras2 ,@slp ,@storage2 );
341
sbool_combine_shapes_aa(op ,@storage1 ,@storage2 ,@slp1 ,@slp2 ,@slp ,@storage );
343
t1:=elapsed_time / 10.0;
347
rgba.ConstrDbl (0.5 ,0.0 ,0 ,0.5 );
348
renp.color_ (@rgba );
349
render_scanlines(@storage ,@slp ,@renp );
353
num_spans:=count_spans(@storage ,@slp );
367
renu.Construct(@rb );
373
{rgba.ConstrDbl (0.5 ,0.0 ,0 ,0.5 );
375
sbool_combine_shapes_aa(op ,ras1 ,ras2 ,@slu1 ,@slu2 ,@slu ,@renu );}
381
render_scanlines(ras1 ,@slu ,@storage1 );
382
render_scanlines(ras2 ,@slu ,@storage2 );
387
sbool_combine_shapes_aa(op ,@storage1 ,@storage2 ,@slu1 ,@slu2 ,@slu ,@storage );
389
t1:=elapsed_time / 10.0;
393
rgba.ConstrDbl (0.5 ,0.0 ,0 ,0.5 );
394
renu.color_ (@rgba );
395
render_scanlines(@storage ,@slu ,@renu );
399
num_spans:=count_spans(@storage ,@slu );{}
413
renb.Construct(@rb );
423
render_scanlines(ras1 ,@slb ,@storageb1 );
424
render_scanlines(ras2 ,@slb ,@storageb2 );
429
sbool_combine_shapes_bin(op ,@storageb1 ,@storageb2 ,@slb1 ,@slb2 ,@slb ,@storageb );
431
t1:=elapsed_time / 10.0;
435
rgba.ConstrDbl ({0.5}1 ,0.0 ,0 {,0.5} );
436
renb.color_ (@rgba );
437
render_scanlines(@storageb ,@slb ,@renb );
441
num_spans:=count_spans(@storageb ,@slb );
456
sprintf(@buf[0 ] ,'Combine=%.3fms'#13#13 ,t1 );
457
sprintf(@buf[StrLen(@buf ) ] ,'Render=%.3fms'#13#13 ,t2 );
458
sprintf(@buf[StrLen(@buf ) ] ,'num_spans=%d' ,num_spans );
460
rent.Construct(@rb );
463
txt_stroke.Construct(@txt );
464
txt_stroke.width_ (1.0 );
465
txt_stroke.line_cap_(round_cap );
467
txt.start_point_ (420 ,40 );
468
txt.text_ (@buf[0 ] );
471
ras1.add_path (@txt_stroke );
472
rgba.ConstrDbl (0.0 ,0.0 ,0.0 );
473
rent.color_ (@rgba );
474
render_scanlines(ras1 ,@slt ,@rent );
485
function the_application.render_sbool;
489
ren : renderer_scanline_aa_solid;
497
glyph : path_storage;
505
tat : trans_affine_translation;
506
tas : trans_affine_scaling;
509
stroke_gb_poly : conv_stroke;
513
trans_arrows : conv_transform;
520
pixfmt_bgr24(pf ,rbuf_window );
527
if m_fill_rule._cur_item <> 0 then
528
ras1.filling_rule(fill_non_zero )
530
ras1.filling_rule(fill_even_odd );
532
if m_fill_rule._cur_item <> 0 then
533
ras2.filling_rule(fill_non_zero )
535
ras2.filling_rule(fill_even_odd );
537
case m_polygons._cur_item of
538
0 : // Two simple paths
543
x:=m_x - _initial_width / 2 + 100;
544
y:=m_y - _initial_height / 2 + 100;
546
ps1.move_to(x + 140 ,y + 145 );
547
ps1.line_to(x + 225 ,y + 44 );
548
ps1.line_to(x + 296 ,y + 219 );
551
ps1.line_to(x + 226 ,y + 289 );
552
ps1.line_to(x + 82 ,y + 292 );
554
ps1.move_to(x + 220 ,y + 222 );
555
ps1.line_to(x + 363 ,y + 249 );
556
ps1.line_to(x + 265 ,y + 331 );
558
ps1.move_to(x + 242 ,y + 243 );
559
ps1.line_to(x + 268 ,y + 309 );
560
ps1.line_to(x + 325 ,y + 261 );
562
ps1.move_to(x + 259 ,y + 259 );
563
ps1.line_to(x + 273 ,y + 288 );
564
ps1.line_to(x + 298 ,y + 266 );
566
ps2.move_to(100 + 32 ,100 + 77 );
567
ps2.line_to(100 + 473 ,100 + 263 );
568
ps2.line_to(100 + 351 ,100 + 290 );
569
ps2.line_to(100 + 354 ,100 + 374 );
572
ras1.add_path (@ps1 );
573
rgba.ConstrDbl (0 ,0 ,0 ,{0.1}0.0 );
575
{ render_scanlines(ras1 ,@sl ,@ren ); }
578
ras2.add_path (@ps2 );
579
rgba.ConstrDbl (0 ,0.6 ,0 ,{0.1}0.0 );
581
{ render_scanlines(ras2 ,@sl ,@ren ); }
583
render_scanline_boolean(ras1 ,ras2 );
594
stroke.Construct(@ps2 );
595
stroke.width_ (15.0 );
597
x:=m_x - _initial_width / 2 + 100;
598
y:=m_y - _initial_height / 2 + 100;
600
ps1.move_to(x + 140 ,y + 145 );
601
ps1.line_to(x + 225 ,y + 44 );
602
ps1.line_to(x + 296 ,y + 219 );
605
ps1.line_to(x + 226 ,y + 289 );
606
ps1.line_to(x + 82 ,y + 292 );
608
ps1.move_to(x + 220 - 50 ,y + 222 );
609
ps1.line_to(x + 265 - 50 ,y + 331 );
610
ps1.line_to(x + 363 - 50 ,y + 249 );
611
ps1.close_polygon(path_flags_ccw );
613
ps2.move_to(100 + 32 ,100 + 77 );
614
ps2.line_to(100 + 473 ,100 + 263 );
615
ps2.line_to(100 + 351 ,100 + 290 );
616
ps2.line_to(100 + 354 ,100 + 374 );
620
ras1.add_path (@ps1 );
621
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
623
render_scanlines(ras1 ,@sl ,@ren );
626
ras2.add_path (@stroke );
627
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
629
render_scanlines(ras2 ,@sl ,@ren );
631
render_scanline_boolean(ras1 ,ras2 );
639
2 : // Great Britain and Arrows
644
make_gb_poly(@gb_poly );
645
make_arrows (@arrows );
649
tat.Construct(-1150 ,-1150 );
651
mtx1.multiply(@tat );
652
mtx1.multiply(@tas );
656
tat.Construct(m_x - _initial_width / 2 ,m_y - _initial_height / 2 );
658
mtx2.multiply(@tat );
660
trans_gb_poly.Construct(@gb_poly ,@mtx1 );
661
trans_arrows.Construct (@arrows ,@mtx2 );
663
ras2.add_path (@trans_gb_poly );
664
rgba.ConstrDbl (0.5 ,0.5 ,0 ,0.1 );
666
render_scanlines(ras2 ,@sl ,@ren );
668
stroke_gb_poly.Construct(@trans_gb_poly );
669
stroke_gb_poly.width_ (0.1);
670
ras1.add_path (@stroke_gb_poly );
671
rgba.ConstrDbl (0 ,0 ,0 );
673
render_scanlines (ras1 ,@sl ,@ren );
675
ras2.add_path (@trans_arrows );
676
rgba.ConstrDbl (0.0 ,0.5 ,0.5 ,0.1 );
678
render_scanlines(ras2 ,@sl ,@ren );
681
ras1.add_path(@trans_gb_poly );
683
render_scanline_boolean(ras1 ,ras2 );
687
stroke_gb_poly.Destruct;
691
3 : // Great Britain and a Spiral
693
sp.Construct (m_x ,m_y ,10 ,150 ,30 ,0.0 );
694
stroke.Construct(@sp );
695
stroke.width_ (15.0 );
698
make_gb_poly(@gb_poly );
701
tat.Construct(-1150 ,-1150 );
705
mtx.multiply(_trans_affine_resizing );
707
trans_gb_poly.Construct(@gb_poly ,@mtx );
709
ras1.add_path (@trans_gb_poly );
710
rgba.ConstrDbl (0.5 ,0.5 ,0 ,0.1 );
712
render_scanlines(ras1 ,@sl ,@ren );
714
stroke_gb_poly.Construct(@trans_gb_poly );
715
stroke_gb_poly.width_ (0.1 );
716
ras1.add_path (@stroke_gb_poly );
717
rgba.ConstrDbl (0 ,0 ,0 );
719
render_scanlines (ras1 ,@sl ,@ren );
722
ras2.add_path (@stroke );
723
rgba.ConstrDbl (0.0 ,0.5 ,0.5 ,0.1 );
725
render_scanlines(ras2 ,@sl ,@ren );
728
ras1.add_path(@trans_gb_poly );
730
render_scanline_boolean(ras1 ,ras2 );
734
stroke_gb_poly.Destruct;
738
4 : // Spiral and glyph
740
sp.Construct (m_x ,m_y ,10 ,150 ,30 ,0.0 );
741
stroke.Construct(@sp );
742
stroke.width_ (15.0 );
745
glyph.move_to(28.47 ,6.45 );
746
glyph.curve3 (21.58 ,1.12 ,19.82 ,0.29 );
747
glyph.curve3 (17.19 ,-0.93 ,14.21 ,-0.93 );
748
glyph.curve3 (9.57 ,-0.93 ,6.57 ,2.25 );
749
glyph.curve3 (3.56 ,5.42 ,3.56 ,10.60 );
750
glyph.curve3 (3.56 ,13.87 ,5.03 ,16.26 );
751
glyph.curve3 (7.03 ,19.58 ,11.99 ,22.51 );
752
glyph.curve3 (16.94 ,25.44 ,28.47 ,29.64 );
753
glyph.line_to(28.47 ,31.40 );
754
glyph.curve3 (28.47 ,38.09 ,26.34 ,40.58 );
755
glyph.curve3 (24.22 ,43.07 ,20.17 ,43.07 );
756
glyph.curve3 (17.09 ,43.07 ,15.28 ,41.41 );
757
glyph.curve3 (13.43 ,39.75 ,13.43 ,37.60 );
758
glyph.line_to(13.53 ,34.77 );
759
glyph.curve3 (13.53 ,32.52 ,12.38 ,31.30 );
760
glyph.curve3 (11.23 ,30.08 ,9.38 ,30.08 );
761
glyph.curve3 (7.57 ,30.08 ,6.42 ,31.35 );
762
glyph.curve3 (5.27 ,32.62 ,5.27 ,34.81 );
763
glyph.curve3 (5.27 ,39.01 ,9.57 ,42.53 );
764
glyph.curve3 (13.87 ,46.04 ,21.63 ,46.04 );
765
glyph.curve3 (27.59 ,46.04 ,31.40 ,44.04 );
766
glyph.curve3 (34.28 ,42.53 ,35.64 ,39.31 );
767
glyph.curve3 (36.52 ,37.21 ,36.52 ,30.71 );
768
glyph.line_to(36.52 ,15.53 );
769
glyph.curve3 (36.52 ,9.13 ,36.77 ,7.69 );
770
glyph.curve3 (37.01 ,6.25 ,37.57 ,5.76 );
771
glyph.curve3 (38.13 ,5.27 ,38.87 ,5.27 );
772
glyph.curve3 (39.65 ,5.27 ,40.23 ,5.62 );
773
glyph.curve3 (41.26 ,6.25 ,44.19 ,9.18 );
774
glyph.line_to(44.19 ,6.45 );
775
glyph.curve3 (38.72 ,-0.88 ,33.74 ,-0.88 );
776
glyph.curve3 (31.35 ,-0.88 ,29.93 ,0.78 );
777
glyph.curve3 (28.52 ,2.44 ,28.47 ,6.45 );
780
glyph.move_to(28.47 ,9.62 );
781
glyph.line_to(28.47 ,26.66 );
782
glyph.curve3 (21.09 ,23.73 ,18.95 ,22.51 );
783
glyph.curve3 (15.09 ,20.36 ,13.43 ,18.02 );
784
glyph.curve3 (11.77 ,15.67 ,11.77 ,12.89 );
785
glyph.curve3 (11.77 ,9.38 ,13.87 ,7.06 );
786
glyph.curve3 (15.97 ,4.74 ,18.70 ,4.74 );
787
glyph.curve3 (22.41 ,4.74 ,28.47 ,9.62 );
792
tat.Construct(220 ,200 );
796
trans.Construct(@glyph ,@mtx );
797
curve.Construct(@trans );
800
ras1.add_path (@stroke );
801
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
803
render_scanlines(ras1 ,@sl ,@ren );
806
ras2.add_path (@curve );
807
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
809
render_scanlines(ras2 ,@sl ,@ren );
811
render_scanline_boolean(ras1 ,ras2 );
828
procedure the_application.on_init;
836
procedure the_application.on_draw;
842
ren_base : renderer_base;
843
ren_solid : renderer_scanline_aa_solid;
847
ras2 : rasterizer_scanline_aa;
850
// Initialize structures
851
pixfmt_bgr24(pf ,rbuf_window );
853
ren_base.Construct (@pf );
854
ren_solid.Construct(@ren_base );
856
rgba.ConstrDbl(1 ,1 ,1 );
857
ren_base.clear(@rgba );
863
// Render the controls
864
render_ctrl(@ras ,@sl ,@ren_solid ,@m_polygons );
865
render_ctrl(@ras ,@sl ,@ren_solid ,@m_fill_rule );
866
render_ctrl(@ras ,@sl ,@ren_solid ,@m_scanline_type );
867
render_ctrl(@ras ,@sl ,@ren_solid ,@m_operation );
870
render_sbool(@ras ,@ras2 );
872
// Free AGG resources
880
procedure the_application.on_mouse_move;
882
if flags and mouse_left <> 0 then
893
{ ON_MOUSE_BUTTON_DOWN }
894
procedure the_application.on_mouse_button_down;
896
buf : array[0..99 ] of char;
899
if flags and mouse_left <> 0 then
908
if flags and mouse_right <> 0 then
910
sprintf (@buf[0 ] ,'%d ' ,x );
911
sprintf (@buf[StrLen(@buf ) ] ,'%d' ,y );
919
procedure the_application.on_key;
923
'This is another example of using of the scanline boolean algebra. The example is '#13 +
924
'similar to Demo gpc_test. Note that the cost of the boolean operation with Anti-Aliasing '#13 +
925
'is comparable with rendering (the rasterization time is not included). Also note that '#13 +
926
'there is a difference in timings between using of scanline_u and scanline_p. Most often '#13 +
927
'scanline_u works faster, but it''s because of much less number of produced spans.'#13 +
928
'Actually, when using the scanline_u the complexity of the algorithm becomes proportional '#13 +
929
'to the area of the polygons, while in scanline_p it''s proportional to the perimeter. '#13 +
930
'Of course, the binary variant works much faster than the Anti-Aliased one.' +
931
#13#13'Note: F2 key saves current "screenshot" file in this demo''s directory. ' );
936
app : the_application;
939
app.Construct(pix_format_bgr24 ,flip_y );
940
app.caption_ ('AGG Example. Scanline Boolean (F1-Help)' );
942
if app.init(655 ,520 ,window_resize ) then
b'\\ No newline at end of file'