dxruby_sdl 0.0.3 → 0.0.4

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
- ZjNjNDE4NTRhMjM3MjQ3YjZmNDQ3YjBhZTNlNGNmNzUyMDNlMzM0ZQ==
4
+ ZjEyN2ViYzcxYTFiMGVjNTJhOTU2MDg3M2Q1YjMxNDExOWE1NGQ5Yw==
5
5
  data.tar.gz: !binary |-
6
- ODkyNjFjMjgwMGQwOTU0NmNiOTljOTFmYTAxOTI0NmVjMGY1MDUzZA==
6
+ YTg5ZGE1YTUxYWUzNzFlOGVkYTk5MTFkN2MwNTVhMDZkZGQ2ZmI1ZQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NTY5MjMyYWI3YTRiNDVmMTdkZjZkMzI0YjljYmY5NWUxNTQ3M2JiNDEyNWQ3
10
- ZDE1MGEzNzQzOGNhODFhNjQ5MTk0MTE4NTIzYzkzZDQ0NWFjMDlkNWU3Njdl
11
- M2VkZmVlOGUyZWEwMTgyYzA2MDZlODFlMjlhMzAxZDc2N2E0ZmQ=
9
+ NDExY2Q5MzA3YWUyZGY4Y2U0YmJjZTBiNTE1ZGZjNGE2ZGY1NDE2ZTFkNDM3
10
+ Yjg5ZDRhZGMxMDQ2YTRlYTVjYzM0NDY3NTVkMzJjYWFjYzZjNWI2NmU5OWQ0
11
+ YzRiYjg1NmM3YjhjNDVjMmQ1M2FhYmViZGZmOGVmMjI2M2E4ZWI=
12
12
  data.tar.gz: !binary |-
13
- ZWM3ZWQ1MWM4ZTE2MjM1ZGFjMzJmOThkMmRjZDMyODRhMTlmZDhlOTk0ZmNm
14
- MTYxZmQ2Y2UxMjY3OWQ2YjFiOGNmYTEyZDUwMmI4YWNjNDZkYmEyMGJjZjk1
15
- M2Q2NDI0NmY2NjdkMDQ5MGMxYmY2YmE4YzkwYzY3YTUxNWIxZGY=
13
+ OTc1ZGZhZWJmY2QzZTkxMzkzZmFmZWY1MGU4YmRmMGI0MTAxOGI3YmM5ODZk
14
+ NWNjNTY3MWFjNWMxNDE0ODU1NzQ4ZWEzYTVkZmEyNTI3ZGVhMTVhMzUyZTI5
15
+ ZTAxNDJkYmQyZjUxNmY2MGUwYWVlMzI1ODVkZjg0NDFiYWIwY2U=
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .rake_tasks
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
@@ -12,6 +13,7 @@ lib/bundler/man
12
13
  pkg
13
14
  rdoc
14
15
  spec/reports
16
+ spec/rspec
15
17
  test/tmp
16
18
  test/version_tmp
17
19
  tmp
data/Guardfile ADDED
@@ -0,0 +1,25 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ rspec_option = {
5
+ all_after_pass: false,
6
+ all_on_start: false
7
+ }
8
+ if /darwin/ =~ RUBY_PLATFORM
9
+ rspec_option[:binstubs] = 'spec'
10
+ end
11
+ guard :rspec, rspec_option do
12
+ watch(%r{^spec/.+_spec\.rb$})
13
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
14
+ watch('spec/spec_helper.rb') { "spec" }
15
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
16
+ end
17
+
18
+ guard :rubocop, all_on_start: false do
19
+ watch(%r{.+\.rb$})
20
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
21
+ end
22
+
23
+ # Local Variables:
24
+ # mode: ruby
25
+ # End:
data/Rakefile CHANGED
@@ -5,8 +5,28 @@ if /darwin/ =~ RUBY_PLATFORM
5
5
  task :spec do
6
6
  sh "rsdl -S rspec #{ENV['SPEC_OPTS']} #{ENV['SPEC']}"
7
7
  end
8
+
9
+ task :guard do
10
+ rspec_path = 'spec/rspec'
11
+ File.open(rspec_path, 'w') do |f|
12
+ f.write(<<-EOS)
13
+ #!/bin/sh
14
+ bundle exec rsdl -S rspec $@
15
+ EOS
16
+ end
17
+ chmod(0755, rspec_path)
18
+ begin
19
+ sh "bundle exec guard"
20
+ ensure
21
+ rm_rf(rspec_path)
22
+ end
23
+ end
8
24
  else
9
25
  RSpec::Core::RakeTask.new(:spec)
