-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdl_test.rb
executable file
·145 lines (123 loc) · 3.33 KB
/
sdl_test.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
#!/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(600, 400)
s.line(130, 100, 450, 210)
s.circle(130, 100, 50)
s.show
# wait for escape key or window close to quit
e = []
while e[0] != :quit and e[0] != :escape do
e = s.event
sleep 0.01
end
exit