Skip to content

Commit f5ac56a

Browse files
committed
Update examples
1 parent cc02e37 commit f5ac56a

File tree

5 files changed

+62
-78
lines changed

5 files changed

+62
-78
lines changed

examples/diagnostics.cr

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ def test_mouse()
6060
m = SF::Mouse.get_position($window)
6161

6262
shape = SF::CircleShape.new(15)
63-
shape.origin = SF.vector2f(15, 15)
64-
shape.position = SF.vector2f(m.x, m.y)
63+
shape.origin = SF.vector2(15, 15)
64+
shape.position = SF.vector2(m.x, m.y)
6565
shape.fill_color = SF.color(0, 200, 0)
66-
shape.scale SF.vector2f(0.9, 1.1)
66+
shape.scale SF.vector2(0.9, 1.1)
6767
$window.draw shape
6868

6969
shape = SF::CircleShape.new(8)
70-
shape.origin = SF.vector2f(8, 8)
70+
shape.origin = SF.vector2(8, 8)
7171
shape.fill_color = SF.color(255, 128, 0)
7272

7373
buttons = {
@@ -79,18 +79,18 @@ def test_mouse()
7979
}
8080
buttons.each do |btn, delta|
8181
if SF::Mouse.is_button_pressed(btn)
82-
shape.position = SF.vector2f(m.x + delta[0]*20, m.y + delta[1]*20)
82+
shape.position = m + SF.vector2(20, 20) * delta
8383
$window.draw shape
8484
end
8585
end
8686

8787
shape = SF::ConvexShape.new()
8888
shape.point_count = 3
8989
shape.fill_color = SF.color(128, 0, 255)
90-
shape[0] = SF.vector2f(-8, 0)
91-
shape[1] = SF.vector2f(8, 0)
92-
shape[2] = SF.vector2f(0, -wheel_delta*5)
93-
shape.position = SF.vector2f(m.x, m.y-4)
90+
shape[0] = SF.vector2(-8, 0)
91+
shape[1] = SF.vector2(8, 0)
92+
shape[2] = SF.vector2(0, -wheel_delta*5)
93+
shape.position = m - {0, 4}
9494
$window.draw shape
9595

9696
$window.display()
@@ -116,7 +116,7 @@ def test_controller()
116116
$window.draw text
117117

118118
shape = SF::CircleShape.new(15)
119-
shape.origin = SF.vector2f(15, 15)
119+
shape.origin = SF.vector2(15, 15)
120120

121121
text = SF::Text.new("", $font, 20)
122122
text.color = SF.color(0, 0, 0)
@@ -133,13 +133,13 @@ def test_controller()
133133
]
134134
(0...SF::Joystick.get_button_count(js)).each do |btn|
135135
text.string = (btn+1).to_s
136-
text.origin = SF.vector2f(text.local_bounds.width * 0.6, text.local_bounds.height * 0.85)
136+
text.origin = SF.vector2(text.local_bounds.width * 0.6, text.local_bounds.height * 0.85)
137137
begin
138138
delta = button_pos[btn]
139139
rescue
140140
delta = {0, button_pos.length - btn - 1}
141141
end
142-
shape.position = SF.vector2f(400 + delta[0]*30, 300 + delta[1]*30)
142+
shape.position = SF.vector2(400, 300) + SF.vector2(delta) * {30, 30}
143143
text.position = shape.position
144144
shape.fill_color = SF::Joystick.is_button_pressed(js, btn) ? SF.color(255, 128, 0): SF.color(0, 128, 0)
145145

@@ -148,7 +148,7 @@ def test_controller()
148148
end
149149

150150
shape = SF::CircleShape.new(10)
151-
shape.origin = SF.vector2f(10, 10)
151+
shape.origin = SF.vector2(10, 10)
152152
shape.fill_color = SF.color(128, 0, 255)
153153

