dxruby_sdl 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+ # http://dxruby.sourceforge.jp/DXRubyReference/200953123211781.htm
3
+ require 'dxruby'
4
+
5
+ image = Image.load('data.png') # data.pngを読み込む
6
+ x = 0
7
+ y = 0
8
+ dx = 0
9
+ dy = 0
10
+
11
+ Window.loop do
12
+ dx = Input.x # x座標の移動量
13
+ dy = Input.y # y座標の移動量
14
+
15
+ if Input.padDown?(P_BUTTON0) # Zキーかパッドのボタン0を押しているか判定
16
+ dx = dx * 2
17
+ dy = dy * 2
18
+ end
19
+
20
+ x = x + dx
21
+ y = y + dy
22
+
23
+ Window.draw(x, y, image) # data.pngを描画する
24
+ end
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+ # http://dxruby.sourceforge.jp/DXRubyReference/2009531233720546.htm
3
+ require 'dxruby'
4
+
5
+ image = Image.load('data.png') # data.pngを読み込む
6
+
7
+ Window.loop do
8
+ x = Input.mousePosX # マウスカーソルのx座標
9
+ y = Input.mousePosY # マウスカーソルのy座標
10
+
11
+ Window.draw(x, y, image) # data.pngを描画する
12
+ end
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+ # http://dxruby.sourceforge.jp/DXRubyReference/2009610612281.htm
3
+ require 'dxruby'
4
+
5
+ image = Image.load('data.png') # data.pngを読み込む
6
+
7
+ Window.loop do
8
+ Window.draw(100, 100, image) # data.pngを描画する
9
+ if Input.keyPush?(K_ESCAPE) # Escキーで終了
10
+ break
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+ # http://dxruby.sourceforge.jp/DXRubyReference/2009610505893.htm
3
+ require 'dxruby'
4
+
5
+ font = Font.new(32) # 第2引数を省略するとMS Pゴシックになります
6
+
7
+ Window.loop do
8
+ Window.drawFont(100, 100, 'ふぉんと', font) # "ふぉんと"を描画する
9
+ end
@@ -0,0 +1,14 @@
1
+ # -*- coding: utf-8 -*-
2
+ # http://dxruby.sourceforge.jp/DXRubyReference/2009610590500.htm
3
+ require 'dxruby'
4
+
5
+ sound = Sound.new('sound.wav') # sound.wav読み込み
6
+ bgm = Sound.new('bgm.mid') # bgm.mid読み込み
7
+
8
+ bgm.play
9
+
10
+ Window.loop do
11
+ if Input.keyPush?(K_Z) # Zキーで再生
12
+ sound.play
13
+ end
14
+ end
@@ -34,21 +34,124 @@ describe DXRubySDL::Image, '画像を表すクラス' do
34
34
  end
35
35
  end
36
36
 
37
- describe '#line' do
38
- it '呼び出すことができる' do
39
- image.line(0, 0, 100, 100, [255, 255, 255])
37
+ shared_context '.load_tiles' do
38
+ context '画像の幅と高さが割り切れる(518x232、xcountが2、ycountが4)場合' do
39
+ let(:xcount) { 2 }
40
+ let(:ycount) { 4 }
41
+
42
+ its(:length) { should eq(xcount * ycount) }
43
+
44
+ it '各オブジェクトの幅はxcount(2)等分したものである' do
45
+ subject.each do |image|
46
+ expect(image.width).to eq(518 / xcount)
47
+ end
48
+ end
49
+
50
+ it '各オブジェクトの高さはycount(4)等分したものである' do
51
+ subject.each do |image|
52
+ expect(image.height).to eq(232 / ycount)
53
+ end
54
+ end
40
55
  end
41
56
  end
42
57
 
