Skip to content

Commit af86723

Browse files
committed
Fix according to breaking changes in Crystal
1 parent fa35b1c commit af86723

File tree

13 files changed

+92
-59
lines changed

13 files changed

+92
-59
lines changed

examples/diagnostics.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_controller()
146146
begin
147147
delta = button_pos[btn]
148148
rescue
149-
delta = {0, button_pos.length - btn - 1}
149+
delta = {0, button_pos.size - btn - 1}
150150
end
151151
shape.position = SF.vector2(400, 300) + SF.vector2(delta) * {30, 30}
152152
text.position = shape.position

examples/shapes.cr

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
require "crsfml"
22

3+
class CustomShape < SF::Shape
4+
def point_count
5+
3
6+
end
7+
def get_point(i)
8+
case i
9+
when 0
10+
SF.vector2(100, 200)
11+
when 1
12+
SF.vector2(200, 200)
13+
else
14+
SF.vector2(200, 100)
15+
end
16+
end
17+
end
18+
319
window = SF::RenderWindow.new(
420
SF.video_mode(200, 200), "Shapes",
521
settings: SF.context_settings(depth: 24, antialiasing: 8)
@@ -37,21 +53,6 @@ while window.open?
3753
window.draw convex_shape
3854

3955
# Bottom right
40-
class CustomShape < SF::Shape
41-
def point_count
42-
3
43-
end
44-
def get_point(i)
45-
case i
46-
when 0
47-
SF.vector2(100, 200)
48-
when 1
49-
SF.vector2(200, 200)
50-
else
51-
SF.vector2(200, 100)
52-
end
53-
end
54-
end
5556
custom_shape = CustomShape.new()
5657
custom_shape.fill_color = SF::Color::Green
5758
window.draw custom_shape

examples/snakes.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Directions = [Left, Up, Right, Down]
1111
# Missing functionality from Ruby
1212
module Enumerable
1313
def drop(n)
14-
self[n...length]
14+
self[n...size]
1515
end
1616
end
1717

@@ -95,7 +95,7 @@ class Snake
9595
td.y %= @field.size.y
9696

9797
if (i > 0 && td == @body[i-1]) ||\
98-
(i < @body.length-1 && td == @body[i+1])
98+
(i < @body.size-1 && td == @body[i+1])
9999
connection = SF::RectangleShape.new({0.9, 0.9})
100100
connection.position = current + d / 2.0 + {0.05, 0.05}
101101
connection.fill_color = @color
@@ -134,7 +134,7 @@ class Field
134134
end
135135

136136
def step()
137-
while @foods.length < @snakes.length + 1
137+
while @foods.size < @snakes.size + 1
138138
food = Food.new(SF.vector2(rand(@size.x), rand(@size.y)), random_color())
139139

140140
@foods.push food unless @snakes.any? do |snake|

examples/sound_capture.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ unless SF::SoundRecorder.available?
1111
end
1212

1313
puts "Please choose the sample rate for sound capture (44100 is CD quality):"
14-
sample_rate = gets().to_i
14+
sample_rate = gets().not_nil!.to_i
1515

1616
recorder = SF::SoundBufferRecorder.new()
1717

generate/generate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def handle_function(main, params, alias=None):
419419
conv.append('{0} = {0}.to_i32'.format(n))
420420
elif t == 'LibC::SizeT':
421421
t = 'Int'
422-
conv.append('{0} = LibC::SizeT.cast({0})'.format(n))
422+
conv.append('{0} = LibC::SizeT.new({0})'.format(n))
423423
elif t == 'UInt64':
424424
t = 'Int'
425425
conv.append('{0} = {0}.to_u64'.format(n))
@@ -433,9 +433,9 @@ def handle_function(main, params, alias=None):
433433
conv.append('{0} = SF.{2}({0}) unless {0}.is_a? {1}'.format(n, t, t.lower()))
434434
t = None
435435
elif t == 'Slice|Array':
436-
conv.append('{0} = ({1}.to_unsafe as Pointer(Void)), LibC::SizeT.cast({1}.length*sizeof(typeof({1}[0])))'.format(n, n.split(', ')[0]))
436+
conv.append('{0} = ({1}.to_unsafe as Pointer(Void)), LibC::SizeT.new({1}.size*sizeof(typeof({1}[0])))'.format(n, n.split(', ')[0]))
437437
elif 'Slice(' in t:
438-
conv.append('{0} = {1}.to_unsafe, LibC::SizeT.cast({1}.length*sizeof(typeof({1}[0])))'.format(n, n.split(', ')[0]))
438+
conv.append('{0} = {1}.to_unsafe, LibC::SizeT.new({1}.size*sizeof(typeof({1}[0])))'.format(n, n.split(', ')[0]))
439439
if n in const and t not in classes:
440440
conv += (
441441
'if {0}.responds_to?(:to_unsafe); '

src/audio.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ module SF
7676
->(data: CSFML::SoundStreamChunk*) {
7777
slice = on_get_data()
7878
data.value.samples = slice.to_unsafe
79-
data.value.sample_count = slice.length
80-
slice.length > 0 ? 1 : 0
79+
data.value.sample_count = slice.size
80+
slice.size > 0 ? 1 : 0
8181
},
8282
->(time_offset: Time) { on_seek(time_offset); nil }
8383
})