154154
axis_pos = [
@@ -172,7 +172,7 @@ def test_controller()
172172
any = true
173173
end
174174
next unless any
175-
shape.position = SF.vector2f(400 + delta[0]*30 + dx*0.3, 300 + delta[1]*30 + dy*0.3)
175+
shape.position = SF.vector2(400, 300) + SF.vector2(delta) * {30, 30} + {dx*0.3, dy*0.3}
176176

177177
$window.draw shape
178178
end
@@ -192,10 +192,10 @@ class Button < SF::RectangleShape
192192
include SF::TransformableM
193193

194194
def initialize(message, width, height, color=SF.color(0, 128, 0))
195-
super(SF.vector2f(width, height))
195+
super(SF.vector2(width, height))
196196
@text = SF::Text.new(message, $font, (height*0.8).to_i)
197197
self.fill_color = color
198-
@text.position = SF.vector2f((width - @text.global_bounds.width)*0.5, -height/20)
198+
@text.position = SF.vector2((width - @text.global_bounds.width) / 2, -height / 20)
199199
end
200200

201201
def draw(target, states: RenderStates)
@@ -218,7 +218,7 @@ actions = {
218218
}
219219

220220
actions.each_key do |btn|
221-
btn.position = SF.vector2f(x, y)
221+
btn.position = SF.vector2(x, y)
222222
y += h + h/2
223223
end
224224

examples/flippy_bird.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ bird_texture = SF::Texture.from_file("resources/bird.png")
99
sz = bird_texture.size
1010

1111
bird = SF::Sprite.new(bird_texture)
12-
bird.origin = SF.vector2f(sz.x / 2.0, sz.y / 2.0)
13-
bird.scale = SF.vector2f(2.5, 2.5)
14-
bird.position = SF.vector2f(250, 300)
12+
bird.origin = SF.vector2(sz.x / 2.0, sz.y / 2.0)
13+
bird.scale = SF.vector2(2.5, 2.5)
14+
bird.position = SF.vector2(250, 300)
1515

1616
speed = 0.0
1717

@@ -30,7 +30,7 @@ while window.open?
3030
end
3131

3232
speed += 0.3
33-
bird.move SF.vector2f(0.0, speed)
33+
bird.move SF.vector2(0.0, speed)
3434
bird.rotation = speed*8.0 < 90.0 ? speed*8.0 : 90.0
3535

3636
window.clear SF.color(112, 197, 206)

examples/shader.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ px_shader.set_parameter "texture", SF::Shader::CurrentTexture
1717
ipsum = HTTP::Client.get("http://loripsum.net/api/12/short/plaintext").body
1818
font = SF::Font.from_file("resources/font/Ubuntu-R.ttf")
1919
text = SF::Text.new(ipsum, font, 22)
20-
text.position = SF.vector2f(30, 20)
20+
text.position = {30, 20}
2121
text.color = SF::Color::Black
2222
wb_shader = SF::Shader.from_file("resources/shaders/wave.vert", "resources/shaders/blur.frag")
2323

examples/snakes.cr

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
require "crsfml"
22

33

4-
Left = {-1, 0}
5-
Up = {0, -1}
6-
Right = {1, 0}
7-
Down = {0, 1}
4+
Left = SF.vector2(-1, 0)
5+
Up = SF.vector2(0, -1)
6+
Right = SF.vector2(1, 0)
7+
Down = SF.vector2(0, 1)
88
Directions = [Left, Up, Right, Down]
99

1010

11-
# https://github.com/manastech/crystal/issues/559
12-
def modulo(a: Int, b: Int)
13-
(a % b + b) % b
14-
end
15-
1611
# Missing functionality from Ruby
1712
module Enumerable
1813
def drop(n)
@@ -34,7 +29,7 @@ struct Food
3429

3530
def draw(target, states)
3631
circle = SF::CircleShape.new(0.9/2)
37-
circle.position = SF.vector2f(@position[0] + 0.05, @position[1] + 0.05)
32+
circle.position = position + {0.05, 0.05}
3833
circle.fill_color = @color
3934
target.draw circle, states
4035
end
@@ -45,48 +40,47 @@ class Snake
4540

4641
def initialize(@field, start, @color)
4742
@direction = Up
48-
@body = [] of {Int32, Int32}
43+
@body = [] of SF::Vector2(Int32)
4944
(0...3).each do |i|
50-
@body.push({start[0], start[1] + i})
45+
@body.push(start + {0, i})
5146
end
5247
end
5348

5449
def step()
55-
head = {@body[0][0] + @direction[0], @body[0][1] + @direction[1]}
56-
head = {modulo(head[0], @field.size[0]), modulo(head[1], @field.size[1])}
50+
head = @body[0] + @direction
51+
head.x %= @field.size.x
52+
head.y %= @field.size.y
5753
@body.insert(0, head)
5854
@body.pop()
5955
end
6056

6157
def turn(direction)
62-
if @body[1] != {@body[0][0] + direction[0], @body[0][1] + direction[1]}
63-
@direction = direction
64-
end
58+
@direction = direction unless @body[1] == @body[0] + direction
6559
end
6660

6761
def grow()
6862
tail = @body[-1]
69-
3.times do |i|
63+
3.times do
7064
@body.push tail
7165
end
7266
end
7367

74-
def collide(other: self)
68+
def collides?(other: self)
7569
other.body.any? { |part| @body[0] == part }
7670
end
7771

78-
def collide(food: Food)
72+
def collides?(food: Food)
7973
@body[0] == food.position
8074
end
8175

82-
def collide()
76+
def collides?()
8377
@body.drop(1).any? { |part| @body[0] == part }
8478
end
8579

8680
def draw(target, states)
8781
@body.each_with_index do |current, i|
8882
segment = SF::CircleShape.new(0.9 / 2)
89-
segment.position = SF.vector2f(current[0] + 0.05, current[1] + 0.05)
83+
segment.position = current + {0.05, 0.05}
9084
segment.fill_color = @color
9185
target.draw segment, states
9286

@@ -96,18 +90,14 @@ class Snake
9690
# Look in 4 directions around this segment. If there is another one
9791
# neighboring it, draw a square between them
9892
Directions.each do |d|
99-
td = {
100-
modulo((current[0] + d[0]), @field.size[0]),
101-
modulo((current[1] + d[1]), @field.size[1])
102-
}
103-
93+
td = current + d
94+
td.x %= @field.size.x
95+
td.y %= @field.size.y
96+
10497
if (i > 0 && td == @body[i-1]) ||\
10598
(i < @body.length-1 && td == @body[i+1])
106-
connection = SF::RectangleShape.new(SF.vector2f(0.9, 0.9))
107-
connection.position = SF.vector2f(
108-
current[0] + d[0] / 2.0 + 0.05,
109-
current[1] + d[1] / 2.0 + 0.05
110-
)
99+
connection = SF::RectangleShape.new({0.9, 0.9})
100+
connection.position = current + d / {2.0, 2.0} + {0.05, 0.05}
111101
connection.fill_color = @color
112102
target.draw connection, states
113103
end
@@ -119,19 +109,13 @@ class Snake
119109
@color.r / 3, @color.g / 3, @color.b / 3
120110
)
121111