43
- describe '#circle' do
44
- it '呼び出すことができる' do
45
- image.circle(50, 50, 25, [255, 255, 255])
58
+ describe '.load_tiles',
59
+ '画像を読み込み、横・縦がそれぞれxcount個、' \
60
+ 'ycount個であると仮定して自動で分割し、' \
61
+ '左上から右に向かう順序でImageオブジェクトの配列を生成して返す' do
62
+ subject {
63
+ DXRubySDL::Image.load_tiles(fixture_path('logo.png'), xcount, ycount)
64
+ }
65
+
66
+ include_context '.load_tiles'
67
+
68
+ describe 'alias' do
69
+ describe '.loadTiles' do
70
+ it_behaves_like '.load_tiles' do
71
+ subject {
72
+ DXRubySDL::Image.loadTiles(fixture_path('logo.png'),
73
+ xcount, ycount)
74
+ }
75
+ end
76
+ end
77
+
78
+ describe '.load_to_array' do
79
+ it_behaves_like '.load_tiles' do
80
+ subject {
81
+ DXRubySDL::Image.load_to_array(fixture_path('logo.png'),
82
+ xcount, ycount)
83
+ }
84
+ end
85
+ end
86
+
87
+ describe '.loadToArray' do
88
+ it_behaves_like '.load_tiles' do
89
+ subject {
90
+ DXRubySDL::Image.loadToArray(fixture_path('logo.png'),
91
+ xcount, ycount)
92
+ }
93
+ end
94
+ end
46
95
  end
47
96
  end
48
97
 
49
- describe '#box' do
98
+ describe '#slice' do
99
+ let(:image) { DXRubySDL::Image.load(fixture_path('logo.png')) }
100
+
101
+ subject { image.slice(100, 100, 20, 40) }
102
+
103
+ before do
104
+ SDL::Surface.auto_lock_on
105
+ end
106
+
107
+ after do
108
+ SDL::Surface.auto_lock_off
109
+ end
110
+
111
+ its(:width) { should eq(20) }
112
+ its(:height) { should eq(40) }
113
+ it { should be_instance_of(DXRubySDL::Image) }
114
+ it '各ピクセルデータが正しい' do
115
+ expect(subject._surface.pixels)
116
+ .to eq(image._surface.copy_rect(100, 100, 20, 40).pixels)
117
+ end
118
+ end
119
+
120
+ shared_context 'draw methods' do
50
121
  it '呼び出すことができる' do
51
- image.box(0, 0, 100, 100, [255, 255, 255])
122
+ subject
123
+ end
124
+
125
+ context 'auto_lockを有効にした場合' do
126
+ before do
127
+ SDL::Surface.auto_lock_on
128
+ end
129
+
130
+ after do
131
+ SDL::Surface.auto_lock_off
132
+ end
133
+
134
+ it '呼び出すことができる' do
135
+ subject
136
+ end
52
137
  end
53
138
  end
139
+
140
+ describe '#line' do
141
+ subject { image.line(0, 0, 100, 100, [255, 255, 255]) }
142
+
143
+ include_context 'draw methods'
144
+ end
145
+
146
+ describe '#circle' do
147
+ subject { image.circle(50, 50, 25, [255, 255, 255]) }
148
+
149
+ include_context 'draw methods'
150
+ end
151
+
152
+ describe '#box' do
153
+ subject { image.box(0, 0, 100, 100, [255, 255, 255]) }
154
+
155
+ include_context 'draw methods'
156
+ end
54
157
  end
