File: pattern_fill.dpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (522 lines) | stat: -rw-r--r-- 11,275 bytes parent folder | download | duplicates (6)
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
//
// 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
 pattern_fill ;

{$DEFINE AGG_BGR24 }

uses
 agg_basics ,
 agg_platform_support ,
 agg_pixfmt_rgba ,

 agg_ctrl ,
 agg_rbox_ctrl ,
 agg_cbox_ctrl ,
 agg_slider_ctrl ,

 agg_rasterizer_scanline_aa ,
 agg_scanline ,
 agg_scanline_p ,

 agg_rendering_buffer ,
 agg_renderer_base ,
 agg_renderer_scanline ,
 agg_render_scanlines ,

 agg_path_storage ,
 agg_trans_affine ,
 agg_conv_stroke ,
 agg_conv_transform ,
 agg_conv_smooth_poly1 ,
 agg_span_allocator ,
 agg_span_pattern ,
 agg_span_pattern_rgba

{$I pixel_formats.inc }
{$I agg_mode.inc }
{$Q- }
{$R- }
const
 flip_y = true;

type
 the_application = object(platform_support )
   m_polygon_angle ,
   m_polygon_scale ,
   m_pattern_angle ,
   m_pattern_size  ,
   m_pattern_alpha : slider_ctrl;

   m_rotate_polygon ,
   m_rotate_pattern ,
   m_tie_pattern    : cbox_ctrl;

   m_polygon_cx ,
   m_polygon_cy ,

   m_dx ,
   m_dy : double;

   m_flag : int;

   m_pattern : int8u_ptr;
   m_pt_aloc : unsigned;

   m_pattern_rbuf : rendering_buffer;

   m_ras : rasterizer_scanline_aa;
   m_sl  : scanline_p8;
   m_ps  : path_storage;

   constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
   destructor  Destruct;

   procedure create_star(xc ,yc ,r1 ,r2 : double; n : unsigned; start_angle : double = 2.0 );
   procedure generate_pattern;

   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_mouse_button_up  (x ,y : int; flags : unsigned ); virtual;

   procedure on_ctrl_change; virtual;
   procedure on_idle; virtual;

   procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;

  end;

{ CONSTRUCT }
constructor the_application.Construct;
begin
 inherited Construct(format_ ,flip_y_ );

 m_polygon_angle.Construct (5 ,5 ,145 ,12 ,not flip_y_ );
 m_polygon_scale.Construct (5 ,5 + 14 ,145 ,12 + 14 ,not flip_y_ );
 m_pattern_angle.Construct (155 ,5 ,300 ,12 ,not flip_y_ );
 m_pattern_size.Construct  (155 ,5 + 14 ,300 ,12 + 14 ,not flip_y_ );
 m_pattern_alpha.Construct (310 ,5 ,460 ,12 ,not flip_y_ );
 m_rotate_polygon.Construct(5 ,5 + 14 + 14 ,'Rotate Polygon' ,not flip_y_ );
 m_rotate_pattern.Construct(5 ,5 + 14 + 14 + 14 ,'Rotate Pattern' ,not flip_y_ );
 m_tie_pattern.Construct   (155 ,5 + 14 + 14 ,'Tie pattern to polygon' ,not flip_y_ );

 m_pattern_rbuf.Construct;

 m_ras.Construct;
 m_sl.Construct;
 m_ps.Construct;

 m_flag   :=0;
 m_pattern:=NIL;
 m_pt_aloc:=0;

 add_ctrl(@m_polygon_angle );
 add_ctrl(@m_polygon_scale );
 add_ctrl(@m_pattern_angle );
 add_ctrl(@m_pattern_size );
 add_ctrl(@m_pattern_alpha );
 add_ctrl(@m_rotate_polygon );
 add_ctrl(@m_rotate_pattern );
 add_ctrl(@m_tie_pattern );

 m_polygon_angle.label_('Polygon Angle=%3.2f' );
 m_polygon_angle.range_(-180.0 ,180.0 );

 m_polygon_scale.label_('Polygon Scale=%3.2f' );
 m_polygon_scale.range_(0.1 ,5.0 );
 m_polygon_scale.value_(1.0 );

 m_pattern_angle.label_('Pattern Angle=%3.2f' );
 m_pattern_angle.range_(-180.0 ,180.0 );

 m_pattern_size.label_('Pattern Size=%3.2f' );
 m_pattern_size.range_(10 ,40 );
 m_pattern_size.value_(30 );

 m_pattern_alpha.label_('Background Alpha=%.2f' );
 m_pattern_alpha.value_(0.1 );

