forked from ysbaddaden/sdl.cr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfx.cr
103 lines (87 loc) · 3.38 KB
/
gfx.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
require "../src/sdl"
require "../src/gfx"
WHITE = SDL::Color[255, 255, 255, 255]
BLACK = SDL::Color[0, 0, 0, 255]
RED = SDL::Color[255, 0, 0, 255]
GREEN = SDL::Color[0, 255, 0, 255]
BLUE = SDL::Color[0, 0, 255, 255]
YELLOW = SDL::Color[255, 255, 0, 255]
SDL.init(SDL::Init::VIDEO)
at_exit { SDL.quit }
window = SDL::Window.new("SDL gfx", 640, 480)
renderer = SDL::Renderer.new(window)
width, height = window.size
cx = width / 2
cy = height / 2
angle = 0
loop do
case event = SDL::Event.wait
when SDL::Event::Quit
break
end
# clear screen in black
renderer.draw_color = BLUE
renderer.clear
# draw a rounded rect
screen_rect = SDL::Rect.new(0, 0, width, height)
renderer.draw_color = BLACK
renderer.fill_rounded_rect screen_rect, 30
renderer.draw_color = WHITE
renderer.draw_rounded_rect screen_rect, 30
# draw white centered cross
renderer.draw_color = WHITE
renderer.draw_line(0, cy, width, cy)
renderer.draw_line(cx, 0, cx, height)
# draw a green centered ellipse and some meridians with anti aliasing
renderer.draw_color = GREEN
renderer.draw_ellipse(cx, cy, width / 6, height / 6, anti_aliased: true)
renderer.draw_circle(cx, cy, height / 6, anti_aliased: true)
renderer.draw_ellipse(cx, cy, width / 12, height / 6, anti_aliased: true)
renderer.draw_ellipse(cx, cy, width / 26, height / 6, anti_aliased: true)
# draw two triangles within a square on the bottom right side of the screen
rect = SDL::Rect[cx + width/4 - 51, cy + height/4 - 51, 102, 102]
triangle1 = SDL::Triangle[rect.x + 1, rect.y + 1, rect.x + 100, rect.y + 1, rect.x + 1, rect.y + 100]
triangle2 = SDL::Triangle[rect.x + 1, rect.y + 100, rect.x + 100, rect.y + 100, rect.x + 100, rect.y + 1]
renderer.draw_color = RED
renderer.draw_rect(rect)
renderer.draw_color = YELLOW
renderer.fill_triangle(triangle1)
renderer.draw_color = BLUE
renderer.fill_triangle(triangle2)
# draw a red ball on the top left side of the screen
renderer.draw_color = RED
renderer.fill_circle SDL::Circle.new(width/4, cy - height/4, 50)
renderer.draw_color = WHITE
renderer.fill_ellipse SDL::Ellipse.new(width/4 + 25, cy - height/4 - 25, 8, 7)
# draw triangles within a circle on the top right side of the screen
circle = SDL::Circle.new(cx + width/4, cy - height/4, 50)
renderer.draw_color = RED
renderer.draw_circle circle, anti_aliased: true
renderer.draw_color = YELLOW
renderer.draw_point(circle.x, circle.y)
30.step(by: 30, to: 360) do |a|
a = a + angle
x1 = circle.x + circle.radius * Math.cos(a * Math::PI / 180.0)
y1 = circle.y + circle.radius * Math.sin(a * Math::PI / 180.0)
x2 = circle.x + circle.radius * Math.cos((a - 30) * Math::PI / 180.0)
y2 = circle.y + circle.radius * Math.sin((a - 30) * Math::PI / 180.0)
renderer.draw_triangle SDL::Triangle.new(x1, y1, x2, y2, circle.x, circle.y), anti_aliased: true
end
# draw random lines on the bottom left side of the screen with antialiasing on and off
renderer.draw_color = GREEN
12.times do |i|
x1 = rand(50..cx - 100)
y1 = rand(cy + 50..height - 50)
x2 = rand(50..cx - 100)
y2 = rand(cy + 50..height - 50)
if i % 2 == 0
renderer.draw_color = GREEN
renderer.draw_line(x1, y1, x2, y2, anti_aliased: true)
else
renderer.draw_color = RED
renderer.draw_line(x1, y1, x2, y2, anti_aliased: false)
end
end
angle = angle + 1 % 360
renderer.present
end