sgl 0.4.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +254 -234
- data/History.txt +6 -0
- data/Manifest.txt +10 -0
- data/Rakefile +27 -7
- data/examples/cocoa1-basic.rb +14 -14
- data/examples/cocoa10-transparent.rb +26 -26
- data/examples/cocoa11-application.rb +13 -13
- data/examples/cocoa2-draw.rb +23 -23
- data/examples/cocoa3-affine.rb +29 -29
- data/examples/cocoa4-font.rb +24 -24
- data/examples/cocoa5-image.rb +22 -22
- data/examples/cocoa6-sound.rb +24 -24
- data/examples/cocoa7-movie.rb +40 -40
- data/examples/cocoa8-movieoverlay.rb +42 -42
- data/examples/cocoa9-streaming.rb +40 -40
- data/examples/opengl1-basic.rb +14 -14
- data/examples/opengl11-application.rb +13 -13
- data/examples/opengl2-draw.rb +23 -23
- data/examples/opengl3-affine.rb +29 -29
- data/examples/sample1.rb +11 -11
- data/examples/sample10.rb +16 -16
- data/examples/sample11.rb +22 -22
- data/examples/sample11a.rb +33 -33
- data/examples/sample12.rb +30 -30
- data/examples/sample12a.rb +35 -35
- data/examples/sample13.rb +116 -116
- data/examples/sample15.rb +24 -24
- data/examples/sample16.rb +31 -31
- data/examples/sample16a.rb +35 -35
- data/examples/sample6.rb +18 -18
- data/examples/sample7.rb +16 -16
- data/examples/sample9.rb +20 -20
- data/examples/testgl.rb +169 -169
- data/lib/sgl/bass.rb +46 -46
- data/lib/sgl/cocoa-app.rb +43 -43
- data/lib/sgl/cocoa-color.rb +65 -65
- data/lib/sgl/cocoa-draw.rb +72 -72
- data/lib/sgl/cocoa-event.rb +229 -229
- data/lib/sgl/cocoa-media.rb +144 -144
- data/lib/sgl/cocoa-notuse.rb +493 -493
- data/lib/sgl/cocoa-window.rb +203 -203
- data/lib/sgl/cocoa.rb +8 -8
- data/lib/sgl/opengl-app.rb +38 -38
- data/lib/sgl/opengl-color.rb +44 -44
- data/lib/sgl/opengl-draw.rb +260 -260
- data/lib/sgl/opengl-event.rb +325 -325
- data/lib/sgl/opengl-modules.rb +22 -22
- data/lib/sgl/opengl-window.rb +224 -224
- data/lib/sgl/opengl.rb +7 -7
- data/lib/sgl/sgl-button.rb +135 -135
- data/lib/sgl/sgl-client.rb +21 -21
- data/lib/sgl/sgl-color.rb +68 -82
- data/lib/sgl/sgl-connect.rb +9 -9
- data/lib/sgl/sgl-server.rb +58 -58
- data/lib/sgl/sgl-spring.rb +216 -216
- data/lib/sgl/version.rb +2 -2
- data/scripts/lib-txt2html.rb +130 -0
- data/scripts/txt2html +2 -63
- data/spec/sgl_spec.rb +13 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +1 -0
- data/test/test_cocoa_app.rb +291 -291
- data/test/test_module_ruby16.rb +30 -30
- data/test/test_opengl_app.rb +147 -147
- data/test/test_opengl_basic.rb +22 -22
- data/test/test_opengl_fullscreen.rb +23 -23
- data/test/test_opengl_novice.rb +35 -35
- data/website/challenge1.html +91 -91
- data/website/challenge1.txt +3 -3
- data/website/challenge2.html +143 -144
- data/website/challenge2.txt +13 -14
- data/website/challenge3.html +66 -63
- data/website/challenge3.txt +7 -7
- data/website/cocoa.html +85 -88
- data/website/cocoa.txt +2 -4
- data/website/description.html +138 -138
- data/website/description.txt +1 -1
- data/website/exercise1.html +262 -264
- data/website/exercise1.txt +58 -53
- data/website/exercise2.html +241 -683
- data/website/exercise2.txt +11 -427
- data/website/exercise3.html +206 -0
- data/website/exercise3.txt +155 -0
- data/website/exercise4.html +198 -0
- data/website/exercise4.txt +151 -0
- data/website/exercise5.html +162 -0
- data/website/exercise5.txt +113 -0
- data/website/exhibition.html +84 -84
- data/website/howto.html +146 -134
- data/website/howto.txt +9 -0
- data/website/index.html +178 -178
- data/website/index.txt +20 -19
- data/website/sound.html +141 -141
- metadata +16 -3
data/lib/sgl/opengl-event.rb
CHANGED
@@ -1,325 +1,325 @@
|
|
1
|
-
# Copyright (C) 2004-2007 Kouichirou Eto, All rights reserved.
|
2
|
-
# License: Ruby License
|
3
|
-
|
4
|
-
module SGL
|
5
|
-
# callback functions
|
6
|
-
def setup() end
|
7
|
-
def onMouseDown(x,y) end
|
8
|
-
def onMouseUp(x,y) end
|
9
|
-
def onKeyDown(k) end
|
10
|
-
def onKeyUp(k) end
|
11
|
-
def display() end
|
12
|
-
|
13
|
-
# callback functions for fullscreen
|
14
|
-
def onMouseDown0(x,y) end
|
15
|
-
def display0() end
|
16
|
-
|
17
|
-
# mainloop
|
18
|
-
def mainloop
|
19
|
-
$__a__.set_setup { setup }
|
20
|
-
$__a__.set_mousedown {|x, y| onMouseDown(x, y) }
|
21
|
-
$__a__.set_mouseup {|x, y| onMouseUp(x, y) }
|
22
|
-
$__a__.set_keydown {|k| onKeyDown(k) }
|
23
|
-
$__a__.set_keyup {|k| onKeyUp(k) }
|
24
|
-
if ! $__a__.check_display0
|
25
|
-
$__a__.set_display0 { display0 }
|
26
|
-
$__a__.set_mousedown0 {|x, y| onMouseDown0(x, y) }
|
27
|
-
end
|
28
|
-
$__a__.set_display { display }
|
29
|
-
|
30
|
-
if ! defined?($__sgl_in_mainloop__)
|
31
|
-
$__sgl_in_mainloop__ = true
|
32
|
-
$__a__.mainloop
|
33
|
-
else
|
34
|
-
# do setup only.
|
35
|
-
$__a__.mainloop_setup
|
36
|
-
|
37
|
-
# for debug
|
38
|
-
# $__a__.mainloop
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# novice mode
|
43
|
-
def flip(*a) $__a__.flip(*a) end
|
44
|
-
def wait(*a) $__a__.wait(*a) end
|
45
|
-
def process(&b) $__a__.process(&b) end
|
46
|
-
|
47
|
-
# get status functions
|
48
|
-
def mouseX() $__a__.mouseX; end
|
49
|
-
def mouseY() $__a__.mouseY; end
|
50
|
-
def mouseDown() $__a__.mouseDown; end
|
51
|
-
def key(k) $__a__.key[k]; end
|
52
|
-
def keynum() $__a__.keynum; end
|
53
|
-
|
54
|
-
# get status functions for fullscreen
|
55
|
-
def mouseX0() $__a__.mouseX0; end
|
56
|
-
def mouseY0() $__a__.mouseY0; end
|
57
|
-
|
58
|
-
# event control
|
59
|
-
def useDelay(*a) $__a__.useDelay(*a) end
|
60
|
-
def useFramerate(*a) $__a__.useFramerate(*a) end
|
61
|
-
def useRuntime(*a) $__a__.useRuntime(*a) end
|
62
|
-
|
63
|
-
class Application
|
64
|
-
def initialize_event
|
65
|
-
@setup_done = nil
|
66
|
-
@display_drawing = nil
|
67
|
-
@block = {}
|
68
|
-
|
69
|
-
# status setting
|
70
|
-
@mouseX, @mouseY = 0, 0
|
71
|
-
@mouseX0, @mouseY0 = 0, 0
|
72
|
-
@mouseDown = 0
|
73
|
-
@keynum = 0
|
74
|
-
|
75
|
-
@starttime = nil
|
76
|
-
end
|
77
|
-
private :initialize_event
|
78
|
-
|
79
|
-
# get status
|
80
|
-
attr_reader :mouseX, :mouseY
|
81
|
-
attr_reader :mouseX0, :mouseY0
|
82
|
-
attr_reader :mouseDown
|
83
|
-
attr_reader :keynum
|
84
|
-
|
85
|
-
# setup
|
86
|
-
def set_setup(&b)
|
87
|
-
return unless block_given?
|
88
|
-
@block[:setup] = Proc.new
|
89
|
-
end
|
90
|
-
|
91
|
-
def setup_pre
|
92
|
-
# do nothing
|
93
|
-
end
|
94
|
-
|
95
|
-
def do_setup
|
96
|
-
setup_pre
|
97
|
-
@block[:setup].call if @block[:setup]
|
98
|
-
setup_post
|
99
|
-
end
|
100
|
-
|
101
|
-
def setup_post
|
102
|
-
@setup_done = true
|
103
|
-
end
|
104
|
-
private :setup_pre, :setup_post
|
105
|
-
|
106
|
-
# display
|
107
|
-
def set_display(&b)
|
108
|
-
return unless block_given?
|
109
|
-
@block[:display] = Proc.new
|
110
|
-
end
|
111
|
-
|
112
|
-
def set_display0(&b)
|
113
|
-
return unless block_given?
|
114
|
-
@block[:display0] = Proc.new
|
115
|
-
end
|
116
|
-
|
117
|
-
def check_display0
|
118
|
-
return ! @block[:display0].nil?
|
119
|
-
end
|
120
|
-
|
121
|
-
def display_pre
|
122
|
-
set_camera_position
|
123
|
-
check_event
|
124
|
-
clear
|
125
|
-
end
|
126
|
-
|
127
|
-
def do_display # callback
|
128
|
-
return if @setup_done.nil?
|
129
|
-
# return if @display_drawing
|
130
|
-
@display_drawing = true
|
131
|
-
display_pre
|
132
|
-
@block[:display].call if @block[:display]
|
133
|
-
display_post
|
134
|
-
@display_drawing = nil
|
135
|
-
end
|
136
|
-
|
137
|
-
def display_post
|
138
|
-
set_fullscreen_camera_position
|
139
|
-
cur_color = @cur_color
|
140
|
-
@block[:display0].call if @block[:display0]
|
141
|
-
color(*cur_color)
|
142
|
-
SDL.GLSwapBuffers
|
143
|
-
#GC.start
|
144
|
-
end
|
145
|
-
private :display_pre, :display_post
|
146
|
-
|
147
|
-
# mouse events
|
148
|
-
def set_mousedown(&b)
|
149
|
-
return unless block_given?
|
150
|
-
@block[:mousedown] = Proc.new
|
151
|
-
end
|
152
|
-
|
153
|
-
def do_mousedown
|
154
|
-
@mouseDown = 1
|
155
|
-
@block[:mousedown].call(@mouseX, @mouseY) if @block[:mousedown]
|
156
|
-
@block[:mousedown0].call(@mouseX0, @mouseY0) if @block[:mousedown0]
|
157
|
-
end
|
158
|
-
|
159
|
-
def set_mouseup(&b)
|
160
|
-
return unless block_given?
|
161
|
-
@block[:mouseup] = Proc.new
|
162
|
-
end
|
163
|
-
|
164
|
-
def do_mouseup
|
165
|
-
@mouseDown = 0
|
166
|
-
@block[:mouseup].call(@mouseX, @mouseY) if @block[:mouseup]
|
167
|
-
end
|
168
|
-
|
169
|
-
# mouse events for fullscreen
|
170
|
-
def set_mousedown0(&b)
|
171
|
-
return unless block_given?
|
172
|
-
@block[:mousedown0] = Proc.new
|
173
|
-
end
|
174
|
-
|
175
|
-
def check_mousedown0
|
176
|
-
return ! @block[:mousedown0].nil?
|
177
|
-
end
|
178
|
-
|
179
|
-
# key events
|
180
|
-
def set_keydown(&b)
|
181
|
-
return unless block_given?
|
182
|
-
@block[:keydown] = Proc.new
|
183
|
-
end
|
184
|
-
|
185
|
-
def keydown_pre(key)
|
186
|
-
exit if key == SDL::Key::ESCAPE
|
187
|
-
end
|
188
|
-
private :keydown_pre
|
189
|
-
|
190
|
-
def do_keydown(key)
|
191
|
-
keydown_pre(key)
|
192
|
-
@block[:keydown].call(key) if @block[:keydown]
|
193
|
-
end
|
194
|
-
|
195
|
-
def set_keyup(&b)
|
196
|
-
return unless block_given?
|
197
|
-
@block[:keyup] = Proc.new
|
198
|
-
end
|
199
|
-
|
200
|
-
def do_keyup(key)
|
201
|
-
@block[:keyup].call(key) if @block[:keyup]
|
202
|
-
end
|
203
|
-
|
204
|
-
def calc_keynum(e)
|
205
|
-
input = e.characters
|
206
|
-
@keynum = input.to_s[0]
|
207
|
-
end
|
208
|
-
private :calc_keynum
|
209
|
-
|
210
|
-
def mainloop_setup
|
211
|
-
do_setup
|
212
|
-
end
|
213
|
-
|
214
|
-
def mainloop
|
215
|
-
mainloop_setup
|
216
|
-
@starttime = Time.now
|
217
|
-
loop {
|
218
|
-
@begintime = Time.now
|
219
|
-
do_display
|
220
|
-
delay
|
221
|
-
return if check_runtime_finished(@starttime)
|
222
|
-
}
|
223
|
-
end
|
224
|
-
|
225
|
-
def delay
|
226
|
-
if @options[:framerate]
|
227
|
-
sec_per_frame = 1.0 / @options[:framerate]
|
228
|
-
diff = sec_per_frame - (Time.now - @begintime)
|
229
|
-
sleep(diff) if 0 < diff
|
230
|
-
else
|
231
|
-
delaytime = @options[:delaytime]
|
232
|
-
sleep(delaytime)
|
233
|
-
end
|
234
|
-
end
|
235
|
-
private :delay
|
236
|
-
|
237
|
-
def check_runtime_finished(starttime)
|
238
|
-
runtime = @options[:runtime]
|
239
|
-
return false if runtime.nil?
|
240
|
-
diff = Time.now - starttime
|
241
|
-
return (runtime && runtime < diff)
|
242
|
-
end
|
243
|
-
private :check_runtime_finished
|
244
|
-
|
245
|
-
# novice mode
|
246
|
-
def flip
|
247
|
-
@starttime = Time.now if @starttime.nil?
|
248
|
-
display_post
|
249
|
-
delay
|
250
|
-
display_pre
|
251
|
-
#exit if check_runtime_finished(@starttime)
|
252
|
-
end
|
253
|
-
|
254
|
-
def wait
|
255
|
-
#SGL.flip if !$__v__.flipped
|
256
|
-
loop {
|
257
|
-
check_event
|
258
|
-
delay
|
259
|
-
return if check_runtime_finished(@starttime)
|
260
|
-
}
|
261
|
-
end
|
262
|
-
|
263
|
-
def process(&b)
|
264
|
-
block = Proc.new
|
265
|
-
@starttime = Time.now
|
266
|
-
loop {
|
267
|
-
check_event
|
268
|
-
block.call
|
269
|
-
#yield
|
270
|
-
delay
|
271
|
-
return if check_runtime_finished(@starttime)
|
272
|
-
}
|
273
|
-
end
|
274
|
-
|
275
|
-
# check event
|
276
|
-
def check_event
|
277
|
-
x, y, l, m, r = SDL::Mouse.state
|
278
|
-
# x pos, y pos, left button, middle button, right button
|
279
|
-
@mouseX, @mouseY = calc_mouse_xy(x, y)
|
280
|
-
@mouseX0, @mouseY0 = calc_fullscreen_mouse_xy(x, y)
|
281
|
-
event = @sdl_event
|
282
|
-
while event.poll != 0
|
283
|
-
case event.type
|
284
|
-
when SDL::Event::MOUSEBUTTONDOWN
|
285
|
-
do_mousedown
|
286
|
-
when SDL::Event::MOUSEBUTTONUP
|
287
|
-
do_mouseup
|
288
|
-
when SDL::Event::KEYDOWN
|
289
|
-
@keynum = event.info[2]
|
290
|
-
do_keydown(@keynum)
|
291
|
-
when SDL::Event::KEYUP
|
292
|
-
@keynum = event.info[2]
|
293
|
-
do_keyup(@keynum)
|
294
|
-
when SDL::Event::QUIT
|
295
|
-
exit
|
296
|
-
end
|
297
|
-
end
|
298
|
-
end
|
299
|
-
|
300
|
-
def calc_mouse_xy(x, y)
|
301
|
-
if @options[:fullscreen]
|
302
|
-
w, h = @options[:fullscreen]
|
303
|
-
mx = @cameraX - (w / 2) + x
|
304
|
-
my = @cameraY - (h / 2) + (h - y)
|
305
|
-
else
|
306
|
-
mx = @left + x
|
307
|
-
my = @bottom + (@height - y)
|
308
|
-
end
|
309
|
-
return [mx, my]
|
310
|
-
end
|
311
|
-
|
312
|
-
def calc_fullscreen_mouse_xy(x, y)
|
313
|
-
if @options[:fullscreen]
|
314
|
-
w, h = @options[:fullscreen]
|
315
|
-
mx = - (w / 2) + x
|
316
|
-
my = - (h / 2) + (h - y)
|
317
|
-
else
|
318
|
-
mx = @left + x
|
319
|
-
my = @bottom + (@height - y)
|
320
|
-
end
|
321
|
-
return [mx, my]
|
322
|
-
end
|
323
|
-
private :check_event, :calc_mouse_xy, :calc_fullscreen_mouse_xy
|
324
|
-
end
|
325
|
-
end
|
1
|
+
# Copyright (C) 2004-2007 Kouichirou Eto, All rights reserved.
|
2
|
+
# License: Ruby License
|
3
|
+
|
4
|
+
module SGL
|
5
|
+
# callback functions
|
6
|
+
def setup() end
|
7
|
+
def onMouseDown(x,y) end
|
8
|
+
def onMouseUp(x,y) end
|
9
|
+
def onKeyDown(k) end
|
10
|
+
def onKeyUp(k) end
|
11
|
+
def display() end
|
12
|
+
|
13
|
+
# callback functions for fullscreen
|
14
|
+
def onMouseDown0(x,y) end
|
15
|
+
def display0() end
|
16
|
+
|
17
|
+
# mainloop
|
18
|
+
def mainloop
|
19
|
+
$__a__.set_setup { setup }
|
20
|
+
$__a__.set_mousedown {|x, y| onMouseDown(x, y) }
|
21
|
+
$__a__.set_mouseup {|x, y| onMouseUp(x, y) }
|
22
|
+
$__a__.set_keydown {|k| onKeyDown(k) }
|
23
|
+
$__a__.set_keyup {|k| onKeyUp(k) }
|
24
|
+
if ! $__a__.check_display0
|
25
|
+
$__a__.set_display0 { display0 }
|
26
|
+
$__a__.set_mousedown0 {|x, y| onMouseDown0(x, y) }
|
27
|
+
end
|
28
|
+
$__a__.set_display { display }
|
29
|
+
|
30
|
+
if ! defined?($__sgl_in_mainloop__)
|
31
|
+
$__sgl_in_mainloop__ = true
|
32
|
+
$__a__.mainloop
|
33
|
+
else
|
34
|
+
# do setup only.
|
35
|
+
$__a__.mainloop_setup
|
36
|
+
|
37
|
+
# for debug
|
38
|
+
# $__a__.mainloop
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# novice mode
|
43
|
+
def flip(*a) $__a__.flip(*a) end
|
44
|
+
def wait(*a) $__a__.wait(*a) end
|
45
|
+
def process(&b) $__a__.process(&b) end
|
46
|
+
|
47
|
+
# get status functions
|
48
|
+
def mouseX() $__a__.mouseX; end
|
49
|
+
def mouseY() $__a__.mouseY; end
|
50
|
+
def mouseDown() $__a__.mouseDown; end
|
51
|
+
def key(k) $__a__.key[k]; end
|
52
|
+
def keynum() $__a__.keynum; end
|
53
|
+
|
54
|
+
# get status functions for fullscreen
|
55
|
+
def mouseX0() $__a__.mouseX0; end
|
56
|
+
def mouseY0() $__a__.mouseY0; end
|
57
|
+
|
58
|
+
# event control
|
59
|
+
def useDelay(*a) $__a__.useDelay(*a) end
|
60
|
+
def useFramerate(*a) $__a__.useFramerate(*a) end
|
61
|
+
def useRuntime(*a) $__a__.useRuntime(*a) end
|
62
|
+
|
63
|
+
class Application
|
64
|
+
def initialize_event
|
65
|
+
@setup_done = nil
|
66
|
+
@display_drawing = nil
|
67
|
+
@block = {}
|
68
|
+
|
69
|
+
# status setting
|
70
|
+
@mouseX, @mouseY = 0, 0
|
71
|
+
@mouseX0, @mouseY0 = 0, 0
|
72
|
+
@mouseDown = 0
|
73
|
+
@keynum = 0
|
74
|
+
|
75
|
+
@starttime = nil
|
76
|
+
end
|
77
|
+
private :initialize_event
|
78
|
+
|
79
|
+
# get status
|
80
|
+
attr_reader :mouseX, :mouseY
|
81
|
+
attr_reader :mouseX0, :mouseY0
|
82
|
+
attr_reader :mouseDown
|
83
|
+
attr_reader :keynum
|
84
|
+
|
85
|
+
# setup
|
86
|
+
def set_setup(&b)
|
87
|
+
return unless block_given?
|
88
|
+
@block[:setup] = Proc.new
|
89
|
+
end
|
90
|
+
|
91
|
+
def setup_pre
|
92
|
+
# do nothing
|
93
|
+
end
|
94
|
+
|
95
|
+
def do_setup
|
96
|
+
setup_pre
|
97
|
+
@block[:setup].call if @block[:setup]
|
98
|
+
setup_post
|
99
|
+
end
|
100
|
+
|
101
|
+
def setup_post
|
102
|
+
@setup_done = true
|
103
|
+
end
|
104
|
+
private :setup_pre, :setup_post
|
105
|
+
|
106
|
+
# display
|
107
|
+
def set_display(&b)
|
108
|
+
return unless block_given?
|
109
|
+
@block[:display] = Proc.new
|
110
|
+
end
|
111
|
+
|
112
|
+
def set_display0(&b)
|
113
|
+
return unless block_given?
|
114
|
+
@block[:display0] = Proc.new
|
115
|
+
end
|
116
|
+
|
117
|
+
def check_display0
|
118
|
+
return ! @block[:display0].nil?
|
119
|
+
end
|
120
|
+
|
121
|
+
def display_pre
|
122
|
+
set_camera_position
|
123
|
+
check_event
|
124
|
+
clear
|
125
|
+
end
|
126
|
+
|
127
|
+
def do_display # callback
|
128
|
+
return if @setup_done.nil?
|
129
|
+
# return if @display_drawing
|
130
|
+
@display_drawing = true
|
131
|
+
display_pre
|
132
|
+
@block[:display].call if @block[:display]
|
133
|
+
display_post
|
134
|
+
@display_drawing = nil
|
135
|
+
end
|
136
|
+
|
137
|
+
def display_post
|
138
|
+
set_fullscreen_camera_position
|
139
|
+
cur_color = @cur_color
|
140
|
+
@block[:display0].call if @block[:display0]
|
141
|
+
color(*cur_color)
|
142
|
+
SDL.GLSwapBuffers
|
143
|
+
#GC.start
|
144
|
+
end
|
145
|
+
private :display_pre, :display_post
|
146
|
+
|
147
|
+
# mouse events
|
148
|
+
def set_mousedown(&b)
|
149
|
+
return unless block_given?
|
150
|
+
@block[:mousedown] = Proc.new
|
151
|
+
end
|
152
|
+
|
153
|
+
def do_mousedown
|
154
|
+
@mouseDown = 1
|
155
|
+
@block[:mousedown].call(@mouseX, @mouseY) if @block[:mousedown]
|
156
|
+
@block[:mousedown0].call(@mouseX0, @mouseY0) if @block[:mousedown0]
|
157
|
+
end
|
158
|
+
|
159
|
+
def set_mouseup(&b)
|
160
|
+
return unless block_given?
|
161
|
+
@block[:mouseup] = Proc.new
|
162
|
+
end
|
163
|
+
|
164
|
+
def do_mouseup
|
165
|
+
@mouseDown = 0
|
166
|
+
@block[:mouseup].call(@mouseX, @mouseY) if @block[:mouseup]
|
167
|
+
end
|
168
|
+
|
169
|
+
# mouse events for fullscreen
|
170
|
+
def set_mousedown0(&b)
|
171
|
+
return unless block_given?
|
172
|
+
@block[:mousedown0] = Proc.new
|
173
|
+
end
|
174
|
+
|
175
|
+
def check_mousedown0
|
176
|
+
return ! @block[:mousedown0].nil?
|
177
|
+
end
|
178
|
+
|
179
|
+
# key events
|
180
|
+
def set_keydown(&b)
|
181
|
+
return unless block_given?
|
182
|
+
@block[:keydown] = Proc.new
|
183
|
+
end
|
184
|
+
|
185
|
+
def keydown_pre(key)
|
186
|
+
exit if key == SDL::Key::ESCAPE
|
187
|
+
end
|
188
|
+
private :keydown_pre
|
189
|
+
|
190
|
+
def do_keydown(key)
|
191
|
+
keydown_pre(key)
|
192
|
+
@block[:keydown].call(key) if @block[:keydown]
|
193
|
+
end
|
194
|
+
|
195
|
+
def set_keyup(&b)
|
196
|
+
return unless block_given?
|
197
|
+
@block[:keyup] = Proc.new
|
198
|
+
end
|
199
|
+
|
200
|
+
def do_keyup(key)
|
201
|
+
@block[:keyup].call(key) if @block[:keyup]
|
202
|
+
end
|
203
|
+
|
204
|
+
def calc_keynum(e)
|
205
|
+
input = e.characters
|
206
|
+
@keynum = input.to_s[0]
|
207
|
+
end
|
208
|
+
private :calc_keynum
|
209
|
+
|
210
|
+
def mainloop_setup
|
211
|
+
do_setup
|
212
|
+
end
|
213
|
+
|
214
|
+
def mainloop
|
215
|
+
mainloop_setup
|
216
|
+
@starttime = Time.now
|
217
|
+
loop {
|
218
|
+
@begintime = Time.now
|
219
|
+
do_display
|
220
|
+
delay
|
221
|
+
return if check_runtime_finished(@starttime)
|
222
|
+
}
|
223
|
+
end
|
224
|
+
|
225
|
+
def delay
|
226
|
+
if @options[:framerate]
|
227
|
+
sec_per_frame = 1.0 / @options[:framerate]
|
228
|
+
diff = sec_per_frame - (Time.now - @begintime)
|
229
|
+
sleep(diff) if 0 < diff
|
230
|
+
else
|
231
|
+
delaytime = @options[:delaytime]
|
232
|
+
sleep(delaytime)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
private :delay
|
236
|
+
|
237
|
+
def check_runtime_finished(starttime)
|
238
|
+
runtime = @options[:runtime]
|
239
|
+
return false if runtime.nil?
|
240
|
+
diff = Time.now - starttime
|
241
|
+
return (runtime && runtime < diff)
|
242
|
+
end
|
243
|
+
private :check_runtime_finished
|
244
|
+
|
245
|
+
# novice mode
|
246
|
+
def flip
|
247
|
+
@starttime = Time.now if @starttime.nil?
|
248
|
+
display_post
|
249
|
+
delay
|
250
|
+
display_pre
|
251
|
+
#exit if check_runtime_finished(@starttime)
|
252
|
+
end
|
253
|
+
|
254
|
+
def wait
|
255
|
+
#SGL.flip if !$__v__.flipped
|
256
|
+
loop {
|
257
|
+
check_event
|
258
|
+
delay
|
259
|
+
return if check_runtime_finished(@starttime)
|
260
|
+
}
|
261
|
+
end
|
262
|
+
|
263
|
+
def process(&b)
|
264
|
+
block = Proc.new
|
265
|
+
@starttime = Time.now
|
266
|
+
loop {
|
267
|
+
check_event
|
268
|
+
block.call
|
269
|
+
#yield
|
270
|
+
delay
|
271
|
+
return if check_runtime_finished(@starttime)
|
272
|
+
}
|
273
|
+
end
|
274
|
+
|
275
|
+
# check event
|
276
|
+
def check_event
|
277
|
+
x, y, l, m, r = SDL::Mouse.state
|
278
|
+
# x pos, y pos, left button, middle button, right button
|
279
|
+
@mouseX, @mouseY = calc_mouse_xy(x, y)
|
280
|
+
@mouseX0, @mouseY0 = calc_fullscreen_mouse_xy(x, y)
|
281
|
+
event = @sdl_event
|
282
|
+
while event.poll != 0
|
283
|
+
case event.type
|
284
|
+
when SDL::Event::MOUSEBUTTONDOWN
|
285
|
+
do_mousedown
|
286
|
+
when SDL::Event::MOUSEBUTTONUP
|
287
|
+
do_mouseup
|
288
|
+
when SDL::Event::KEYDOWN
|
289
|
+
@keynum = event.info[2]
|
290
|
+
do_keydown(@keynum)
|
291
|
+
when SDL::Event::KEYUP
|
292
|
+
@keynum = event.info[2]
|
293
|
+
do_keyup(@keynum)
|
294
|
+
when SDL::Event::QUIT
|
295
|
+
exit
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
def calc_mouse_xy(x, y)
|
301
|
+
if @options[:fullscreen]
|
302
|
+
w, h = @options[:fullscreen]
|
303
|
+
mx = @cameraX - (w / 2) + x
|
304
|
+
my = @cameraY - (h / 2) + (h - y)
|
305
|
+
else
|
306
|
+
mx = @left + x
|
307
|
+
my = @bottom + (@height - y)
|
308
|
+
end
|
309
|
+
return [mx, my]
|
310
|
+
end
|
311
|
+
|
312
|
+
def calc_fullscreen_mouse_xy(x, y)
|
313
|
+
if @options[:fullscreen]
|
314
|
+
w, h = @options[:fullscreen]
|
315
|
+
mx = - (w / 2) + x
|
316
|
+
my = - (h / 2) + (h - y)
|
317
|
+
else
|
318
|
+
mx = @left + x
|
319
|
+
my = @bottom + (@height - y)
|
320
|
+
end
|
321
|
+
return [mx, my]
|
322
|
+
end
|
323
|
+
private :check_event, :calc_mouse_xy, :calc_fullscreen_mouse_xy
|
324
|
+
end
|
325
|
+
end
|