src/audio_obj.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ module SF
180180
#
181181
# *Returns*: A new Music object (raises `NullResult` if failed)
182182
def self.from_memory(data: Slice|Array)
183-
data, size_in_bytes = (data.to_unsafe as Pointer(Void)), LibC::SizeT.cast(data.length*sizeof(typeof(data[0])))
183+
data, size_in_bytes = (data.to_unsafe as Pointer(Void)), LibC::SizeT.new(data.size*sizeof(typeof(data[0])))
184184
Music.transfer_ptr(CSFML.music_create_from_memory(data, size_in_bytes))
185185
end
186186

@@ -877,7 +877,7 @@ module SF
877877
#
878878
# *Returns*: A new SoundBuffer object (raises `NullResult` if failed)
879879
def self.from_memory(data: Slice|Array)
880-
data, size_in_bytes = (data.to_unsafe as Pointer(Void)), LibC::SizeT.cast(data.length*sizeof(typeof(data[0])))
880+
data, size_in_bytes = (data.to_unsafe as Pointer(Void)), LibC::SizeT.new(data.size*sizeof(typeof(data[0])))
881881
SoundBuffer.transfer_ptr(CSFML.sound_buffer_create_from_memory(data, size_in_bytes))
882882
end
883883

@@ -910,7 +910,7 @@ module SF
910910
#
911911
# *Returns*: A new SoundBuffer object (raises `NullResult` if failed)
912912
def self.from_samples(samples: Slice(Int16)|Array(Int16), channel_count: Int, sample_rate: Int)
913-
samples, sample_count = samples.to_unsafe, LibC::SizeT.cast(samples.length*sizeof(typeof(samples[0])))
913+
samples, sample_count = samples.to_unsafe, LibC::SizeT.new(samples.size*sizeof(typeof(samples[0])))
914914
channel_count = channel_count.to_i32
915915
sample_rate = sample_rate.to_i32
916916
SoundBuffer.transfer_ptr(CSFML.sound_buffer_create_from_samples(samples, sample_count, channel_count, sample_rate))

src/common_obj.cr

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,35 @@ module SF
6363
end
6464
end
6565
end
66+
67+
# Backwards compatibility
68+
def Int8.new(value)
69+
value.to_i8
70+
end
71+
def Int16.new(value)
72+
value.to_i16
73+
end
74+
def Int32.new(value)
75+
value.to_i32
76+
end
77+
def Int64.new(value)
78+
value.to_i64
79+
end
80+
def UInt8.new(value)
81+
value.to_u8
82+
end
83+
def UInt16.new(value)
84+
value.to_u16
85+
end
86+
def UInt32.new(value)
87+
value.to_u32
88+
end
89+
def UInt64.new(value)
90+
value.to_u64
91+
end
92+
def Float32.new(value)
93+
value.to_f32
94+
end
95+
def Float64.new(value)
96+
value.to_f64
97+
end

