1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
|
{target:win}
//
// AggPas 2.4 RM3 Demo application
// Note: Press F1 key on run to see more info about this demo
//
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
//
program
gpc_test ;
uses
SysUtils ,
agg_basics ,
agg_platform_support ,
agg_color ,
agg_pixfmt ,
agg_pixfmt_rgb ,
agg_ctrl ,
agg_slider_ctrl ,
agg_cbox_ctrl ,
agg_rbox_ctrl ,
agg_rendering_buffer ,
agg_renderer_base ,
agg_renderer_scanline ,
agg_renderer_primitives ,
agg_rasterizer_scanline_aa ,
agg_scanline ,
agg_scanline_u ,
agg_scanline_p ,
agg_render_scanlines ,
agg_math_stroke ,
agg_path_storage ,
agg_span_solid ,
agg_conv_curve ,
agg_conv_stroke ,
agg_conv_transform ,
agg_conv_clip_polygon ,
agg_conv_gpc ,
agg_gsv_text ,
agg_trans_affine ,
agg_vertex_source ,
make_gb_poly_ ,
make_arrows_ ;
{$I agg_mode.inc }
const
flip_y = true;
type
spiral = object(vertex_source )
m_x ,
m_y ,
m_r1 ,
m_r2 ,
m_step ,
m_start_angle ,
m_angle ,
m_curr_r ,
m_da ,
m_dr : double;
m_start : boolean;
constructor Construct(x ,y ,r1 ,r2 ,step : double; start_angle : double = 0 );
procedure rewind(path_id : unsigned ); virtual;
function vertex(x ,y : double_ptr ) : unsigned; virtual;
end;
conv_poly_counter = object(vertex_source )
m_src : vertex_source_ptr;
m_contours ,
m_points : unsigned;
constructor Construct(src : vertex_source_ptr );
procedure rewind(path_id : unsigned ); virtual;
function vertex(x ,y : double_ptr ) : unsigned; virtual;
end;
the_application = object(platform_support )
m_polygons ,
m_operation : rbox_ctrl;
m_x ,
m_y : double;
constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
destructor Destruct;
procedure perform_rendering(
sl : scanline_ptr;
ras : rasterizer_scanline_ptr;
ren : renderer_scanline_ptr;
gpc : conv_gpc_ptr );
function render_gpc(sl : scanline_ptr; ras : rasterizer_scanline_ptr ) : unsigned;
procedure on_init; virtual;
procedure on_draw; virtual;
procedure on_mouse_move (x ,y : int; flags : unsigned ); virtual;
procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
procedure stress_test;
end;
{ CONSTRUCT }
constructor spiral.Construct;
begin
m_x :=x;
m_y :=y;
m_r1:=r1;
m_r2:=r2;
m_step :=step;
m_start_angle:=start_angle;
m_angle :=start_angle;
m_da:=deg2rad(4.0 );
m_dr:=m_step / 90.0;
end;
{ REWIND }
procedure spiral.rewind;
begin
m_angle :=m_start_angle;
m_curr_r:=m_r1;
m_start :=true;
end;
{ VERTEX }
function spiral.vertex;
begin
if m_curr_r > m_r2 then
begin
result:=path_cmd_stop;
exit;
end;
x^:=m_x + Cos(m_angle ) * m_curr_r;
y^:=m_y + Sin(m_angle ) * m_curr_r;
m_curr_r:=m_curr_r + m_dr;
m_angle :=m_angle + m_da;
if m_start then
begin
m_start:=false;
result:=path_cmd_move_to;
end
else
result:=path_cmd_line_to;
end;
{ CONSTRUCT }
constructor conv_poly_counter.Construct;
begin
m_src:=src;
m_contours:=0;
m_points :=0;
end;
{ REWIND }
procedure conv_poly_counter.rewind;
begin
m_contours:=0;
m_points :=0;
m_src.rewind(path_id );
end;
{ VERTEX }
function conv_poly_counter.vertex;
var
cmd : unsigned;
begin
cmd:=m_src.vertex(x ,y );
if is_vertex(cmd ) then
inc(m_points );
if is_move_to(cmd ) then
inc(m_contours );
result:=cmd;
end;
{ CONSTRUCT }
constructor the_application.Construct;
begin
inherited Construct(format_ ,flip_y_ );
m_polygons.Construct (5.0 ,5.0 ,5.0 + 205.0 ,110.0 ,not flip_y_ );
m_operation.Construct(555.0 ,5.0 ,555.0 + 80.0 ,130.0 ,not flip_y_ );
m_operation.add_item ('None' );
m_operation.add_item ('OR' );
m_operation.add_item ('AND' );
m_operation.add_item ('XOR' );
m_operation.add_item ('A-B' );
m_operation.add_item ('B-A' );
m_operation.cur_item_(2 );
add_ctrl(@m_operation );
m_polygons.add_item ('Two Simple Paths' );
m_polygons.add_item ('Closed Stroke' );
m_polygons.add_item ('Great Britain and Arrows' );
m_polygons.add_item ('Great Britain and Spiral' );
m_polygons.add_item ('Spiral and Glyph' );
m_polygons.cur_item_(3 );
add_ctrl(@m_polygons );
end;
{ DESTRUCT }
destructor the_application.Destruct;
begin
inherited Destruct;
m_polygons.Destruct;
m_operation.Destruct;
end;
{ PERFORM_RENDERING }
procedure the_application.perform_rendering;
var
counter : conv_poly_counter;
t1 ,t2 ,x ,y : double;
cmd : unsigned;
buf : array[0..99 ] of char;
rgba : aggclr;
txt : gsv_text;
txt_stroke : conv_stroke;
begin
if m_operation._cur_item > 0 then
begin
// Render clipped polygon
ras.reset;
case m_operation._cur_item of
1: gpc.operation(gpc_or );
2: gpc.operation(gpc_and );
3: gpc.operation(gpc_xor );
4: gpc.operation(gpc_a_minus_b );
5: gpc.operation(gpc_b_minus_a );
end;
counter.Construct(gpc );
start_timer;
counter.rewind(0 );
t1:=elapsed_time;
ras.reset;
start_timer;
cmd:=counter.vertex(@x ,@y );
while not is_stop(cmd ) do
begin
ras.add_vertex(x ,y ,cmd );
cmd:=counter.vertex(@x ,@y );
end;
rgba.ConstrDbl (0.5 ,0.0 ,0 ,0.5 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,ren );
t2:=elapsed_time;
// Render information text
sprintf(@buf[0 ] ,'Contours: %d ' ,counter.m_contours );
sprintf(@buf[StrLen(@buf ) ] ,'Points: %d' ,counter.m_points );
txt.Construct;
txt_stroke.Construct(@txt );
txt_stroke.width_ (1.5 );
txt_stroke.line_cap_(round_cap );
txt.size_ (10.0 );
txt.start_point_ (250 ,5 );
txt.text_ (@buf[0 ] );
ras.add_path (@txt_stroke );
rgba.ConstrDbl (0.0 ,0.0 ,0.0 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,ren );
sprintf(@buf[0 ] ,'GPC=%.3fms ' ,t1 );
sprintf(@buf[StrLen(buf ) ] ,'Render=%.3fms' ,t2 );
txt.start_point_(250 ,20 );
txt.text_ (@buf[0 ] );
ras.add_path (@txt_stroke );
rgba.ConstrDbl (0.0 ,0.0 ,0.0 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,ren );
// Free
txt.Destruct;
txt_stroke.Destruct;
end;
end;
{ RENDER_GPC }
function the_application.render_gpc;
var
pf : pixel_formats;
rb : renderer_base;
ren : renderer_scanline_aa_solid;
ps1 ,
ps2 ,
gb_poly ,
arrows ,
glyph : path_storage;
rgba : aggclr;
x ,y : double;
mtx1 ,
mtx2 ,
mtx : trans_affine;
tat : trans_affine_translation;
tas : trans_affine_scaling;
stroke ,
stroke_gb_poly : conv_stroke;
trans ,
trans_gb_poly ,
trans_arrows : conv_transform;
curve : conv_curve;
sp : spiral;
gpc : conv_gpc;
begin
pixfmt_bgr24(pf ,rbuf_window );
rb.Construct (@pf );
ren.Construct(@rb );
case m_polygons._cur_item of
0 : // Two simple paths
begin
ps1.Construct;
ps2.Construct;
gpc.Construct(@ps1 ,@ps2 );
x:=m_x - _initial_width / 2 + 100;
y:=m_y - _initial_height / 2 + 100;
ps1.move_to(x + 140 ,y + 145 );
ps1.line_to(x + 225 ,y + 44 );
ps1.line_to(x + 296 ,y + 219 );
ps1.close_polygon;
ps1.line_to(x + 226 ,y + 289 );
ps1.line_to(x + 82 ,y + 292 );
ps1.move_to(x + 220 ,y + 222 );
ps1.line_to(x + 363 ,y + 249 );
ps1.line_to(x + 265 ,y + 331 );
ps1.move_to(x + 242 ,y + 243 );
ps1.line_to(x + 268 ,y + 309 );
ps1.line_to(x + 325 ,y + 261 );
ps1.move_to(x + 259 ,y + 259 );
ps1.line_to(x + 273 ,y + 288 );
ps1.line_to(x + 298 ,y + 266 );
ps2.move_to(100 + 32 ,100 + 77 );
ps2.line_to(100 + 473 ,100 + 263 );
ps2.line_to(100 + 351 ,100 + 290 );
ps2.line_to(100 + 354 ,100 + 374 );
ras.reset;
ras.add_path (@ps1 );
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
ras.reset;
ras.add_path (@ps2 );
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
perform_rendering(sl ,ras ,@ren ,@gpc );
ps1.Destruct;
ps2.Destruct;
gpc.Destruct;
end;
1 : // Closed stroke
begin
ps1.Construct;
ps2.Construct;
stroke.Construct(@ps2 );
stroke.width_ (10.0 );
gpc.Construct(@ps1 ,@stroke );
x:=m_x - _initial_width / 2 + 100;
y:=m_y - _initial_height / 2 + 100;
ps1.move_to(x + 140 ,y + 145 );
ps1.line_to(x + 225 ,y + 44 );
ps1.line_to(x + 296 ,y + 219 );
ps1.close_polygon;
ps1.line_to(x + 226 ,y + 289 );
ps1.line_to(x + 82 ,y + 292 );
ps1.move_to(x + 220 - 50 ,y + 222 );
ps1.line_to(x + 265 - 50 ,y + 331 );
ps1.line_to(x + 363 - 50 ,y + 249 );
ps1.close_polygon(path_flags_ccw );
ps2.move_to(100 + 32 ,100 + 77 );
ps2.line_to(100 + 473 ,100 + 263 );
ps2.line_to(100 + 351 ,100 + 290 );
ps2.line_to(100 + 354 ,100 + 374 );
ps2.close_polygon;
ras.reset;
ras.add_path (@ps1 );
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
ras.reset;
ras.add_path (@stroke );
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
perform_rendering(sl ,ras ,@ren ,@gpc );
ps1.Destruct;
ps2.Destruct;
stroke.Destruct;
gpc.Destruct;
end;
2 : // Great Britain and Arrows
begin
gb_poly.Construct;
arrows.Construct;
make_gb_poly(@gb_poly );
make_arrows (@arrows );
mtx1.Construct;
mtx2.Construct;
tat.Construct(-1150 ,-1150 );
tas.Construct(2.0 );
mtx1.multiply(@tat );
mtx1.multiply(@tas );
mtx2:=mtx1;
tat.Construct(m_x - _initial_width / 2 ,m_y - _initial_height / 2 );
mtx2.multiply(@tat );
trans_gb_poly.Construct(@gb_poly ,@mtx1 );
trans_arrows.Construct (@arrows ,@mtx2 );
gpc.Construct(@trans_gb_poly ,@trans_arrows );
ras.add_path (@trans_gb_poly );
rgba.ConstrDbl (0.5 ,0.5 ,0 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
stroke_gb_poly.Construct(@trans_gb_poly );
stroke_gb_poly.width_ (0.1);
ras.add_path (@stroke_gb_poly );
rgba.ConstrDbl (0 ,0 ,0 );
ren.color_ (@rgba );
render_scanlines (ras ,sl ,@ren );
ras.add_path (@trans_arrows );
rgba.ConstrDbl (0.0 ,0.5 ,0.5 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
perform_rendering(sl ,ras ,@ren ,@gpc );
gb_poly.Destruct;
arrows.Destruct;
stroke_gb_poly.Destruct;
gpc.Destruct;
end;
3 : // Great Britain and a Spiral
begin
sp.Construct (m_x ,m_y ,10 ,150 ,30 ,0.0 );
stroke.Construct(@sp );
stroke.width_ (15.0 );
gb_poly.Construct;
make_gb_poly(@gb_poly );
mtx.Construct;
tat.Construct(-1150 ,-1150 );
tas.Construct(2.0 );
mtx.multiply(@tat );
mtx.multiply(@tas );
trans_gb_poly.Construct(@gb_poly ,@mtx );
gpc.Construct(@trans_gb_poly ,@stroke );
ras.add_path (@trans_gb_poly );
rgba.ConstrDbl (0.5 ,0.5 ,0 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
stroke_gb_poly.Construct(@trans_gb_poly );
stroke_gb_poly.width_ (0.1 );
ras.add_path (@stroke_gb_poly );
rgba.ConstrDbl (0 ,0 ,0 );
ren.color_ (@rgba );
render_scanlines (ras ,sl ,@ren );
ras.add_path (@stroke );
rgba.ConstrDbl (0.0 ,0.5 ,0.5 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
perform_rendering(sl ,ras ,@ren ,@gpc );
stroke.Destruct;
gb_poly.Destruct;
stroke_gb_poly.Destruct;
gpc.Destruct;
end;
4 : // Spiral and glyph
begin
sp.Construct (m_x ,m_y ,10 ,150 ,30 ,0.0 );
stroke.Construct(@sp );
stroke.width_ (15.0 );
glyph.Construct;
glyph.move_to(28.47 ,6.45 );
glyph.curve3 (21.58 ,1.12 ,19.82 ,0.29 );
glyph.curve3 (17.19 ,-0.93 ,14.21 ,-0.93 );
glyph.curve3 (9.57 ,-0.93 ,6.57 ,2.25 );
glyph.curve3 (3.56 ,5.42 ,3.56 ,10.60 );
glyph.curve3 (3.56 ,13.87 ,5.03 ,16.26 );
glyph.curve3 (7.03 ,19.58 ,11.99 ,22.51 );
glyph.curve3 (16.94 ,25.44 ,28.47 ,29.64 );
glyph.line_to(28.47 ,31.40 );
glyph.curve3 (28.47 ,38.09 ,26.34 ,40.58 );
glyph.curve3 (24.22 ,43.07 ,20.17 ,43.07 );
glyph.curve3 (17.09 ,43.07 ,15.28 ,41.41 );
glyph.curve3 (13.43 ,39.75 ,13.43 ,37.60 );
glyph.line_to(13.53 ,34.77 );
glyph.curve3 (13.53 ,32.52 ,12.38 ,31.30 );
glyph.curve3 (11.23 ,30.08 ,9.38 ,30.08 );
glyph.curve3 (7.57 ,30.08 ,6.42 ,31.35 );
glyph.curve3 (5.27 ,32.62 ,5.27 ,34.81 );
glyph.curve3 (5.27 ,39.01 ,9.57 ,42.53 );
glyph.curve3 (13.87 ,46.04 ,21.63 ,46.04 );
glyph.curve3 (27.59 ,46.04 ,31.40 ,44.04 );
glyph.curve3 (34.28 ,42.53 ,35.64 ,39.31 );
glyph.curve3 (36.52 ,37.21 ,36.52 ,30.71 );
glyph.line_to(36.52 ,15.53 );
glyph.curve3 (36.52 ,9.13 ,36.77 ,7.69 );
glyph.curve3 (37.01 ,6.25 ,37.57 ,5.76 );
glyph.curve3 (38.13 ,5.27 ,38.87 ,5.27 );
glyph.curve3 (39.65 ,5.27 ,40.23 ,5.62 );
glyph.curve3 (41.26 ,6.25 ,44.19 ,9.18 );
glyph.line_to(44.19 ,6.45 );
glyph.curve3 (38.72 ,-0.88 ,33.74 ,-0.88 );
glyph.curve3 (31.35 ,-0.88 ,29.93 ,0.78 );
glyph.curve3 (28.52 ,2.44 ,28.47 ,6.45 );
glyph.close_polygon;
glyph.move_to(28.47 ,9.62 );
glyph.line_to(28.47 ,26.66 );
glyph.curve3 (21.09 ,23.73 ,18.95 ,22.51 );
glyph.curve3 (15.09 ,20.36 ,13.43 ,18.02 );
glyph.curve3 (11.77 ,15.67 ,11.77 ,12.89 );
glyph.curve3 (11.77 ,9.38 ,13.87 ,7.06 );
glyph.curve3 (15.97 ,4.74 ,18.70 ,4.74 );
glyph.curve3 (22.41 ,4.74 ,28.47 ,9.62 );
glyph.close_polygon;
mtx.Construct;
tas.Construct(4.0 );
tat.Construct(220 ,200 );
mtx.multiply(@tas );
mtx.multiply(@tat );
trans.Construct(@glyph ,@mtx );
curve.Construct(@trans );
gpc.Construct(@stroke ,@curve );
ras.reset;
ras.add_path (@stroke );
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
ras.reset;
ras.add_path (@curve );
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
ren.color_ (@rgba );
render_scanlines(ras ,sl ,@ren );
perform_rendering(sl ,ras ,@ren ,@gpc );
stroke.Destruct;
glyph.Destruct;
curve.Destruct;
gpc.Destruct;
end;
end;
result:=0;
end;
{ ON_INIT }
procedure the_application.on_init;
begin
m_x:=_width / 2.0;
m_y:=_height / 2.0;
end;
{ ON_DRAW }
procedure the_application.on_draw;
var
pf : pixel_formats;
rgba : aggclr;
ren_base : renderer_base;
ren_solid : renderer_scanline_aa_solid;
sl : scanline_u8;
ras : rasterizer_scanline_aa;
begin
// Initialize structures
pixfmt_bgr24(pf ,rbuf_window );
ren_base.Construct (@pf );
ren_solid.Construct(@ren_base );
rgba.ConstrDbl(1 ,1 ,1 );
ren_base.clear(@rgba );
sl.Construct;
ras.Construct;
// Render
render_gpc(@sl ,@ras );
// Render the controls
render_ctrl(@ras ,@sl ,@ren_solid ,@m_polygons );
render_ctrl(@ras ,@sl ,@ren_solid ,@m_operation );
// Free AGG resources
sl.Destruct;
ras.Destruct;
end;
{ ON_MOUSE_MOVE }
procedure the_application.on_mouse_move;
begin
if flags and mouse_left <> 0 then
begin
m_x:=x;
m_y:=y;
force_redraw;
end;
end;
{ ON_MOUSE_BUTTON_DOWN }
procedure the_application.on_mouse_button_down;
var
buf : array[0..99 ] of char;
begin
if flags and mouse_left <> 0 then
begin
m_x:=x;
m_y:=y;
force_redraw;
end;
if flags and mouse_right <> 0 then
begin
sprintf (@buf[0 ] ,'%d ' ,x );
sprintf (@buf[StrLen(@buf ) ] ,'%d' ,y );
message_(@buf[0 ] );
end;
end;
{ ON_KEY }
procedure the_application.on_key;
begin
case key of
byte('t' ) ,
byte('T' ) :
stress_test;
end;
if key = key_f1 then
message_(
'General Polygon Clipper by Alan Murta is the most reliable implementation of the '#13 +
'polygon boolean algebra. It implements Bala R. Vatti''s algorithm of arbitrary '#13 +
'polygon clipping and allows you to calculate the Union, Intersection, Difference, '#13 +
'and Exclusive OR between two poly-polygons (i.e., polygonal areas consisted of '#13 +
'several contours). AGG has a simple wrapper class that can be used in the coordinate '#13 +
'conversion pipeline. The implementation by Alan Murta has restrictions of using it '#13 +
'in commercial software, so that, please contact the author to settle the legal issues. '#13 +
'The example demonstrates the use of GPC. Note, that all operations are done in the '#13 +
'vectorial representation of the contours before rendering.'#13#13 +
'How to play with:'#13#13 +
'You can drag one polygon with the left mouse button pressed.'#13 +
'Press the "T" key to perform the random polygon clipping stress testing.'#13 +
'(may take some time)' +
#13#13'Note: F2 key saves current "screenshot" file in this demo''s directory. ' );
end;
{ STRESS_TEST }
// Stress-test.
// Works quite well on random polygons, no crashes, no memory leaks!
// Sometimes takes long to produce the result
procedure the_application.stress_test;
{ random }
function random(min ,max : double ) : double;
var
r : int;
begin
r:=(System.Random($7fff ) shl 15 ) or System.Random($7fff );
result:=$FFFFFFF + 1;
result:=((r and $FFFFFFF) / result ) * (max - min ) + min;
end;
var
sl : scanline_u8;
ras : rasterizer_scanline_aa;
pf : pixel_formats;
ren_base : renderer_base;
ren_solid : renderer_scanline_aa_solid;
ps1 ,ps2 : path_storage;
gpc : conv_gpc;
rgba : aggclr;
i ,num_poly1 ,num_poly2 ,j ,k ,np ,op: unsigned;
buf : array[0..99 ] of char;
txt : gsv_text;
txt_stroke : conv_stroke;
begin
sl.Construct;
ras.Construct;
pixfmt_bgr24(pf ,rbuf_window );
ren_base.Construct (@pf );
ren_solid.Construct(@ren_base );
ps1.Construct;
ps2.Construct;
gpc.Construct(@ps1 ,@ps2 );
txt.Construct;
txt_stroke.Construct(@txt );
txt_stroke.width_ (1.5 );
txt_stroke.line_cap_(round_cap );
txt.size_ (10.0 );
txt.start_point_ (5 ,5 );
for i:=0 to 999 do
begin
rgba.ConstrDbl(1 ,1 ,1 );
ren_base.clear(@rgba );
num_poly1:=System.Random($7fff ) mod 10 + 1;
num_poly2:=System.Random($7fff ) mod 10 + 1;
ps1.remove_all;
ps2.remove_all;
for j:=0 to num_poly1 - 1 do
begin
ps1.move_to(random(0 ,_width ) ,random(0 ,_height ) );
np:=System.Random($7fff ) mod 20 + 2;
for k:=0 to np - 1 do
ps1.line_to(random(0 ,_width ) ,random(0 ,_height ) );
end;
for j:=0 to num_poly2 - 1 do
begin
ps2.move_to(random(0 ,_width ) ,random(0 ,_height ) );
np:=System.Random($7fff ) mod 20 + 2;
for k:=0 to np - 1 do
ps2.line_to(random(0 ,_width ) ,random(0 ,_height ) );
end;
op:=System.Random($7fff ) mod 5;
case op of
0 :
gpc.operation(gpc_or );
1 :
gpc.operation(gpc_and );
2 :
gpc.operation(gpc_xor );
3 :
gpc.operation(gpc_a_minus_b );
else
gpc.operation(gpc_b_minus_a );
end;
// Clipping result
ras.add_path (@gpc );
rgba.ConstrDbl (0.5 ,0.0 ,0 ,0.5 );
ren_solid.color_(@rgba );
render_scanlines(@ras ,@sl ,@ren_solid );
// Counter display
sprintf (@buf[0 ] ,'%d / 1000' ,i + 1 );
txt.text_(@buf[0 ] );
txt.start_point_(5 ,5 );
ras.add_path (@txt_stroke );
rgba.ConstrDbl (0.0 ,0.0 ,0.0 );
ren_solid.color_(@rgba );
render_scanlines(@ras ,@sl ,@ren_solid );
// Refresh
update_window;
end;
message_('Done' );
ps1.Destruct;
ps2.Destruct;
gpc.Destruct;
sl.Destruct;
ras.Destruct;
txt.Destruct;
txt_stroke.Destruct;
force_redraw;
end;
VAR
app : the_application;
BEGIN
app.Construct(pix_format_bgr24 ,flip_y );
app.caption_ ('AGG Example. General Polygon Clipping - GPC (F1-Help)' );
if app.init(640 ,520 ,window_resize ) then
app.run;
app.Destruct;
END.
|