26
+
27
+ task :guard do
28
+ sh "bundle exec guard"
29
+ end
10
30
  end
11
31
 
12
32
  task :rubocop do
data/dxruby_sdl.gemspec CHANGED
@@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'coveralls'
25
25
  spec.add_development_dependency 'travis-lint'
26
26
  spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'guard-rspec'
28
+ spec.add_development_dependency 'guard-rubocop'
27
29
 
28
30
  spec.add_runtime_dependency 'rubysdl'
29
31
  spec.add_runtime_dependency 'rsdl' if /darwin/ =~ RUBY_PLATFORM
@@ -37,7 +37,7 @@ module DXRubySDL
37
37
  end
38
38
 
39
39
  @_surface =
40
- SDL::Surface.new(SDL::SWSURFACE, width, height, Window._screen)
40
+ SDL::Surface.new(SDL::SWSURFACE, width, height, Window.send(:screen))
41
41
  @_surface.fill_rect(0, 0, width, height, @color)
42
42
  end
43
43
 
@@ -61,6 +61,7 @@ module DXRubySDL
61
61
  @_surface.draw_line(x1, y1, x2, y2,
62
62
  to_sdl_color(color), true, to_sdl_alpha(color))
63
63
  end
64
+ return self
64
65
  end
65
66
 
66
67
  def circle(x, y, r, color)
@@ -68,6 +69,7 @@ module DXRubySDL
68
69
  @_surface.draw_circle(x, y, r, to_sdl_color(color), false, true,
69
70
  to_sdl_alpha(color))
70
71
  end
72
+ return self
71
73
  end
72
74
 
73
75
  def circle_fill(x, y, r, color)
@@ -75,6 +77,7 @@ module DXRubySDL
75
77
  @_surface.draw_circle(x, y, r, to_sdl_color(color), true, false,
76
78
  to_sdl_alpha(color))
77
79
  end
80
+ return self
78
81
  end
79
82
 
80
83
  def box(x1, y1, x2, y2, color)
@@ -86,6 +89,7 @@ module DXRubySDL
86
89
  @_surface.draw_rect(x, y, w, h, to_sdl_color(color), false,
87
90
  to_sdl_alpha(color))
88
91
  end
92
+ return self
89
93
  end
90
94
 
91
95
  def box_fill(x1, y1, x2, y2, color)
@@ -97,6 +101,7 @@ module DXRubySDL
97
101
  @_surface.draw_rect(x, y, w, h, to_sdl_color(color), true,
98
102
  to_sdl_alpha(color))
99
103
  end
104
+ return self
100
105
  end
101
106
 
102
107
  # rubocop:disable SymbolName
@@ -4,12 +4,16 @@ module DXRubySDL
4
4
  module Input
5
5
  module_function
6
6
 
7
+ def set_repeat(wait, interval)
8
+ SDL::Key.enable_key_repeat(wait, interval)
9
+ end
10
+
7
11
  def x(pad_number = 0)
8
12
  res = 0
9
- if key_press?(SDL::Key::LEFT)
13
+ if sdl_key_press?(SDL::Key::LEFT)
10
14
  res -= 1
11
15
  end
12
- if key_press?(SDL::Key::RIGHT)
16
+ if sdl_key_press?(SDL::Key::RIGHT)
13
17
  res += 1
14
18
  end
15
19
  return res
@@ -17,19 +21,37 @@ module DXRubySDL
17
21
 
18
22
  def y(pad_number = 0)
19
23
  res = 0
20
- if key_press?(SDL::Key::UP)
24
+ if sdl_key_press?(SDL::Key::UP)
21
25
  res -= 1
22
26
  end
23
- if key_press?(SDL::Key::DOWN)
27
+ if sdl_key_press?(SDL::Key::DOWN)
24
28
  res += 1
25
29
  end
26
30
  return res
27
31
  end
28
32
 
29
33
  def pad_down?(button_code, pad_number = 0)
