-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdl_test2.rb
executable file
·199 lines (163 loc) · 4.36 KB
/
sdl_test2.rb
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env ruby
# info: Screen class for simple 2D graphics using SDL
# depends: libsdl-ruby
# original example code taken from:
# http://lorenzod8n.wordpress.com/2007/05/30/ruby-meet-sdl/
# license: GPLv3+ <http://www.gnu.org/licenses/gpl.txt>
# Andras Horvath <[email protected]>
#
# SDL INSTALL:
#
# on Debian or Ubuntu:
# sudo apt-get install libsdl-sge libsdl-sge-dev libsdl-dev
# sudo gem install rubysdl
#
# on Fedora, RHEL or clones:
# su -c "yum groupinstall 'Development Tools'"
# su -c "yum install ruby ruby-devel rubygems SDL SDL-devel SDL_image SDL_image-devel freetype-devel"
#
# # compile and install ruby 1.9.x rather from source if only 1.8.x is available
#
# # get source code from these sites:
# # http://www.digitalfanatics.org/cal/sge/
# # http://packages.debian.org/source/stable/libsdl-sge
#
# wget "http://ftp.de.debian.org/debian/pool/main/libs/libsdl-sge/libsdl-sge_030809dfsg.orig.tar.gz"
# wget "http://ftp.de.debian.org/debian/pool/main/libs/libsdl-sge/libsdl-sge_030809dfsg-3.debian.tar.gz"
# tar xf *sge*orig*
# tar xf *sge*debian*
#
# cd sge*
# find ../debian/patches/ -iname "*.diff" | sort | while read FF; do patch < "$FF"; done
#
# make
# su -c "make install; ldconfig"
#
# su -c "gem install rubysdl"
#
require 'sdl'
# Screen object using SDL
# rubysdl home:
# http://www.kmc.gr.jp/~ohai/rubysdl.en.html
# SDL doc:
# http://www.kmc.gr.jp/~ohai/rubysdl_doc.en.html
class Screen
attr_reader :width, :height
# init and show screen
def initialize(width = 800, height = 600)
@width = width
@height = height
SDL.init SDL::INIT_VIDEO
@screen = SDL::set_video_mode @width, @height, 24, SDL::SWSURFACE
@color_bg = @screen.format.mapRGB 240, 240, 240
@color_fg = @screen.format.mapRGB 0, 0, 0
clear
end
# clear the content of screen
def clear
@screen.fill_rect 0, 0, @width, @height, @color_bg
end
# switch screen to the active buffer and show it
def show
@screen.flip
end
# return an event in an array [:description, param1, param2, ...]
def event
while event = SDL::Event2.poll
case event
when SDL::Event2::Quit
return [:quit]
when SDL::Event2::MouseButtonDown
return [:mouse, event.x, event.y]
when SDL::Event2::KeyDown
return [:escape] if event.sym == SDL::Key::ESCAPE
return [:space] if event.sym == SDL::Key::SPACE
end
end
return []
end
# draw a point
def point(x, y)
@screen.drawLine x, y, x, y, @color_fg
end
# draw a line
def line(x1, y1, x2, y2)
@screen.drawAALine x1, y1, x2, y2, @color_fg
end
# draw a rectangle
def rect(x1, y1, x2, y2)
@screen.drawRectAlpha x1, y1, x2-x1, y2-y1, @color_fg, 255
end
# draw a filled rectangle
def rect_filled(x1, y1, x2, y2)
@screen.drawFilledRectAlpha x1, y1, x2-x1, y2-y1, @color_fg, 255
end
# draw a cirlce
def circle(x, y, r)
@screen.drawAACircle x, y, r, @color_fg
end
# draw a filled cirlce
def circle_filled(x, y, r)
@screen.drawAAFilledCircle x, y, r, @color_fg
end
# draw an ellipse
def ellipse(x, y, rx, ry)
@screen.drawAAEllipse x, y, rx, ry, @color_fg
end
# draw a filled ellipse
def ellipse_filled(x, y, rx, ry)
@screen.drawAAFilledEllipse x, y, rx, ry, @color_fg
end
end
# main
s = Screen.new(1000, 700)
# consts
num_of_points = 500
speed_of_points = 3
radius_of_points = 2
# store random pixel coords
points = []
for i in (1..num_of_points) do points[i] = \
[rand * s.width, rand * s.height] end
# run it
e = []
while e[0] != :quit and e[0] != :escape do
if e[0] == :space
# store new random pixel coords
for i in (1..num_of_points) do points[i] = \
[rand * s.width, rand * s.height] end
end
# clear screen
s.clear
# draw points
for i in (1..num_of_points)
s.circle_filled points[i][0], points[i][1], radius_of_points
end
# show drawing
s.show
# move points toward each other
r_max = 0
for i1 in (1..num_of_points)
i2 = i1 + 1
if i2 > num_of_points then i2 = 1 end
x1 = points[i1][0]
y1 = points[i1][1]
x2 = points[i2][0]
y2 = points[i2][1]
r = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
x3 = (x2 - x1) * speed_of_points / r + x1
y3 = (y2 - y1) * speed_of_points / r + y1
if r > r_max then r_max = r end
points[i1][0] = x3
points[i1][1] = y3
end
# generate new points when all points are too close
if r_max < speed_of_points * 2 then
for i in (1..num_of_points) do points[i] = \
[rand * s.width, rand * s.height] end
end
# store new event
e = s.event
sleep 0.01
end
exit