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 ,
26
agg_rendering_buffer ,
28
agg_renderer_scanline ,
29
agg_renderer_primitives ,
30
agg_rasterizer_scanline_aa ,
33
agg_render_scanlines ,
39
agg_pixfmt_amask_adaptor ,
55
spiral = object(vertex_source )
70
constructor Construct(x ,y ,r1 ,r2 ,step : double; start_angle : double = 0 );
72
procedure rewind(path_id : unsigned ); virtual;
73
function vertex(x ,y : double_ptr ) : unsigned; virtual;
77
the_application = object(platform_support )
79
m_operation : rbox_ctrl;
81
m_alpha_buf : int8u_ptr;
82
m_alpha_aloc : unsigned;
83
m_alpha_mask_rbuf : rendering_buffer;
84
m_alpha_mask : amask_no_clip_gray8;
86
m_ras : rasterizer_scanline_aa;
92
constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
95
procedure draw_text(x ,y : double; str : char_ptr );
97
procedure generate_alpha_mask(vs : vertex_source_ptr );
98
procedure perform_rendering (vs : vertex_source_ptr );
100
function render : unsigned;
102
procedure on_init; virtual;
103
procedure on_draw; virtual;
105
procedure on_mouse_move (x ,y : int; flags : unsigned ); virtual;
106
procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
108
procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
113
constructor spiral.Construct;
121
m_start_angle:=start_angle;
122
m_angle :=start_angle;
130
procedure spiral.rewind;
132
m_angle :=m_start_angle;
139
function spiral.vertex;
141
if m_curr_r > m_r2 then
143
result:=path_cmd_stop;
149
x^:=m_x + Cos(m_angle ) * m_curr_r;
150
y^:=m_y + Sin(m_angle ) * m_curr_r;
152
m_curr_r:=m_curr_r + m_dr;
153
m_angle :=m_angle + m_da;
159
result:=path_cmd_move_to;
163
result:=path_cmd_line_to;
168
constructor the_application.Construct;
170
inherited Construct(format_ ,flip_y_ );
172
m_polygons.Construct (5.0 ,5.0 ,5.0 + 205.0 ,110.0 ,not flip_y_ );
173
m_operation.Construct(555.0 ,5.0 ,555.0 + 80.0 ,55.0 ,not flip_y_ );
178
m_alpha_mask_rbuf.Construct;
179
m_alpha_mask.Construct(@m_alpha_mask_rbuf );
187
m_operation.add_item ('AND' );
188
m_operation.add_item ('SUB' );
189
m_operation.cur_item_(0 );
191
add_ctrl(@m_operation );
193
m_operation.no_transform;
195
m_polygons.add_item ('Two Simple Paths' );
196
m_polygons.add_item ('Closed Stroke' );
197
m_polygons.add_item ('Great Britain and Arrows' );
198
m_polygons.add_item ('Great Britain and Spiral' );
199
m_polygons.add_item ('Spiral and Glyph' );
200
m_polygons.cur_item_(3 );
202
add_ctrl(@m_polygons );
204
m_polygons.no_transform;
209
destructor the_application.Destruct;
214
m_operation.Destruct;
216
m_alpha_mask_rbuf.Destruct;
221
agg_freemem(pointer(m_alpha_buf ) ,m_alpha_aloc );
226
procedure the_application.draw_text;
230
ren : renderer_scanline_aa_solid;
233
txt_stroke : conv_stroke;
238
pixfmt_bgr24(pf ,rbuf_window );
245
txt_stroke.Construct(@txt );
246
txt_stroke.width_ (1.5 );
247
txt_stroke.line_cap_(round_cap );
250
txt.start_point_(x ,y );
251
txt.text_ (PChar(str ) );
253
m_ras.add_path (@txt_stroke);
254
rgba.ConstrDbl (0.0 ,0.0 ,0.0 );
256
render_scanlines(@m_ras ,@m_sl ,@ren );
263
{ GENERATE_ALPHA_MASK }
264
procedure the_application.generate_alpha_mask;
268
pixf : pixel_formats;
272
ren : renderer_scanline_aa_solid;
275
buf : array[0..99 ] of char;
281
agg_freemem(pointer(m_alpha_buf ) ,m_alpha_aloc );
283
m_alpha_aloc:=cx * cy;
285
agg_getmem(pointer(m_alpha_buf ) ,m_alpha_aloc );
287
m_alpha_mask_rbuf.attach(m_alpha_buf ,cx ,cy ,cx );
289
pixfmt_gray8(pixf ,@m_alpha_mask_rbuf );
291
rb.Construct (@pixf );
296
if m_operation._cur_item = 0 then
301
gray.ConstrInt(255 );
307
gray.ConstrInt(255 );
315
m_ras.add_path (vs );
316
render_scanlines(@m_ras ,@m_sl ,@ren );
320
sprintf (@buf[0 ] ,'Generate AlphaMask: %.3fms' ,t1 );
321
draw_text(250 ,20 ,@buf[0 ] );
325
{ PERFORM_RENDERING }
326
procedure the_application.perform_rendering;
328
pixf : pixel_formats;
329
pixfa : pixfmt_amask_adaptor;
330
rbase : renderer_base;
332
ren : renderer_scanline_aa_solid;
336
buf : array[0..99 ] of char;
339
pixfmt_bgr24(pixf ,rbuf_window );
341
pixfa.Construct(@pixf ,@m_alpha_mask );
342
rbase.Construct(@pixfa );
343
ren.Construct (@rbase );
345
rgba.ConstrDbl(0.5 ,0.0 ,0 ,0.5 );
350
m_ras.add_path (vs );
351
render_scanlines(@m_ras ,@m_sl ,@ren );
355
sprintf (@buf[0 ] ,'Render with AlphaMask: %.3fms' ,t1 );
356
draw_text(250 ,5 ,@buf[0 ] );
363
function the_application.render;
368
ren : renderer_scanline_aa_solid;
374
glyph : path_storage;
382
tat : trans_affine_translation;
383
tas : trans_affine_scaling;
386
stroke_gb_poly : conv_stroke;
390
trans_arrows : conv_transform;
397
pixfmt_bgr24(pf ,rbuf_window );
402
case m_polygons._cur_item of
403
0 : // Two simple paths
408
x:=m_x - _initial_width / 2 + 100;
409
y:=m_y - _initial_height / 2 + 100;
411
ps1.move_to(x + 140 ,y + 145 );
412
ps1.line_to(x + 225 ,y + 44 );
413
ps1.line_to(x + 296 ,y + 219 );
416
ps1.line_to(x + 226 ,y + 289 );
417
ps1.line_to(x + 82 ,y + 292 );
419
ps1.move_to(x + 220 ,y + 222 );
420
ps1.line_to(x + 363 ,y + 249 );
421
ps1.line_to(x + 265 ,y + 331 );
423
ps1.move_to(x + 242 ,y + 243 );
424
ps1.line_to(x + 268 ,y + 309 );
425
ps1.line_to(x + 325 ,y + 261 );
427
ps1.move_to(x + 259 ,y + 259 );
428
ps1.line_to(x + 273 ,y + 288 );
429
ps1.line_to(x + 298 ,y + 266 );
431
ps2.move_to(100 + 32 ,100 + 77 );
432
ps2.line_to(100 + 473 ,100 + 263 );
433
ps2.line_to(100 + 351 ,100 + 290 );
434
ps2.line_to(100 + 354 ,100 + 374 );
437
m_ras.add_path (@ps1 );
438
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
440
render_scanlines(@m_ras ,@m_sl ,@ren );
443
m_ras.add_path (@ps2 );
444
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
446
render_scanlines(@m_ras ,@m_sl ,@ren );
448
generate_alpha_mask(@ps1 );
449
perform_rendering (@ps2 );
460
stroke.Construct(@ps2 );
461
stroke.width_ (10.0 );
463
x:=m_x - _initial_width / 2 + 100;
464
y:=m_y - _initial_height / 2 + 100;
466
ps1.move_to(x + 140 ,y + 145 );
467
ps1.line_to(x + 225 ,y + 44 );
468
ps1.line_to(x + 296 ,y + 219 );
471
ps1.line_to(x + 226 ,y + 289 );
472
ps1.line_to(x + 82 ,y + 292 );
474
ps1.move_to(x + 220 - 50 ,y + 222 );
475
ps1.line_to(x + 265 - 50 ,y + 331 );
476
ps1.line_to(x + 363 - 50 ,y + 249 );
477
ps1.close_polygon(path_flags_ccw );
479
ps2.move_to(100 + 32 ,100 + 77 );
480
ps2.line_to(100 + 473 ,100 + 263 );
481
ps2.line_to(100 + 351 ,100 + 290 );
482
ps2.line_to(100 + 354 ,100 + 374 );
486
m_ras.add_path (@ps1 );
487
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
489
render_scanlines(@m_ras ,@m_sl ,@ren );
492
m_ras.add_path (@stroke );
493
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
495
render_scanlines(@m_ras ,@m_sl ,@ren );
497
generate_alpha_mask(@ps1 );
498
perform_rendering (@stroke );
506
2 : // Great Britain and Arrows
511
make_gb_poly(@gb_poly );
512
make_arrows (@arrows );
516
tat.Construct(-1150 ,-1150 );
518
mtx1.multiply(@tat );
519
mtx1.multiply(@tas );
523
tat.Construct(m_x - _initial_width / 2 ,m_y - _initial_height / 2 );
525
mtx2.multiply(@tat );
527
trans_gb_poly.Construct(@gb_poly ,@mtx1 );
528
trans_arrows.Construct (@arrows ,@mtx2 );
530
m_ras.add_path (@trans_gb_poly );
531
rgba.ConstrDbl (0.5 ,0.5 ,0 ,0.1 );
533
render_scanlines(@m_ras ,@m_sl ,@ren );
535
stroke_gb_poly.Construct(@trans_gb_poly );
536
stroke_gb_poly.width_ (0.1);
537
m_ras.add_path (@stroke_gb_poly );
538
rgba.ConstrDbl (0 ,0 ,0 );
540
render_scanlines (@m_ras ,@m_sl ,@ren );
542
m_ras.add_path (@trans_arrows );
543
rgba.ConstrDbl (0.0 ,0.5 ,0.5 ,0.1 );
545
render_scanlines(@m_ras ,@m_sl ,@ren );
547
generate_alpha_mask(@trans_gb_poly );
548
perform_rendering (@trans_arrows );
552
stroke_gb_poly.Destruct;
556
3 : // Great Britain and a Spiral
558
sp.Construct (m_x ,m_y ,10 ,150 ,30 ,0.0 );
559
stroke.Construct(@sp );
560
stroke.width_ (15.0 );
563
make_gb_poly(@gb_poly );
566
tat.Construct(-1150 ,-1150 );
571
trans_gb_poly.Construct(@gb_poly ,@mtx );
573
m_ras.add_path (@trans_gb_poly );
574
rgba.ConstrDbl (0.5 ,0.5 ,0 ,0.1 );
576
render_scanlines(@m_ras ,@m_sl ,@ren );
578
stroke_gb_poly.Construct(@trans_gb_poly );
579
stroke_gb_poly.width_ (0.1 );
580
m_ras.add_path (@stroke_gb_poly );
581
rgba.ConstrDbl (0 ,0 ,0 );
583
render_scanlines (@m_ras ,@m_sl ,@ren );
585
m_ras.add_path (@stroke );
586
rgba.ConstrDbl (0.0 ,0.5 ,0.5 ,0.1 );
588
render_scanlines(@m_ras ,@m_sl ,@ren );
590
generate_alpha_mask(@trans_gb_poly );
591
perform_rendering (@stroke );
595
stroke_gb_poly.Destruct;
599
4 : // Spiral and glyph
601
sp.Construct (m_x ,m_y ,10 ,150 ,30 ,0.0 );
602
stroke.Construct(@sp );
603
stroke.width_ (15.0 );
606
glyph.move_to(28.47 ,6.45 );
607
glyph.curve3 (21.58 ,1.12 ,19.82 ,0.29 );
608
glyph.curve3 (17.19 ,-0.93 ,14.21 ,-0.93 );
609
glyph.curve3 (9.57 ,-0.93 ,6.57 ,2.25 );
610
glyph.curve3 (3.56 ,5.42 ,3.56 ,10.60 );
611
glyph.curve3 (3.56 ,13.87 ,5.03 ,16.26 );
612
glyph.curve3 (7.03 ,19.58 ,11.99 ,22.51 );
613
glyph.curve3 (16.94 ,25.44 ,28.47 ,29.64 );
614
glyph.line_to(28.47 ,31.40 );
615
glyph.curve3 (28.47 ,38.09 ,26.34 ,40.58 );
616
glyph.curve3 (24.22 ,43.07 ,20.17 ,43.07 );
617
glyph.curve3 (17.09 ,43.07 ,15.28 ,41.41 );
618
glyph.curve3 (13.43 ,39.75 ,13.43 ,37.60 );
619
glyph.line_to(13.53 ,34.77 );
620
glyph.curve3 (13.53 ,32.52 ,12.38 ,31.30 );
621
glyph.curve3 (11.23 ,30.08 ,9.38 ,30.08 );
622
glyph.curve3 (7.57 ,30.08 ,6.42 ,31.35 );
623
glyph.curve3 (5.27 ,32.62 ,5.27 ,34.81 );
624
glyph.curve3 (5.27 ,39.01 ,9.57 ,42.53 );
625
glyph.curve3 (13.87 ,46.04 ,21.63 ,46.04 );
626
glyph.curve3 (27.59 ,46.04 ,31.40 ,44.04 );
627
glyph.curve3 (34.28 ,42.53 ,35.64 ,39.31 );
628
glyph.curve3 (36.52 ,37.21 ,36.52 ,30.71 );
629
glyph.line_to(36.52 ,15.53 );
630
glyph.curve3 (36.52 ,9.13 ,36.77 ,7.69 );
631
glyph.curve3 (37.01 ,6.25 ,37.57 ,5.76 );
632
glyph.curve3 (38.13 ,5.27 ,38.87 ,5.27 );
633
glyph.curve3 (39.65 ,5.27 ,40.23 ,5.62 );
634
glyph.curve3 (41.26 ,6.25 ,44.19 ,9.18 );
635
glyph.line_to(44.19 ,6.45 );
636
glyph.curve3 (38.72 ,-0.88 ,33.74 ,-0.88 );
637
glyph.curve3 (31.35 ,-0.88 ,29.93 ,0.78 );
638
glyph.curve3 (28.52 ,2.44 ,28.47 ,6.45 );
641
glyph.move_to(28.47 ,9.62 );
642
glyph.line_to(28.47 ,26.66 );
643
glyph.curve3 (21.09 ,23.73 ,18.95 ,22.51 );
644
glyph.curve3 (15.09 ,20.36 ,13.43 ,18.02 );
645
glyph.curve3 (11.77 ,15.67 ,11.77 ,12.89 );
646
glyph.curve3 (11.77 ,9.38 ,13.87 ,7.06 );
647
glyph.curve3 (15.97 ,4.74 ,18.70 ,4.74 );
648
glyph.curve3 (22.41 ,4.74 ,28.47 ,9.62 );
653
tat.Construct(220 ,200 );
657
trans.Construct(@glyph ,@mtx );
658
curve.Construct(@trans );
661
m_ras.add_path (@stroke );
662
rgba.ConstrDbl (0 ,0 ,0 ,0.1 );
664
render_scanlines(@m_ras ,@m_sl ,@ren );
667
m_ras.add_path (@curve );
668
rgba.ConstrDbl (0 ,0.6 ,0 ,0.1 );
670
render_scanlines(@m_ras ,@m_sl ,@ren );
672
generate_alpha_mask(@stroke );
673
perform_rendering (@curve );
686
procedure the_application.on_init;
694
procedure the_application.on_draw;
700
ren_base : renderer_base;
701
ren_solid : renderer_scanline_aa_solid;
704
// Initialize structures
705
pixfmt_bgr24(pf ,rbuf_window );
707
ren_base.Construct (@pf );
708
ren_solid.Construct(@ren_base );
710
rgba.ConstrDbl(1 ,1 ,1 );
711
ren_base.clear(@rgba );
716
// Render the controls
717
render_ctrl(@m_ras ,@m_sl ,@ren_solid ,@m_polygons );
718
render_ctrl(@m_ras ,@m_sl ,@ren_solid ,@m_operation );
723
procedure the_application.on_mouse_move;
725
if flags and mouse_left <> 0 then
736
{ ON_MOUSE_BUTTON_DOWN }
737
procedure the_application.on_mouse_button_down;
739
buf : array[0..99 ] of char;
742
if flags and mouse_left <> 0 then
751
if flags and mouse_right <> 0 then
753
sprintf (@buf[0 ] ,'%d ' ,x );
754
sprintf (@buf[StrLen(@buf ) ] ,'%d' ,y );
762
procedure the_application.on_key;
766
'Yet another example of alpha-masking. It simulates arbitrary polygon clipping '#13 +
767
'similar to "gpc_test". Alpha-Masking allows you to perform only the Intersection '#13 +
768
'(AND) and Difference (SUB) operations, but works much faster that conv_gpc. '#13 +
769
'Actually, there''re different complexities and different dependencies. '#13 +
770
'The performance of conv_gpc depends on the number of vertices, while'#13 +
771
'Alpha-Masking depends on the area of the rendered polygons. Still, with typical '#13 +
772
'screen resolutions, Alpha-Masking works much faster than General Polygon Clipper. '#13 +
773
'Compare the timings between "alpha_mask3" and "gpc_test"".'#13#13 +
774
'How to play with:'#13#13 +
775
'Use the left mouse button to move the upper shape around.'#13 +
776
'Use the right mouse button to display a message with current coordinates.' +
777
#13#13'Note: F2 key saves current "screenshot" file in this demo''s directory. ' );
782
app : the_application;
785
app.Construct(pix_format_bgr24 ,flip_y );
786
app.caption_ ('AGG Example. Alpha-Mask as a Polygon Clipper (F1-Help)' );
788
if app.init(640 ,520 ,window_resize ) then
b'\\ No newline at end of file'