30
- if button_code == P_BUTTON0 && key_press?(SDL::Key::Z) ||
31
- button_code == P_BUTTON1 && key_press?(SDL::Key::X) ||
32
- button_code == P_BUTTON2 && key_press?(SDL::Key::C) ||
34
+ if button_code == P_BUTTON0 && sdl_key_press?(SDL::Key::Z) ||
35
+ button_code == P_BUTTON1 && sdl_key_press?(SDL::Key::X) ||
36
+ button_code == P_BUTTON2 && sdl_key_press?(SDL::Key::C) ||
37
+ button_code == P_LEFT && sdl_key_press?(SDL::Key::LEFT) ||
38
+ button_code == P_RIGHT && sdl_key_press?(SDL::Key::RIGHT) ||
39
+ button_code == P_UP && sdl_key_press?(SDL::Key::UP) ||
40
+ button_code == P_DOWN && sdl_key_press?(SDL::Key::DOWN) ||
41
+ ((j = joystick(pad_number)) && j.button(button_code))
42
+ return true
43
+ end
44
+ return false
45
+ end
46
+
47
+ def pad_push?(button_code, pad_number = 0)
48
+ if button_code == P_BUTTON0 && sdl_key_push?(SDL::Key::Z) ||
49
+ button_code == P_BUTTON1 && sdl_key_push?(SDL::Key::X) ||
50
+ button_code == P_BUTTON2 && sdl_key_push?(SDL::Key::C) ||
51
+ button_code == P_LEFT && sdl_key_push?(SDL::Key::LEFT) ||
52
+ button_code == P_RIGHT && sdl_key_push?(SDL::Key::RIGHT) ||
53
+ button_code == P_UP && sdl_key_push?(SDL::Key::UP) ||
54
+ button_code == P_DOWN && sdl_key_push?(SDL::Key::DOWN) ||
33
55
  ((j = joystick(pad_number)) && j.button(button_code))
34
56
  return true
35
57
  end
@@ -44,8 +66,12 @@ module DXRubySDL
44
66
  return SDL::Mouse.state[1]
45
67
  end
46
68
 
69
+ def key_down?(key_code)
70
+ return sdl_key_press?(to_sdl_key(key_code))
71
+ end
72
+
47
73
  def key_push?(key_code)
48
- return key_press?(to_sdl_key(key_code))
74
+ return sdl_key_push?(to_sdl_key(key_code))
49
75
  end
50
76
 
51
77
  def mouse_down?(button)
@@ -69,15 +95,17 @@ module DXRubySDL
69
95
  when M_RBUTTON
70
96
  index = 4
71
97
  end
72
- return SDL::Mouse.state[index] &&
73
- !Window.instance_variable_get('@last_mouse_state')[index]
98
+ return SDL::Mouse.state[index] && !@last_mouse_state[index]
74
99
  end
75
100
 
76
101
  # rubocop:disable SymbolName
77
102
  class << self
103
+ alias_method :setRepeat, :set_repeat
78
104
  alias_method :padDown?, :pad_down?
105
+ alias_method :padPush?, :pad_push?
79
106
  alias_method :mousePosX, :mouse_pos_x
80
107
  alias_method :mousePosY, :mouse_pos_y
108
+ alias_method :keyDown?, :key_down?
81
109
  alias_method :keyPush?, :key_push?
82
110
  alias_method :mouseDown?, :mouse_down?
83
111
  alias_method :mousePush?, :mouse_push?
@@ -86,6 +114,9 @@ module DXRubySDL
86
114
 
87
115
  private
88
116
 
117
+ @current_key_state = Set.new
118
+ @last_key_state = Set.new
119
+ @last_mouse_state = [false, false, false]
89
120
  @joysticks = []
90
121
 
91
122
  class << self
@@ -131,9 +162,34 @@ module DXRubySDL
131
162
  end
132
163
  private_constant :KEY_TABLE
133
164
 
134
- def key_press?(key)
165
+ def store_last_state
166
+ keys = (@last_key_state - @current_key_state)
167
+ if keys.length > 0
168
+ SDL::Key.scan
169
+ keys.each do |key|
170
+ if SDL::Key.press?(key)
171
+ @current_key_state.add(key)
172
+ end
173
+ end
174
+ end
175
+ @last_key_state = @current_key_state
176
+ @current_key_state = Set.new
177
+ @last_mouse_state = SDL::Mouse.state
178
+ end
179
+
180
+ def sdl_key_press?(key)
135
181
  SDL::Key.scan
136
- return SDL::Key.press?(key)
182
+ if SDL::Key.press?(key)
183
+ @current_key_state.add(key)
184
+ return true
185
+ else
186
+ @current_key_state.delete(key)
187
+ return false
188
+ end
189
+ end
190
+
191
+ def sdl_key_push?(key)
192
+ return sdl_key_press?(key) && !@last_key_state.include?(key)
137
193
  end
138
194
 
139
195
  def joystick(pad_number)
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module DXRubySDL
4
+ class SoundEffect
5
+ def initialize(time, wavetype = WAVE_RECT, resolution = 1000)
6
+ end
7
+
8
+ def add(wavetype = WAVE_RECT, resolution = 1000)
9
+ end
10
+
11
+ def play
12
+ end
13
+
14
+ def stop
15
+ end
16
+ end
17
+ end
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DXRubySDL
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
@@ -1,18 +1,33 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'dxruby_sdl/window/fpstimer'
4
+ require 'set'
4
5
 