src/graphics.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ module SF
379379
else
380380
pstates = nil
381381
end
382-
CSFML.render_texture_draw_primitives(@this, vertices, LibC::SizeT.cast(vertices.length), type, pstates)
382+
CSFML.render_texture_draw_primitives(@this, vertices, LibC::SizeT.new(vertices.size), type, pstates)
383383
end
384384

385385
def map_pixel_to_coords(point)
@@ -406,7 +406,7 @@ module SF
406406
else
407407
pstates = nil
408408
end
409-
CSFML.render_window_draw_primitives(@this, vertices, LibC::SizeT.cast(vertices.length), type, pstates)
409+
CSFML.render_window_draw_primitives(@this, vertices, LibC::SizeT.new(vertices.size), type, pstates)
410410
end
411411

412412
def poll_event()

src/graphics_obj.cr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ module SF
544544
#
545545
# *Returns*: Index-th point of the shape
546546
def get_point(index: Int)
547-
index = LibC::SizeT.cast(index)
547+
index = LibC::SizeT.new(index)
548548
SF.vector2(CSFML.circle_shape_get_point(@this, index))
549549
end
550550

@@ -577,7 +577,7 @@ module SF
577577
# * `shape`: Shape object
578578
# * `count`: New number of points of the circle
579579
def point_count=(count: Int)
580-
count = LibC::SizeT.cast(count)
580+
count = LibC::SizeT.new(count)
581581
CSFML.circle_shape_set_point_count(@this, count)
582582
end
583583

@@ -984,7 +984,7 @@ module SF
984984
#
985985
# *Returns*: Index-th point of the shape
986986
def get_point(index: Int)
987-
index = LibC::SizeT.cast(index)
987+
index = LibC::SizeT.new(index)
988988
SF.vector2(CSFML.convex_shape_get_point(@this, index))
989989
end
990990

@@ -997,7 +997,7 @@ module SF
997997
# * `shape`: Shape object
998998
# * `count`: New number of points of the shape
999999
def point_count=(count: Int)
1000-
count = LibC::SizeT.cast(count)
1000+
count = LibC::SizeT.new(count)
10011001
CSFML.convex_shape_set_point_count(@this, count)
10021002
end
10031003

@@ -1015,7 +1015,7 @@ module SF
10151015
# * `index`: Index of the point to change, in range [0 .. GetPointCount() - 1]
10161016
# * `point`: New point
10171017
def set_point(index: Int, point)
1018-
index = LibC::SizeT.cast(index)
1018+
index = LibC::SizeT.new(index)
10191019
point = SF.vector2f(point) unless point.is_a? Vector2f
10201020
CSFML.convex_shape_set_point(@this, index, point)
10211021
end
@@ -1079,7 +1079,7 @@ module SF
10791079
#
10801080
# *Returns*: A new Font object, or raises `NullResult` if it failed
10811081
def self.from_memory(data: Slice|Array)
1082-
data, size_in_bytes = (data.to_unsafe as Pointer(Void)), LibC::SizeT.cast(data.length*sizeof(typeof(data[0])))
1082+
data, size_in_bytes = (data.to_unsafe as Pointer(Void)), LibC::SizeT.new(data.size*sizeof(typeof(data[0])))
10831083
Font.transfer_ptr(CSFML.font_create_from_memory(data, size_in_bytes))
10841084
end
10851085

@@ -1304,7 +1304,7 @@ module SF
13041304
#
13051305
# *Returns*: A new Image object, or raises `NullResult` if it failed
13061306
def self.from_memory(data: Slice|Array)
1307-
data, size = (data.to_unsafe as Pointer(Void)), LibC::SizeT.cast(data.length*sizeof(typeof(data[0])))
1307+
data, size = (data.to_unsafe as Pointer(Void)), LibC::SizeT.new(data.size*sizeof(typeof(data[0])))
13081308
Image.transfer_ptr(CSFML.image_create_from_memory(data, size))
13091309
end
13101310

