3
// AggPas 2.4 RM3 Demo application
4
// Note: Press F1 key on run to see more info about this demo
6
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
15
agg_platform_support ,
26
agg_rendering_buffer ,
28
agg_renderer_scanline ,
29
agg_renderer_primitives ,
30
agg_rasterizer_scanline_aa ,
34
agg_render_scanlines ,
42
agg_conv_clip_polygon ,
57
spiral = object(vertex_source )
72
constructor Construct(x ,y ,r1 ,r2 ,step : double; start_angle : double = 0 );
74
procedure rewind(path_id : unsigned ); virtual;
75
function vertex(x ,y : double_ptr ) : unsigned; virtual;
79
conv_poly_counter = object(vertex_source )
80
m_src : vertex_source_ptr;
85
constructor Construct(src : vertex_source_ptr );
87
procedure rewind(path_id : unsigned ); virtual;
88
function vertex(x ,y : double_ptr ) : unsigned; virtual;
92
the_application = object(platform_support )
94
m_operation : rbox_ctrl;
99
constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
102
procedure perform_rendering(
104
ras : rasterizer_scanline_ptr;
105
ren : renderer_scanline_ptr;
106
gpc : conv_gpc_ptr );
108
function render_gpc(sl : scanline_ptr; ras : rasterizer_scanline_ptr ) : unsigned;
110
procedure on_init; virtual;
111
procedure on_draw; virtual;
113
procedure on_mouse_move (x ,y : int; flags : unsigned ); virtual;
114
procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
116
procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
118
procedure stress_test;
123
constructor spiral.Construct;
131
m_start_angle:=start_angle;
132
m_angle :=start_angle;
140
procedure spiral.rewind;
142
m_angle :=m_start_angle;
149
function spiral.vertex;
151
if m_curr_r > m_r2 then
153
result:=path_cmd_stop;
159
x^:=m_x + Cos(m_angle ) * m_curr_r;
160
y^:=m_y + Sin(m_angle ) * m_curr_r;
162
m_curr_r:=m_curr_r + m_dr;
163
m_angle :=m_angle + m_da;
169
result:=path_cmd_move_to;
173
result:=path_cmd_line_to;
178
constructor conv_poly_counter.Construct;
188
procedure conv_poly_counter.rewind;
193
m_src.rewind(path_id );
198
function conv_poly_counter.vertex;
203
cmd:=m_src.vertex(x ,y );
205
if is_vertex(cmd ) then
208
if is_move_to(cmd ) then
216
constructor the_application.Construct;
218
inherited Construct(format_ ,flip_y_ );
220
m_polygons.Construct (5.0 ,5.0 ,5.0 + 205.0 ,110.0 ,not flip_y_ );
221
m_operation.Construct(555.0 ,5.0 ,555.0 + 80.0 ,130.0 ,not flip_y_ );
223
m_operation.add_item ('None' );
224
m_operation.add_item ('OR' );
225
m_operation.add_item ('AND' );
226
m_operation.add_item ('XOR' );
227
m_operation.add_item ('A-B' );
228
m_operation.add_item ('B-A' );
229
m_operation.cur_item_(2 );
231
add_ctrl(@m_operation );
233
m_polygons.add_item ('Two Simple Paths' );
234
m_polygons.add_item ('Closed Stroke' );
235
m_polygons.add_item ('Great Britain and Arrows' );
236
m_polygons.add_item ('Great Britain and Spiral' );
237
m_polygons.add_item ('Spiral and Glyph' );
238
m_polygons.cur_item_(3 );
240
add_ctrl(@m_polygons );
245
destructor the_application.Destruct;
250
m_operation.Destruct;
254
{ PERFORM_RENDERING }
255
procedure the_application.perform_rendering;
257
counter : conv_poly_counter;
259
t1 ,t2 ,x ,y : double;
262
buf : array[0..99 ] of char;
267
txt_stroke : conv_stroke;
270
if m_operation._cur_item > 0 then
272
// Render clipped polygon
275
case m_operation._cur_item of
276
1: gpc.operation(gpc_or );
277
2: gpc.operation(gpc_and );
278
3: gpc.operation(gpc_xor );
279
4: gpc.operation(gpc_a_minus_b );
280
5: gpc.operation(gpc_b_minus_a );
284
counter.Construct(gpc );
294
cmd:=counter.vertex(@x ,@y );
296
while not is_stop(cmd ) do
298
ras.add_vertex(x ,y ,cmd );
300
cmd:=counter.vertex(@x ,@y );
304
rgba.ConstrDbl (0.5 ,0.0 ,0 ,0.5 );
306
render_scanlines(ras ,sl ,ren );
310
// Render information text
311
sprintf(@buf[0 ] ,'Contours: %d ' ,counter.m_contours );
312
sprintf(@buf[StrLen(@buf ) ] ,'Points: %d' ,counter.m_points );
315
txt_stroke.Construct(@txt );
317
txt_stroke.width_ (1.5 );
318
txt_stroke.line_cap_(round_cap );
320
txt.start_point_ (250 ,5 );
321
txt.text_ (@buf[0 ] );
323
ras.add_path (@txt_stroke );
324
rgba.ConstrDbl (0.0 ,0.0 ,0.0 );
326
render_scanlines(ras ,sl ,ren );
328
sprintf(@buf[0 ] ,'GPC=%.3fms ' ,t1 );
329
sprintf(@buf[StrLen(buf ) ] ,'Render=%.3fms' ,t2 );
331
txt.start_point_(250 ,20 );
332
txt.text_ (@buf[0 ] );
334
ras.add_path (@txt_stroke );
335
rgba.ConstrDbl (0.0 ,0.0 ,0.0 );
337
render_scanlines(ras ,sl ,ren );
348
function the_application.render_gpc;
353
ren : renderer_scanline_aa_solid;
359
glyph : path_storage;
367
tat : trans_affine_translation;
368
tas : trans_affine_scaling;
371
stroke_gb_poly : conv_stroke;
375
trans_arrows : conv_transform;
383
pixfmt_bgr24(pf ,rbuf_window );
388
case m_polygons._cur_item of
389
0 : // Two simple paths
394
gpc.Construct(@ps1 ,@ps2 );
396
x:=m_x - _initial_width / 2 + 100;
397
y:=m_y - _initial_height / 2 + 100;
399
ps1.move_to(x + 140 ,y + 145 );
400
ps1.line_to(x + 225 ,y + 44 );
401
ps1.line_to(x + 296 ,y + 219 );
404
ps1.line_to(x + 226 ,y + 289 );
405
ps1.line_to(x + 82 ,y + 292 );
407
ps1.move_to(x + 220 ,y + 222 );
408
ps1.line_to(x + 363 ,y + 249 );
409
ps1.line_to(x + 265 ,y + 331 );
411
ps1.move_to(x + 242 ,y + 243 );
412
ps1.line_to(x + 268 ,y + 309 );
413
ps1.line_to(x + 325 ,y + 261 );
415
ps1.move_to(x + 259 ,y + 259 );
416
ps1.line_to(x + 273 ,y + 288 );
417
ps1.line_to(x + 298 ,y + 266 );
419
ps2.move_to(100 + 32 ,100 + 77 );
420
ps2.line_to(100 + 473 ,100 + 263 );
421
ps2.line_to(100 + 351 ,100 + 290 );
422
ps2.line_to(100 + 354 ,100 + 374 );
425
ras.add_path (@ps1 );
426
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
428
render_scanlines(ras ,sl ,@ren );
431
ras.add_path (@ps2 );
432
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
434
render_scanlines(ras ,sl ,@ren );
436
perform_rendering(sl ,ras ,@ren ,@gpc );
448
stroke.Construct(@ps2 );
449
stroke.width_ (10.0 );
451
gpc.Construct(@ps1 ,@stroke );
453
x:=m_x - _initial_width / 2 + 100;
454
y:=m_y - _initial_height / 2 + 100;
456
ps1.move_to(x + 140 ,y + 145 );
457
ps1.line_to(x + 225 ,y + 44 );
458
ps1.line_to(x + 296 ,y + 219 );
461
ps1.line_to(x + 226 ,y + 289 );
462
ps1.line_to(x + 82 ,y + 292 );
464
ps1.move_to(x + 220 - 50 ,y + 222 );
465
ps1.line_to(x + 265 - 50 ,y + 331 );
466
ps1.line_to(x + 363 - 50 ,y + 249 );
467
ps1.close_polygon(path_flags_ccw );
469
ps2.move_to(100 + 32 ,100 + 77 );
470
ps2.line_to(100 + 473 ,100 + 263 );
471
ps2.line_to(100 + 351 ,100 + 290 );
472
ps2.line_to(100 + 354 ,100 + 374 );
476
ras.add_path (@ps1 );
477
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
479
render_scanlines(ras ,sl ,@ren );
482
ras.add_path (@stroke );
483
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
485
render_scanlines(ras ,sl ,@ren );
487
perform_rendering(sl ,ras ,@ren ,@gpc );
496
2 : // Great Britain and Arrows
501
make_gb_poly(@gb_poly );
502
make_arrows (@arrows );
506
tat.Construct(-1150 ,-1150 );
508
mtx1.multiply(@tat );
509
mtx1.multiply(@tas );
513
tat.Construct(m_x - _initial_width / 2 ,m_y - _initial_height / 2 );
515
mtx2.multiply(@tat );
517
trans_gb_poly.Construct(@gb_poly ,@mtx1 );
518
trans_arrows.Construct (@arrows ,@mtx2 );
520
gpc.Construct(@trans_gb_poly ,@trans_arrows );
522
ras.add_path (@trans_gb_poly );
523
rgba.ConstrDbl (0.5 ,0.5 ,0 ,0.1 );
525
render_scanlines(ras ,sl ,@ren );
527
stroke_gb_poly.Construct(@trans_gb_poly );
528
stroke_gb_poly.width_ (0.1);
529
ras.add_path (@stroke_gb_poly );
530
rgba.ConstrDbl (0 ,0 ,0 );
532
render_scanlines (ras ,sl ,@ren );
534
ras.add_path (@trans_arrows );
535
rgba.ConstrDbl (0.0 ,0.5 ,0.5 ,0.1 );
537
render_scanlines(ras ,sl ,@ren );
539
perform_rendering(sl ,ras ,@ren ,@gpc );
543
stroke_gb_poly.Destruct;
548
3 : // Great Britain and a Spiral
550
sp.Construct (m_x ,m_y ,10 ,150 ,30 ,0.0 );
551
stroke.Construct(@sp );
552
stroke.width_ (15.0 );
555
make_gb_poly(@gb_poly );
558
tat.Construct(-1150 ,-1150 );
563
trans_gb_poly.Construct(@gb_poly ,@mtx );
565
gpc.Construct(@trans_gb_poly ,@stroke );
567
ras.add_path (@trans_gb_poly );
568
rgba.ConstrDbl (0.5 ,0.5 ,0 ,0.1 );
570
render_scanlines(ras ,sl ,@ren );
572
stroke_gb_poly.Construct(@trans_gb_poly );
573
stroke_gb_poly.width_ (0.1 );
574
ras.add_path (@stroke_gb_poly );
575
rgba.ConstrDbl (0 ,0 ,0 );
577
render_scanlines (ras ,sl ,@ren );
579
ras.add_path (@stroke );
580
rgba.ConstrDbl (0.0 ,0.5 ,0.5 ,0.1 );
582
render_scanlines(ras ,sl ,@ren );
584
perform_rendering(sl ,ras ,@ren ,@gpc );
588
stroke_gb_poly.Destruct;
593
4 : // Spiral and glyph
595
sp.Construct (m_x ,m_y ,10 ,150 ,30 ,0.0 );
596
stroke.Construct(@sp );
597
stroke.width_ (15.0 );
600
glyph.move_to(28.47 ,6.45 );
601
glyph.curve3 (21.58 ,1.12 ,19.82 ,0.29 );
602
glyph.curve3 (17.19 ,-0.93 ,14.21 ,-0.93 );
603
glyph.curve3 (9.57 ,-0.93 ,6.57 ,2.25 );
604
glyph.curve3 (3.56 ,5.42 ,3.56 ,10.60 );
605
glyph.curve3 (3.56 ,13.87 ,5.03 ,16.26 );
606
glyph.curve3 (7.03 ,19.58 ,11.99 ,22.51 );
607
glyph.curve3 (16.94 ,25.44 ,28.47 ,29.64 );
608
glyph.line_to(28.47 ,31.40 );
609
glyph.curve3 (28.47 ,38.09 ,26.34 ,40.58 );
610
glyph.curve3 (24.22 ,43.07 ,20.17 ,43.07 );
611
glyph.curve3 (17.09 ,43.07 ,15.28 ,41.41 );
612
glyph.curve3 (13.43 ,39.75 ,13.43 ,37.60 );
613
glyph.line_to(13.53 ,34.77 );
614
glyph.curve3 (13.53 ,32.52 ,12.38 ,31.30 );
615
glyph.curve3 (11.23 ,30.08 ,9.38 ,30.08 );
616
glyph.curve3 (7.57 ,30.08 ,6.42 ,31.35 );
617
glyph.curve3 (5.27 ,32.62 ,5.27 ,34.81 );
618
glyph.curve3 (5.27 ,39.01 ,9.57 ,42.53 );
619
glyph.curve3 (13.87 ,46.04 ,21.63 ,46.04 );
620
glyph.curve3 (27.59 ,46.04 ,31.40 ,44.04 );
621
glyph.curve3 (34.28 ,42.53 ,35.64 ,39.31 );
622
glyph.curve3 (36.52 ,37.21 ,36.52 ,30.71 );
623
glyph.line_to(36.52 ,15.53 );
624
glyph.curve3 (36.52 ,9.13 ,36.77 ,7.69 );
625
glyph.curve3 (37.01 ,6.25 ,37.57 ,5.76 );
626
glyph.curve3 (38.13 ,5.27 ,38.87 ,5.27 );
627
glyph.curve3 (39.65 ,5.27 ,40.23 ,5.62 );
628
glyph.curve3 (41.26 ,6.25 ,44.19 ,9.18 );
629
glyph.line_to(44.19 ,6.45 );
630
glyph.curve3 (38.72 ,-0.88 ,33.74 ,-0.88 );
631
glyph.curve3 (31.35 ,-0.88 ,29.93 ,0.78 );
632
glyph.curve3 (28.52 ,2.44 ,28.47 ,6.45 );
635
glyph.move_to(28.47 ,9.62 );
636
glyph.line_to(28.47 ,26.66 );
637
glyph.curve3 (21.09 ,23.73 ,18.95 ,22.51 );
638
glyph.curve3 (15.09 ,20.36 ,13.43 ,18.02 );
639
glyph.curve3 (11.77 ,15.67 ,11.77 ,12.89 );
640
glyph.curve3 (11.77 ,9.38 ,13.87 ,7.06 );
641
glyph.curve3 (15.97 ,4.74 ,18.70 ,4.74 );
642
glyph.curve3 (22.41 ,4.74 ,28.47 ,9.62 );
647
tat.Construct(220 ,200 );
651
trans.Construct(@glyph ,@mtx );
652
curve.Construct(@trans );
654
gpc.Construct(@stroke ,@curve );
657
ras.add_path (@stroke );
658
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
660
render_scanlines(ras ,sl ,@ren );
663
ras.add_path (@curve );
664
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
666
render_scanlines(ras ,sl ,@ren );
668
perform_rendering(sl ,ras ,@ren ,@gpc );
684
procedure the_application.on_init;
692
procedure the_application.on_draw;
698
ren_base : renderer_base;
699
ren_solid : renderer_scanline_aa_solid;
702
ras : rasterizer_scanline_aa;
705
// Initialize structures
706
pixfmt_bgr24(pf ,rbuf_window );
708
ren_base.Construct (@pf );
709
ren_solid.Construct(@ren_base );
711
rgba.ConstrDbl(1 ,1 ,1 );
712
ren_base.clear(@rgba );
718
render_gpc(@sl ,@ras );
720
// Render the controls
721
render_ctrl(@ras ,@sl ,@ren_solid ,@m_polygons );
722
render_ctrl(@ras ,@sl ,@ren_solid ,@m_operation );
724
// Free AGG resources
731
procedure the_application.on_mouse_move;
733
if flags and mouse_left <> 0 then
744
{ ON_MOUSE_BUTTON_DOWN }
745
procedure the_application.on_mouse_button_down;
747
buf : array[0..99 ] of char;
750
if flags and mouse_left <> 0 then
759
if flags and mouse_right <> 0 then
761
sprintf (@buf[0 ] ,'%d ' ,x );
762
sprintf (@buf[StrLen(@buf ) ] ,'%d' ,y );
770
procedure the_application.on_key;
781
'General Polygon Clipper by Alan Murta is the most reliable implementation of the '#13 +
782
'polygon boolean algebra. It implements Bala R. Vatti''s algorithm of arbitrary '#13 +
783
'polygon clipping and allows you to calculate the Union, Intersection, Difference, '#13 +
784
'and Exclusive OR between two poly-polygons (i.e., polygonal areas consisted of '#13 +
785
'several contours). AGG has a simple wrapper class that can be used in the coordinate '#13 +
786
'conversion pipeline. The implementation by Alan Murta has restrictions of using it '#13 +
787
'in commercial software, so that, please contact the author to settle the legal issues. '#13 +
788
'The example demonstrates the use of GPC. Note, that all operations are done in the '#13 +
789
'vectorial representation of the contours before rendering.'#13#13 +
790
'How to play with:'#13#13 +
791
'You can drag one polygon with the left mouse button pressed.'#13 +
792
'Press the "T" key to perform the random polygon clipping stress testing.'#13 +
793
'(may take some time)' +
794
#13#13'Note: F2 key saves current "screenshot" file in this demo''s directory. ' );
800
// Works quite well on random polygons, no crashes, no memory leaks!
801
// Sometimes takes long to produce the result
802
procedure the_application.stress_test;
805
function random(min ,max : double ) : double;
810
r:=(System.Random($7fff ) shl 15 ) or System.Random($7fff );
812
result:=$FFFFFFF + 1;
813
result:=((r and $FFFFFFF) / result ) * (max - min ) + min;
819
ras : rasterizer_scanline_aa;
823
ren_base : renderer_base;
824
ren_solid : renderer_scanline_aa_solid;
826
ps1 ,ps2 : path_storage;
831
i ,num_poly1 ,num_poly2 ,j ,k ,np ,op: unsigned;
833
buf : array[0..99 ] of char;
836
txt_stroke : conv_stroke;
842
pixfmt_bgr24(pf ,rbuf_window );
844
ren_base.Construct (@pf );
845
ren_solid.Construct(@ren_base );
849
gpc.Construct(@ps1 ,@ps2 );
852
txt_stroke.Construct(@txt );
854
txt_stroke.width_ (1.5 );
855
txt_stroke.line_cap_(round_cap );
857
txt.start_point_ (5 ,5 );
861
rgba.ConstrDbl(1 ,1 ,1 );
862
ren_base.clear(@rgba );
864
num_poly1:=System.Random($7fff ) mod 10 + 1;
865
num_poly2:=System.Random($7fff ) mod 10 + 1;
870
for j:=0 to num_poly1 - 1 do
872
ps1.move_to(random(0 ,_width ) ,random(0 ,_height ) );
874
np:=System.Random($7fff ) mod 20 + 2;
876
for k:=0 to np - 1 do
877
ps1.line_to(random(0 ,_width ) ,random(0 ,_height ) );
881
for j:=0 to num_poly2 - 1 do
883
ps2.move_to(random(0 ,_width ) ,random(0 ,_height ) );
885
np:=System.Random($7fff ) mod 20 + 2;
887
for k:=0 to np - 1 do
888
ps2.line_to(random(0 ,_width ) ,random(0 ,_height ) );
892
op:=System.Random($7fff ) mod 5;
896
gpc.operation(gpc_or );
899
gpc.operation(gpc_and );
902
gpc.operation(gpc_xor );
905
gpc.operation(gpc_a_minus_b );
908
gpc.operation(gpc_b_minus_a );
913
ras.add_path (@gpc );
914
rgba.ConstrDbl (0.5 ,0.0 ,0 ,0.5 );
915
ren_solid.color_(@rgba );
916
render_scanlines(@ras ,@sl ,@ren_solid );
919
sprintf (@buf[0 ] ,'%d / 1000' ,i + 1 );
920
txt.text_(@buf[0 ] );
922
txt.start_point_(5 ,5 );
924
ras.add_path (@txt_stroke );
925
rgba.ConstrDbl (0.0 ,0.0 ,0.0 );
926
ren_solid.color_(@rgba );
927
render_scanlines(@ras ,@sl ,@ren_solid );
951
app : the_application;
954
app.Construct(pix_format_bgr24 ,flip_y );
955
app.caption_ ('AGG Example. General Polygon Clipping - GPC (F1-Help)' );
957
if app.init(640 ,520 ,window_resize ) then
b'\\ No newline at end of file'