5
6
  module DXRubySDL
6
7
  module Window
7
- @last_mouse_state = [false, false, false]
8
-
9
8
  module_function
10
9
 
11
- def _screen
12
- return SDL::Screen.get
13
- rescue SDL::Error
14
- return SDL::Screen.open(DEFAULTS[:width], DEFAULTS[:height], 16,
15
- SDL::SWSURFACE)
10
+ def width
11
+ @width ||= DEFAULTS[:width]
12
+ return @width
13
+ end
14
+
15
+ def height
16
+ @height ||= DEFAULTS[:height]
17
+ return @height
18
+ end
19
+
20
+ def scale
21
+ @scale ||= 1
22
+ return @scale
23
+ end
24
+
25
+ def caption
26
+ return SDL::WM.caption[0]
27
+ end
28
+
29
+ def caption=(val)
30
+ SDL::WM.set_caption(val, '')
16
31
  end
17
32
 
18
33
  def fps=(val)
@@ -32,20 +47,19 @@ module DXRubySDL
32
47
  end
33
48
  end
34
49
 
35
- _screen.fill_rect(0, 0, DEFAULTS[:width], DEFAULTS[:height],
36
- DEFAULTS[:background_color])
50
+ screen.fill_rect(0, 0, width, height, DEFAULTS[:background_color])
37
51
 
38
52
  yield
39
53
 
40
- _screen.update_rect(0, 0, 0, 0)
54
+ screen.update_rect(0, 0, 0, 0)
41
55
 
42
- @last_mouse_state = SDL::Mouse.state
56
+ Input.send(:store_last_state)
43
57
  end
44
58
  end
45
59
  end
46
60
 
47
61
  def draw(x, y, image, z = 0)
48
- _screen.put(image._surface, x, y)
62
+ screen.put(image._surface, x, y)
49
63
  end
50
64
 
51
65
  def draw_font(x, y, string, font, hash = {})
@@ -54,12 +68,35 @@ module DXRubySDL
54
68
  else
55
69
  r, g, b = 255, 255, 255
56
70
  end
57
- font._ttf.draw_blended_utf8(_screen, string, x, y, r, g, b)
71
+ font._ttf.draw_blended_utf8(screen, string, x, y, r, g, b)
72
+ end
73
+
74
+ def open_filename(filter, title)
75
+ if /darwin/ =~ RUBY_PLATFORM
76
+ apple_script = <<-EOS
77
+ on run
78
+ tell application "Finder"
79
+ activate
80
+ set theImage to choose file
81
+ return theImage as Unicode text
82
+ end tell
83
+ end run
84
+ EOS
85
+ s = `osascript -e '#{apple_script}'`
86
+ return s.chomp.sub(/^ "/, '').gsub(/:/, '/')
87
+ else
88
+ raise NotImplementedError, 'Window.open_filename'
89
+ end
58
90
  end
59
91
 
60
92
  # rubocop:disable SymbolName
61
93
  class << self
94
+ attr_writer :width
95
+ attr_writer :height
96
+ attr_writer :scale
97
+
62
98
  alias_method :drawFont, :draw_font
99
+ alias_method :openFilename, :open_filename
63
100
  end
64
101
  # rubocop:enable SymbolName
65
102
 
@@ -71,5 +108,16 @@ module DXRubySDL
71
108
  background_color: [0, 0, 0],
72
109
  }
73
110
  private_constant :DEFAULTS
111
+
112
+ class << self
113
+
114
+ private
115
+
116
+ def screen
117
+ return SDL::Screen.get
118
+ rescue SDL::Error
119
+ return SDL::Screen.open(width, height, 16, SDL::SWSURFACE)
120
+ end
121
+ end
74
122
  end
75
123
  end
data/lib/dxruby_sdl.rb CHANGED
@@ -202,6 +202,15 @@ module DXRubySDL
202
202
  ].each.with_index do |name, i|
203
203
  const_set(name.to_sym, i)
204
204
  end
205
+
206
+ %w[
207
+ WAVE_SIN
208
+ WAVE_SAW
209
+ WAVE_TRI
210
+ WAVE_RECT
211
+ ].each.with_index do |name, i|
212
+ const_set(name.to_sym, i)
213
+ end
205
214
  end
206
215
 
207
216
  require 'dxruby_sdl/window'
