dxruby_sdl 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OGIxYTRjODcxNGNiZjU5MDlkNDY5ZjMxZmZhOTA3MTBjYzk1NmE0ZA==
4
+ ZjNjNDE4NTRhMjM3MjQ3YjZmNDQ3YjBhZTNlNGNmNzUyMDNlMzM0ZQ==
5
5
  data.tar.gz: !binary |-
6
- ODQ2MTgzODg3YWY5YWJkNzNkZDI2NjRhYzczMDAxNTUzNmU1MGVjOA==
6
+ ODkyNjFjMjgwMGQwOTU0NmNiOTljOTFmYTAxOTI0NmVjMGY1MDUzZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YmQ0YTVjNzFjZjlkZTQ4OGM3MmIwNzgyZjRiZTdkMmUzZTQ1ZDY0OWUyNzA5
10
- ZjEyZjhlNzM3NGM5N2UwNTBmNDQwYjdmMjE5MzM5NGM0NDUzODMyZjAzZTI0
11
- OWEzMWFmOTA2ODY1NDkyN2Q2ZTZlYzAyZjQxN2FiNGY3NGZlZjE=
9
+ NTY5MjMyYWI3YTRiNDVmMTdkZjZkMzI0YjljYmY5NWUxNTQ3M2JiNDEyNWQ3
10
+ ZDE1MGEzNzQzOGNhODFhNjQ5MTk0MTE4NTIzYzkzZDQ0NWFjMDlkNWU3Njdl
11
+ M2VkZmVlOGUyZWEwMTgyYzA2MDZlODFlMjlhMzAxZDc2N2E0ZmQ=
12
12
  data.tar.gz: !binary |-
13
- MzQ5NTBmNjJhMTljMTQyOGM2OWU0NmY0NTQzNjEzODVkZmMwODcxODg1M2U4
14
- Mzc5M2Q1MjNlZTkzNzg2MGU4MzAzMDJmYzc1MTg0YTViNmU2MmJhY2M5NjQz
15
- MzlkZDkzZjU5NzFlMDg4ZjVlNWVjOTEzY2ZmNDI2Y2Y5Njc0YTQ=
13
+ ZWM3ZWQ1MWM4ZTE2MjM1ZGFjMzJmOThkMmRjZDMyODRhMTlmZDhlOTk0ZmNm
14
+ MTYxZmQ2Y2UxMjY3OWQ2YjFiOGNmYTEyZDUwMmI4YWNjNDZkYmEyMGJjZjk1
15
+ M2Q2NDI0NmY2NjdkMDQ5MGMxYmY2YmE4YzkwYzY3YTUxNWIxZGY=
@@ -70,13 +70,32 @@ module DXRubySDL
70
70
  end
71
71
  end
72
72
 
73
+ def circle_fill(x, y, r, color)
74
+ lock do
75
+ @_surface.draw_circle(x, y, r, to_sdl_color(color), true, false,
76
+ to_sdl_alpha(color))
77
+ end
78
+ end
79
+
73
80
  def box(x1, y1, x2, y2, color)
74
81
  x = x1 < x2 ? x1 : x2
75
82
  w = (x2 - x1).abs
76
83
  y = y1 < y2 ? y1 : y2
77
84
  h = (y2 - y1).abs
78
85
  lock do
79
- @_surface.draw_rect(x, y, w, h, to_sdl_color(color))
86
+ @_surface.draw_rect(x, y, w, h, to_sdl_color(color), false,
87
+ to_sdl_alpha(color))
88
+ end
89
+ end
90
+
91
+ def box_fill(x1, y1, x2, y2, color)
92
+ x = x1 < x2 ? x1 : x2
93
+ w = (x2 - x1).abs
94
+ y = y1 < y2 ? y1 : y2
95
+ h = (y2 - y1).abs
96
+ lock do
97
+ @_surface.draw_rect(x, y, w, h, to_sdl_color(color), true,
98
+ to_sdl_alpha(color))
80
99
  end
81
100
  end
82
101
 
@@ -86,6 +105,8 @@ module DXRubySDL
86
105
  alias_method :load_to_array, :load_tiles
87
106
  alias_method :loadToArray, :load_to_array