@@ -0,0 +1,230 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe DXRubySDL::Input,
5
+ 'キーボード・ゲームパッド・マウスの入力を扱うモジュール' do
6
+ after do
7
+ DXRubySDL::Input.instance_variable_set('@joysticks', [])
8
+ end
9
+
10
+ shared_context 'push_key' do
11
+ let(:_keys) { [] }
12
+
13
+ before do
14
+ allow(SDL::Key).to receive(:scan)
15
+ allow(SDL::Key).to receive(:press?) { |key|
16
+ [_keys].flatten.include?(key) ? true : false
17
+ }
18
+ end
19
+ end
20
+
21
+ describe '.x' do
22
+ include_context 'push_key'
23
+
24
+ subject { described_class.x }
25
+
26
+ context '左カーソルが押されている場合' do
27
+ let(:_keys) { SDL::Key::LEFT }
28
+
29
+ it { should eq(-1) }
30
+ end
31
+
32
+ context '右カーソルが押されている場合' do
33
+ let(:_keys) { SDL::Key::RIGHT }
34
+
35
+ it { should eq(1) }
36
+ end
37
+
38
+ context '左・右カーソルが両方とも押されている場合' do
39
+ let(:_keys) { [SDL::Key::LEFT, SDL::Key::RIGHT] }
40
+
41
+ it { should eq(0) }
42
+ end
43
+
44
+ context '左・右カーソルのどちらも押されていない場合' do
45
+ it { should eq(0) }
46
+ end
47
+ end
48
+
49
+ describe '.y' do
50
+ include_context 'push_key'
51
+
52
+ subject { described_class.y }
53
+
54
+ context '上カーソルが押されている場合' do
55
+ let(:_keys) { SDL::Key::UP }
56
+
57
+ it { should eq(-1) }
58
+ end
59
+
60
+ context '下カーソルが押されている場合' do
61
+ let(:_keys) { SDL::Key::DOWN }
62
+
63
+ it { should eq(1) }
64
+ end
65
+
66
+ context '上・下カーソルが両方とも押されている場合' do
67
+ let(:_keys) { [SDL::Key::UP, SDL::Key::DOWN] }
68
+
69
+ it { should eq(0) }
70
+ end
71
+
72
+ context '上・下カーソルのどちらも押されていない場合' do
73
+ it { should eq(0) }
74
+ end
75
+ end
76
+
77
+ shared_context '.pad_down?' do
78
+ include_context 'push_key'
79
+
80
+ context 'Joystickが接続されている場合' do
81
+ let(:joystick) {
82
+ j = double('Joystick')
83
+ allow(j).to receive(:button).with(anything).and_return(false)
84
+ j
85
+ }
86
+
87
+ before do
88
+ allow(SDL::Joystick).to receive(:num).and_return(1)
89
+ allow(SDL::Joystick).to receive(:open).with(0).and_return(joystick)
90
+ end
91
+
92
+ [
93
+ # rubocop:disable SymbolName
94
+ [:P_BUTTON0, :Z],
95
+ [:P_BUTTON1, :X],
96
+ [:P_BUTTON2, :C],
97
+ # rubocop:enable SymbolName
98
+ ].each.with_index do |(_button, _key), i|
99
+ describe "#{i}番目(#{_button})のボタン" do
100
+ let(:button_code) { DXRubySDL.const_get(_button) }
101
+
102
+ context "#{i}番目のボタンが押されている場合" do
103
+ let(:joystick) {
104
+ j = double('Joystick')
105
+ allow(j).to receive(:button) { |button_index|
106
+ button_index == DXRubySDL.const_get(_button)
107
+ }
108
+ j
109
+ }
110
+
111
+ it { should be_true }
112
+ end
113
+
114
+ context "#{_key}キーが押されている場合" do
115
+ let(:_keys) { SDL::Key.const_get(_key) }
116
+
117
+ it { should be_true }
118
+ end
119
+
120
+ context 'ボタンやキーが押されていない場合' do
121
+ it { should be_false }
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ context 'Joystickが接続されていない場合' do
128
+ before do
129
+ allow(SDL::Joystick).to receive(:num).and_return(0)
130
+ end
131
+
132
+ [
133
+ # rubocop:disable SymbolName
134
+ [:P_BUTTON0, :Z],
135
+ [:P_BUTTON1, :X],
136
+ [:P_BUTTON2, :C],
137
+ # rubocop:enable SymbolName
138
+ ].each.with_index do |(_button, _key), i|
139
+ describe "#{i}番目(#{_button})のボタン" do
140
+ let(:button_code) { DXRubySDL.const_get(_button) }
141
+
142
+ context "#{_key}キーが押されている場合" do
143
+ let(:_keys) { SDL::Key.const_get(_key) }
144
+
145
+ it { should be_true }
146
+ end
147
+
148
+ context 'ボタンやキーが押されていない場合' do
149
+ it { should be_false }
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ describe '.pad_down?', 'パッドのボタン状態を返す' do
157
+ subject { described_class.pad_down?(button_code) }
158
+
159
+ include_context '.pad_down?'
160
+
161
+ describe 'alias' do
162
+ describe '.padDown?' do
163
+ it_behaves_like '.pad_down?' do
164
+ subject { described_class.padDown?(button_code) }
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ shared_context '.mouse_pos_x' do
171
+ it { should be_kind_of(Integer) }
172
+ end
173
+
174
+ describe '.mouse_pos_x' do
175
+ subject { described_class.mouse_pos_x }
176
+
177
+ include_context '.mouse_pos_x'
178
+
179
+ describe 'alias' do
180
+ describe '.mousePosX' do
181
+ it_behaves_like '.mouse_pos_x' do
182
+ subject { described_class.mousePosX }
183
+ end
184
+ end
185
+ end
186
+ end
187
+
188
+ shared_context '.mouse_pos_y' do
189
+ it { should be_kind_of(Integer) }
190
+ end
191
+
192
+ describe '.mouse_pos_y' do
193
+ subject { described_class.mouse_pos_y }
194
+
195
+ include_context '.mouse_pos_y'
196
+
197
+ describe 'alias' do
198
+ describe '.mousePosY' do
199
+ it_behaves_like '.mouse_pos_y' do
200
+ subject { described_class.mousePosY }
201
+ end
202
+ end
203
+ end
204
+ end
205
+
206
+ shared_context '.key_push?' do
207
+ include_context 'push_key'
208
+
209
+ context 'ESCAPEキーが押されている場合' do
210
+ let(:_keys) { SDL::Key::ESCAPE }
211
+ let(:key_code) { DXRubySDL::K_ESCAPE }
212
+
213
+ it { should be_true }
214
+ end
215
+ end
216
+
217
+ describe '.key_push?' do
218
+ subject { described_class.key_push?(key_code) }
219
+
220
+ include_context '.key_push?'
221
+
222
+ describe 'alias' do
223
+ describe '.keyPush?' do
224
+ it_behaves_like '.key_push?' do
225
+ subject { described_class.keyPush?(key_code) }
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
@@ -0,0 +1,79 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe DXRubySDL::Sound, '音を表すクラス' do
5
+ describe '.new' do
6
+ shared_context '.new' do
7
+ subject { DXRubySDL::Sound.new(fixture_path(filename)) }
8
+
9
+ it '呼び出すことができる' do
10
+ subject
11
+ end
12
+ end
13
+
14
+ context 'WAVE形式のファイルの場合' do
15
+ let(:filename) { 'sound.wav' }
16
+
17
+ include_context '.new'
18
+ end
19
+
20
+ context 'MIDI形式のファイルの場合' do
21
+ let(:filename) { 'bgm.mid' }
22
+
23
+ include_context '.new'
24
+ end
25
+ end
26
+
27
+ describe '#play' do
28
+ context 'WAVE形式のファイルの場合' do
29
+ let(:path) { fixture_path('sound.wav') }
30
+ let(:sound) { DXRubySDL::Sound.new(path) }
31
+
32
+ subject { sound.play }
33
+
34
+ it 'SDL::Mixer.play_channelを呼び出す' do
35
+ wave =
36
+ sound.instance_variable_get('@sound').instance_variable_get('@wave')
37
+ expect(SDL::Mixer).to receive(:play_channel).with(-1, wave, 0)
38
+ subject
39
+ end
40
+
41
+ context '3回連続で呼び出した場合' do
42
+ before do
43
+ wave = sound.instance_variable_get('@sound')
44
+ .instance_variable_get('@wave')
45
+ count = 0
46
+ expect(SDL::Mixer)
47
+ .to receive(:play_channel).with(-1, wave, 0).exactly(4).times {
48
+ count += 1
49
+ if count == 3
50
+ count = 0
51
+ raise SDL::Error.new('couldn\'t play wave:' \
52
+ ' No free channels available')
53
+ end
54
+ (count - 1) % 2
55
+ }
56
+ expect(SDL::Mixer).to receive(:halt).with(0)
57
+ end
58
+
59
+ it '最初のものを停止する' do
60
+ 3.times { sound.play }
61
+ end
62
+ end
63
+ end
64
+
65
+ context 'MIDI形式のファイルの場合' do
66
+ let(:path) { fixture_path('bgm.mid') }
67
+ let(:sound) { DXRubySDL::Sound.new(path) }
68
+
69
+ subject { sound.play }
70
+
71
+ it 'SDL::Mixer.play_musicを呼び出す' do
72
+ music =
73
+ sound.instance_variable_get('@sound').instance_variable_get('@music')
74
+ expect(SDL::Mixer).to receive(:play_music).with(music, -1)
75
+ subject
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,85 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe DXRubySDL::Window do
5
+ describe '.draw', 'Imageオブジェクトを描画する' do
6
+ subject do
7
+ expect {
8
+ DXRubySDL::Window.loop do
9
+ DXRubySDL::Window.draw(0, 0, image)
10
+ SDL::Event.push(SDL::Event::Quit.new)
11
+ end
12
+ }.to raise_error(SystemExit)
13
+ end
14
+
15
+ context '(0, 0)-(100,100)の白い線を描いたImageオブジェクトを指定した場合' do
16
+ let!(:image) {
17
+ i = DXRubySDL::Image.new(640, 480)
18
+ i.line(0, 0, 100, 100, [255, 255, 255])
19
+ i
20
+ }
21
+
22
+ it '白い線を描画する' do
23
+ subject
24
+ end
25
+ end
26
+
27
+ context '(0, 0)-(100,100)の矩形を描いたImageオブジェクトを指定した場合' do
28
+ let!(:image) {
29
+ i = DXRubySDL::Image.new(640, 480)
30
+ i.box(0, 0, 100, 100, [255, 255, 255])
31
+ i
32
+ }
33
+
34
+ it '矩形を描画する' do
35
+ subject
36
+ end
37
+ end
38
+
39
+ context '(50, 50)、半径25の円を描いたImageオブジェクトを指定した場合' do
40
+ let!(:image) {
41
+ i = DXRubySDL::Image.new(640, 480)
42
+ i.circle(50, 50, 25, [255, 255, 255])
43
+ i
44
+ }
45
+
46
+ it '円を描画する' do
47
+ subject
48
+ end
49
+ end
50
+
51
+ context '画像を読み込んだImageオブジェクトを指定した場合' do
52
+ let!(:image) {
53
+ DXRubySDL::Image.load(fixture_path('logo.png'))
54
+ }
55
+
56
+ it '画像を描画する' do
57
+ subject
58
+ end
59
+ end
60
+
61
+ context '画像を分割して読み込んだImageオブジェクトを指定した場合' do
62
+ let!(:images) {
63
+ DXRubySDL::Image.load_tiles(fixture_path('logo.png'), 2, 4)
64
+ }
65
+
66
+ specify '画像を描画する' do
67
+ expect {
68
+ DXRubySDL::Window.loop do
69
+ margin = 64
70
+ i = 0
71
+ 4.times do |y|
72
+ 2.times do |x|
73
+ DXRubySDL::Window.draw(x * (images[i].width + margin),
74
+ y * (images[i].height + margin),
75
+ images[i])
76
+ i += 1
77
+ end
78
+ end
79
+ SDL::Event.push(SDL::Event::Quit.new)
80
+ end
81
+ }.to raise_error(SystemExit)
82
+ end
83
+ end
84
+ end
85
+ end