end;

{ DESTRUCT }
destructor the_application.Destruct;
begin
 inherited Destruct;

 m_polygon_angle.Destruct;
 m_polygon_scale.Destruct;
 m_pattern_angle.Destruct;
 m_pattern_size.Destruct;
 m_pattern_alpha.Destruct;
 m_rotate_polygon.Destruct;
 m_rotate_pattern.Destruct;
 m_tie_pattern.Destruct;

 m_pattern_rbuf.Destruct;

 m_ras.Destruct;
 m_sl.Destruct;
 m_ps.Destruct;

 agg_freemem(pointer(m_pattern ) ,m_pt_aloc );

end;

{ CREATE_STAR }
procedure the_application.create_star;
var
 i : unsigned;

 a ,dx ,dy : double;

begin
 m_ps.remove_all;

 start_angle:=start_angle * pi / 180.0;

 i:=0;

 while i < n do
  begin
   a :=pi * 2.0 * i / n - pi / 2.0;
   dx:=Cos(a + start_angle );
   dy:=Sin(a + start_angle );

   if i and 1 <> 0 then
    m_ps.line_to(xc + dx * r1 ,yc + dy * r1 )
   else
    if i <> 0 then
     m_ps.line_to(xc + dx * r2 ,yc + dy * r2 )
    else
     m_ps.move_to(xc + dx * r2 ,yc + dy * r2 );

   inc(i );  

  end;

 m_ps.close_polygon;

end;

{ GENERATE_PATTERN }
procedure the_application.generate_pattern;
var
 size : unsigned;
 pixf : pixel_formats;
 rgba : aggclr;

 smooth : conv_smooth_poly1_curve;
 stroke : conv_stroke;

 rb : renderer_base;
 rs : renderer_scanline_aa_solid;

begin
 size:=trunc(m_pattern_size._value );

 create_star(
  m_pattern_size._value / 2.0 ,
  m_pattern_size._value / 2.0 ,
  m_pattern_size._value / 2.5 ,
  m_pattern_size._value / 6.0 ,
  6 ,
  m_pattern_angle._value );

 smooth.Construct(@m_ps );
 stroke.Construct(@smooth );

 smooth.smooth_value_       (0.0 );
 smooth.approximation_scale_(4.0 );

 stroke.width_(m_pattern_size._value / 15.0);

 agg_freemem(pointer(m_pattern ) ,m_pt_aloc );

 m_pt_aloc:=size * size * 4 * sizeof(int8u );

 agg_getmem(pointer(m_pattern ) ,m_pt_aloc );

 m_pattern_rbuf.attach(m_pattern ,size ,size ,size * 4 );

 pixfmt_rgba32(pixf ,@m_pattern_rbuf );
 rb.Construct (@pixf );
 rs.Construct (@rb );

 rgba.ConstrDbl(0.4 ,0.0 ,0.1 ,m_pattern_alpha._value ); // Pattern background color
 rb.clear      (@rgba );

 m_ras.add_path  (@smooth );
 rgba.ConstrInt  (110 ,130 ,50 );
 rs.color_       (@rgba );
 render_scanlines(@m_ras ,@m_sl ,@rs );

 m_ras.add_path  (@stroke );
 rgba.ConstrInt  (0 ,50 ,80 );
 rs.color_       (@rgba );
 render_scanlines(@m_ras ,@m_sl ,@rs );

 smooth.Destruct;
 stroke.Destruct;

