/
dxopal_sdl.rb
260 lines (202 loc) · 4.74 KB
/
dxopal_sdl.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
require "sdl"
RUBY_ENGINE = "opal"
def require_remote(path)
path.sub!(/\.rb/, "")
require "./#{path}"
end
def p_(*args)
DXOpal.p_(*args)
end
module DXOpal
C_BLACK = [255, 0, 0, 0]
C_WHITE = [255, 255, 255, 255]
M_LBUTTON = :m_lbutton
@@p_count = 0
def self.p_(*args)
args.each { |arg| p arg } if @@p_count < 10
@@p_count += 1
end
module Window
@@width = 640
@@height = 480
@@last_update = Time.now
class << self
def width
@@width
end
def width=(w)
@@width = w
end
def height
@@height
end
def height=(h)
@@height = h
end
def bgcolor=(color)
@@bgcolor = color
end
def fps=(fps)
# dummy
end
def real_fps
-1 # dummy
end
def to_rgb_a(color)
if color.size == 4
[color[1..3], color[0]]
else
[color, 255]
end
end
def load_resources
SDL.init(SDL::INIT_EVERYTHING)
SDL::Mixer.open
Sound.load_all
@@screen = SDL.set_video_mode(
@@width,
@@height,
16,
SDL::SWSURFACE
)
yield
end
def handle_event(event)
case event
when SDL::Event::Quit
exit
when SDL::Event::MouseMotion
Input.mouse_x = event.x
Input.mouse_y = event.y
when SDL::Event::MouseButtonDown
if event.button == SDL::Mouse::BUTTON_LEFT
DXOpal::Input.mouse_pushed_map_set(M_LBUTTON, true)
end
when SDL::Event::MouseButtonUp
if event.button == SDL::Mouse::BUTTON_LEFT
DXOpal::Input.mouse_pushed_map_set(M_LBUTTON, false)
end
end
end
def fill_bg
draw_box_fill(0, 0, @@width, @@height, @@bgcolor)
end
def loop
fps = 10
interval_sec = 1 / fps.to_f
while true
while event = SDL::Event.poll
handle_event(event)
end
if @@last_update + interval_sec < Time.now
@@last_update = Time.now
fill_bg
yield
@@screen.update_rect(0, 0, 0, 0)
end
end
end
def draw_line(x1, y1, x2, y2, color, z=0)
rgb, alpha = to_rgb_a(color)
antialias = false
@@screen.draw_line(x1, y1, x2, y2, rgb, antialias, alpha)
end
def sdl_draw_box(x1, y1, x2, y2, color, fill)
rgb, alpha = to_rgb_a(color)
w = x2 - x1
h = y2 - y1
@@screen.draw_rect(x1, y1, w, h, rgb, fill, alpha)
end
def draw_box(x1, y1, x2, y2, color, z=0)
sdl_draw_box(x1, y1, x2, y2, color, false)
end
def draw_box_fill(x1, y1, x2, y2, color, z=0)
sdl_draw_box(x1, y1, x2, y2, color, true)
end
def sdl_draw_circle(x, y, r, color, fill)
rgb, alpha = to_rgb_a(color)
antialias = false
@@screen.draw_circle(x, y, r, rgb, fill, antialias, alpha)
end
def draw_circle(x, y, r, color, z=0)
sdl_draw_circle(x, y, r, color, false)
end
def draw_circle_fill(x, y, r, color, z=0)
sdl_draw_circle(x, y, r, color, true)
end
def draw_font(x, y, string, font, option={})
# dummy
end
end
end
class Sound
@@map = {}
class << self
def register(name, *args, &block)
path, _ = args
@@map[name] = {
path: path,
sound: nil
}
end
def [](name)
@@map[name][:sound]
end
def load_all
@@map.each { |name, inner_map|
wave = SDL::Mixer::Wave.load(inner_map[:path])
sound = Sound.new(wave)
@@map[name][:sound] = sound
}
end
end
def initialize(wave)
@wave = wave
end
def play
SDL::Mixer.play_channel(-1, @wave, 0)
end
end
module Input
@@mouse_pushed_map = {}
@@mouse_pushed_map[M_LBUTTON] = false
@@mouse_x = 0
@@mouse_y = 0
class << self
def mouse_pushed_map_set(code, val)
@@mouse_pushed_map[code] = val
end
def mouse_x=(val)
@@mouse_x = val
end
def mouse_y=(val)
@@mouse_y = val
end
def mouse_x
@@mouse_x
end
def mouse_y
@@mouse_y
end
def mouse_push?(mouse_code)
pushed = @@mouse_pushed_map[mouse_code]
@@mouse_pushed_map[mouse_code] = nil
pushed
end
def touch_x
0 # not supported
end
def touch_y
0 # not supported
end
def touch_push?
false # not supported
end
end
end
class Font
def initialize(size, fontname=nil, option={})
nil # dummy
end
end
end