122-
delta = {
123-
@direction[1].abs / 4.0,
124-
@direction[0].abs / 4.0
125-
}
126-
eye.position = SF.vector2f(
127-
@body[0][0] + 0.4 + delta[0],
128-
@body[0][1] + 0.4 + delta[1]
112+
delta = SF.vector2(
113+
@direction.y.abs / 4.0,
114+
@direction.x.abs / 4.0
129115
)
116+
eye.position = @body[0] + {0.4, 0.4} + delta
130117
target.draw eye, states
131-
eye.position = SF.vector2f(
132-
@body[0][0] + 0.4 - delta[0],
133-
@body[0][1] + 0.4 - delta[1]
134-
)
118+
eye.position = @body[0] + {0.4, 0.4} - delta
135119
target.draw eye, states
136120
end
137121
end
@@ -151,7 +135,7 @@ class Field
151135

152136
def step()
153137
while @foods.length < @snakes.length + 1
154-
food = Food.new({rand(@size[0]), rand(@size[1])}, random_color())
138+
food = Food.new(SF.vector2(rand(@size.x), rand(@size.y)), random_color())
155139

156140
@foods.push food unless @snakes.any? do |snake|
157141
snake.body.any? { |part| part == food.position }
@@ -162,7 +146,7 @@ class Field
162146
snake.step()
163147

164148
@foods = @foods.reject do |food|
165-
if snake.collide food
149+
if snake.collides? food
166150
snake.grow()
167151
true
168152
end
@@ -171,8 +155,8 @@ class Field
171155

172156
snakes = @snakes
173157
@snakes = snakes.reject do |snake|
174-
snake.collide ||\
175-
snakes.any? { |snake2| snake != snake2 && snake.collide snake2 }
158+
snake.collides? ||\
159+
snakes.any? { |snake2| snake != snake2 && snake.collides? snake2 }
176160
end
177161
end
178162

@@ -187,17 +171,17 @@ class Field
187171
end
188172

189173

190-
field = Field.new({40, 40})
174+
field = Field.new(SF.vector2(40, 40))
191175

192-
snake1 = Snake.new(field, {field.size[0] / 2 - 5, field.size[1] / 2}, random_color())
193-
snake2 = Snake.new(field, {field.size[0] / 2 + 5, field.size[1] / 2}, random_color())
176+
snake1 = Snake.new(field, field.size / {2, 2} - {5, 0}, random_color())
177+
snake2 = Snake.new(field, field.size / {2, 2} + {5, 0}, random_color())
194178
field.add snake1
195179
field.add snake2
196180

197181
scale = 20
198182

199183
window = SF::RenderWindow.new(
200-
SF.video_mode(field.size[0]*scale, field.size[1]*scale), "Snakes",
184+
SF.video_mode(field.size.x*scale, field.size.y*scale), "Snakes",
201185
settings: SF.context_settings(depth: 32, antialiasing: 8)
202186
)
203187
window.vertical_sync_enabled = true

examples/transformable.cr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class Logo
99
def initialize(message="CrSFML")
1010
@text = SF::Text.new(message, $font, 200)
1111
bounds = @text.local_bounds
12-
@shape = SF::RectangleShape.new(SF.vector2f(bounds.width*1.2, bounds.height*2))
12+
@shape = SF::RectangleShape.new(SF.vector2(bounds.width*1.2, bounds.height*2))
1313
@shape.fill_color = SF.color(0, 0, 128)
14-
@shape.origin = SF.vector2f(@shape.size.x / 2, @shape.size.y / 2)
15-
@text.origin = SF.vector2f(bounds.width / 2, bounds.height * 0.8)
14+
@shape.origin = @shape.size / {2, 2}
15+
@text.origin = SF.vector2(bounds.width / 2, bounds.height * 0.8)
1616
end
1717

1818
def draw(target, states: RenderStates)
@@ -28,7 +28,7 @@ window.vertical_sync_enabled = true
2828

2929
logo = Logo.new()
3030

31-
logo.position = SF.vector2f(400, 300)
31+
logo.position = SF.vector2(400, 300)
3232

3333
clock = SF::Clock.new()
3434

@@ -43,7 +43,7 @@ while window.open?
4343
t = clock.elapsed_time.as_seconds
4444

4545
logo.rotation = t*50
46-
logo.scale = SF.vector2f(Math.sin(t), Math.cos(t))
46+
logo.scale = SF.vector2(Math.sin(t), Math.cos(t))
4747

4848
window.clear
4949
window.draw logo

0 commit comments

Comments
 (0)