88
107
  end
108
+ alias_method :circleFill, :circle_fill
109
+ alias_method :boxFill, :box_fill
89
110
  # rubocop:enable SymbolName
90
111
 
91
112
  private
@@ -48,12 +48,39 @@ module DXRubySDL
48
48
  return key_press?(to_sdl_key(key_code))
49
49
  end
50
50
 
51
+ def mouse_down?(button)
52
+ case button
53
+ when M_LBUTTON
54
+ index = 2
55
+ when M_MBUTTON
56
+ index = 3
57
+ when M_RBUTTON
58
+ index = 4
59
+ end
60
+ return SDL::Mouse.state[index]
61
+ end
62
+
63
+ def mouse_push?(button)
64
+ case button
65
+ when M_LBUTTON
66
+ index = 2
67
+ when M_MBUTTON
68
+ index = 3
69
+ when M_RBUTTON
70
+ index = 4
71
+ end
72
+ return SDL::Mouse.state[index] &&
73
+ !Window.instance_variable_get('@last_mouse_state')[index]
74
+ end
75
+
51
76
  # rubocop:disable SymbolName
52
77
  class << self
53
78
  alias_method :padDown?, :pad_down?
54
79
  alias_method :mousePosX, :mouse_pos_x
55
80
  alias_method :mousePosY, :mouse_pos_y
56
81
  alias_method :keyPush?, :key_push?
82
+ alias_method :mouseDown?, :mouse_down?
83
+ alias_method :mousePush?, :mouse_push?
57
84
  end
58
85
  # rubocop:enable SymbolName
59
86
 
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DXRubySDL
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -4,6 +4,8 @@ require 'dxruby_sdl/window/fpstimer'
4
4
 
5
5
  module DXRubySDL
6
6
  module Window
7
+ @last_mouse_state = [false, false, false]
8
+
7
9
  module_function
8
10
 
9
11
  def _screen
@@ -13,6 +15,10 @@ module DXRubySDL
13
15
  SDL::SWSURFACE)
14
16
  end
15
17
 
18
+ def fps=(val)
19
+ FPSTimer.instance.fps = val
20
+ end
21
+
16
22
  def loop(&block)
17
23
  timer = FPSTimer.instance
18
24
  timer.reset
@@ -32,6 +38,8 @@ module DXRubySDL
32
38
  yield
33
39
 
34
40
  _screen.update_rect(0, 0, 0, 0)
41
+
42
+ @last_mouse_state = SDL::Mouse.state
35
43
  end
36
44
  end
37
45
  end