@@ -210,5 +219,6 @@ require 'dxruby_sdl/image'
210
219
  require 'dxruby_sdl/font'
211
220
  require 'dxruby_sdl/input'
212
221
  require 'dxruby_sdl/sound'
222
+ require 'dxruby_sdl/sound_effect'
213
223
 
214
224
  SDL.init(SDL::INIT_EVERYTHING)
@@ -122,6 +122,10 @@ describe DXRubySDL::Image, '画像を表すクラス' do
122
122
  subject
123
123
  end
124
124
 
125
+ it '戻り値はレシーバである' do
126
+ expect(subject).to eq(image)
127
+ end
128
+
125
129
  context 'auto_lockを有効にした場合' do
126
130
  before do
127
131
  SDL::Surface.auto_lock_on
@@ -134,6 +138,10 @@ describe DXRubySDL::Image, '画像を表すクラス' do
134
138
  it '呼び出すことができる' do
135
139
  subject
136
140
  end
141
+
142
+ it '戻り値はレシーバである' do
143
+ expect(subject).to eq(image)
144
+ end
137
145
  end
138
146
  end
139
147
 
@@ -3,10 +3,35 @@ require 'spec_helper'
3
3
 
4
4
  describe DXRubySDL::Input,
5
5
  'キーボード・ゲームパッド・マウスの入力を扱うモジュール' do
6
- after do
6
+ before do
7
7
  DXRubySDL::Input.instance_variable_set('@joysticks', [])
8
8
  end
9
9
 
10
+ shared_context '.set_repeat' do
11
+ before do
12
+ SDL::Key.stub(:enable_key_repeat)
13
+ subject
14
+ end
15
+
16
+ specify do
17
+ expect(SDL::Key).to have_received(:enable_key_repeat).with(15, 2)
18
+ end
19
+ end
20
+
21
+ describe '.set_repeat', 'キーリピートを設定する' do
22
+ subject { DXRubySDL::Input.set_repeat(15, 2) }
23
+
24
+ include_context '.set_repeat'
25
+
26
+ describe 'alias' do
27
+ describe '.setRepeat' do
28
+ it_behaves_like '.set_repeat' do
29
+ subject { DXRubySDL::Input.setRepeat(15, 2) }
30
+ end
31
+ end
32
+ end
33
+ end
34
+
10
35
  shared_context 'push_key' do
11
36
  let(:_keys) { [] }
12
37
 
@@ -91,15 +116,19 @@ describe DXRubySDL::Input,
91
116
 
92
117
  [
93
118
  # rubocop:disable SymbolName
94
- [:P_BUTTON0, :Z],
95
- [:P_BUTTON1, :X],
96
- [:P_BUTTON2, :C],
119
+ ['ボタン0', :P_BUTTON0, :Z],
120
+ ['ボタン1', :P_BUTTON1, :X],
121
+ ['ボタン2', :P_BUTTON2, :C],
122
+ ['左ボタン', :P_LEFT, :LEFT],
123
+ ['右ボタン', :P_RIGHT, :RIGHT],
124
+ ['上ボタン', :P_UP, :UP],
125
+ ['下ボタン', :P_DOWN, :DOWN],
97
126
  # rubocop:enable SymbolName
98
- ].each.with_index do |(_button, _key), i|
99
- describe "#{i}番目(#{_button})のボタン" do
127
+ ].each do |_button_name, _button, _key|
128
+ describe "ゲームパッドの#{_button_name}" do
100
129
  let(:button_code) { DXRubySDL.const_get(_button) }
101
130
 