end;

{ ON_INIT }
procedure the_application.on_init;
begin
 m_polygon_cx:=_initial_width / 2.0;
 m_polygon_cy:=_initial_height / 2.0;

 generate_pattern;

end;

{ ON_DRAW }
procedure the_application.on_draw;
var
 width_ ,height_ : double;

 pixf : pixel_formats;
 rgba : aggclr;

 rb : renderer_base;
 rs : renderer_scanline_aa_solid;

 polygon_mtx : trans_affine;

 tat : trans_affine_translation;
 tar : trans_affine_rotation;
 tas : trans_affine_scaling;

 r : double;

 offset_x ,offset_y : unsigned;

 tr : conv_transform;
 wx ,
 wy : wrap_mode_reflect_auto_pow2;
 sa : span_allocator;
 sg : span_pattern_rgba;
 rp : renderer_scanline_aa;

begin
// Initialize structures
 width_ :=rbuf_window._width;
 height_:=rbuf_window._height;

 pixfmt(pixf ,rbuf_window );

 rb.Construct(@pixf );
 rs.Construct(@rb );

 rgba.ConstrDbl(1.0 ,1.0 ,1.0 );
 rb.clear      (@rgba );

// Render
 polygon_mtx.Construct;

 tat.Construct(-m_polygon_cx ,-m_polygon_cy );        polygon_mtx.multiply(@tat );
 tar.Construct(m_polygon_angle._value * pi / 180.0 ); polygon_mtx.multiply(@tar );
 tas.Construct(m_polygon_scale._value );              polygon_mtx.multiply(@tas );
 tat.Construct(m_polygon_cx ,m_polygon_cy );          polygon_mtx.multiply(@tat );

 r:=_initial_width / 3.0 - 8.0;

 create_star(m_polygon_cx ,m_polygon_cy ,r ,r / 1.45 ,14 );

 tr.Construct(@m_ps ,@polygon_mtx );

 offset_x:=0;
 offset_y:=0;

 if m_tie_pattern._status then
  begin
   offset_x:=trunc(width_ - m_polygon_cx );
   offset_y:=trunc(height_ - m_polygon_cy );

  end;

 sa.Construct;
 wx.Construct;
 wy.Construct;
 sg.Construct(@sa ,@m_pattern_rbuf ,offset_x ,offset_y ,@wx ,@wy ,rgba_order );
 rp.Construct(@rb ,@sg );