data/samples/kadai1.rb ADDED
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'dxruby'
3
+
4
+ i = Image.new(640, 480)
5
+ i.boxFill(50, 50, 350, 200, [255, 0, 0])
6
+ i.circle(50, 50, 10, [0, 255, 0])
7
+ i.circle(350, 50, 10, [0, 255, 0])
8
+ i.circle(50, 200, 10, [0, 255, 0])
9
+ i.circle(350, 200, 10, [0, 255, 0])
10
+
11
+ font = Font.new(50, 'MS Gothic')
12
+
13
+ Window.loop do
14
+ Window.draw(0, 0, i)
15
+
16
+ t = Time.now
17
+ if t.sec.even?
18
+ s = t.strftime('%H:%M:%S')
19
+ else
20
+ s = t.strftime('%H %M %S')
21
+ end
22
+ Window.drawFont(100, 100, s, font)
23
+ end
data/samples/kadai2.rb ADDED
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'dxruby'
3
+
4
+ c = 255
5
+ r = 1
6
+
7
+ font = Font.new(32)
8
+ i = Image.new(640, 480)
9
+
10
+ Window.loop do
11
+ c -= Input.x
12
+ if c > 255
13
+ c = 255
14
+ end
15
+ if c < 0
16
+ c = 0
17
+ end
18
+
19
+ r -= Input.y
20
+ if r > 20
21
+ r = 20
22
+ end
23
+ if r < 1
24
+ r = 1
25
+ end
26
+
27
+ Window.drawFont(0, 0, "C:#{c} R:#{r}", font)
28
+
29
+ if Input.mouseDown?(M_LBUTTON)
30
+ x = Input.mousePosX
31
+ y = Input.mousePosY
32
+ i.circleFill(x, y, r, [c, c, c])
33
+ end
34
+ Window.draw(0, 0, i)
35
+ end
data/samples/kadai3.rb ADDED
@@ -0,0 +1,49 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'dxruby'
3
+
4
+ f = ['7', '@', 'G', '!', '*']
5
+ no1_f = f.sort_by { rand }
6
+ no2_f = f.sort_by { rand }
7
+ no3_f = f.sort_by { rand }
8
+
9
+ i = 0
10
+ no1 = no1_f[i]
11
+ no2 = no2_f[i]
12
+ no3 = no3_f[i]
13
+
14
+ no1_s = false
15
+ no2_s = false
16
+ no3_s = false
17
+ font = Font.new(64, 'MS Gothic')
18
+ Window.fps = 15
19
+ Window.loop do
20
+ Window.drawFont(20, 10, "#{no1} #{no2} #{no3}", font)
21
+ if Input.mousePush?(M_LBUTTON)
22
+ if !no1_s
23
+ no1_s = true
24
+ elsif !no2_s
25
+ no2_s = true
26
+ elsif !no3_s
27
+ no3_s = true
28
+ end
29
+ end
30
+ i += 1
31
+ if i >= f.length
32
+ i = 0
33
+ end
34
+ if !no1_s
35
+ no1 = no1_f[i]
36
+ end
37
+ if !no2_s
38
+ no2 = no2_f[i]
39
+ end
40
+ if no3_s
41
+ if no1 == no2 && no1 == no3
42
+ Window.drawFont(120, 100, 'Good!!', font)
43
+ else
44
+ Window.drawFont(120, 100, 'Bad.', font)
45
+ end
46
+ else
47
+ no3 = no3_f[i]
48
+ end
49
+ end
@@ -149,9 +149,45 @@ describe DXRubySDL::Image, '画像を表すクラス' do
149
149
  include_context 'draw methods'
150
150
  end
151
151
 
152
+ shared_context '#circle_fill' do
153
+ include_context 'draw methods'
154
+ end
155
+
156
+ describe '#circle_fill' do
157
+ subject { image.circle_fill(50, 50, 25, [255, 255, 255]) }
158
+
159
+ include_context '#circle_fill'
160
+
161
+ describe 'alias' do
162
+ describe '#circleFill' do
163
+ it_behaves_like '#circle_fill' do
164
+ subject { image.circleFill(50, 50, 25, [255, 255, 255]) }
165
+ end
166
+ end
167
+ end
168
+ end
169
+
152
170
  describe '#box' do
153
171
  subject { image.box(0, 0, 100, 100, [255, 255, 255]) }
154
172
 
155
173
  include_context 'draw methods'
156
174
  end
175
+
176
+ shared_context '#box_fill' do
177
+ include_context 'draw methods'
178
+ end
179
+
180
+ describe '#box_fill' do
181
+ subject { image.box_fill(0, 0, 100, 100, [255, 255, 255]) }
182
+
183
+ include_context '#box_fill'
184
+
185
+ describe 'alias' do
186
+ describe '#boxFill' do
187
+ it_behaves_like '#box_fill' do
188
+ subject { image.boxFill(0, 0, 100, 100, [255, 255, 255]) }
189
+ end
190
+ end
191
+ end
192
+ end
157
193
  end
@@ -227,4 +227,275 @@ describe DXRubySDL::Input,
227
227
  end
228
228
  end
229
229
  end