102
- context "#{i}番目のボタンが押されている場合" do
131
+ context "ゲームパッドの#{_button_name}が押されている場合" do
103
132
  let(:joystick) {
104
133
  j = double('Joystick')
105
134
  allow(j).to receive(:button) { |button_index|
@@ -131,12 +160,16 @@ describe DXRubySDL::Input,
131
160
 
132
161
  [
133
162
  # rubocop:disable SymbolName
134
- [:P_BUTTON0, :Z],
135
- [:P_BUTTON1, :X],
136
- [:P_BUTTON2, :C],
163
+ ['ボタン0', :P_BUTTON0, :Z],
164
+ ['ボタン1', :P_BUTTON1, :X],
165
+ ['ボタン2', :P_BUTTON2, :C],
166
+ ['左ボタン', :P_LEFT, :LEFT],
167
+ ['右ボタン', :P_RIGHT, :RIGHT],
168
+ ['上ボタン', :P_UP, :UP],
169
+ ['下ボタン', :P_DOWN, :DOWN],
137
170
  # rubocop:enable SymbolName
138
- ].each.with_index do |(_button, _key), i|
139
- describe "#{i}番目(#{_button})のボタン" do
171
+ ].each do |_button_name, _button, _key|
172
+ describe "ゲームパッドの#{_button_name}" do
140
173
  let(:button_code) { DXRubySDL.const_get(_button) }
141
174
 
142
175
  context "#{_key}キーが押されている場合" do
@@ -167,6 +200,112 @@ describe DXRubySDL::Input,
167
200
  end
168
201
  end
169
202
 
203
+ shared_context '.pad_push?' do
204
+ include_context 'push_key'
205
+
206
+ before do
207
+ DXRubySDL::Input.instance_variable_set('@current_key_state', Set.new)
208
+ DXRubySDL::Input.instance_variable_set('@last_key_state', Set.new)
209
+ end
210
+
211
+ context 'Joystickが接続されている場合' do
212
+ let(:joystick) {
213
+ j = double('Joystick')
214
+ allow(j).to receive(:button).with(anything).and_return(false)
215
+ j
216
+ }
217
+
218
+ before do
219
+ allow(SDL::Joystick).to receive(:num).and_return(1)
220
+ allow(SDL::Joystick).to receive(:open).with(0).and_return(joystick)
221
+ end
222
+
223
+ [
224
+ # rubocop:disable SymbolName
225
+ ['ボタン0', :P_BUTTON0, :Z],
226
+ ['ボタン1', :P_BUTTON1, :X],
227
+ ['ボタン2', :P_BUTTON2, :C],
228
+ ['左ボタン', :P_LEFT, :LEFT],
229
+ ['右ボタン', :P_RIGHT, :RIGHT],
230
+ ['上ボタン', :P_UP, :UP],
231
+ ['下ボタン', :P_DOWN, :DOWN],
232
+ # rubocop:enable SymbolName
233
+ ].each do |_button_name, _button, _key|
234
+ describe "ゲームパッドの#{_button_name}" do
235
+ let(:button_code) { DXRubySDL.const_get(_button) }
236
+
237
+ context "ゲームパッドの#{_button_name}が押されている場合" do
238
+ let(:joystick) {
239
+ j = double('Joystick')
240
+ allow(j).to receive(:button) { |button_index|
241
+ button_index == DXRubySDL.const_get(_button)
242
+ }
243
+ j
244
+ }
245
+
246
+ it { should be_true }
247
+ end
248
+
249
+ context "#{_key}キーが押されている場合" do
250
+ let(:_keys) { SDL::Key.const_get(_key) }
251
+
252
+ it { should be_true }
253
+ end
254
+
255
+ context 'ボタンやキーが押されていない場合' do
256
+ it { should be_false }
257
+ end
258
+ end
259
+ end
260
+ end
261
+
262
+ context 'Joystickが接続されていない場合' do
263
+ before do
264
+ allow(SDL::Joystick).to receive(:num).and_return(0)
265
+ end
266
+
267
+ [
268
+ # rubocop:disable SymbolName
269
+ ['ボタン0', :P_BUTTON0, :Z],
270
+ ['ボタン1', :P_BUTTON1, :X],
271
+ ['ボタン2', :P_BUTTON2, :C],
272
+ ['左ボタン', :P_LEFT, :LEFT],
273
+ ['右ボタン', :P_RIGHT, :RIGHT],
274
+ ['上ボタン', :P_UP, :UP],
275
+ ['下ボタン', :P_DOWN, :DOWN],
276
+ # rubocop:enable SymbolName
277
+ ].each do |_button_name, _button, _key|
278
+ describe "ゲームパッドの#{_button_name}" do
279
+ let(:button_code) { DXRubySDL.const_get(_button) }
280
+
281
+ context "#{_key}キーが押されている場合" do
282
+ let(:_keys) { SDL::Key.const_get(_key) }
283
+
284
+ it { should be_true }
285
+ end
286
+
287
+ context 'ボタンやキーが押されていない場合' do
288
+ it { should be_false }
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ describe '.pad_push?', 'パッドのボタン状態を返す' do
296
+ subject { described_class.pad_push?(button_code) }
297
+
298
+ include_context '.pad_push?'
299
+
300
+ describe 'alias' do
301
+ describe '.padPush?' do
302
+ it_behaves_like '.pad_push?' do
303
+ subject { described_class.padPush?(button_code) }
304
+ end
305
+ end
306
+ end
307
+ end
308
+
170
309
  shared_context '.mouse_pos_x' do
171
310
  it { should be_kind_of(Integer) }
172
311
  end
@@ -203,27 +342,94 @@ describe DXRubySDL::Input,
203
342
  end
204
343
  end
205
344
 
345
+ shared_context '.key_down?' do
346
+ include_context 'push_key'
347
+
348
+ context 'ESCAPEキーが押されている場合' do
349
+ let(:_keys) { SDL::Key::ESCAPE }
350
+ let(:key_code) { DXRubySDL::K_ESCAPE }
351
+
352
+ it { should be_true }
353
+ end
354
+ end
355
+
356
+ describe '.key_down?' do
357
+ subject { described_class.key_down?(key_code) }
358
+
359
+ include_context '.key_down?'
360
+
361
+ describe 'alias' do
362
+ describe '.keyDown?' do
363
+ it_behaves_like '.key_down?' do
364
+ subject { described_class.keyDown?(key_code) }
365
+ end
366
+ end
367
+ end
368
+ end
369
+
206
370
  shared_context '.key_push?' do
207
371
  include_context 'push_key'
208
372
 
373
+ subject { described_class.send(method, key_code) }
374
+
375
+ before do
376
+ DXRubySDL::Input.instance_variable_set('@current_key_state', Set.new)
377
+ DXRubySDL::Input.instance_variable_set('@last_key_state', Set.new)
378
+ end
379
+
209
380
  context 'ESCAPEキーが押されている場合' do
210
381
  let(:_keys) { SDL::Key::ESCAPE }
211
382
  let(:key_code) { DXRubySDL::K_ESCAPE }
212
383
 
213
384
  it { should be_true }
385
+
386
+ context 'キーが押しっぱなしの場合' do
387
+ before do
388
+ last_key_state =
389
+ DXRubySDL::Input.instance_variable_get('@last_key_state')
390
+ last_key_state.add(*_keys)
391
+ end
392
+
393
+ it { should be_false }
394
+ end
395
+
396
+ context 'キーが押しっぱなしだが、' \
397
+ 'Input.key_push?をメインループで毎回処理しない場合' do
398
+ it '最初の1回以外は全てfalseを返す' do
399
+ begin
400
+ first = true
401
+ i = 0
402
+ DXRubySDL::Window.loop do
403
+ if first
404
+ expect(described_class.send(method, key_code)).to be_true
405
+ first = false
406
+ else
407
+ if i.even?
408
+ expect(described_class.send(method, key_code)).to be_false
409
+ end
410
+ end
411
+ i += 1
412
+ if i > 10
413
+ SDL::Event.push(SDL::Event::Quit.new)
414
+ end
415
+ end
416
+ rescue SystemExit
417
+ end
418
+ end
419
+ end
214
420
  end
215
421
  end
216
422
 
217
423
  describe '.key_push?' do
218
- subject { described_class.key_push?(key_code) }
424
+ let(:method) { :key_push? }
219
425
 
220
426
  include_context '.key_push?'
221
427
 
222
428
  describe 'alias' do
223
429
  describe '.keyPush?' do
224
- it_behaves_like '.key_push?' do
225
- subject { described_class.keyPush?(key_code) }
226
- end
430
+ let(:method) { 'keyPush?' }
431
+
432
+ it_behaves_like '.key_push?'
227
433
  end
228
434
  end
229
435
  end
@@ -322,8 +528,8 @@ describe DXRubySDL::Input,
322
528
  shared_context '.mouse_push?' do
323
529
  context '直前にマウスのボタンを押していない場合' do
324
530
  before do
325
- DXRubySDL::Window.instance_variable_set('@last_mouse_state',
326
- [0, 0, false, false, false])
531
+ DXRubySDL::Input.instance_variable_set('@last_mouse_state',
532
+ [0, 0, false, false, false])
327
533
  end
328
534
 
329
535
  context 'マウスの左ボタンを押している場合' do
@@ -404,8 +610,8 @@ describe DXRubySDL::Input,
404
610
 
405
611
  context '直前にマウスのボタンを全て押していた場合' do
406
612
  before do
407
- DXRubySDL::Window.instance_variable_set('@last_mouse_state',
408
- [0, 0, true, true, true])
613
+ DXRubySDL::Input.instance_variable_set('@last_mouse_state',
614
+ [0, 0, true, true, true])
409
615
  end
410
616
 
411
617
  context 'マウスの左ボタンを押している場合' do
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe DXRubySDL::SoundEffect, '効果音を生成するクラス' do
5
+ let(:sound_effect) {
6
+ v = 80
7
+ described_class.new(50, DXRubySDL::WAVE_RECT, 1000) {
8
+ v = v - 4 if v > 0
9
+ [220, v]
10
+ }
11
+ }
12
+
13
+ describe '.new' do
14
+ it '呼び出すことができる' do
15
+ expect { sound_effect }.not_to raise_error
16
+ end
17
+ end
18
+
19
+ describe '#add' do
20
+ it '呼び出すことができる' do
21
+ expect {
22
+ v = 80
23
+ sound_effect.add(DXRubySDL::WAVE_RECT, 1000) {
24
+ v = v - 4 if v > 0
25
+ [440, v]
26
+ }
27
+ }.not_to raise_error
28
+ end
29
+ end
30
+
31
+ describe '#play' do
32
+ it '呼び出すことができる' do
33
+ expect { sound_effect.play }.not_to raise_error
34
+ end
35
+ end
36
+
37
+ describe '#stop' do
38
+ it '呼び出すことができる' do
39
+ expect { sound_effect.stop }.not_to raise_error
40
+ end
41
+ end
42
+ end
@@ -26,6 +26,40 @@ describe DXRubySDL::Window do
26
26
  end
27
27
  end
28
28
 
29
+ describe '.caption', 'ウィンドウのタイトルを取得する' do
30
+ before do
31
+ allow(SDL::WM)
32
+ .to receive(:caption).and_return(['window title', 'icon name'])
33
+ end
34
+
35
+ subject { DXRubySDL::Window.caption }
36
+
37
+ it { should eq('window title') }
38
+
39
+ describe SDL::WM do
40
+ before do
41
+ DXRubySDL::Window.caption
42
+ end
43
+
44
+ subject { SDL::WM }
45
+
46
+ it { should have_received(:caption).once }
47
+ end
48
+ end
49
+
50
+ describe '.caption=', 'ウィンドウのタイトルを設定する' do
51
+ before do
52
+ allow(SDL::WM).to receive(:set_caption)
53
+ DXRubySDL::Window.caption = 'window title'
54
+ end
55
+
56
+ describe SDL::WM do
57
+ subject { SDL::WM }
58
+
59
+ it { should have_received(:set_caption).with('window title', '').once }
60
+ end
61
+ end
62
+
29
63
  describe '.fps=', 'FPSを設定する' do
30
64
  context '15に設定した場合' do
31
65
  let(:fps) { 15 }
@@ -213,4 +213,13 @@ describe DXRubySDL do
213
213
  ].each.with_index do |name, i|