{ m_ras.clip_box (-1 ,0 ,width_ ,height_ );
 m_ras.move_to_d(-1 ,100 );
 m_ras.line_to_d(100 ,100 );
 m_ras.line_to_d(100 ,200 );
 m_ras.line_to_d(-1 ,200 );
 m_ras.close_polygon;{}

 m_ras.add_path  (@tr );
 rgba.ConstrDbl  (0 ,0 ,0 );
 rs.color_       (@rgba );
 render_scanlines(@m_ras ,@m_sl ,@rp );

// Render the controls
 render_ctrl(@m_ras ,@m_sl ,@rs ,@m_polygon_angle );
 render_ctrl(@m_ras ,@m_sl ,@rs ,@m_polygon_scale );
 render_ctrl(@m_ras ,@m_sl ,@rs ,@m_pattern_angle );
 render_ctrl(@m_ras ,@m_sl ,@rs ,@m_pattern_size );
 render_ctrl(@m_ras ,@m_sl ,@rs ,@m_pattern_alpha );
 render_ctrl(@m_ras ,@m_sl ,@rs ,@m_rotate_polygon );
 render_ctrl(@m_ras ,@m_sl ,@rs ,@m_rotate_pattern );
 render_ctrl(@m_ras ,@m_sl ,@rs ,@m_tie_pattern );

// Free AGG resources
 sa.Destruct;

end;

{ ON_MOUSE_MOVE }
procedure the_application.on_mouse_move;
begin
 if flags and mouse_left <> 0 then
  if m_flag <> 0 then
   begin
    m_polygon_cx:=x - m_dx;
    m_polygon_cy:=y - m_dy;

    force_redraw;

   end
  else
 else
  on_mouse_button_up(x ,y ,flags );

end;

{ ON_MOUSE_BUTTON_DOWN }
procedure the_application.on_mouse_button_down;
var
 polygon_mtx : trans_affine;

 tat : trans_affine_translation;
 tar : trans_affine_rotation;
 tas : trans_affine_scaling;

 r  : double;
 tr : conv_transform;

begin
 if flags and mouse_left <> 0 then
  begin
   polygon_mtx.Construct;

   tat.Construct(-m_polygon_cx ,-m_polygon_cy );                   polygon_mtx.multiply(@tat );
   tar.Construct(m_polygon_angle._value * pi / 180.0 );            polygon_mtx.multiply(@tar );
   tas.Construct(m_polygon_scale._value ,m_polygon_scale._value ); polygon_mtx.multiply(@tas );
   tat.Construct(m_polygon_cx ,m_polygon_cy );                     polygon_mtx.multiply(@tat );

   r:=_initial_width / 3.0 - 8.0;

   create_star(m_polygon_cx ,m_polygon_cy ,r ,r / 1.45 ,14 );

   tr.Construct  (@m_ps ,@polygon_mtx );
   m_ras.add_path(@tr );

   if m_ras.hit_test(x ,y ) then
    begin
     m_dx:=x - m_polygon_cx;
     m_dy:=y - m_polygon_cy;

     m_flag:=1;

    end;

  end;

end;

{ ON_MOUSE_BUTTON_UP }
procedure the_application.on_mouse_button_up;
begin
 m_flag:=0;

end;

{ ON_CTRL_CHANGE }
procedure the_application.on_ctrl_change;
begin
 if m_rotate_polygon._status or
    m_rotate_pattern._status then
  wait_mode_(false )
 else
  wait_mode_(true);

 generate_pattern;
 force_redraw;

end;

{ ON_IDLE }
procedure the_application.on_idle;
var
 redraw : boolean;

begin
 redraw:=false;
 if m_rotate_polygon._status then
  begin
   m_polygon_angle.value_(m_polygon_angle._value + 0.5 );

   if m_polygon_angle._value >= 180.0 then
    m_polygon_angle.value_(m_polygon_angle._value - 360.0 );

   redraw:=true;

  end;

 if m_rotate_pattern._status then
  begin
   m_pattern_angle.value_(m_pattern_angle._value - 0.5 );

   if m_pattern_angle._value <= -180.0 then
    m_pattern_angle.value_(m_pattern_angle._value + 360.0 );

   generate_pattern;

   redraw:=true;

  end;

 if redraw then
  force_redraw;

end;

{ ON_KEY }
procedure the_application.on_key;
begin
 if key = key_f1 then
  message_(
   'The example demonstrates how to use arbitrary images as fill patterns. '#13 +
   'This span generator is very simple, so, it doesn''t allow you to apply '#13 +
   'arbitrary transformations to the pattern, i.e., it cannot be used as '#13 +
   'a texturing tool. But it works pretty fast and can be useful in some applications.   '#13#13 +
   'How to play with:'#13#13 +
   'Use the left mouse button to move the polygon around.' +
   #13#13'Note: F2 key saves current "screenshot" file in this demo''s directory.  ' );

end;

VAR
 app : the_application;

BEGIN
 app.Construct(pix_format_bgr24 ,flip_y );
 app.caption_ ('AGG Example. Pattern Filling (F1-Help)' );

 if app.init(640 ,480 ,0 ) then
  app.run;

 app.Destruct;

END.