230
+
231
+ shared_context '.mouse_down?' do
232
+ context 'マウスの左ボタンを押している場合' do
233
+ before do
234
+ allow(SDL::Mouse)
235
+ .to receive(:state).and_return([0, 0, true, false, false])
236
+ end
237
+
238
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
239
+ let(:button) { DXRubySDL::M_LBUTTON }
240
+
241
+ it { should be_true }
242
+ end
243
+
244
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
245
+ let(:button) { DXRubySDL::M_MBUTTON }
246
+
247
+ it { should be_false }
248
+ end
249
+
250
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
251
+ let(:button) { DXRubySDL::M_RBUTTON }
252
+
253
+ it { should be_false }
254
+ end
255
+ end
256
+
257
+ context 'マウスの中ボタンを押している場合' do
258
+ before do
259
+ allow(SDL::Mouse)
260
+ .to receive(:state).and_return([0, 0, false, true, false])
261
+ end
262
+
263
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
264
+ let(:button) { DXRubySDL::M_LBUTTON }
265
+
266
+ it { should be_false }
267
+ end
268
+
269
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
270
+ let(:button) { DXRubySDL::M_MBUTTON }
271
+
272
+ it { should be_true }
273
+ end
274
+
275
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
276
+ let(:button) { DXRubySDL::M_RBUTTON }
277
+
278
+ it { should be_false }
279
+ end
280
+ end
281
+
282
+ context 'マウスの右ボタンを押している場合' do
283
+ before do
284
+ allow(SDL::Mouse)
285
+ .to receive(:state).and_return([0, 0, false, false, true])
286
+ end
287
+
288
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
289
+ let(:button) { DXRubySDL::M_LBUTTON }
290
+
291
+ it { should be_false }
292
+ end
293
+
294
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
295
+ let(:button) { DXRubySDL::M_MBUTTON }
296
+
297
+ it { should be_false }
298
+ end
299
+
300
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
301
+ let(:button) { DXRubySDL::M_RBUTTON }
302
+
303
+ it { should be_true }
304
+ end
305
+ end
306
+ end
307
+
308
+ describe '.mouse_down?' do
309
+ subject { described_class.mouse_down?(button) }
310
+
311
+ include_context '.mouse_down?'
312
+
313
+ describe 'alias' do
314
+ describe '.mouseDown?' do
315
+ it_behaves_like '.mouse_down?' do
316
+ subject { described_class.mouseDown?(button) }
317
+ end
318
+ end
319
+ end
320
+ end
321
+
322
+ shared_context '.mouse_push?' do
323
+ context '直前にマウスのボタンを押していない場合' do
324
+ before do
325
+ DXRubySDL::Window.instance_variable_set('@last_mouse_state',
326
+ [0, 0, false, false, false])
327
+ end
328
+
329
+ context 'マウスの左ボタンを押している場合' do
330
+ before do
331
+ allow(SDL::Mouse)
332
+ .to receive(:state).and_return([0, 0, true, false, false])
333
+ end
334
+
335
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
336
+ let(:button) { DXRubySDL::M_LBUTTON }
337
+
338
+ it { should be_true }
339
+ end
340
+
341
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
342
+ let(:button) { DXRubySDL::M_MBUTTON }
343
+
344
+ it { should be_false }
345
+ end
346
+
347
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
348
+ let(:button) { DXRubySDL::M_RBUTTON }
349
+
350
+ it { should be_false }
351
+ end
352
+ end
353
+
354
+ context 'マウスの中ボタンを押している場合' do
355
+ before do
356
+ allow(SDL::Mouse)
357
+ .to receive(:state).and_return([0, 0, false, true, false])
358
+ end
359
+
360
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
361
+ let(:button) { DXRubySDL::M_LBUTTON }
362
+
363
+ it { should be_false }
364
+ end
365
+
366
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
367
+ let(:button) { DXRubySDL::M_MBUTTON }
368
+
369
+ it { should be_true }
370
+ end
371
+
372
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
373
+ let(:button) { DXRubySDL::M_RBUTTON }
374
+
375
+ it { should be_false }
376
+ end
377
+ end
378
+
379
+ context 'マウスの右ボタンを押している場合' do
380
+ before do
381
+ allow(SDL::Mouse)
382
+ .to receive(:state).and_return([0, 0, false, false, true])
383
+ end
384
+
385
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
386
+ let(:button) { DXRubySDL::M_LBUTTON }
387
+
388
+ it { should be_false }
389
+ end
390
+
391
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
392
+ let(:button) { DXRubySDL::M_MBUTTON }
393
+
394
+ it { should be_false }
395
+ end
396
+
397
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
398
+ let(:button) { DXRubySDL::M_RBUTTON }
399
+
400
+ it { should be_true }
401
+ end
402
+ end
403
+ end
404
+
405
+ context '直前にマウスのボタンを全て押していた場合' do
406
+ before do
407
+ DXRubySDL::Window.instance_variable_set('@last_mouse_state',
408
+ [0, 0, true, true, true])
409
+ end
410
+
411
+ context 'マウスの左ボタンを押している場合' do
412
+ before do
413
+ allow(SDL::Mouse)
414
+ .to receive(:state).and_return([0, 0, true, false, false])
415
+ end
416
+
417
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
418
+ let(:button) { DXRubySDL::M_LBUTTON }
419
+
420
+ it { should be_false }
421
+ end
422
+
423
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
424
+ let(:button) { DXRubySDL::M_MBUTTON }
425
+
426
+ it { should be_false }
427
+ end
428
+
429
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
430
+ let(:button) { DXRubySDL::M_RBUTTON }
431
+
432
+ it { should be_false }
433
+ end
434
+ end
435
+
436
+ context 'マウスの中ボタンを押している場合' do
437
+ before do
438
+ allow(SDL::Mouse)
439
+ .to receive(:state).and_return([0, 0, false, true, false])
440
+ end
441
+
442
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
443
+ let(:button) { DXRubySDL::M_LBUTTON }
444
+
445
+ it { should be_false }
446
+ end
447
+
448
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
449
+ let(:button) { DXRubySDL::M_MBUTTON }
450
+
451
+ it { should be_false }
452
+ end
453
+
454
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
455
+ let(:button) { DXRubySDL::M_RBUTTON }
456
+
457
+ it { should be_false }
458
+ end
459
+ end
460
+
461
+ context 'マウスの右ボタンを押している場合' do
462
+ before do
463
+ allow(SDL::Mouse)
464
+ .to receive(:state).and_return([0, 0, false, false, true])
465
+ end
466
+
467
+ describe 'マウスの左ボタン(M_LBUTTON)を指定する' do
468
+ let(:button) { DXRubySDL::M_LBUTTON }
469
+
470
+ it { should be_false }
471
+ end
472
+
473
+ describe 'マウスの中ボタン(M_MBUTTON)を指定する' do
474
+ let(:button) { DXRubySDL::M_MBUTTON }
475
+
476
+ it { should be_false }
477
+ end
478
+
479
+ describe 'マウスの右ボタン(M_RBUTTON)を指定する' do
480
+ let(:button) { DXRubySDL::M_RBUTTON }
481
+
482
+ it { should be_false }
483
+ end
484
+ end
485
+ end
486
+ end
487
+
488
+ describe '.mouse_push?' do
489
+ subject { described_class.mouse_push?(button) }
490
+
491
+ include_context '.mouse_push?'
492
+
493
+ describe 'alias' do
494
+ describe '.mousePush?' do
495
+ it_behaves_like '.mouse_push?' do
496
+ subject { described_class.mousePush?(button) }
497
+ end
498
+ end
499
+ end
500
+ end
230
501
  end
