/
obj.cr
7209 lines (7209 loc) · 259 KB
/
obj.cr
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "./lib"
require "../common"
require "../window"
require "../system"
module SF
extend self
# Blending modes for drawing
#
# `SF::BlendMode` is a struct that represents a blend mode. A blend
# mode determines how the colors of an object you draw are
# mixed with the colors that are already in the buffer.
#
# The struct is composed of 6 components, each of which has its
# own public member variable:
# * Color Source Factor (*color_src_factor*)
# * Color Destination Factor (*color_dst_factor*)
# * Color Blend Equation (*color_equation*)
# * Alpha Source Factor (*alpha_src_factor*)
# * Alpha Destination Factor (*alpha_dst_factor*)
# * Alpha Blend Equation (*alpha_equation*)
#
# The source factor specifies how the pixel you are drawing contributes
# to the final color. The destination factor specifies how the pixel
# already drawn in the buffer contributes to the final color.
#
# The color channels RGB (red, green, blue; simply referred to as
# color) and A (alpha; the transparency) can be treated separately. This
# separation can be useful for specific blend modes, but most often you
# won't need it and will simply treat the color as a single unit.
#
# The blend factors and equations correspond to their OpenGL equivalents.
# In general, the color of the resulting pixel is calculated according
# to the following formula (*src* is the color of the source pixel, *dst*
# the color of the destination pixel, the other variables correspond to the
# public members, with the equations being + or - operators):
# ```
# dst.rgb = color_src_factor * src.rgb (color_equation) color_dst_factor * dst.rgb
# dst.a = alpha_src_factor * src.a (alpha_equation) alpha_dst_factor * dst.a
# ```
# All factors and colors are represented as floating point numbers between
# 0 and 1. Where necessary, the result is clamped to fit in that range.
#
# The most common blending modes are defined as constants in the SF module:
# `SF::BlendAlpha`, `SF::BlendAdd`, `SF::BlendMultiply`, `SF::BlendNone`.
#
# In SFML, a blend mode can be specified every time you draw a `SF::Drawable`
# object to a render target. It is part of the `SF::RenderStates` compound
# that is passed to the member function `SF::RenderTarget.draw`().
#
# *See also:* `SF::RenderStates`, `SF::RenderTarget`
struct BlendMode
@color_src_factor : BlendMode::Factor
@color_dst_factor : BlendMode::Factor
@color_equation : BlendMode::Equation
@alpha_src_factor : BlendMode::Factor
@alpha_dst_factor : BlendMode::Factor
@alpha_equation : BlendMode::Equation
# Enumeration of the blending factors
#
# The factors are mapped directly to their OpenGL equivalents,
# specified by gl_blend_func() or gl_blend_func_separate().
enum Factor
# (0, 0, 0, 0)
Zero
# (1, 1, 1, 1)
One
# (src.r, src.g, src.b, src.a)
SrcColor
# (1, 1, 1, 1) - (src.r, src.g, src.b, src.a)
OneMinusSrcColor
# (dst.r, dst.g, dst.b, dst.a)
DstColor
# (1, 1, 1, 1) - (dst.r, dst.g, dst.b, dst.a)
OneMinusDstColor
# (src.a, src.a, src.a, src.a)
SrcAlpha
# (1, 1, 1, 1) - (src.a, src.a, src.a, src.a)
OneMinusSrcAlpha
# (dst.a, dst.a, dst.a, dst.a)
DstAlpha
# (1, 1, 1, 1) - (dst.a, dst.a, dst.a, dst.a)
OneMinusDstAlpha
end
Util.extract BlendMode::Factor
# Enumeration of the blending equations
#
# The equations are mapped directly to their OpenGL equivalents,
# specified by gl_blend_equation() or gl_blend_equation_separate().
enum Equation
# Pixel = Src * SrcFactor + Dst * DstFactor
Add
# Pixel = Src * SrcFactor - Dst * DstFactor
Subtract
# Pixel = Dst * DstFactor - Src * SrcFactor
ReverseSubtract
end
Util.extract BlendMode::Equation
# Default constructor
#
# Constructs a blending mode that does alpha blending.
def initialize()
@color_src_factor = uninitialized BlendMode::Factor
@color_dst_factor = uninitialized BlendMode::Factor
@color_equation = uninitialized BlendMode::Equation
@alpha_src_factor = uninitialized BlendMode::Factor
@alpha_dst_factor = uninitialized BlendMode::Factor
@alpha_equation = uninitialized BlendMode::Equation
VoidCSFML.sfml_blendmode_initialize(to_unsafe)
end
# Construct the blend mode given the factors and equation.
#
# This constructor uses the same factors and equation for both
# color and alpha components. It also defaults to the Add equation.
#
# * *source_factor* - Specifies how to compute the source factor for the color and alpha channels.
# * *destination_factor* - Specifies how to compute the destination factor for the color and alpha channels.
# * *blend_equation* - Specifies how to combine the source and destination colors and alpha.
def initialize(source_factor : BlendMode::Factor, destination_factor : BlendMode::Factor, blend_equation : BlendMode::Equation = Add)
@color_src_factor = uninitialized BlendMode::Factor
@color_dst_factor = uninitialized BlendMode::Factor
@color_equation = uninitialized BlendMode::Equation
@alpha_src_factor = uninitialized BlendMode::Factor
@alpha_dst_factor = uninitialized BlendMode::Factor
@alpha_equation = uninitialized BlendMode::Equation
VoidCSFML.sfml_blendmode_initialize_8xr8xrBw1(to_unsafe, source_factor, destination_factor, blend_equation)
end
# Construct the blend mode given the factors and equation.
#
# * *color_source_factor* - Specifies how to compute the source factor for the color channels.
# * *color_destination_factor* - Specifies how to compute the destination factor for the color channels.
# * *color_blend_equation* - Specifies how to combine the source and destination colors.
# * *alpha_source_factor* - Specifies how to compute the source factor.
# * *alpha_destination_factor* - Specifies how to compute the destination factor.
# * *alpha_blend_equation* - Specifies how to combine the source and destination alphas.
def initialize(color_source_factor : BlendMode::Factor, color_destination_factor : BlendMode::Factor, color_blend_equation : BlendMode::Equation, alpha_source_factor : BlendMode::Factor, alpha_destination_factor : BlendMode::Factor, alpha_blend_equation : BlendMode::Equation)
@color_src_factor = uninitialized BlendMode::Factor
@color_dst_factor = uninitialized BlendMode::Factor
@color_equation = uninitialized BlendMode::Equation
@alpha_src_factor = uninitialized BlendMode::Factor
@alpha_dst_factor = uninitialized BlendMode::Factor
@alpha_equation = uninitialized BlendMode::Equation
VoidCSFML.sfml_blendmode_initialize_8xr8xrBw18xr8xrBw1(to_unsafe, color_source_factor, color_destination_factor, color_blend_equation, alpha_source_factor, alpha_destination_factor, alpha_blend_equation)
end
@color_src_factor : BlendMode::Factor
# Source blending factor for the color channels
def color_src_factor : BlendMode::Factor
@color_src_factor
end
def color_src_factor=(color_src_factor : BlendMode::Factor)
@color_src_factor = color_src_factor
end
@color_dst_factor : BlendMode::Factor
# Destination blending factor for the color channels
def color_dst_factor : BlendMode::Factor
@color_dst_factor
end
def color_dst_factor=(color_dst_factor : BlendMode::Factor)
@color_dst_factor = color_dst_factor
end
@color_equation : BlendMode::Equation
# Blending equation for the color channels
def color_equation : BlendMode::Equation
@color_equation
end
def color_equation=(color_equation : BlendMode::Equation)
@color_equation = color_equation
end
@alpha_src_factor : BlendMode::Factor
# Source blending factor for the alpha channel
def alpha_src_factor : BlendMode::Factor
@alpha_src_factor
end
def alpha_src_factor=(alpha_src_factor : BlendMode::Factor)
@alpha_src_factor = alpha_src_factor
end
@alpha_dst_factor : BlendMode::Factor
# Destination blending factor for the alpha channel
def alpha_dst_factor : BlendMode::Factor
@alpha_dst_factor
end
def alpha_dst_factor=(alpha_dst_factor : BlendMode::Factor)
@alpha_dst_factor = alpha_dst_factor
end
@alpha_equation : BlendMode::Equation
# Blending equation for the alpha channel
def alpha_equation : BlendMode::Equation
@alpha_equation
end
def alpha_equation=(alpha_equation : BlendMode::Equation)
@alpha_equation = alpha_equation
end
# Overload of the == operator
#
# * *left* - Left operand
# * *right* - Right operand
#
# *Returns:* True if blending modes are equal, false if they are different
def ==(right : BlendMode) : Bool
VoidCSFML.sfml_operator_eq_PG5PG5(to_unsafe, right, out result)
return result
end
# Overload of the != operator
#
# * *left* - Left operand
# * *right* - Right operand
#
# *Returns:* True if blending modes are different, false if they are equal
def !=(right : BlendMode) : Bool
VoidCSFML.sfml_operator_ne_PG5PG5(to_unsafe, right, out result)
return result
end
# :nodoc:
def to_unsafe()
pointerof(@color_src_factor).as(Void*)
end
# :nodoc:
def initialize(copy : BlendMode)
@color_src_factor = uninitialized BlendMode::Factor
@color_dst_factor = uninitialized BlendMode::Factor
@color_equation = uninitialized BlendMode::Equation
@alpha_src_factor = uninitialized BlendMode::Factor
@alpha_dst_factor = uninitialized BlendMode::Factor
@alpha_equation = uninitialized BlendMode::Equation
VoidCSFML.sfml_blendmode_initialize_PG5(to_unsafe, copy)
end
def dup() : self
return typeof(self).new(self)
end
end
# Define a 3x3 transform matrix
#
# A `SF::Transform` specifies how to translate, rotate, scale,
# shear, project, whatever things. In mathematical terms, it defines
# how to transform a coordinate system into another.
#
# For example, if you apply a rotation transform to a sprite, the
# result will be a rotated sprite. And anything that is transformed
# by this rotation transform will be rotated the same way, according
# to its initial position.
#
# Transforms are typically used for drawing. But they can also be
# used for any computation that requires to transform points between
# the local and global coordinate systems of an entity (like collision
# detection).
#
# Example:
# ```
# # define a translation transform
# translation = SF::Transform.new
# translation.translate(20, 50)
#
# # define a rotation transform
# rotation = SF::Transform.new
# rotation.rotate(45)
#
# # combine them
# transform = translation * rotation
#
# # use the result to transform stuff...
# point = transform.transform_point(10, 20)
# rect = transform.transform_rect(SF.float_rect(0, 0, 10, 100))
# ```
#
# *See also:* `SF::Transformable`, `SF::RenderStates`
struct Transform
@matrix : LibC::Float[16]
# Default constructor
#
# Creates an identity transform (a transform that does nothing).
def initialize()
@matrix = uninitialized Float32[16]
VoidCSFML.sfml_transform_initialize(to_unsafe)
end
# Construct a transform from a 3x3 matrix
#
# * *a00* - Element (0, 0) of the matrix
# * *a01* - Element (0, 1) of the matrix
# * *a02* - Element (0, 2) of the matrix
# * *a10* - Element (1, 0) of the matrix
# * *a11* - Element (1, 1) of the matrix
# * *a12* - Element (1, 2) of the matrix
# * *a20* - Element (2, 0) of the matrix
# * *a21* - Element (2, 1) of the matrix
# * *a22* - Element (2, 2) of the matrix
def initialize(a00 : Number, a01 : Number, a02 : Number, a10 : Number, a11 : Number, a12 : Number, a20 : Number, a21 : Number, a22 : Number)
@matrix = uninitialized Float32[16]
VoidCSFML.sfml_transform_initialize_Bw9Bw9Bw9Bw9Bw9Bw9Bw9Bw9Bw9(to_unsafe, LibC::Float.new(a00), LibC::Float.new(a01), LibC::Float.new(a02), LibC::Float.new(a10), LibC::Float.new(a11), LibC::Float.new(a12), LibC::Float.new(a20), LibC::Float.new(a21), LibC::Float.new(a22))
end
# Return the transform as a 4x4 matrix
#
# This function returns a pointer to an array of 16 floats
# containing the transform elements as a 4x4 matrix, which
# is directly compatible with OpenGL functions.
#
# ```
# transform = ...
# glLoadMatrixf(transform.matrix())
# ```
#
# *Returns:* Pointer to a 4x4 matrix
def matrix() : Float32*
VoidCSFML.sfml_transform_getmatrix(to_unsafe, out result)
return result
end
# Return the inverse of the transform
#
# If the inverse cannot be computed, an identity transform
# is returned.
#
# *Returns:* A new transform which is the inverse of self
def inverse() : Transform
result = Transform.allocate
VoidCSFML.sfml_transform_getinverse(to_unsafe, result)
return result
end
# Transform a 2D point
#
# * *x* - X coordinate of the point to transform
# * *y* - Y coordinate of the point to transform
#
# *Returns:* Transformed point
def transform_point(x : Number, y : Number) : Vector2f
result = Vector2f.allocate
VoidCSFML.sfml_transform_transformpoint_Bw9Bw9(to_unsafe, LibC::Float.new(x), LibC::Float.new(y), result)
return result
end
# Transform a 2D point
#
# * *point* - Point to transform
#
# *Returns:* Transformed point
def transform_point(point : Vector2|Tuple) : Vector2f
result = Vector2f.allocate
point = Vector2f.new(point[0].to_f32, point[1].to_f32)
VoidCSFML.sfml_transform_transformpoint_UU2(to_unsafe, point, result)
return result
end
# Transform a rectangle
#
# Since SFML doesn't provide support for oriented rectangles,
# the result of this function is always an axis-aligned
# rectangle. Which means that if the transform contains a
# rotation, the bounding rectangle of the transformed rectangle
# is returned.
#
# * *rectangle* - Rectangle to transform
#
# *Returns:* Transformed rectangle
def transform_rect(rectangle : FloatRect) : FloatRect
result = FloatRect.allocate
VoidCSFML.sfml_transform_transformrect_WPZ(to_unsafe, rectangle, result)
return result
end
# Combine the current transform with another one
#
# The result is a transform that is equivalent to applying
# *this followed by *transform.* Mathematically, it is
# equivalent to a matrix multiplication.
#
# * *transform* - Transform to combine with this transform
#
# *Returns:* Reference to *this
def combine(transform : Transform) : Transform
result = Transform.allocate
VoidCSFML.sfml_transform_combine_FPe(to_unsafe, transform, result)
return result
end
# Combine the current transform with a translation
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.translate(100, 200).rotate(45)
# ```
#
# * *x* - Offset to apply on X axis
# * *y* - Offset to apply on Y axis
#
# *Returns:* Reference to *this
#
# *See also:* `rotate`, `scale`
def translate(x : Number, y : Number) : Transform
result = Transform.allocate
VoidCSFML.sfml_transform_translate_Bw9Bw9(to_unsafe, LibC::Float.new(x), LibC::Float.new(y), result)
return result
end
# Combine the current transform with a translation
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.translate(SF.vector2f(100, 200)).rotate(45)
# ```
#
# * *offset* - Translation offset to apply
#
# *Returns:* Reference to *this
#
# *See also:* `rotate`, `scale`
def translate(offset : Vector2|Tuple) : Transform
result = Transform.allocate
offset = Vector2f.new(offset[0].to_f32, offset[1].to_f32)
VoidCSFML.sfml_transform_translate_UU2(to_unsafe, offset, result)
return result
end
# Combine the current transform with a rotation
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.rotate(90).translate(50, 20)
# ```
#
# * *angle* - Rotation angle, in degrees
#
# *Returns:* Reference to *this
#
# *See also:* `translate`, `scale`
def rotate(angle : Number) : Transform
result = Transform.allocate
VoidCSFML.sfml_transform_rotate_Bw9(to_unsafe, LibC::Float.new(angle), result)
return result
end
# Combine the current transform with a rotation
#
# The center of rotation is provided for convenience as a second
# argument, so that you can build rotations around arbitrary points
# more easily (and efficiently) than the usual
# translate(-center).rotate(angle).translate(center).
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.rotate(90, 8, 3).translate(50, 20)
# ```
#
# * *angle* - Rotation angle, in degrees
# * *center_x* - X coordinate of the center of rotation
# * *center_y* - Y coordinate of the center of rotation
#
# *Returns:* Reference to *this
#
# *See also:* `translate`, `scale`
def rotate(angle : Number, center_x : Number, center_y : Number) : Transform
result = Transform.allocate
VoidCSFML.sfml_transform_rotate_Bw9Bw9Bw9(to_unsafe, LibC::Float.new(angle), LibC::Float.new(center_x), LibC::Float.new(center_y), result)
return result
end
# Combine the current transform with a rotation
#
# The center of rotation is provided for convenience as a second
# argument, so that you can build rotations around arbitrary points
# more easily (and efficiently) than the usual
# translate(-center).rotate(angle).translate(center).
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.rotate(90, SF.vector2f(8, 3)).translate(SF.vector2f(50, 20))
# ```
#
# * *angle* - Rotation angle, in degrees
# * *center* - Center of rotation
#
# *Returns:* Reference to *this
#
# *See also:* `translate`, `scale`
def rotate(angle : Number, center : Vector2|Tuple) : Transform
result = Transform.allocate
center = Vector2f.new(center[0].to_f32, center[1].to_f32)
VoidCSFML.sfml_transform_rotate_Bw9UU2(to_unsafe, LibC::Float.new(angle), center, result)
return result
end
# Combine the current transform with a scaling
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.scale(2, 1).rotate(45)
# ```
#
# * *scale_x* - Scaling factor on the X axis
# * *scale_y* - Scaling factor on the Y axis
#
# *Returns:* Reference to *this
#
# *See also:* `translate`, `rotate`
def scale(scale_x : Number, scale_y : Number) : Transform
result = Transform.allocate
VoidCSFML.sfml_transform_scale_Bw9Bw9(to_unsafe, LibC::Float.new(scale_x), LibC::Float.new(scale_y), result)
return result
end
# Combine the current transform with a scaling
#
# The center of scaling is provided for convenience as a second
# argument, so that you can build scaling around arbitrary points
# more easily (and efficiently) than the usual
# translate(-center).scale(factors).translate(center).
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.scale(2, 1, 8, 3).rotate(45)
# ```
#
# * *scale_x* - Scaling factor on X axis
# * *scale_y* - Scaling factor on Y axis
# * *center_x* - X coordinate of the center of scaling
# * *center_y* - Y coordinate of the center of scaling
#
# *Returns:* Reference to *this
#
# *See also:* `translate`, `rotate`
def scale(scale_x : Number, scale_y : Number, center_x : Number, center_y : Number) : Transform
result = Transform.allocate
VoidCSFML.sfml_transform_scale_Bw9Bw9Bw9Bw9(to_unsafe, LibC::Float.new(scale_x), LibC::Float.new(scale_y), LibC::Float.new(center_x), LibC::Float.new(center_y), result)
return result
end
# Combine the current transform with a scaling
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.scale(SF.vector2f(2, 1)).rotate(45)
# ```
#
# * *factors* - Scaling factors
#
# *Returns:* Reference to *this
#
# *See also:* `translate`, `rotate`
def scale(factors : Vector2|Tuple) : Transform
result = Transform.allocate
factors = Vector2f.new(factors[0].to_f32, factors[1].to_f32)
VoidCSFML.sfml_transform_scale_UU2(to_unsafe, factors, result)
return result
end
# Combine the current transform with a scaling
#
# The center of scaling is provided for convenience as a second
# argument, so that you can build scaling around arbitrary points
# more easily (and efficiently) than the usual
# translate(-center).scale(factors).translate(center).
#
# This function returns a reference to *this, so that calls
# can be chained.
# ```
# transform = SF::Transform.new
# transform.scale(SF.vector2f(2, 1), SF.vector2f(8, 3)).rotate(45)
# ```
#
# * *factors* - Scaling factors
# * *center* - Center of scaling
#
# *Returns:* Reference to *this
#
# *See also:* `translate`, `rotate`
def scale(factors : Vector2|Tuple, center : Vector2|Tuple) : Transform
result = Transform.allocate
factors = Vector2f.new(factors[0].to_f32, factors[1].to_f32)
center = Vector2f.new(center[0].to_f32, center[1].to_f32)
VoidCSFML.sfml_transform_scale_UU2UU2(to_unsafe, factors, center, result)
return result
end
@matrix : LibC::Float[16]
# Overload of binary operator * to combine two transforms
#
# This call is equivalent to calling Transform(left).combine(right).
#
# * *left* - Left operand (the first transform)
# * *right* - Right operand (the second transform)
#
# *Returns:* New combined transform
def *(right : Transform) : Transform
result = Transform.allocate
VoidCSFML.sfml_operator_mul_FPeFPe(to_unsafe, right, result)
return result
end
# Overload of binary operator * to transform a point
#
# This call is equivalent to calling left.transform_point(right).
#
# * *left* - Left operand (the transform)
# * *right* - Right operand (the point to transform)
#
# *Returns:* New transformed point
def *(right : Vector2|Tuple) : Vector2f
result = Vector2f.allocate
right = Vector2f.new(right[0].to_f32, right[1].to_f32)
VoidCSFML.sfml_operator_mul_FPeUU2(to_unsafe, right, result)
return result
end
# Overload of binary operator == to compare two transforms
#
# Performs an element-wise comparison of the elements of the
# left transform with the elements of the right transform.
#
# * *left* - Left operand (the first transform)
# * *right* - Right operand (the second transform)
#
# *Returns:* true if the transforms are equal, false otherwise
def ==(right : Transform) : Bool
VoidCSFML.sfml_operator_eq_FPeFPe(to_unsafe, right, out result)
return result
end
# Overload of binary operator != to compare two transforms
#
# This call is equivalent to !(left == right).
#
# * *left* - Left operand (the first transform)
# * *right* - Right operand (the second transform)
#
# *Returns:* true if the transforms are not equal, false otherwise
def !=(right : Transform) : Bool
VoidCSFML.sfml_operator_ne_FPeFPe(to_unsafe, right, out result)
return result
end
# :nodoc:
def to_unsafe()
pointerof(@matrix).as(Void*)
end
# :nodoc:
def initialize(copy : Transform)
@matrix = uninitialized Float32[16]
VoidCSFML.sfml_transform_initialize_FPe(to_unsafe, copy)
end
def dup() : self
return typeof(self).new(self)
end
end
# Define the states used for drawing to a RenderTarget
#
# There are four global states that can be applied to
# the drawn objects:
# * the blend mode: how pixels of the object are blended with the background
# * the transform: how the object is positioned/rotated/scaled
# * the texture: what image is mapped to the object
# * the shader: what custom effect is applied to the object
#
# High-level objects such as sprites or text force some of
# these states when they are drawn. For example, a sprite
# will set its own texture, so that you don't have to care
# about it when drawing the sprite.
#
# The transform is a special case: sprites, texts and shapes
# (and it's a good idea to do it with your own drawable classes
# too) combine their transform with the one that is passed in the
# RenderStates structure. So that you can use a "global" transform
# on top of each object's transform.
#
# Most objects, especially high-level drawables, can be drawn
# directly without defining render states explicitly -- the
# default set of states is OK in most cases.
# ```
# window.draw(sprite)
# ```
#
# If you want to use a single specific render state, for example a
# shader, you can pass it to the constructor of `SF::RenderStates`.
# ```
# window.draw(sprite, SF::RenderStates.new(shader))
# ```
#
# When you're inside the Draw function of a drawable
# object (one that includes `SF::Drawable`), you can
# either pass the render states unmodified, or change
# some of them.
# For example, a transformable object will combine the
# current transform with its own transform. A sprite will
# set its texture. Etc.
#
# *See also:* `SF::RenderTarget`, `SF::Drawable`
struct RenderStates
@blend_mode : BlendMode
@transform : Transform
@texture : Void*
@shader : Void*
# Default constructor
#
# Constructing a default set of render states is equivalent
# to using `SF::RenderStates::Default`.
# The default set defines:
# * the BlendAlpha blend mode
# * the identity transform
# * a null texture
# * a null shader
def initialize()
@blend_mode = uninitialized BlendMode
@transform = uninitialized Transform
@texture = uninitialized Void*
@shader = uninitialized Void*
VoidCSFML.sfml_renderstates_initialize(to_unsafe)
end
# Construct a default set of render states with a custom blend mode
#
# * *blend_mode* - Blend mode to use
def initialize(blend_mode : BlendMode)
@blend_mode = uninitialized BlendMode
@transform = uninitialized Transform
@texture = uninitialized Void*
@shader = uninitialized Void*
VoidCSFML.sfml_renderstates_initialize_PG5(to_unsafe, blend_mode)
end
# Construct a default set of render states with a custom transform
#
# * *transform* - Transform to use
def initialize(transform : Transform)
@blend_mode = uninitialized BlendMode
@transform = uninitialized Transform
@texture = uninitialized Void*
@shader = uninitialized Void*
VoidCSFML.sfml_renderstates_initialize_FPe(to_unsafe, transform)
end
# Construct a default set of render states with a custom texture
#
# * *texture* - Texture to use
def initialize(texture : Texture?)
@blend_mode = uninitialized BlendMode
@transform = uninitialized Transform
@texture = uninitialized Void*
@shader = uninitialized Void*
@_renderstates_texture = texture
VoidCSFML.sfml_renderstates_initialize_MXd(to_unsafe, texture)
end
# Construct a default set of render states with a custom shader
#
# * *shader* - Shader to use
def initialize(shader : Shader)
@blend_mode = uninitialized BlendMode
@transform = uninitialized Transform
@texture = uninitialized Void*
@shader = uninitialized Void*
@_renderstates_shader = shader
VoidCSFML.sfml_renderstates_initialize_8P6(to_unsafe, shader)
end
# Construct a set of render states with all its attributes
#
# * *blend_mode* - Blend mode to use
# * *transform* - Transform to use
# * *texture* - Texture to use
# * *shader* - Shader to use
def initialize(blend_mode : BlendMode, transform : Transform, texture : Texture?, shader : Shader)
@blend_mode = uninitialized BlendMode
@transform = uninitialized Transform
@texture = uninitialized Void*
@shader = uninitialized Void*
@_renderstates_texture = texture
@_renderstates_shader = shader
VoidCSFML.sfml_renderstates_initialize_PG5FPeMXd8P6(to_unsafe, blend_mode, transform, texture, shader)
end
@blend_mode : BlendMode
# Blending mode
def blend_mode : BlendMode
@blend_mode
end
def blend_mode=(blend_mode : BlendMode)
@blend_mode = blend_mode
end
@transform : Transform
# Transform
def transform : Transform
@transform
end
def transform=(transform : Transform)
@transform = transform
end
@texture : Void*
# Texture
def texture : Texture?
@_renderstates_texture
end
def texture=(texture : Texture?)
@_renderstates_texture = texture
@texture = texture ? texture.to_unsafe : Pointer(Void).null
end
@_renderstates_texture : Texture? = nil
@shader : Void*
# Shader
def shader : Shader?
@_renderstates_shader
end
def shader=(shader : Shader)
@_renderstates_shader = shader
@shader = shader ? shader.to_unsafe : Pointer(Void).null
end
@_renderstates_shader : Shader? = nil
# :nodoc:
def to_unsafe()
pointerof(@blend_mode).as(Void*)
end
# :nodoc:
def initialize(copy : RenderStates)
@blend_mode = uninitialized BlendMode
@transform = uninitialized Transform
@texture = uninitialized Void*
@shader = uninitialized Void*
VoidCSFML.sfml_renderstates_initialize_mi4(to_unsafe, copy)
end
def dup() : self
return typeof(self).new(self)
end
end
# Abstract module for objects that can be drawn
# to a render target
#
# `SF::Drawable` is a very simple module that allows objects
# of derived classes to be drawn to a `SF::RenderTarget`.
#
# All you have to do in your derived class is to implement the
# `draw` function.
#
# Note that including `SF::Drawable` is not mandatory,
# but it allows this nice syntax `window.draw(object)` rather
# than `object.draw(window)`, which is more consistent with other
# SFML classes.
#
# Example:
# ```
# class MyDrawable
# include SF::Drawable
# def draw(target : SF::RenderTarget, states : SF::RenderStates)
# # You can draw other high-level objects
# target.draw(@sprite, states)
#
# # ... or use the low-level API
# states.texture = @texture
# target.draw(@vertices, states)
#
# # ... or draw with OpenGL directly
# glBegin(GL_QUADS)
# ...
# glEnd()
# end
#
# @sprite : SF::Sprite
# @texture : SF::Texture
# @vertices : SF::VertexArray
# end
# ```
#
# *See also:* `SF::RenderTarget`
module Drawable
end
# Decomposed transform defined by a position, a rotation and a scale
#
# This class is provided for convenience, on top of `SF::Transform`.
#
# `SF::Transform`, as a low-level class, offers a great level of
# flexibility but it is not always convenient to manage. Indeed,
# one can easily combine any kind of operation, such as a translation
# followed by a rotation followed by a scaling, but once the result
# transform is built, there's no way to go backward and, let's say,
# change only the rotation without modifying the translation and scaling.
# The entire transform must be recomputed, which means that you
# need to retrieve the initial translation and scale factors as
# well, and combine them the same way you did before updating the
# rotation. This is a tedious operation, and it requires to store
# all the individual components of the final transform.
#
# That's exactly what `SF::Transformable` was written for: it hides
# these variables and the composed transform behind an easy to use
# interface. You can set or get any of the individual components
# without worrying about the others. It also provides the composed
# transform (as a `SF::Transform`), and keeps it up-to-date.
#
# In addition to the position, rotation and scale, `SF::Transformable`
# provides an "origin" component, which represents the local origin
# of the three other components. Let's take an example with a 10x10
# pixels sprite. By default, the sprite is positioned/rotated/scaled
# relatively to its top-left corner, because it is the local point
# (0, 0). But if we change the origin to be (5, 5), the sprite will
# be positioned/rotated/scaled around its center instead. And if
# we set the origin to (10, 10), it will be transformed around its
# bottom-right corner.
#
# To keep the `SF::Transformable` class simple, there's only one
# origin for all the components. You cannot position the sprite
# relatively to its top-left corner while rotating it around its
# center, for example. To do such things, use `SF::Transform` directly.
#
# `SF::Transformable` can be used as a base class. It is often
# combined with `SF::Drawable` -- that's what SFML's sprites,
# texts and shapes do.
# ```
# class MyEntity < SF::Transformable
# include SF::Drawable
#
# def draw(target, states)
# states.transform *= self.transform
# target.draw(..., states)
# end
# end
#
# entity = MyEntity.new
# entity.position = {10, 20}
# entity.rotation = 45
# window.draw entity
# ```
#
# It can also be used as a member, if you don't want to use
# its API directly (because you don't need all its functions,
# or you have different naming conventions for example).
# ```
# class MyEntity
# @transform : SF::Transformable
# forward_missing_to @transform
# end
# ```
#
# A note on coordinates and undistorted rendering:
# By default, SFML (or more exactly, OpenGL) may interpolate drawable objects
# such as sprites or texts when rendering. While this allows transitions
# like slow movements or rotations to appear smoothly, it can lead to
# unwanted results in some cases, for example blurred or distorted objects.
# In order to render a `SF::Drawable` object pixel-perfectly, make sure
# the involved coordinates allow a 1:1 mapping of pixels in the window
# to texels (pixels in the texture). More specifically, this means:
# * The object's position, origin and scale have no fractional part
# * The object's and the view's rotation are a multiple of 90 degrees
# * The view's center and size have no fractional part
#
# *See also:* `SF::Transform`
class Transformable
@this : Void*
# Default constructor
def initialize()
VoidCSFML.sfml_transformable_allocate(out @this)
VoidCSFML.sfml_transformable_initialize(to_unsafe)
end
# Virtual destructor
def finalize()
VoidCSFML.sfml_transformable_finalize(to_unsafe)
VoidCSFML.sfml_transformable_free(@this)
end
# set the position of the object
#
# This function completely overwrites the previous position.
# See the move function to apply an offset based on the previous position instead.
# The default position of a transformable object is (0, 0).
#
# * *x* - X coordinate of the new position
# * *y* - Y coordinate of the new position
#
# *See also:* `move`, `position`
def set_position(x : Number, y : Number)
VoidCSFML.sfml_transformable_setposition_Bw9Bw9(to_unsafe, LibC::Float.new(x), LibC::Float.new(y))
end
# set the position of the object
#
# This function completely overwrites the previous position.
# See the move function to apply an offset based on the previous position instead.
# The default position of a transformable object is (0, 0).
#
# * *position* - New position
#
# *See also:* `move`, `position`
def position=(position : Vector2|Tuple)
position = Vector2f.new(position[0].to_f32, position[1].to_f32)
VoidCSFML.sfml_transformable_setposition_UU2(to_unsafe, position)
end
# set the orientation of the object
#
# This function completely overwrites the previous rotation.
# See the rotate function to add an angle based on the previous rotation instead.
# The default rotation of a transformable object is 0.
#
# * *angle* - New rotation, in degrees
#
# *See also:* `rotate`, `rotation`
def rotation=(angle : Number)
VoidCSFML.sfml_transformable_setrotation_Bw9(to_unsafe, LibC::Float.new(angle))
end
# set the scale factors of the object
#
# This function completely overwrites the previous scale.
# See the scale function to add a factor based on the previous scale instead.
# The default scale of a transformable object is (1, 1).
#
# * *factor_x* - New horizontal scale factor
# * *factor_y* - New vertical scale factor
#
# *See also:* `scale`, `scale`
def set_scale(factor_x : Number, factor_y : Number)
VoidCSFML.sfml_transformable_setscale_Bw9Bw9(to_unsafe, LibC::Float.new(factor_x), LibC::Float.new(factor_y))
end
# set the scale factors of the object
#
# This function completely overwrites the previous scale.
# See the scale function to add a factor based on the previous scale instead.
# The default scale of a transformable object is (1, 1).
#
# * *factors* - New scale factors