214
214
  include_examples 'constant', name, i
215
215
  end
216
+
217
+ %w[
218
+ WAVE_SIN
219
+ WAVE_SAW
220
+ WAVE_TRI
221
+ WAVE_RECT
222
+ ].each.with_index do |name, i|
223
+ include_examples 'constant', name, i
224
+ end
216
225
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dxruby_sdl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-09 00:00:00.000000000 Z
11
+ date: 2013-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,34 @@ dependencies:
94
94
  - - ! '>='
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: rubysdl
99
127
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +164,7 @@ files:
136
164
  - .ruby-version
137
165
  - .travis.yml
138
166
  - Gemfile
167
+ - Guardfile
139
168
  - LEGAL
140
169
  - LICENSE
141
170
  - README.md
@@ -148,6 +177,7 @@ files:
148
177
  - lib/dxruby_sdl/image.rb
149
178
  - lib/dxruby_sdl/input.rb
150
179
  - lib/dxruby_sdl/sound.rb
180
+ - lib/dxruby_sdl/sound_effect.rb
151
181
  - lib/dxruby_sdl/version.rb
152
182
  - lib/dxruby_sdl/window.rb
153
183
  - lib/dxruby_sdl/window/fpstimer.rb
@@ -171,6 +201,7 @@ files:
171
201
  - spec/dxruby_sdl/font_spec.rb
172
202
  - spec/dxruby_sdl/image_spec.rb
173
203
  - spec/dxruby_sdl/input_spec.rb
204
+ - spec/dxruby_sdl/sound_effect_spec.rb
174
205
  - spec/dxruby_sdl/sound_spec.rb
175
206
  - spec/dxruby_sdl/window__draw_spec.rb
176
207
  - spec/dxruby_sdl/window_spec.rb
@@ -211,6 +242,7 @@ test_files:
211
242
  - spec/dxruby_sdl/font_spec.rb
212
243
  - spec/dxruby_sdl/image_spec.rb
213
244
  - spec/dxruby_sdl/input_spec.rb
245
+ - spec/dxruby_sdl/sound_effect_spec.rb
214
246
  - spec/dxruby_sdl/sound_spec.rb
215
247
  - spec/dxruby_sdl/window__draw_spec.rb
216
248
  - spec/dxruby_sdl/window_spec.rb