@@ -26,6 +26,22 @@ describe DXRubySDL::Window do
26
26
  end
27
27
  end
28
28
 
29
+ describe '.fps=', 'FPSを設定する' do
30
+ context '15に設定した場合' do
31
+ let(:fps) { 15 }
32
+
33
+ before do
34
+ DXRubySDL::Window.fps = fps
35
+ end
36
+
37
+ describe 'DXRubySDL::Window::FPSTimer.instance' do
38
+ subject { DXRubySDL::Window::FPSTimer.instance }
39
+
40
+ its(:fps) { should eq(fps) }
41
+ end
42
+ end
43
+ end
44
+
29
45
  shared_context '.draw_font' do
30
46
  context 'サイズのみを設定したフォントを指定した場合' do
31
47
  let!(:font) { DXRubySDL::Font.new(32) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dxruby_sdl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
@@ -153,6 +153,9 @@ files:
153
153
  - lib/dxruby_sdl/window/fpstimer.rb
154
154
  - samples/bgm.mid
155
155
  - samples/data.png
156
+ - samples/kadai1.rb
157
+ - samples/kadai2.rb
158
+ - samples/kadai3.rb
156
159
  - samples/sound.wav
157
160
  - samples/tutorial-2-01.rb
158
161
  - samples/tutorial-2-02.rb