texplay 0.2.800 → 0.2.900
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +138 -119
- data/README.markdown +43 -41
- data/Rakefile +68 -68
- data/examples/common.rb +8 -8
- data/examples/example_alpha_blend.rb +31 -31
- data/examples/example_bezier.rb +42 -42
- data/examples/example_color_control.rb +69 -69
- data/examples/example_color_transform.rb +64 -67
- data/examples/example_color_transform_circle.rb +65 -0
- data/examples/example_dup.rb +75 -75
- data/examples/example_each.rb +42 -42
- data/examples/example_effect.rb +35 -35
- data/examples/example_fill.rb +44 -44
- data/examples/example_fill_old.rb +49 -49
- data/examples/example_fluent.rb +31 -31
- data/examples/example_gen_eval.rb +34 -34
- data/examples/example_hash_arguments.rb +47 -47
- data/examples/example_light.rb +77 -0
- data/examples/example_lsystem.rb +61 -61
- data/examples/example_melt.rb +27 -27
- data/examples/example_meyet.rb +64 -0
- data/examples/example_polyline.rb +43 -43
- data/examples/example_scale.rb +29 -29
- data/examples/example_simple.rb +48 -38
- data/examples/example_sync.rb +60 -60
- data/examples/example_trace.rb +1 -1
- data/examples/example_turtle.rb +40 -40
- data/examples/example_weird.rb +3 -1
- data/examples/media/Thumbs.db +0 -0
- data/ext/texplay/actions.c +999 -1001
- data/ext/texplay/actions.h +60 -60
- data/ext/texplay/bindings.c +1162 -1149
- data/ext/texplay/bindings.h +46 -46
- data/ext/texplay/cache.c +118 -118
- data/ext/texplay/cache.h +24 -24
- data/ext/texplay/compat.h +27 -27
- data/ext/texplay/extconf.rb +28 -28
- data/ext/texplay/gen_eval.c +211 -211
- data/ext/texplay/gen_eval.h +20 -20
- data/ext/texplay/graphics_utils.c +188 -63
- data/ext/texplay/graphics_utils.h +0 -1
- data/ext/texplay/object2module.c +171 -171
- data/ext/texplay/object2module.h +11 -11
- data/ext/texplay/texplay.c +169 -169
- data/ext/texplay/texplay.h +147 -130
- data/ext/texplay/utils.c +816 -752
- data/ext/texplay/utils.h +151 -145
- data/lib/texplay-contrib.rb +171 -171
- data/lib/texplay.rb +162 -137
- data/lib/texplay/version.rb +1 -1
- metadata +9 -5
data/examples/example_weird.rb
CHANGED
@@ -7,7 +7,9 @@ class W < Gosu::Window
|
|
7
7
|
def initialize
|
8
8
|
super(1024, 768, false, 20)
|
9
9
|
@img = Gosu::Image.new(self, "#{Common::MEDIA}/gob.png")
|
10
|
-
@
|
10
|
+
@sun = Gosu::Image.new(self, "#{Common::MEDIA}/sunset.png")
|
11
|
+
@img.splice @sun, 0, 0, :mode => :exclusion
|
12
|
+
# @img.rect 0, 10, 300, 300, :color => :blue, :fill => true, :mode => :softlight
|
11
13
|
end
|
12
14
|
|
13
15
|
def draw
|
Binary file
|
data/ext/texplay/actions.c
CHANGED
@@ -1,1001 +1,999 @@
|
|
1
|
-
#include "texplay.h"
|
2
|
-
#include "utils.h"
|
3
|
-
#include "graphics_utils.h"
|
4
|
-
#include "actions.h"
|
5
|
-
#include <assert.h>
|
6
|
-
#include <math.h>
|
7
|
-
#ifdef __APPLE__
|
8
|
-
# include <glut.h>
|
9
|
-
#else
|
10
|
-
# include <GL/glut.h>
|
11
|
-
#endif
|
12
|
-
|
13
|
-
|
14
|
-
/** line_do_action, bresenham's algorithm **/
|
15
|
-
typedef enum { no_mode, until_color, while_color} trace_mode_type;
|
16
|
-
|
17
|
-
static inline bool
|
18
|
-
is_trace_match(rgba c, rgba trace_color, trace_mode_type trace_mode)
|
19
|
-
{
|
20
|
-
if(trace_mode == while_color) {
|
21
|
-
if(!cmp_color(c, trace_color))
|
22
|
-
return true;
|
23
|
-
}
|
24
|
-
else if(trace_mode == until_color) {
|
25
|
-
if(cmp_color(c, trace_color))
|
26
|
-
return true;
|
27
|
-
}
|
28
|
-
|
29
|
-
return false;
|
30
|
-
}
|
31
|
-
|
32
|
-
trace_match
|
33
|
-
line_do_action(int x1, int y1, int x2, int y2, texture_info * tex, VALUE hash_arg,
|
34
|
-
sync sync_mode, bool primary, action_struct * payload)
|
35
|
-
{
|
36
|
-
int x, y, W, H, F;
|
37
|
-
int xinc, yinc;
|
38
|
-
action_struct cur;
|
39
|
-
int thickness = 1;
|
40
|
-
|
41
|
-
bool has_trace = false;
|
42
|
-
rgba trace_color;
|
43
|
-
trace_mode_type trace_mode = no_mode;
|
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
|
-
xinc = 1;
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
yinc = 1;
|
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
|
-
F += 2 * H;
|
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
|
-
F += 2 * W;
|
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
|
-
int
|
207
|
-
int
|
208
|
-
int
|
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
|
-
cur.
|
297
|
-
cur.
|
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
|
-
hash_arg
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
cur.
|
356
|
-
cur.
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
line_do_action(x1,
|
364
|
-
line_do_action(
|
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
|
-
hash_arg
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
set_pixel_color_with_style(payload, tex, x1
|
413
|
-
set_pixel_color_with_style(payload, tex, x1
|
414
|
-
set_pixel_color_with_style(payload, tex, x1
|
415
|
-
set_pixel_color_with_style(payload, tex, x1
|
416
|
-
set_pixel_color_with_style(payload, tex, x1
|
417
|
-
set_pixel_color_with_style(payload, tex, x1
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
line_do_action(x1 -
|
434
|
-
line_do_action(x1 -
|
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
|
-
#define
|
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
|
-
old_color
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
y1
|
710
|
-
|
711
|
-
y1
|
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
|
-
xy_index
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
float
|
785
|
-
|
786
|
-
|
787
|
-
int
|
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
|
-
set_array_value(ary,
|
884
|
-
set_array_value(ary,
|
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
|
-
pix
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
/**
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
bool
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
payload->color =
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1
|
+
#include "texplay.h"
|
2
|
+
#include "utils.h"
|
3
|
+
#include "graphics_utils.h"
|
4
|
+
#include "actions.h"
|
5
|
+
#include <assert.h>
|
6
|
+
#include <math.h>
|
7
|
+
#ifdef __APPLE__
|
8
|
+
# include <glut.h>
|
9
|
+
#else
|
10
|
+
# include <GL/glut.h>
|
11
|
+
#endif
|
12
|
+
|
13
|
+
|
14
|
+
/** line_do_action, bresenham's algorithm **/
|
15
|
+
typedef enum { no_mode, until_color, while_color} trace_mode_type;
|
16
|
+
|
17
|
+
static inline bool
|
18
|
+
is_trace_match(rgba c, rgba trace_color, trace_mode_type trace_mode)
|
19
|
+
{
|
20
|
+
if(trace_mode == while_color) {
|
21
|
+
if(!cmp_color(c, trace_color))
|
22
|
+
return true;
|
23
|
+
}
|
24
|
+
else if(trace_mode == until_color) {
|
25
|
+
if(cmp_color(c, trace_color))
|
26
|
+
return true;
|
27
|
+
}
|
28
|
+
|
29
|
+
return false;
|
30
|
+
}
|
31
|
+
|
32
|
+
trace_match
|
33
|
+
line_do_action(int x1, int y1, int x2, int y2, texture_info * tex, VALUE hash_arg,
|
34
|
+
sync sync_mode, bool primary, action_struct * payload)
|
35
|
+
{
|
36
|
+
int x, y, W, H, F;
|
37
|
+
int xinc, yinc;
|
38
|
+
action_struct cur;
|
39
|
+
int thickness = 1;
|
40
|
+
|
41
|
+
bool has_trace = false;
|
42
|
+
rgba trace_color;
|
43
|
+
trace_mode_type trace_mode = no_mode;
|
44
|
+
|
45
|
+
if(has_optional_hash_arg(hash_arg, "thickness"))
|
46
|
+
thickness = NUM2INT(get_from_hash(hash_arg, "thickness"));
|
47
|
+
|
48
|
+
if(has_optional_hash_arg(hash_arg, "trace") && primary) {
|
49
|
+
VALUE trace_hash = get_from_hash(hash_arg, "trace");
|
50
|
+
|
51
|
+
/* we're tracing (not drawing) */
|
52
|
+
has_trace = true;
|
53
|
+
|
54
|
+
/* since we're not drawing, no need to sync */
|
55
|
+
sync_mode = no_sync;
|
56
|
+
|
57
|
+
if(has_optional_hash_arg(trace_hash, "until_color")) {
|
58
|
+
VALUE c = get_from_hash(trace_hash, "until_color");
|
59
|
+
trace_color = convert_rb_color_to_rgba(c);
|
60
|
+
trace_mode = until_color;
|
61
|
+
}
|
62
|
+
else if(has_optional_hash_arg(trace_hash, "while_color")) {
|
63
|
+
VALUE c = get_from_hash(trace_hash, "while_color");
|
64
|
+
trace_color = convert_rb_color_to_rgba(c);
|
65
|
+
trace_mode = while_color;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
draw_prologue(&cur, tex, x1, y1,
|
70
|
+
x2, y2, &hash_arg, sync_mode, primary, &payload);
|
71
|
+
|
72
|
+
|
73
|
+
/* clip the line */
|
74
|
+
cohen_sutherland_clip(&x1, &y1, &x2, &y2, 0, 0, tex->width - 1, tex->height - 1);
|
75
|
+
|
76
|
+
W = ABS(x2 - x1);
|
77
|
+
H = ABS(y2 - y1);
|
78
|
+
|
79
|
+
if(x1 < x2)
|
80
|
+
xinc = 1;
|
81
|
+
else
|
82
|
+
xinc = -1;
|
83
|
+
|
84
|
+
if(y1 < y2)
|
85
|
+
yinc = 1;
|
86
|
+
else
|
87
|
+
yinc = -1;
|
88
|
+
|
89
|
+
x = x1;
|
90
|
+
y = y1;
|
91
|
+
|
92
|
+
if(W >= H) {
|
93
|
+
F = 2 * H - W;
|
94
|
+
while(x != x2) {
|
95
|
+
if(thickness <= 1) {
|
96
|
+
if(!has_trace)
|
97
|
+
set_pixel_color_with_style(payload, tex, x, y);
|
98
|
+
else {
|
99
|
+
rgba c = get_pixel_color(tex, x, y);
|
100
|
+
|
101
|
+
if (is_trace_match(c, trace_color, trace_mode))
|
102
|
+
return (trace_match) { x, y, c };
|
103
|
+
}
|
104
|
+
}
|
105
|
+
else {
|
106
|
+
set_hash_value(hash_arg, "fill", Qtrue);
|
107
|
+
circle_do_action(x, y, thickness / 2, tex, hash_arg, no_sync, false, payload);
|
108
|
+
}
|
109
|
+
|
110
|
+
if(F < 0)
|
111
|
+
F += 2 * H;
|
112
|
+
else {
|
113
|
+
F += 2 * (H - W);
|
114
|
+
y += yinc;
|
115
|
+
}
|
116
|
+
x += xinc;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
else {
|
120
|
+
F = 2 * W - H;
|
121
|
+
while(y != y2 ) {
|
122
|
+
|
123
|
+
if(thickness <= 1) {
|
124
|
+
if(!has_trace)
|
125
|
+
set_pixel_color_with_style(payload, tex, x, y);
|
126
|
+
else {
|
127
|
+
rgba c = get_pixel_color(tex, x, y);
|
128
|
+
|
129
|
+
if (is_trace_match(c, trace_color, trace_mode))
|
130
|
+
return (trace_match) { x, y, c };
|
131
|
+
}
|
132
|
+
}
|
133
|
+
else {
|
134
|
+
set_hash_value(hash_arg, "fill", Qtrue);
|
135
|
+
circle_do_action(x, y, thickness / 2, tex, hash_arg, no_sync, false, payload);
|
136
|
+
}
|
137
|
+
|
138
|
+
if(F < 0)
|
139
|
+
F += 2 * W;
|
140
|
+
else {
|
141
|
+
F += 2 * (W - H);
|
142
|
+
x += xinc;
|
143
|
+
}
|
144
|
+
y += yinc;
|
145
|
+
}
|
146
|
+
}
|
147
|
+
|
148
|
+
if(thickness <= 1) {
|
149
|
+
if(!has_trace)
|
150
|
+
set_pixel_color_with_style(payload, tex, x2, y2);
|
151
|
+
else {
|
152
|
+
rgba c = get_pixel_color(tex, x, y);
|
153
|
+
|
154
|
+
if (is_trace_match(c, trace_color, trace_mode))
|
155
|
+
return (trace_match) { x, y, c };
|
156
|
+
}
|
157
|
+
}
|
158
|
+
else {
|
159
|
+
set_hash_value(hash_arg, "fill", Qtrue);
|
160
|
+
circle_do_action(x2, y2, thickness / 2, tex, hash_arg, no_sync, false, payload);
|
161
|
+
|
162
|
+
}
|
163
|
+
draw_epilogue(&cur, tex, primary);
|
164
|
+
|
165
|
+
return (trace_match) { .x = -1, .y = -1 };
|
166
|
+
}
|
167
|
+
/** end line **/
|
168
|
+
|
169
|
+
/** polyline algorithm **/
|
170
|
+
|
171
|
+
/* used by both polyline and bezier */
|
172
|
+
#define SIMPLE_FORMAT 0
|
173
|
+
#define POINT_FORMAT 1
|
174
|
+
|
175
|
+
/* calculate a single point */
|
176
|
+
static void
|
177
|
+
polyline_point(VALUE points, int k, int * x, int * y, int format, int draw_offset_x,
|
178
|
+
int draw_offset_y)
|
179
|
+
{
|
180
|
+
int xy_index;
|
181
|
+
|
182
|
+
switch(format) {
|
183
|
+
case POINT_FORMAT:
|
184
|
+
*x = NUM2INT(point_x(get_from_array(points, k))) + draw_offset_x;
|
185
|
+
*y = NUM2INT(point_y(get_from_array(points, k))) + draw_offset_y;
|
186
|
+
break;
|
187
|
+
|
188
|
+
case SIMPLE_FORMAT:
|
189
|
+
xy_index = k * 2;
|
190
|
+
*x = NUM2INT(get_from_array(points, xy_index)) + draw_offset_x;
|
191
|
+
*y = NUM2INT(get_from_array(points, xy_index + 1)) + draw_offset_y;
|
192
|
+
|
193
|
+
break;
|
194
|
+
default:
|
195
|
+
rb_raise(rb_eArgError, "pixel format must be either POINT_FORMAT or SIMPLE_FORMAT");
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
void
|
200
|
+
polyline_do_action(VALUE points, texture_info * tex, VALUE hash_arg,
|
201
|
+
sync sync_mode, bool primary, action_struct * payload)
|
202
|
+
{
|
203
|
+
|
204
|
+
int x1, y1, x2, y2;
|
205
|
+
int format;
|
206
|
+
int num_point_pairs;
|
207
|
+
int k;
|
208
|
+
int draw_offset_y, draw_offset_x;
|
209
|
+
action_struct cur;
|
210
|
+
VALUE offset_val;
|
211
|
+
bool closed = false;
|
212
|
+
|
213
|
+
draw_prologue(&cur, tex, XMAX_OOB, YMAX_OOB, XMIN_OOB, YMIN_OOB, &hash_arg, sync_mode, primary, &payload);
|
214
|
+
|
215
|
+
/* calculate offset */
|
216
|
+
offset_val = get_image_local(tex->image, DRAW_OFFSET);
|
217
|
+
|
218
|
+
draw_offset_x = NUM2INT(get_from_array(offset_val, 0));
|
219
|
+
draw_offset_y = NUM2INT(get_from_array(offset_val, 1));
|
220
|
+
|
221
|
+
/* if the polyline is 'closed' make the last point the first */
|
222
|
+
if(is_a_hash(hash_arg))
|
223
|
+
if(RTEST(get_from_hash(hash_arg, "closed")) || RTEST(get_from_hash(hash_arg, "close"))) {
|
224
|
+
|
225
|
+
/* so that our additional point is not persistent */
|
226
|
+
points = rb_obj_dup(points);
|
227
|
+
closed = true;
|
228
|
+
}
|
229
|
+
/* determine format of points */
|
230
|
+
if(is_a_point(get_from_array(points, 0))) {
|
231
|
+
format = POINT_FORMAT;
|
232
|
+
|
233
|
+
/* if the polyline is closed to form a polygon then make the last point and first point identical */
|
234
|
+
if(closed)
|
235
|
+
rb_ary_push(points, get_from_array(points, 0));
|
236
|
+
|
237
|
+
num_point_pairs = RARRAY_LEN(points);
|
238
|
+
}
|
239
|
+
else {
|
240
|
+
format = SIMPLE_FORMAT;
|
241
|
+
|
242
|
+
/* ensure there is an 'x' for every 'y' */
|
243
|
+
if(RARRAY_LEN(points) % 2)
|
244
|
+
rb_raise(rb_eArgError, "polyline needs an even number of points. got %d\n",
|
245
|
+
(int)RARRAY_LEN(points));
|
246
|
+
|
247
|
+
if(closed) {
|
248
|
+
rb_ary_push(points, get_from_array(points, 0));
|
249
|
+
rb_ary_push(points, get_from_array(points, 1));
|
250
|
+
}
|
251
|
+
|
252
|
+
num_point_pairs = RARRAY_LEN(points) / 2;
|
253
|
+
}
|
254
|
+
|
255
|
+
/* calculate first point */
|
256
|
+
polyline_point(points, 0, &x1, &y1, format, draw_offset_x, draw_offset_y);
|
257
|
+
|
258
|
+
/* calc the points and draw the polyline */
|
259
|
+
for(k = 1; k < num_point_pairs; k++) {
|
260
|
+
|
261
|
+
polyline_point(points, k, &x2, &y2, format, draw_offset_x, draw_offset_y);
|
262
|
+
|
263
|
+
line_do_action(x1, y1, x2, y2, tex, hash_arg, no_sync, false, payload);
|
264
|
+
|
265
|
+
/* update drawing rectangle */
|
266
|
+
update_bounds(payload, x1, y1, x2, y2);
|
267
|
+
|
268
|
+
x1 = x2; y1 = y2;
|
269
|
+
}
|
270
|
+
|
271
|
+
draw_epilogue(&cur, tex, primary);
|
272
|
+
}
|
273
|
+
/** end polyline **/
|
274
|
+
|
275
|
+
/* regular polygon algorithm */
|
276
|
+
void
|
277
|
+
ngon_do_action(int x, int y, int r, int num_sides, texture_info * tex, VALUE hash_arg,
|
278
|
+
sync sync_mode, bool primary, action_struct * payload)
|
279
|
+
{
|
280
|
+
action_struct cur;
|
281
|
+
int x1, y1, x2, y2, x0, y0;
|
282
|
+
int thickness;
|
283
|
+
float angle = 0;
|
284
|
+
|
285
|
+
draw_prologue(&cur, tex, x - r, y - r,
|
286
|
+
x + r, y + r, &hash_arg, sync_mode, primary, &payload);
|
287
|
+
|
288
|
+
|
289
|
+
if(is_a_hash(hash_arg)) {
|
290
|
+
if(RTEST(get_from_hash(hash_arg, "thickness"))) {
|
291
|
+
thickness = NUM2INT(get_from_hash(hash_arg, "thickness"));
|
292
|
+
|
293
|
+
/* TO DO: find a better way of doing this */
|
294
|
+
cur.xmin = x - r - thickness / 2;
|
295
|
+
cur.ymin = y - r - thickness / 2;
|
296
|
+
cur.xmax = x + r + thickness / 2;
|
297
|
+
cur.ymax = y + r + thickness / 2;
|
298
|
+
}
|
299
|
+
|
300
|
+
if(RTEST(get_from_hash(hash_arg, "start_angle"))) {
|
301
|
+
angle = NUM2INT(get_from_hash(hash_arg, "start_angle")) / 360.0 * 2 * PI;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
|
305
|
+
/* calculate first point */
|
306
|
+
x0 = x1 = x + r * cos(angle);
|
307
|
+
y0 = y1 = y + r * sin(angle);
|
308
|
+
|
309
|
+
for(int n = 0; n < num_sides; n++) {
|
310
|
+
x2 = x + r * cos((2 * PI / num_sides) * n + angle);
|
311
|
+
y2 = y + r * sin((2 * PI / num_sides) * n + angle);
|
312
|
+
|
313
|
+
line_do_action(x1, y1, x2, y2, tex, hash_arg, no_sync, false, payload);
|
314
|
+
|
315
|
+
x1 = x2; y1 = y2;
|
316
|
+
}
|
317
|
+
|
318
|
+
line_do_action(x0, y0, x1, y1, tex, hash_arg, no_sync, false, payload);
|
319
|
+
|
320
|
+
draw_epilogue(&cur, tex, primary);
|
321
|
+
}
|
322
|
+
/** end of ngon */
|
323
|
+
|
324
|
+
/** rectangle algorithm **/
|
325
|
+
void
|
326
|
+
rect_do_action(int x1, int y1, int x2, int y2, texture_info * tex, VALUE hash_arg,
|
327
|
+
sync sync_mode, bool primary, action_struct * payload)
|
328
|
+
{
|
329
|
+
action_struct cur;
|
330
|
+
bool fill = false;
|
331
|
+
int thickness = 1;
|
332
|
+
|
333
|
+
draw_prologue(&cur, tex, x1, y1,
|
334
|
+
x2, y2, &hash_arg, sync_mode, primary, &payload);
|
335
|
+
|
336
|
+
|
337
|
+
if(is_a_hash(hash_arg)) {
|
338
|
+
|
339
|
+
/* make our private copy of the hash so we can mess with it */
|
340
|
+
hash_arg = rb_obj_dup(hash_arg);
|
341
|
+
|
342
|
+
if(RTEST(get_from_hash(hash_arg, "fill")) || RTEST(get_from_hash(hash_arg, "filled"))) {
|
343
|
+
fill = true;
|
344
|
+
|
345
|
+
/* since we're filling the rect, line thickness is irrelevant */
|
346
|
+
delete_from_hash(hash_arg, "thickness");
|
347
|
+
}
|
348
|
+
else if(RTEST(get_from_hash(hash_arg, "thickness"))) {
|
349
|
+
thickness = NUM2INT(get_from_hash(hash_arg, "thickness"));
|
350
|
+
/* TO DO: find a better way of doing this */
|
351
|
+
|
352
|
+
if(thickness > 1) {
|
353
|
+
cur.xmin = x1 - thickness / 2;
|
354
|
+
cur.ymin = y1 - thickness / 2;
|
355
|
+
cur.xmax = x2 + thickness / 2 + 1;
|
356
|
+
cur.ymax = y2 + thickness / 2 + 1;
|
357
|
+
}
|
358
|
+
}
|
359
|
+
}
|
360
|
+
if(!fill) {
|
361
|
+
line_do_action(x1, y1, x2, y1, tex, hash_arg, no_sync, false, payload);
|
362
|
+
line_do_action(x1, y1, x1, y2, tex, hash_arg, no_sync, false, payload);
|
363
|
+
line_do_action(x1, y2, x2, y2, tex, hash_arg, no_sync, false, payload);
|
364
|
+
line_do_action(x2, y1, x2, y2, tex, hash_arg, no_sync, false, payload);
|
365
|
+
}
|
366
|
+
else {
|
367
|
+
if(y1 > y2) SWAP(y1, y2);
|
368
|
+
|
369
|
+
for(int y = y1; y <= y2; y++)
|
370
|
+
line_do_action(x1, y, x2, y, tex, hash_arg, no_sync, false, payload);
|
371
|
+
}
|
372
|
+
|
373
|
+
draw_epilogue(&cur, tex, primary);
|
374
|
+
}
|
375
|
+
|
376
|
+
|
377
|
+
/** midpoint circle algorithm **/
|
378
|
+
void
|
379
|
+
circle_do_action(int x1, int y1, int r, texture_info * tex, VALUE hash_arg,
|
380
|
+
sync sync_mode, bool primary, action_struct * payload)
|
381
|
+
{
|
382
|
+
|
383
|
+
int x, y;
|
384
|
+
float p;
|
385
|
+
action_struct cur;
|
386
|
+
bool fill = false;
|
387
|
+
|
388
|
+
draw_prologue(&cur, tex, x1 - r, y1 - r, x1 + r, y1 + r, &hash_arg,
|
389
|
+
sync_mode, primary, &payload);
|
390
|
+
|
391
|
+
|
392
|
+
if(is_a_hash(hash_arg)) {
|
393
|
+
|
394
|
+
/* make our private copy of the hash so we can mess with it */
|
395
|
+
hash_arg = rb_obj_dup(hash_arg);
|
396
|
+
|
397
|
+
if(RTEST(get_from_hash(hash_arg, "fill")) || RTEST(get_from_hash(hash_arg, "filled"))) {
|
398
|
+
fill = true;
|
399
|
+
|
400
|
+
/* to prevent infinite recursion set line thickness to 1 :D
|
401
|
+
NB: a filled circle uses lines and a thick line uses filled circles :D */
|
402
|
+
delete_from_hash(hash_arg, "thickness");
|
403
|
+
}
|
404
|
+
}
|
405
|
+
|
406
|
+
x = 0 ; y = r;
|
407
|
+
p = 5 / 4 - r;
|
408
|
+
if(!fill) {
|
409
|
+
while (x <= y) {
|
410
|
+
set_pixel_color_with_style(payload, tex, x1 + x, y1 + y);
|
411
|
+
set_pixel_color_with_style(payload, tex, x1 + x, y1 - y);
|
412
|
+
set_pixel_color_with_style(payload, tex, x1 - x, y1 + y);
|
413
|
+
set_pixel_color_with_style(payload, tex, x1 - x, y1 - y);
|
414
|
+
set_pixel_color_with_style(payload, tex, x1 + y, y1 + x);
|
415
|
+
set_pixel_color_with_style(payload, tex, x1 + y, y1 - x);
|
416
|
+
set_pixel_color_with_style(payload, tex, x1 - y, y1 + x);
|
417
|
+
set_pixel_color_with_style(payload, tex, x1 - y, y1 - x);
|
418
|
+
|
419
|
+
if (p < 0) {
|
420
|
+
p += 2 * x + 3;
|
421
|
+
}
|
422
|
+
else {
|
423
|
+
y--;
|
424
|
+
p += 2 * (x - y) + 5;
|
425
|
+
}
|
426
|
+
x++;
|
427
|
+
}
|
428
|
+
}
|
429
|
+
else {
|
430
|
+
while (x <= y) {
|
431
|
+
line_do_action(x1 - x, y1 + y, x1 + x, y1 + y, tex, hash_arg, no_sync, false, payload);
|
432
|
+
line_do_action(x1 - x, y1 - y, x1 + x, y1 - y, tex, hash_arg, no_sync, false, payload);
|
433
|
+
line_do_action(x1 - y, y1 + x, x1 + y, y1 + x, tex, hash_arg, no_sync, false, payload);
|
434
|
+
line_do_action(x1 - y, y1 - x, x1 + y, y1 - x, tex, hash_arg, no_sync, false, payload);
|
435
|
+
|
436
|
+
if (p < 0) {
|
437
|
+
p += 2 * x + 3;
|
438
|
+
}
|
439
|
+
else {
|
440
|
+
y--;
|
441
|
+
p += 2 * (x - y) + 5;
|
442
|
+
}
|
443
|
+
x++;
|
444
|
+
}
|
445
|
+
}
|
446
|
+
|
447
|
+
draw_epilogue(&cur, tex, primary);
|
448
|
+
}
|
449
|
+
/** end circle **/
|
450
|
+
|
451
|
+
/** set pixel algorithm **/
|
452
|
+
void
|
453
|
+
pixel_do_action(int x1, int y1, texture_info * tex, VALUE hash_arg,
|
454
|
+
sync sync_mode, bool primary, action_struct * payload)
|
455
|
+
{
|
456
|
+
action_struct cur;
|
457
|
+
|
458
|
+
draw_prologue(&cur, tex, x1, y1, x1, y1, &hash_arg, sync_mode, primary, &payload);
|
459
|
+
|
460
|
+
set_pixel_color_with_style(payload, tex, x1, y1);
|
461
|
+
|
462
|
+
draw_epilogue(&cur, tex, primary);
|
463
|
+
}
|
464
|
+
/** end set pixel **/
|
465
|
+
|
466
|
+
/*** non recursive FLOOD FILL, inspired by John R. Shaw ***/
|
467
|
+
typedef struct { int x1, x2, y, dy; } LINESEGMENT;
|
468
|
+
|
469
|
+
#define MAXDEPTH 10000
|
470
|
+
|
471
|
+
#define PUSH(XL, XR, Y, DY) \
|
472
|
+
if( sp < stack+MAXDEPTH && Y+(DY) >= nMinX && Y+(DY) <= nMaxY ) \
|
473
|
+
{ sp->x1 = XL; sp->x2 = XR; sp->y = Y; sp->dy = DY; ++sp; }
|
474
|
+
|
475
|
+
#define POP(XL, XR, Y, DY) \
|
476
|
+
{ --sp; XL = sp->x1; XR = sp->x2; Y = sp->y+(DY = sp->dy); }
|
477
|
+
|
478
|
+
void
|
479
|
+
flood_fill_do_action(int x, int y, texture_info * tex, VALUE hash_arg,
|
480
|
+
sync sync_mode, bool primary, action_struct * payload)
|
481
|
+
{
|
482
|
+
int left, x1, x2, dy;
|
483
|
+
rgba old_color;
|
484
|
+
LINESEGMENT stack[MAXDEPTH], *sp = stack;
|
485
|
+
|
486
|
+
action_struct cur;
|
487
|
+
|
488
|
+
int nMinX, nMinY, nMaxX, nMaxY;
|
489
|
+
|
490
|
+
/* NOTE: 1024 is just a place-holder to indicate maximum possible width/height.
|
491
|
+
Values will be constrained to realistic dimensions by constrain_boundaries() function */
|
492
|
+
draw_prologue(&cur, tex, 0, 0, 1024, 1024, &hash_arg, sync_mode, primary, &payload);
|
493
|
+
|
494
|
+
/* fill hates alpha_blend so let's turn it off */
|
495
|
+
payload->pen.alpha_blend = false;
|
496
|
+
|
497
|
+
nMinX = cur.xmin; nMinY = cur.ymin;
|
498
|
+
nMaxX = cur.xmax; nMaxY = cur.ymax;
|
499
|
+
|
500
|
+
old_color = get_pixel_color(tex, x, y);
|
501
|
+
if( cmp_color(old_color, cur.color) )
|
502
|
+
return;
|
503
|
+
|
504
|
+
if( x < nMinX || x > nMaxX || y < nMinX || y > nMaxY )
|
505
|
+
return;
|
506
|
+
|
507
|
+
PUSH(x, x, y, 1); /* needed in some cases */
|
508
|
+
PUSH(x, x, y + 1, -1); /* seed segment (popped 1st) */
|
509
|
+
|
510
|
+
while( sp > stack ) {
|
511
|
+
POP(x1, x2, y, dy);
|
512
|
+
|
513
|
+
for( x = x1; x >= nMinX && cmp_color(get_pixel_color(tex, x, y), old_color); --x ) {
|
514
|
+
set_pixel_color_with_style(payload, tex, x, y);
|
515
|
+
}
|
516
|
+
|
517
|
+
if( x >= x1 )
|
518
|
+
goto SKIP;
|
519
|
+
|
520
|
+
left = x + 1;
|
521
|
+
if( left < x1 )
|
522
|
+
PUSH(y, left, x1 - 1, -dy); /* leak on left? */
|
523
|
+
|
524
|
+
x = x1 + 1;
|
525
|
+
|
526
|
+
do {
|
527
|
+
for( ; x <= nMaxX && cmp_color(get_pixel_color(tex, x, y), old_color); ++x ){
|
528
|
+
set_pixel_color_with_style(payload, tex, x, y);
|
529
|
+
}
|
530
|
+
|
531
|
+
PUSH(left, x - 1, y, dy);
|
532
|
+
|
533
|
+
if( x > x2 + 1 )
|
534
|
+
PUSH(x2 + 1, x - 1, y, -dy); /* leak on right? */
|
535
|
+
|
536
|
+
SKIP: for( ++x; x <= x2 && !cmp_color(get_pixel_color(tex, x, y), old_color); ++x ) {;}
|
537
|
+
|
538
|
+
left = x;
|
539
|
+
} while( x <= x2 );
|
540
|
+
}
|
541
|
+
|
542
|
+
draw_epilogue(&cur, tex, primary);
|
543
|
+
}
|
544
|
+
/*** END FLOOD FILL ***/
|
545
|
+
|
546
|
+
/** glow fill algorithm, from the gosu forums **/
|
547
|
+
static void
|
548
|
+
glow_floodFill( int x, int y, rgba * seed_color, action_struct * cur, texture_info * tex, texture_info * tex2 )
|
549
|
+
{
|
550
|
+
/* used to flood in both horizontal directions from the given point to form a line. */
|
551
|
+
int fillL, fillR;
|
552
|
+
int i;
|
553
|
+
|
554
|
+
/* initialize the flood directions */
|
555
|
+
fillL = fillR = x;
|
556
|
+
|
557
|
+
/* flood left until a new color is hit - or the edge of the image */
|
558
|
+
do {
|
559
|
+
/* for texture filling */
|
560
|
+
if(tex2)
|
561
|
+
cur->color = get_pixel_color(tex2, fillL % tex2->width, y % tex2->height);
|
562
|
+
|
563
|
+
/* TWO VERSIONS of below */
|
564
|
+
|
565
|
+
/* SLOW BUT MODERN VERSION */
|
566
|
+
/* set_pixel_color_with_style(cur, tex, fillL, y); */
|
567
|
+
|
568
|
+
/* FAST but old version */
|
569
|
+
set_pixel_color(&cur->color, tex, fillL, y);
|
570
|
+
|
571
|
+
fillL--;
|
572
|
+
} while( (fillL >= 0 ) && (cmp_color(get_pixel_color(tex, fillL, y), *seed_color)) );
|
573
|
+
|
574
|
+
/* flood right until a new color is hit - or the edge of the image */
|
575
|
+
do {
|
576
|
+
// for texture filling
|
577
|
+
if(tex2)
|
578
|
+
cur->color = get_pixel_color(tex2, fillR % tex2->width, y % tex2->height);
|
579
|
+
|
580
|
+
/* set_pixel_color_with_style(cur, tex, fillR, y); */
|
581
|
+
|
582
|
+
set_pixel_color(&cur->color, tex, fillR, y);
|
583
|
+
|
584
|
+
fillR++;
|
585
|
+
} while( (fillR < cur->xmax - 1) && (cmp_color(get_pixel_color(tex, fillR, y), *seed_color)) );
|
586
|
+
|
587
|
+
/* recurse to the line above and the line below at each point */
|
588
|
+
for( i = fillL + 1; i < fillR; i++ ) {
|
589
|
+
/* Flood above */
|
590
|
+
if( ( y > 0 ) && ( cmp_color(get_pixel_color(tex, i, y - 1), *seed_color) ) ) {
|
591
|
+
|
592
|
+
glow_floodFill( i, y-1, seed_color, cur, tex, tex2 );
|
593
|
+
}
|
594
|
+
/* flood below */
|
595
|
+
if( (y < cur->ymax - 1) && (cmp_color(get_pixel_color(tex, i, y + 1), *seed_color) )) {
|
596
|
+
glow_floodFill( i, y+1, seed_color, cur, tex, tex2 );
|
597
|
+
}
|
598
|
+
}
|
599
|
+
}
|
600
|
+
|
601
|
+
void
|
602
|
+
glow_fill_do_action(int x, int y, texture_info * tex, VALUE hash_arg,
|
603
|
+
sync sync_mode, bool primary, action_struct * payload)
|
604
|
+
{
|
605
|
+
action_struct cur;
|
606
|
+
rgba seed_color;
|
607
|
+
texture_info fill_image;
|
608
|
+
texture_info * fill_image_ptr = NULL;
|
609
|
+
|
610
|
+
if(!bound_by_rect(x, y, 0, 0, tex->width, tex->height)) return;
|
611
|
+
|
612
|
+
draw_prologue(&cur, tex, 0, 0, 1024, 1024, &hash_arg, sync_mode, primary, &payload);
|
613
|
+
|
614
|
+
/* fill hates alpha_blend so let's turn it off */
|
615
|
+
payload->pen.alpha_blend = false;
|
616
|
+
|
617
|
+
if(is_a_hash(hash_arg)) {
|
618
|
+
VALUE try_image = get_from_hash(hash_arg, "texture");
|
619
|
+
if(is_gosu_image(try_image)) {
|
620
|
+
get_texture_info(try_image, &fill_image);
|
621
|
+
fill_image_ptr = &fill_image;
|
622
|
+
}
|
623
|
+
}
|
624
|
+
|
625
|
+
seed_color = get_pixel_color(tex, x, y);
|
626
|
+
|
627
|
+
/* last argument is pointer to texture fill data (if it exists) or NULL (if it doesn't) */
|
628
|
+
glow_floodFill( x, y, &seed_color, &cur, tex, fill_image_ptr );
|
629
|
+
|
630
|
+
draw_epilogue(&cur, tex, primary);
|
631
|
+
}
|
632
|
+
/** end of glow fill **/
|
633
|
+
|
634
|
+
/** scanfill algorithm **/
|
635
|
+
/* the stack */
|
636
|
+
#define stackSize 16777218
|
637
|
+
int stack[stackSize];
|
638
|
+
int stackPointer;
|
639
|
+
|
640
|
+
static bool
|
641
|
+
pop(int * x, int * y, int h)
|
642
|
+
{
|
643
|
+
if(stackPointer > 0)
|
644
|
+
{
|
645
|
+
int p = stack[stackPointer];
|
646
|
+
*x = p / h;
|
647
|
+
*y = p % h;
|
648
|
+
stackPointer--;
|
649
|
+
return true;
|
650
|
+
}
|
651
|
+
else
|
652
|
+
{
|
653
|
+
return false;
|
654
|
+
}
|
655
|
+
}
|
656
|
+
|
657
|
+
static bool
|
658
|
+
push(int x, int y, int h)
|
659
|
+
{
|
660
|
+
if(stackPointer < stackSize - 1)
|
661
|
+
{
|
662
|
+
stackPointer++;
|
663
|
+
stack[stackPointer] = h * x + y;
|
664
|
+
return true;
|
665
|
+
}
|
666
|
+
else
|
667
|
+
{
|
668
|
+
return false;
|
669
|
+
}
|
670
|
+
}
|
671
|
+
|
672
|
+
static void
|
673
|
+
emptyStack()
|
674
|
+
{
|
675
|
+
int x, y;
|
676
|
+
while(pop(&x, &y, 0));
|
677
|
+
}
|
678
|
+
|
679
|
+
void
|
680
|
+
scan_fill_do_action(int x, int y, texture_info * tex, VALUE hash_arg,
|
681
|
+
sync sync_mode, bool primary, action_struct * payload)
|
682
|
+
{
|
683
|
+
action_struct cur;
|
684
|
+
rgba old_color;
|
685
|
+
int y1;
|
686
|
+
bool spanLeft, spanRight;
|
687
|
+
|
688
|
+
if(!bound_by_rect(x, y, 0, 0, tex->width - 1, tex->height - 1)) return;
|
689
|
+
|
690
|
+
/* NB: using XMAX_OOB etc since we dont know the drawing area yet; drawing area will be set by
|
691
|
+
update_bounds() function in main loop */
|
692
|
+
draw_prologue(&cur, tex, XMAX_OOB, YMAX_OOB, XMIN_OOB, YMIN_OOB, &hash_arg, sync_mode, primary, &payload);
|
693
|
+
|
694
|
+
/* fill hates alpha_blend so let's turn it off */
|
695
|
+
payload->pen.alpha_blend = false;
|
696
|
+
|
697
|
+
old_color = get_pixel_color(tex, x, y);
|
698
|
+
|
699
|
+
if(cmp_color(old_color, cur.color)) return;
|
700
|
+
|
701
|
+
emptyStack();
|
702
|
+
|
703
|
+
if(!push(x, y, tex->width - 1)) return;
|
704
|
+
|
705
|
+
while(pop(&x, &y, tex->width - 1))
|
706
|
+
{
|
707
|
+
y1 = y;
|
708
|
+
while(y1 >= 0 && cmp_color(old_color, get_pixel_color(tex, x, y1))) y1--;
|
709
|
+
y1++;
|
710
|
+
spanLeft = spanRight = false;
|
711
|
+
while(y1 < tex->height && cmp_color(old_color, get_pixel_color(tex, x, y1)) )
|
712
|
+
{
|
713
|
+
set_pixel_color_with_style(payload, tex, x, y1);
|
714
|
+
|
715
|
+
/* update the drawing rectangle */
|
716
|
+
update_bounds(payload, x, y1, x, y1);
|
717
|
+
|
718
|
+
if(!spanLeft && x > 0 && cmp_color(old_color, get_pixel_color(tex, x - 1, y1)))
|
719
|
+
{
|
720
|
+
if(!push(x - 1, y1, tex->width - 1)) return;
|
721
|
+
spanLeft = true;
|
722
|
+
}
|
723
|
+
|
724
|
+
else if(spanLeft && !cmp_color(old_color, get_pixel_color(tex, x - 1, y1)))
|
725
|
+
{
|
726
|
+
spanLeft = false;
|
727
|
+
}
|
728
|
+
|
729
|
+
if(!spanRight && x < tex->width - 1 && cmp_color(old_color,
|
730
|
+
get_pixel_color(tex, x + 1, y1)))
|
731
|
+
{
|
732
|
+
if(!push(x + 1, y1, tex->width - 1)) return;
|
733
|
+
spanRight = true;
|
734
|
+
}
|
735
|
+
|
736
|
+
else if(spanRight && x < tex->width - 1 && !cmp_color(old_color,get_pixel_color(tex, x + 1, y1)))
|
737
|
+
{
|
738
|
+
spanRight = false;
|
739
|
+
}
|
740
|
+
y1++;
|
741
|
+
}
|
742
|
+
}
|
743
|
+
draw_epilogue(&cur, tex, primary);
|
744
|
+
}
|
745
|
+
/** end of scanfill **/
|
746
|
+
|
747
|
+
/** bezier curve algorithm **/
|
748
|
+
static void
|
749
|
+
bezier_point(VALUE points, float u, float * x, float * y, int n, int format,
|
750
|
+
int draw_offset_x, int draw_offset_y)
|
751
|
+
{
|
752
|
+
int xy_index;
|
753
|
+
double sumx = 0, sumy = 0;
|
754
|
+
|
755
|
+
|
756
|
+
for(int k = 0; k < n; k++) {
|
757
|
+
switch(format) {
|
758
|
+
case POINT_FORMAT:
|
759
|
+
|
760
|
+
sumx += NUM2DBL(point_x(get_from_array(points, k))) * bernstein(n - 1, k, u);
|
761
|
+
sumy += NUM2DBL(point_y(get_from_array(points, k))) * bernstein(n - 1, k, u);
|
762
|
+
break;
|
763
|
+
case SIMPLE_FORMAT:
|
764
|
+
|
765
|
+
xy_index = k * 2;
|
766
|
+
sumx += NUM2DBL(get_from_array(points, xy_index)) * bernstein(n - 1, k, u);
|
767
|
+
sumy += NUM2DBL(get_from_array(points, xy_index + 1)) * bernstein(n - 1, k, u);
|
768
|
+
break;
|
769
|
+
default:
|
770
|
+
rb_raise(rb_eArgError, "pixel format must be either POINT_FORMAT or SIMPLE_FORMAT");
|
771
|
+
}
|
772
|
+
}
|
773
|
+
|
774
|
+
*x = sumx + draw_offset_x;
|
775
|
+
*y = sumy + draw_offset_y;
|
776
|
+
}
|
777
|
+
|
778
|
+
void
|
779
|
+
bezier_do_action(VALUE points, texture_info * tex, VALUE hash_arg, sync sync_mode,
|
780
|
+
bool primary, action_struct * payload)
|
781
|
+
{
|
782
|
+
float u = 0.0;
|
783
|
+
action_struct cur;
|
784
|
+
float x1, y1, x2, y2;
|
785
|
+
int first_x, first_y;
|
786
|
+
int format;
|
787
|
+
int num_point_pairs;
|
788
|
+
bool closed = false;
|
789
|
+
VALUE offset_val;
|
790
|
+
int draw_offset_x, draw_offset_y;
|
791
|
+
|
792
|
+
/* defaults to 200 (1 / 0.005) samples per curve */
|
793
|
+
float step_size = 0.005;
|
794
|
+
|
795
|
+
draw_prologue(&cur, tex, XMAX_OOB, YMAX_OOB, XMIN_OOB, YMIN_OOB, &hash_arg, sync_mode, primary, &payload);
|
796
|
+
|
797
|
+
/* calculate offset */
|
798
|
+
offset_val = get_image_local(tex->image, DRAW_OFFSET);
|
799
|
+
|
800
|
+
draw_offset_x = NUM2INT(get_from_array(offset_val, 0));
|
801
|
+
draw_offset_y = NUM2INT(get_from_array(offset_val, 1));
|
802
|
+
|
803
|
+
if(is_a_hash(hash_arg)) {
|
804
|
+
|
805
|
+
/* if the polyline is 'closed' make the last point the first */
|
806
|
+
if(RTEST(get_from_hash(hash_arg, "closed")) || RTEST(get_from_hash(hash_arg, "close"))) {
|
807
|
+
|
808
|
+
/* so that our additional point is not persistent */
|
809
|
+
points = rb_obj_dup(points);
|
810
|
+
closed = true;
|
811
|
+
}
|
812
|
+
|
813
|
+
/* number of points to sample */
|
814
|
+
if(RTEST(get_from_hash(hash_arg, "sample_size"))) {
|
815
|
+
VALUE c = get_from_hash(hash_arg, "sample_size");
|
816
|
+
Check_Type(c, T_FIXNUM);
|
817
|
+
step_size = 1.0 / (float)FIX2INT(c);
|
818
|
+
}
|
819
|
+
}
|
820
|
+
|
821
|
+
if(is_a_point(get_from_array(points, 0))) {
|
822
|
+
format = POINT_FORMAT;
|
823
|
+
|
824
|
+
if(closed)
|
825
|
+
rb_ary_push(points, get_from_array(points, 0));
|
826
|
+
|
827
|
+
num_point_pairs = RARRAY_LEN(points);
|
828
|
+
}
|
829
|
+
else {
|
830
|
+
format = SIMPLE_FORMAT;
|
831
|
+
|
832
|
+
/* ensure points are given in pairs */
|
833
|
+
if(RARRAY_LEN(points) % 2)
|
834
|
+
rb_raise(rb_eArgError, "bezier needs an even number of points. got %d\n", (int)RARRAY_LEN(points));
|
835
|
+
|
836
|
+
if(closed) {
|
837
|
+
rb_ary_push(points, get_from_array(points, 0));
|
838
|
+
rb_ary_push(points, get_from_array(points, 1));
|
839
|
+
}
|
840
|
+
|
841
|
+
num_point_pairs = RARRAY_LEN(points) / 2;
|
842
|
+
}
|
843
|
+
|
844
|
+
if(num_point_pairs > 17)
|
845
|
+
rb_raise(rb_eArgError, "too many points for bezier curve. 17 points is current maximum. got %d\n",
|
846
|
+
num_point_pairs);
|
847
|
+
|
848
|
+
/* get the first point */
|
849
|
+
bezier_point(points, 0, &x1, &y1, num_point_pairs, format, draw_offset_x, draw_offset_y);
|
850
|
+
|
851
|
+
/* save it so we can link up with last point properly if the curve is 'closed' */
|
852
|
+
first_x = x1;
|
853
|
+
first_y = y1;
|
854
|
+
|
855
|
+
while(u <= 1) {
|
856
|
+
bezier_point(points, u, &x2, &y2, num_point_pairs, format, draw_offset_x, draw_offset_y);
|
857
|
+
|
858
|
+
line_do_action(x1, y1, x2, y2, tex, hash_arg, no_sync, false, payload);
|
859
|
+
|
860
|
+
/* update drawing rectangle */
|
861
|
+
update_bounds(payload, x1, y1, x2, y2);
|
862
|
+
|
863
|
+
x1 = x2;
|
864
|
+
y1 = y2;
|
865
|
+
|
866
|
+
u += step_size;
|
867
|
+
}
|
868
|
+
|
869
|
+
/* sometimes beziers dont close properly, so we'll ensure it's closed */
|
870
|
+
if(closed)
|
871
|
+
line_do_action(x2, y2, first_x, first_y, tex, hash_arg, no_sync, false, payload);
|
872
|
+
|
873
|
+
draw_epilogue(&cur, tex, primary);
|
874
|
+
}
|
875
|
+
/** end of bezier **/
|
876
|
+
|
877
|
+
/** each_pixel **/
|
878
|
+
static void
|
879
|
+
set_color_array(VALUE ary, rgba * color)
|
880
|
+
{
|
881
|
+
set_array_value(ary, 0, rb_float_new(color->red));
|
882
|
+
set_array_value(ary, 1, rb_float_new(color->green));
|
883
|
+
set_array_value(ary, 2, rb_float_new(color->blue));
|
884
|
+
set_array_value(ary, 3, rb_float_new(color->alpha));
|
885
|
+
}
|
886
|
+
|
887
|
+
void
|
888
|
+
each_pixel_do_action(int x1, int y1, int x2, int y2, VALUE proc, texture_info * tex, VALUE hash_arg,
|
889
|
+
sync sync_mode, bool primary, action_struct * payload)
|
890
|
+
{
|
891
|
+
action_struct cur;
|
892
|
+
int arity;
|
893
|
+
VALUE rb_pix = rb_ary_new2(4);
|
894
|
+
|
895
|
+
draw_prologue(&cur, tex, x1, y1, x2, y2, &hash_arg, sync_mode, primary, &payload);
|
896
|
+
|
897
|
+
arity = FIX2INT(rb_funcall(proc, rb_intern("arity"), 0));
|
898
|
+
|
899
|
+
for(int y = y1; y < y2 + 1; y++)
|
900
|
+
for(int x = x1; x < x2 + 1; x++) {
|
901
|
+
rgba pix = get_pixel_color(tex, x, y);
|
902
|
+
|
903
|
+
set_color_array(rb_pix, &pix);
|
904
|
+
|
905
|
+
/* invoke the block */
|
906
|
+
switch(arity) {
|
907
|
+
case 1:
|
908
|
+
rb_funcall(proc, rb_intern("call"), 1, rb_pix);
|
909
|
+
break;
|
910
|
+
case 3:
|
911
|
+
rb_funcall(proc, rb_intern("call"), 3, rb_pix, INT2FIX(x), INT2FIX(y));
|
912
|
+
break;
|
913
|
+
default:
|
914
|
+
rb_raise(rb_eArgError, "permissible arities for each are 1 & 3. Got %d\n",
|
915
|
+
arity);
|
916
|
+
|
917
|
+
}
|
918
|
+
|
919
|
+
pix = convert_rb_color_to_rgba(rb_pix);
|
920
|
+
|
921
|
+
set_pixel_color(&pix, tex, x, y);
|
922
|
+
}
|
923
|
+
|
924
|
+
draw_epilogue(&cur, tex, primary);
|
925
|
+
}
|
926
|
+
/** end each_pixel iterator **/
|
927
|
+
|
928
|
+
/** splice algorithm **/
|
929
|
+
void
|
930
|
+
splice_do_action(int x0, int y0, int cx1, int cy1, int cx2, int cy2, texture_info * splice_tex,
|
931
|
+
texture_info * tex, VALUE hash_arg, sync sync_mode,
|
932
|
+
bool primary, action_struct * payload)
|
933
|
+
{
|
934
|
+
action_struct cur;
|
935
|
+
int xbound;
|
936
|
+
int ybound;
|
937
|
+
rgba chromakey;
|
938
|
+
float * image_buf = NULL;
|
939
|
+
bool inverse_chroma = false;
|
940
|
+
bool same_image = false;
|
941
|
+
bool has_chroma = false;
|
942
|
+
|
943
|
+
constrain_boundaries(&cx1, &cy1, &cx2, &cy2, splice_tex->width, splice_tex->height);
|
944
|
+
xbound = cx2 - cx1 + 1;
|
945
|
+
ybound = cy2 - cy1 + 1;
|
946
|
+
|
947
|
+
draw_prologue(&cur, tex, x0, y0,
|
948
|
+
x0 + xbound, y0 + ybound, &hash_arg, sync_mode, primary, &payload);
|
949
|
+
|
950
|
+
|
951
|
+
if(has_optional_hash_arg(hash_arg, "chroma_key")) {
|
952
|
+
VALUE c = get_from_hash(hash_arg, "chroma_key");
|
953
|
+
chromakey = convert_rb_color_to_rgba(c);
|
954
|
+
has_chroma = true;
|
955
|
+
}
|
956
|
+
else if(has_optional_hash_arg(hash_arg, "chroma_key_not")) {
|
957
|
+
chromakey = convert_rb_color_to_rgba(get_from_hash(hash_arg, "chroma_key_not"));
|
958
|
+
inverse_chroma = true;
|
959
|
+
has_chroma = true;
|
960
|
+
}
|
961
|
+
|
962
|
+
if(splice_tex->image == tex->image)
|
963
|
+
same_image = true;
|
964
|
+
|
965
|
+
/* NB: we do not use this in the general case since it's almost 1.5 times as slow.
|
966
|
+
It is necessary for splicing from/to the same region of pixels though.
|
967
|
+
*/
|
968
|
+
if(same_image)
|
969
|
+
image_buf = get_image_chunk(splice_tex, cx1, cy1, cx2, cy2);
|
970
|
+
|
971
|
+
for(int y = 0; y < ybound; y++)
|
972
|
+
for(int x = 0; x < xbound; x++) {
|
973
|
+
|
974
|
+
if(!same_image)
|
975
|
+
payload->color = get_pixel_color(splice_tex, cx1 + x, cy1 + y);
|
976
|
+
else
|
977
|
+
payload->color = get_pixel_color_from_chunk(image_buf, xbound, ybound, x, y);
|
978
|
+
|
979
|
+
if(has_chroma) {
|
980
|
+
bool chroma_match = cmp_color(payload->color, chromakey);
|
981
|
+
|
982
|
+
/* look at released 0.2.0 code to see how USED to do this.
|
983
|
+
this is now a simplified boolean expression (XOR) */
|
984
|
+
if(chroma_match == inverse_chroma)
|
985
|
+
set_pixel_color_with_style(payload, tex, x0 + x, y0 + y);
|
986
|
+
}
|
987
|
+
else
|
988
|
+
set_pixel_color_with_style(payload, tex, x0 + x, y0 + y);
|
989
|
+
}
|
990
|
+
|
991
|
+
if(same_image)
|
992
|
+
free(image_buf);
|
993
|
+
|
994
|
+
draw_epilogue(&cur, tex, primary);
|
995
|
+
}
|
996
|
+
/** end splice **/
|
997
|
+
|
998
|
+
|
999
|
+
|