@@ -2165,7 +2165,7 @@ module SF
21652165
#
21662166
# *Returns*: Index-th point of the shape
21672167
def get_point(index: Int)
2168-
index = LibC::SizeT.cast(index)
2168+
index = LibC::SizeT.new(index)
21692169
SF.vector2(CSFML.rectangle_shape_get_point(@this, index))
21702170
end
21712171

@@ -2466,7 +2466,7 @@ module SF
24662466
# * `type`: Type of primitives to draw
24672467
# * `states`: Render states to use for drawing (NULL to use the default states)
24682468
def draw_primitives(vertices: Slice(Vertex)|Array(Vertex), type: PrimitiveType, states)
2469-
vertices, vertex_count = vertices.to_unsafe, LibC::SizeT.cast(vertices.length*sizeof(typeof(vertices[0])))
2469+
vertices, vertex_count = vertices.to_unsafe, LibC::SizeT.new(vertices.size*sizeof(typeof(vertices[0])))
24702470
if states.responds_to?(:to_unsafe); pstates = states.to_unsafe
24712471
elsif states; cstates = states; pstates = pointerof(cstates)
24722472
else; pstates = nil; end
@@ -3052,7 +3052,7 @@ module SF
30523052
# * `type`: Type of primitives to draw
30533053
# * `states`: Render states to use for drawing (NULL to use the default states)
30543054
def draw_primitives(vertices: Slice(Vertex)|Array(Vertex), type: PrimitiveType, states)
3055-
vertices, vertex_count = vertices.to_unsafe, LibC::SizeT.cast(vertices.length*sizeof(typeof(vertices[0])))
3055+
vertices, vertex_count = vertices.to_unsafe, LibC::SizeT.new(vertices.size*sizeof(typeof(vertices[0])))
30563056
if states.responds_to?(:to_unsafe); pstates = states.to_unsafe
30573057
elsif states; cstates = states; pstates = pointerof(cstates)
30583058
else; pstates = nil; end
@@ -3815,7 +3815,7 @@ module SF
38153815
#
38163816
# *Returns*: Position of the character
38173817
def find_character_pos(index: Int)
3818-
index = LibC::SizeT.cast(index)
3818+
index = LibC::SizeT.new(index)
38193819
SF.vector2(CSFML.text_find_character_pos(@this, index))
38203820
end
38213821

@@ -3898,7 +3898,7 @@ module SF
38983898
#
38993899
# *Returns*: A new Texture object, or raises `NullResult` if it failed
39003900
def self.from_memory(data: Slice|Array, area)
3901-
data, size_in_bytes = (data.to_unsafe as Pointer(Void)), LibC::SizeT.cast(data.length*sizeof(typeof(data[0])))
3901+
data, size_in_bytes = (data.to_unsafe as Pointer(Void)), LibC::SizeT.new(data.size*sizeof(typeof(data[0])))
39023902
if area.responds_to?(:to_unsafe); parea = area.to_unsafe
39033903
elsif area; carea = area; parea = pointerof(carea)
39043904
else; parea = nil; end
@@ -4399,7 +4399,7 @@ module SF
43994399
#
44004400
# *Returns*: Pointer to the index-th vertex
44014401
def get_vertex(index: Int)
4402-
index = LibC::SizeT.cast(index)
4402+
index = LibC::SizeT.new(index)
44034403
CSFML.vertex_array_get_vertex(@this, index)
44044404
end
44054405

@@ -4430,7 +4430,7 @@ module SF
44304430
# * `vertex_array`: Vertex array objet
44314431
# * `vertex_count`: New size of the array (number of vertices)
44324432
def resize(vertex_count: Int)
4433-
vertex_count = LibC::SizeT.cast(vertex_count)
4433+
vertex_count = LibC::SizeT.new(vertex_count)
44344434
CSFML.vertex_array_resize(@this, vertex_count)
44354435
end
44364436

0 commit comments

Comments
 (0)