Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Showcase of API differences in the new CrSFML
diff --git a/examples/diagnostics.cr b/examples/diagnostics.cr
index 3e1a86a..6266e54 100644
--- a/examples/diagnostics.cr
+++ b/examples/diagnostics.cr
@@ -7,8 +7,8 @@ $font : SF::Font
$font = SF::Font.from_file("resources/font/Ubuntu-R.ttf")
$window = SF::RenderWindow.new(
- SF.video_mode(800, 600), "Diagnostic information",
- settings: SF.context_settings(depth: 24, antialiasing: 8)
+ SF::VideoMode.new(800, 600), "Diagnostic information",
+ settings: SF::ContextSettings.new(depth: 24, antialiasing: 8)
)
$window.framerate_limit = 30
@@ -47,7 +47,7 @@ def test_mouse()
wheel_delta = SF.vector2(0.0, 0.0)
while true
while event = $window.poll_event()
- case event.type
+ case event
when SF::Event::Closed
$window.mouse_cursor_visible = true
return
@@ -84,7 +84,7 @@ def test_mouse()
SF::Mouse::XButton2 => {1, 0},
}
buttons.each do |btn, delta|
- if SF::Mouse.is_button_pressed(btn)
+ if SF::Mouse.button_pressed?(btn)
shape.position = m + SF.vector2(20, 20) * delta
$window.draw shape
end
@@ -94,13 +94,13 @@ def test_mouse()
shape.point_count = 3
shape.fill_color = SF.color(128, 0, 255)
shape.position = m - {0, 4}
- shape[0] = SF.vector2(-8, 0)
- shape[1] = SF.vector2(8, 0)
- shape[2] = SF.vector2(0, -wheel_delta.y*5)
+ shape[0] = SF.vector2f(-8, 0)
+ shape[1] = SF.vector2f(8, 0)
+ shape[2] = SF.vector2f(0, -wheel_delta.y*5)
$window.draw shape
- shape[0] = SF.vector2(0, -8)
- shape[1] = SF.vector2(0, 8)
- shape[2] = SF.vector2(-wheel_delta.x*5, 0)
+ shape[0] = SF.vector2f(0, -8)
+ shape[1] = SF.vector2f(0, 8)
+ shape[2] = SF.vector2f(-wheel_delta.x*5, 0)
$window.draw shape
$window.display()
@@ -108,13 +108,13 @@ def test_mouse()
end
def test_controller()
- unless js = (0...SF::Joystick::Count).find { |js| SF::Joystick.is_connected(js) }
+ unless js = (0...SF::Joystick::Count).find { |js| SF::Joystick.connected?(js) }
return
end
while true
while event = $window.poll_event()
- case event.type
+ case event
when SF::Event::Closed
return
end
@@ -122,7 +122,7 @@ def test_controller()
$window.clear()
- text = SF::Text.new(String.new(SF::Joystick.get_identification(js).name), $font, 20)
+ text = SF::Text.new(SF::Joystick.get_identification(js).name, $font, 20)
$window.draw text
shape = SF::CircleShape.new(15)
@@ -149,12 +149,12 @@ def test_controller()
rescue
delta = {0, button_pos.size - btn - 1}
end
- shape.position = SF.vector2(400, 300) + SF.vector2(delta) * {30, 30}
+ shape.position = SF.vector2(400, 300) + SF.vector2(*delta) * 30
text.position = shape.position
- shape.fill_color = SF::Joystick.is_button_pressed(js, btn) ? SF.color(255, 128, 0): SF.color(0, 128, 0)
+ shape.fill_color = SF::Joystick.button_pressed?(js, btn) ? SF.color(255, 128, 0) : SF.color(0, 128, 0)
$window.draw shape
- $window.draw text if SF::Joystick.is_button_pressed(js, btn)
+ $window.draw text if SF::Joystick.button_pressed?(js, btn)
end
shape = SF::CircleShape.new(10)
@@ -173,16 +173,16 @@ def test_controller()
dy = 0
any = false
group.each do |a|
- ax = SF::JoystickAxis.new(axi)
+ ax = SF::Joystick::Axis.new(axi)
axi += 1
- next unless SF::Joystick.has_axis(js, ax)
+ next unless SF::Joystick.axis?(js, ax)
p = SF::Joystick.get_axis_position(js, ax)
dx = p if a[2] == :h
dy = p if a[2] == :v
any = true
end
next unless any
- shape.position = SF.vector2(400, 300) + SF.vector2(delta) * {30, 30} + {dx*0.3, dy*0.3}
+ shape.position = SF.vector2(400, 300) + SF.vector2(*delta) * 30 + SF.vector2(dx, dy) * 0.3
$window.draw shape
end
@@ -193,25 +193,26 @@ end
def wait()
while event = $window.wait_event()
- break if [SF::Event::KeyPressed, SF::Event::MouseButtonPressed, SF::Event::JoystickButtonPressed, SF::Event::Closed].includes? event.type
+ case event
+ when SF::Event::KeyPressed, SF::Event::MouseButtonPressed, SF::Event::JoystickButtonPressed, SF::Event::Closed
+ break
+ end
end
end
class Button < SF::RectangleShape
- include SF::TransformableM
-
- def initialize(message, width, height, color=SF.color(0, 128, 0))
+ def initialize(message, width, height, color = SF.color(0, 128, 0))
super(SF.vector2(width, height))
@text = SF::Text.new(message, $font, (height*0.8).to_i)
self.fill_color = color
- @text.position = SF.vector2((width - @text.global_bounds.width) / 2, -height / 20)
+ @text.position = SF.vector2(((width - @text.global_bounds.width) / 2).to_i, -height / 20)
end
def draw(target, states : SF::RenderStates)
- states.transform.combine(transform)
-
super(target, states)
+ states.transform *= transform
+
target.draw(@text, states)
end
end
@@ -234,12 +235,12 @@ end
while $window.open?
while event = $window.poll_event()
- case event.type
+ case event
when SF::Event::Closed
$window.close()
when SF::Event::MouseButtonPressed
actions.each_key do |btn|
- if btn.global_bounds.contains(event.x.to_f - btn.position.x, event.y.to_f - btn.position.y)
+ if btn.global_bounds.contains?(event.x.to_f, event.y.to_f)
actions[btn].call
break
end
diff --git a/examples/echo.cr b/examples/echo.cr
index 1d368ec..e07e4d7 100644
--- a/examples/echo.cr
+++ b/examples/echo.cr
@@ -1,23 +1,17 @@
require "crsfml"
require "crsfml/audio"
-$mutex = SF::Mutex.new()
-
-$samples = Slice(Int16).new(1024*1024, 0i16)
-$count = 0
-
-
class MySoundRecorder < SF::SoundRecorder
+ def initialize(@samples : Array(Int16), @mutex : SF::Mutex)
+ super()
+ end
def on_start()
true
end
def on_process_samples(samples)
- $mutex.lock
- samples.each do |x|
- $samples[$count] = x
- $count += 1
- end
- $mutex.unlock
+ @mutex.lock
+ @samples.concat(samples)
+ @mutex.unlock
true
end
def on_stop()
@@ -25,24 +19,30 @@ class MySoundRecorder < SF::SoundRecorder
end
class MySoundStream < SF::SoundStream
+ def initialize(@samples : Array(Int16), @mutex : SF::Mutex, *args)
+ super(*args)
+ @result = [] of Int16
+ end
def on_get_data()
- while $count == 0
+ while @samples.empty?
SF.sleep(SF.seconds(0.05))
end
- $mutex.lock
- result = $samples.to_unsafe.to_slice($count)
- $count = 0
- $mutex.unlock
- result
+ @mutex.lock
+ @result.replace(@samples)
+ @samples.clear
+ @mutex.unlock
+ @result.to_unsafe.to_slice(@result.size)
end
def on_seek(position)
end
end
+samples = Array(Int16).new
+mutex = SF::Mutex.new()
-sr = MySoundRecorder.new()
+sr = MySoundRecorder.new(samples, mutex)
sr.start(44100)
-ss = MySoundStream.new(1, 44100)
+ss = MySoundStream.new(samples, mutex, 1, 44100)
ss.play()
puts "Recording... Press Enter to stop"
diff --git a/examples/flippy_bird.cr b/examples/flippy_bird.cr
index f2f3a25..f0cb8db 100644
--- a/examples/flippy_bird.cr
+++ b/examples/flippy_bird.cr
@@ -1,7 +1,7 @@
require "crsfml"
-mode = SF.video_mode(800, 600)
+mode = SF::VideoMode.new(800, 600)
window = SF::RenderWindow.new(mode, "pɹıq ʎddılɟ")
window.vertical_sync_enabled = true
@@ -17,11 +17,11 @@ speed = 0.0
while window.open?
while event = window.poll_event()
- case event.type
+ case event
when SF::Event::Closed
window.close()
when SF::Event::KeyPressed
- if event.key.code == SF::Keyboard::Escape
+ if event.code.escape?
window.close()
else
speed = -9.0
@@ -33,7 +33,7 @@ while window.open?
bird.move SF.vector2(0.0, speed)
bird.rotation = speed*8.0 < 90.0 ? speed*8.0 : 90.0
- window.clear SF.color(112, 197, 206)
+ window.clear SF::Color.new(112, 197, 206)
window.draw bird
window.display()
end
diff --git a/examples/gl.cr b/examples/gl.cr
index 91317a6..96738e5 100644
--- a/examples/gl.cr
+++ b/examples/gl.cr
@@ -10,13 +10,13 @@ lib GL
TRIANGLES = 4u32
end
-window = SF::RenderWindow.new(SF.video_mode(500, 500), "OpenGL")
+window = SF::RenderWindow.new(SF::VideoMode.new(500, 500), "OpenGL")
while window.open?
while event = window.poll_event()
- case event.type
+ case event
when SF::Event::Resized
- GL.viewport(0, 0, event.size.width, event.size.height)
+ GL.viewport(0, 0, event.width, event.height)
when SF::Event::Closed
window.close()
end
diff --git a/examples/shader.cr b/examples/shader.cr
index 362adef..04c645e 100644
--- a/examples/shader.cr
+++ b/examples/shader.cr
@@ -4,14 +4,12 @@
require "crsfml"
require "crsfml/network"
-
-texture = SF::Texture.from_file("resources/background.jpg")
-sprite = SF::Sprite.new(texture)
+sprite = SF::Sprite.new(SF::Texture.from_file("resources/background.jpg"))
px_shader = SF::Shader.from_file("resources/shaders/pixelate.frag", SF::Shader::Fragment)
-px_shader.set_parameter "texture", SF::Shader::CurrentTexture
+px_shader.texture SF::Shader::CurrentTexture
http = SF::Http.new("loripsum.net")
-response = http.send_request(SF::HttpRequest.new("/api/12/short/plaintext"))
+response = http.send_request(SF::Http::Request.new("/api/12/short/plaintext"))
if response.status.ok?
ipsum = response.body
@@ -26,8 +24,7 @@ text.position = {30, 20}
text.color = SF::Color::Black
wb_shader = SF::Shader.from_file("resources/shaders/wave.vert", "resources/shaders/blur.frag")
-
-window = SF::RenderWindow.new(SF.video_mode(800, 600), "SFML Shader", SF::Titlebar|SF::Close)
+window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "SFML Shader", SF::Style::Titlebar|SF::Style::Close)
window.vertical_sync_enabled = true
scene = rand(2)
@@ -35,7 +32,7 @@ scene = rand(2)
clock = SF::Clock.new()
while window.open?
while event = window.poll_event()
- case event.type
+ case event
when SF::Event::Closed
window.close()
when SF::Event::MouseButtonPressed, SF::Event::KeyPressed
@@ -50,17 +47,16 @@ while window.open?
case scene
when 0
- px_shader.set_parameter "pixel_threshold", (x+y)/30.0
+ px_shader.pixel_threshold (x+y)/30.0
- window.draw sprite, SF.render_states(shader: px_shader)
+ window.draw sprite, SF::RenderStates.new(shader: px_shader)
when 1
- wb_shader.set_parameter "wave_phase", time
- wb_shader.set_parameter "wave_amplitude", x*40, y*40
- wb_shader.set_parameter "blur_radius", (x+y)*0.008
+ wb_shader.wave_phase time
+ wb_shader.wave_amplitude x*40, y*40
+ wb_shader.blur_radius (x+y)*0.008
window.clear SF::Color::White
- states = SF.render_states(shader: wb_shader)
- window.draw text, states
+ window.draw text, SF::RenderStates.new(shader: wb_shader)
end
window.display()
diff --git a/examples/shapes.cr b/examples/shapes.cr
index 009c117..c7da969 100644
--- a/examples/shapes.cr
+++ b/examples/shapes.cr
@@ -1,6 +1,10 @@
require "crsfml"
class CustomShape < SF::Shape
+ def initialize
+ super()
+ update()
+ end
def point_count
3
end
@@ -17,32 +21,32 @@ class CustomShape < SF::Shape
end
window = SF::RenderWindow.new(
- SF.video_mode(200, 200), "Shapes",
- settings: SF.context_settings(depth: 24, antialiasing: 8)
+ SF::VideoMode.new(200, 200), "Shapes",
+ settings: SF::ContextSettings.new(depth: 24, antialiasing: 8)
)
window.vertical_sync_enabled = true
while window.open?
while event = window.poll_event()
- window.close if event.type == SF::Event::Closed
+ window.close if event.is_a? SF::Event::Closed
end
window.clear SF::Color::Black
# Top left
vertex_array = SF::VertexArray.new(SF::Triangles)
- vertex_array.append SF.vertex(SF.vector2(0, 0), SF::Color::Green)
- vertex_array.append SF.vertex(SF.vector2(100, 0), SF::Color::Red)
- vertex_array.append SF.vertex(SF.vector2(0, 100), SF::Color::Blue)
+ vertex_array.append SF::Vertex.new(SF.vector2(0, 0), SF::Color::Green)
+ vertex_array.append SF::Vertex.new(SF.vector2(100, 0), SF::Color::Red)
+ vertex_array.append SF::Vertex.new(SF.vector2(0, 100), SF::Color::Blue)
window.draw vertex_array
# Top right
vertices = [
- SF.vertex(SF.vector2(200, 0), SF::Color::Green),
- SF.vertex(SF.vector2(100, 0), SF::Color::Red),
- SF.vertex(SF.vector2(200, 100), SF::Color::Blue),
+ SF::Vertex.new(SF.vector2(200, 0), SF::Color::Green),
+ SF::Vertex.new(SF.vector2(100, 0), SF::Color::Red),
+ SF::Vertex.new(SF.vector2(200, 100), SF::Color::Blue),
]
- window.draw vertices, SF::Triangles, SF.render_states()
+ window.draw vertices, SF::Triangles
# Bottom left
convex_shape = SF::ConvexShape.new(3)
diff --git a/examples/simple.cr b/examples/simple.cr
index 85a969e..9e23c17 100644
--- a/examples/simple.cr
+++ b/examples/simple.cr
@@ -1,11 +1,11 @@
require "crsfml/window"
-window = SF::Window.new(SF.video_mode(800, 600), "CrSFML works!")
+window = SF::Window.new(SF::VideoMode.new(800, 600), "CrSFML works!")
while window.open?
while event = window.poll_event()
- if event.type == SF::Event::Closed
+ if event.is_a? SF::Event::Closed
window.close()
end
end
diff --git a/examples/snakes.cr b/examples/snakes.cr
index af150c7..83c9d45 100644
--- a/examples/snakes.cr
+++ b/examples/snakes.cr
@@ -21,6 +21,8 @@ end
struct Food
+ include SF::Drawable
+
property position
property color
@@ -36,6 +38,8 @@ struct Food
end
class Snake
+ include SF::Drawable
+
@direction : SF::Vector2(Int32)
getter body : Array(SF::Vector2(Int32))
@@ -124,6 +128,8 @@ class Snake
end
class Field
+ include SF::Drawable
+
getter size : SF::Vector2(Int32)
def initialize(@size : SF::Vector2(Int32))
@@ -183,8 +189,8 @@ field.add snake2
scale = 20
window = SF::RenderWindow.new(
- SF.video_mode(field.size.x*scale, field.size.y*scale), "Snakes",
- settings: SF.context_settings(depth: 24, antialiasing: 8)
+ SF::VideoMode.new(field.size.x*scale, field.size.y*scale), "Snakes",
+ settings: SF::ContextSettings.new(depth: 24, antialiasing: 8)
)
window.vertical_sync_enabled = true
window.framerate_limit = 10
@@ -193,15 +199,15 @@ window.framerate_limit = 10
transform = SF::Transform::Identity
transform.scale scale, scale
-states = SF.render_states(transform: transform)
+states = SF::RenderStates.new(transform: transform)
while window.open?
while event = window.poll_event()
- if event.type == SF::Event::Closed ||\
- (event.type == SF::Event::KeyPressed && event.key.code == SF::Keyboard::Escape)
+ if event.is_a?(SF::Event::Closed) ||\
+ (event.is_a?(SF::Event::KeyPressed) && event.code.escape?)
window.close()
- elsif event.type == SF::Event::KeyPressed
- case event.key.code
+ elsif event.is_a? SF::Event::KeyPressed
+ case event.code
when .a?
snake1.turn Left
when .w?
diff --git a/examples/sound_capture.cr b/examples/sound_capture.cr
index 9add585..d03aa66 100644
--- a/examples/sound_capture.cr
+++ b/examples/sound_capture.cr
@@ -20,7 +20,7 @@ puts "Recording... press Enter to stop"
gets()
recorder.stop()
-buffer = recorder.buffer.not_nil!
+buffer = recorder.buffer
puts "Sound information:"
puts " #{buffer.duration.as_seconds} seconds"
diff --git a/examples/transformable.cr b/examples/transformable.cr
index fa4114b..5d916bc 100644
--- a/examples/transformable.cr
+++ b/examples/transformable.cr
@@ -4,8 +4,8 @@ require "crsfml"
$font : SF::Font
$font = SF::Font.from_file("resources/font/Ubuntu-R.ttf")
-class Logo
- include SF::TransformableM
+class Logo < SF::Transformable
+ include SF::Drawable
def initialize(message="CrSFML")
@text = SF::Text.new(message, $font, 200)
@@ -17,13 +17,13 @@ class Logo
end
def draw(target, states : SF::RenderStates)
- states.transform.combine(transform)
+ states.transform *= transform
target.draw(@shape, states)
target.draw(@text, states)
end
end
-mode = SF.video_mode(800, 600)
+mode = SF::VideoMode.new(800, 600)
window = SF::RenderWindow.new(mode, "Hello")
window.vertical_sync_enabled = true
@@ -35,7 +35,7 @@ clock = SF::Clock.new()
while window.open?
while event = window.poll_event()
- case event.type
+ case event
when SF::Event::Closed
window.close()
end
diff --git a/examples/typing.cr b/examples/typing.cr
index a4bfe27..e2be506 100644
--- a/examples/typing.cr
+++ b/examples/typing.cr
@@ -1,7 +1,7 @@
require "crsfml"
-window = SF::RenderWindow.new(SF.video_mode(800, 600), "Typing")
+window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "Typing")
str = ""
@@ -10,20 +10,21 @@ font = SF::Font.from_file("resources/font/Ubuntu-R.ttf")
text = SF::Text.new("_", font)
text.color = SF::Color::Black
+
while window.open?
while event = window.poll_event()
- case event.type
+ case event
when SF::Event::KeyPressed
- case event.key.code
+ case event.code
when SF::Keyboard::Escape
window.close()
- when SF::Keyboard::Back
+ when SF::Keyboard::BackSpace
str = str[0...-1]
when SF::Keyboard::Return
str += '\n'
end
when SF::Event::TextEntered
- str += event.text.unicode if event.text.unicode >= ' '
+ str += event.unicode.chr if event.unicode.chr >= ' '
text.string = str + "_"
when SF::Event::Closed
window.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment