Skip to content

Commit 4c327ec

Browse files
committed
Add Shapes example
1 parent f5ac56a commit 4c327ec

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

examples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ Parts of the Shader example [from SFML][shader]
5757
- Set arguments
5858
- Apply
5959

60+
### [shapes](shapes.cr)
61+
62+
- `VertexArray`
63+
- `draw` primitives
64+
- `ConvexShape`
65+
- Custom `Shape`
66+
6067
### [gl](gl.cr)
6168

6269
- Basic OpenGL

examples/shapes.cr

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require "crsfml"
2+
3+
window = SF::RenderWindow.new(
4+
SF.video_mode(200, 200), "Shapes",
5+
settings: SF.context_settings(depth: 32, antialiasing: 8)
6+
)
7+
window.vertical_sync_enabled = true
8+
9+
while window.open?
10+
while event = window.poll_event()
11+
window.close if event.type == SF::Event::Closed
12+
end
13+
14+
window.clear SF::Color::Black
15+
16+
# Top left
17+
vertex_array = SF::VertexArray.new(SF::Triangles)
18+
vertex_array.append SF.vertex(SF.vector2(0, 0), SF::Color::Green)
19+
vertex_array.append SF.vertex(SF.vector2(100, 0), SF::Color::Red)
20+
vertex_array.append SF.vertex(SF.vector2(0, 100), SF::Color::Blue)
21+
window.draw vertex_array
22+
23+
# Top right
24+
vertices = [
25+
SF.vertex(SF.vector2(200, 0), SF::Color::Green),
26+
SF.vertex(SF.vector2(100, 0), SF::Color::Red),
27+
SF.vertex(SF.vector2(200, 100), SF::Color::Blue),
28+
]
29+
window.draw vertices, SF::Triangles, SF.render_states()
30+
31+
# Bottom left
32+
convex_shape = SF::ConvexShape.new(3)
33+
convex_shape[0] = SF.vector2(0, 200)
34+
convex_shape[1] = SF.vector2(100, 200)
35+
convex_shape[2] = SF.vector2(0, 100)
36+
convex_shape.fill_color = SF::Color::Green
37+
window.draw convex_shape
38+
39+
# Bottom right
40+
# broken: https://github.com/manastech/crystal/issues/605
41+
class CustomShape < SF::Shape
42+
def point_count
43+
3
44+
end
45+
def get_point(i)
46+
case i
47+
when 0
48+
SF.vector2(100, 200)
49+
when 1
50+
SF.vector2(200, 200)
51+
else
52+
SF.vector2(200, 100)
53+
end
54+
end
55+
end
56+
custom_shape = CustomShape.new()
57+
custom_shape.fill_color = SF::Color::Green
58+
window.draw custom_shape
59+
60+
window.display()
61+
end

0 commit comments

Comments
 (0)