dxruby_sdl 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba5e8082520e8fb4624e1e4c52aa7ff387109d01
4
- data.tar.gz: 49da563ff5ecb264a44b98929b9f4e93ab8d8a94
3
+ metadata.gz: b492f2b06c03cae99fd83c1f0a97d6d39013c2d3
4
+ data.tar.gz: 2d1b0b218869b7f4f7d3f97d34111c3989b89e1a
5
5
  SHA512:
6
- metadata.gz: a2ccf2367ad0261120a36f7c3e8760a44fe7752775cb3e567ea2e275de25a1592990e97dfd35bf2035ef06a18cd4b6eed7380d8890385f0f14fc45be27b9cd27
7
- data.tar.gz: 529148f8c073c3586e63d01f00a0952145e36af648156cef8112f14d8dda229ae7e13a9a69a59a19840af48a0c35916d953bf86e96632a859ca3b05a0fba2131
6
+ metadata.gz: e9ef344818ea84dae3b66ac4cb8bd378769543bd90b3ae3fef6274737b9fbd49d76932e7bb7ac27f799f6378618e9e591215e6f9561684d717560f8e843a1ede
7
+ data.tar.gz: 47844fd4b4e7f397d3114f4cb20c488894f50197611e7120141c0977afe2c20d04347444f40dd2c14f63894f43d8c4fb80bc88dc9970509c3bffe20b53990cb2
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.3.3
1
+ 2.3.5
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 2.3.3
4
+ - 2.3.5
5
5
 
6
6
  env:
7
7
  AUDIODEV=null
data/dxruby_sdl.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['kouji.takao@gmail.com']
11
11
  spec.description = %q{`dxruby-sdl` is a ruby library for 2D graphics and game. It has same DXRuby API. It use SDL/Ruby.}
12
12
  spec.summary = %q{2D graphics and game library}
13
- spec.homepage = 'https://github.com/takaokouji/dxruby-sdl'
13
+ spec.homepage = 'https://github.com/smalruby/dxruby-sdl'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -6,17 +6,37 @@ module DXRubySDL
6
6
 
7
7
  def to_sdl_color(color)
8
8
  if color.length == 4
9
- return color[0..2]
9
+ return color[1..3]
10
10
  else
11
- return color
11
+ color
12
12
  end
13
13
  end
14
14
 
15
15
  def to_sdl_alpha(color)
16
16
  if color.length == 4
17
- return color[3]
17
+ color[0]
18
18
  else
19
- return nil
19
+ 0xFF
20
+ end
21
+ end
22
+
23
+ def to_sdl_rgba(color)
24
+ [*to_sdl_color(color), to_sdl_alpha(color)]
25
+ end
26
+
27
+ def to_dxruby_argb(color)
28
+ if color.length == 4
29
+ [color[3], *color[0..2]]
30
+ else
31
+ [0xFF, *color]
32
+ end
33
+ end
34
+
35
+ def normalize_dxruby(color)
36
+ if color.length == 4
37
+ color
38
+ else
39
+ [0xFF] + color
20
40
  end
21
41
  end
22
42
  end
@@ -43,6 +43,38 @@ module DXRubySDL
43
43
  if /darwin/ =~ RUBY_PLATFORM
44
44
  font_info =
45
45
  [
46
+ %w[
47
+ 梅Pゴシック
48
+ /Library/Fonts/ume-pgo4.ttf
49
+ ume-pgo4
50
+ MS\ Pゴシック
51
+ MS\ Pゴシック
52
+ MS\ PGothic
53
+ ],
54
+ %w[
55
+ 梅ゴシック
56
+ /Library/Fonts/ume-tgo4.ttf
57
+ ume-tgo4
58
+ MS\ ゴシック
59
+ MS\ ゴシック
60
+ MS\ Gothic
61
+ ],
62
+ %w[
63
+ 梅P明朝
64
+ /Library/Fonts/ume-pmo3.ttf
65
+ ume-pmo3
66
+ MS\ P明朝
67
+ MS\ P明朝
68
+ MS\ PMincho
69
+ ],
70
+ %w[
71
+ 梅明朝
72
+ /Library/Fonts/ume-tmo3.ttf
73
+ ume-tmo3
74
+ MS\ 明朝
75
+ MS\ 明朝
76
+ MS\ Mincho
77
+ ],
46
78
  %w[
47
79
  MS\ Pゴシック
48
80
  /Library/Fonts/Microsoft/MS\ PGothic.ttf
@@ -30,7 +30,7 @@ module DXRubySDL
30
30
  end
31
31
 
32
32
  def initialize(width, height, color = [0, 0, 0, 0])
33
- @color = color
33
+ @color = to_sdl_rgba(color)
34
34
 
35
35
  if width == 0 && height == 0
36
36
  return
@@ -39,12 +39,18 @@ module DXRubySDL
39
39
  @_surface =
40
40
  SDL::Surface.new(SDL::SWSURFACE, width, height, Window.send(:screen))
41
41
  @_surface.fill_rect(0, 0, width, height, @color)
42
+ end
42
43
 
43
- # TODO: a値が0の時にしか対応していない。
44
- # 1から254の値に対応すること
45
- if color[3] == 0
46
- set_color_key(@color[0..2])
47
- end
44
+ def [](x, y)
45
+ pixel = lock { @_surface.get_pixel(x, y) }
46
+ Color.to_dxruby_argb(@_surface.format.get_rgba(pixel))
47
+ end
48
+
49
+ def []=(x, y, color)
50
+ sdl_rgba = Color.to_sdl_rgba(color)
51
+ lock {
52
+ @_surface[x, y] = sdl_rgba
53
+ }
48
54
  end
49
55
 
50
56
  def width
@@ -56,12 +62,11 @@ module DXRubySDL
56
62
  end
57
63
 
58
64
  def set_color_key(color)
59
- @_surface.set_color_key(SDL::SRCCOLORKEY | SDL::RLEACCEL, color)
65
+ @_surface.set_color_key(SDL::SRCCOLORKEY | SDL::RLEACCEL, to_sdl_color(color))
60
66
  end
61
67
 
62
68
  def compare(x, y, color)
63
- pixel = lock { @_surface.get_pixel(x, y) }
64
- return @_surface.format.get_rgb(pixel) == color
69
+ self[x, y] == Color.normalize_dxruby(color)
65
70
  end
66
71
 
67
72
  def slice(x, y, width, height)
@@ -127,7 +132,7 @@ module DXRubySDL
127
132
  if string.empty?
128
133
  return
129
134
  end
130
- r, g, b = *color
135
+ r, g, b = *to_sdl_color(color)
131
136
  h = font._ttf.height + 1
132
137
  string.lines.each.with_index do |line, i|
133
138
  line.chomp!
@@ -6,20 +6,62 @@ module DXRubySDL
6
6
  class RenderTarget
7
7
  extend Forwardable
8
8
 
9
- attr_reader :_surface
10
-
11
9
  def initialize(width, height, color)
12
- @image = Image.new(width, height, color)
13
- @_surface = @image._surface
10
+ @_image = Image.new(width, height, color)
11
+ @disposed = false
12
+ end
13
+
14
+ def dispose
15
+ @disposed = true
16
+ @_image = nil
17
+ end
18
+
19
+ def disposed?
20
+ @disposed
21
+ end
22
+
23
+ def draw(*args)
24
+ check_disposed
25
+ @_image.draw(*args)
26
+ end
27
+
28
+ def draw_ex(x, y, image, hash = {})
29
+ check_disposed
30
+ if hash[:z] && hash[:z] != 0
31
+ raise NotImplementedError, 'Window.draw_ex(x, y, image, z: != 0)'
32
+ end
33
+ option = {
34
+ angle: 0,
35
+ scale_x: 1,
36
+ scale_y: 1,
37
+ center_x: 0,
38
+ center_y: 0,
39
+ }.merge(hash)
40
+ SDL::Surface.transform_blit(image._surface, @_image._surface,
41
+ option[:angle],
42
+ option[:scale_x], option[:scale_y],
43
+ option[:center_x], option[:center_y],
44
+ x + option[:center_x], y + option[:center_y],
45
+ 0)
14
46
  end
15
47
 
16
- def_delegators :@image, :draw
17
-
18
48
  def draw_font(x, y, string, font, option = {})
49
+ check_disposed
19
50
  color = option[:color] || [255, 255, 255]
20
- @image.draw_font(x, y, string, font, color)
51
+ @_image.draw_font(x, y, string, font, color)
52
+ end
53
+
54
+ def to_image
55
+ check_disposed
56
+ @_image
21
57
  end
22
58
 
23
- alias_method :drawFont, :draw_font
59
+ private
60
+
61
+ def check_disposed
62
+ if disposed?
63
+ raise
64
+ end
65
+ end
24
66
  end
25
67
  end
@@ -32,8 +32,9 @@ module DXRubySDL
32
32
  end
33
33
  end
34
34
 
35
- def_delegators :@sound, :play, :set_volume, :stop
35
+ def_delegators :@sound, :play, :loop_count=, :set_volume, :stop
36
36
 
37
+ alias_method :loopCount=, :loop_count=
37
38
  alias_method :setVolume, :set_volume
38
39
 
39
40
  private
@@ -43,17 +44,22 @@ module DXRubySDL
43
44
 
44
45
  def initialize(filename)
45
46
  @music = SDL::Mixer::Music.load(filename)
47
+ @_loop_count = -1
46
48
  end
47
49
 
48
50
  def play
49
- SDL::Mixer.play_music(@music, -1)
51
+ SDL::Mixer.play_music(@music, @_loop_count)
52
+ end
53
+
54
+ def loop_count=(n)
55
+ @_loop_count = n
50
56
  end
51
57
 
52
58
  def set_volume(volume, time = 0)
53
59
  if time > 0
54
60
  raise NotImplementedError, 'Sound#set_volume(volume, time != 0)'
55
61
  end
56
- @music.set_volume_music(dxruby_volume_to_sdl_volume(volume))
62
+ SDL::Mixer.set_volume_music(dxruby_volume_to_sdl_volume(volume))
57
63
  end
58
64
 
59
65
  def stop
@@ -69,10 +75,11 @@ module DXRubySDL
69
75
  def initialize(filename)
70
76
  @wave = SDL::Mixer::Wave.load(filename)
71
77
  @last_played_channel = nil
78
+ @_loop_count = 0
72
79
  end
73
80
 
74
81
  def play
75
- @last_played_channel = SDL::Mixer.play_channel(-1, @wave, 0)
82
+ @last_played_channel = SDL::Mixer.play_channel(-1, @wave, @_loop_count)
76
83
  rescue SDL::Error => e
77
84
  if /No free channels available/ =~ e.message
78
85
  SDL::Mixer.halt(@last_played_channel == 0 ? 1 : 0)
@@ -80,6 +87,10 @@ module DXRubySDL
80
87
  end
81
88
  end
82
89
 
90
+ def loop_count=(n)
91
+ @_loop_count = n
92
+ end
93
+
83
94
  def set_volume(volume, time = 0)
84
95
  if time > 0
85
96
  raise NotImplementedError, 'Sound#set_volume(volume, time != 0)'
@@ -88,7 +99,7 @@ module DXRubySDL
88
99
  end
89
100
 
90
101
  def stop
91
- SDL::Mixer.halt(@last_played_channel)
102
+ SDL::Mixer.halt(@last_played_channel) if @last_played_channel
92
103
  end
93
104
  end
94
105
  private_constant :Wave
@@ -118,7 +118,7 @@ module DXRubySDL
118
118
  if !@visible || vanished?
119
119
  return
120
120
  end
121
- [:target, :blend, :shader].each do |method|
121
+ [:blend, :shader].each do |method|
122
122
  if send(method)
123
123
  raise NotImplementedError, "Sprite#draw with #{method}"
124
124
  end
@@ -139,7 +139,11 @@ module DXRubySDL
139
139
  if center_y
140
140
  options[:center_y] = center_y
141
141
  end
142
- Window.draw_ex(x, y, image, options)
142
+ if target
143
+ target.draw_ex(x, y, image, options)
144
+ else
145
+ Window.draw_ex(x, y, image, options)
146
+ end
143
147
  end
144
148
 
145
149
  def ===(other)
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DXRubySDL
4
- VERSION = '0.0.15'
4
+ VERSION = '0.0.16'
5
5
  end
@@ -7,15 +7,15 @@ describe DXRubySDL::Color, 'カラーを変換するモジュール' do
7
7
 
8
8
  expected = [0, 125, 255]
9
9
  context "引数が3つの要素の配列(#{expected.inspect})の場合" do
10
- let(:color) { [0, 125, 255] }
10
+ let(:color) { expected }
11
11
 
12
- it { should be(color) }
12
+ it { is_expected.to eq(expected) }
13
13
  end
14
14
 
15
- context "引数が4つの要素の配列で最初の3つが(#{expected.inspect})の場合" do
16
- let(:color) { expected + [0] }
15
+ context "引数が4つの要素の配列で後ろから3つが(#{expected.inspect})の場合" do
16
+ let(:color) { [0] + expected }
17
17
 
18
- it { should eq(expected) }
18
+ it { is_expected.to eq(expected) }
19
19
  end
20
20
  end
21
21
 
@@ -25,17 +25,63 @@ describe DXRubySDL::Color, 'カラーを変換するモジュール' do
25
25
  context '引数が3つの要素の配列の場合' do
26
26
  let(:color) { [0, 125, 255] }
27
27
 
28
- it '常にnilを返す' do
29
- should eq(nil)
30
- end
28
+ it { is_expected.to eq(0xFF) }
31
29
  end
32
30
 
33
31
  [0, 125, 255].each do |expected|
34
- context "引数が4つの要素の配列で最後の値が#{expected}の場合" do
35
- let(:color) { [0, 125, 255, expected] }
32
+ context "引数が4つの要素の配列で最初の値が#{expected}の場合" do
33
+ let(:color) { [expected, 0, 125, 255] }
36
34
 
37
- it { should eq(expected) }
35
+ it { is_expected.to eq(expected) }
38
36
  end
39
37
  end
40
38
  end
39
+
40
+ describe '#to_sdl_rgba', 'DXRubyのカラー + アルファ値の情報をSDLのカラー + アルファ値の情報に変換する' do
41
+ subject { described_class.to_sdl_rgba(color) }
42
+
43
+ context "引数が3つの要素の配列([0, 125, 255])の場合" do
44
+ let(:color) { [0, 125, 255] }
45
+
46
+ it { is_expected.to eq(color + [255]) }
47
+ end
48
+
49
+ context "引数が4つの要素の配列で後ろから3つが([0, 125, 255])の場合" do
50
+ let(:color) { [60, 0, 125, 255] }
51
+
52
+ it { is_expected.to eq([0, 125 ,255, 60]) }
53
+ end
54
+ end
55
+
56
+ describe '#to_dxruby_argb', 'SDL\'s RGBA to DXRuby\'s ARGB' do
57
+ subject { described_class.to_dxruby_argb(color) }
58
+
59
+ context "RGB" do
60
+ let(:color) { [0, 125, 255] }
61
+
62
+ it { is_expected.to eq([0xFF] + color) }
63
+ end
64
+
65
+ context "RGBA" do
66
+ let(:color) { [60, 0, 125, 255] }
67
+
68
+ it { is_expected.to eq([255, 60, 0, 125]) }
69
+ end
70
+ end
71
+
72
+ describe '#normalize_dxruby' do
73
+ subject { described_class.normalize_dxruby(color) }
74
+
75
+ context "RGB" do
76
+ let(:color) { [0, 125, 255] }
77
+
78
+ it { is_expected.to eq([0xFF] + color) }
79
+ end
80
+
81
+ context "ARGB" do
82
+ let(:color) { [255, 60, 0, 125] }
83
+
84
+ it { is_expected.to eq(color) }
85
+ end
86
+ end
41
87
  end
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe DXRubySDL::Image, '画像を表すクラス' do
5
5
  let(:image) { DXRubySDL::Image.new(640, 480) }
6
+ let(:logo_image) { DXRubySDL::Image.load(fixture_path("logo.png")) }
6
7
 
7
8
  describe '.new' do
8
9
  context '幅と高さを指定した場合' do
@@ -92,6 +93,40 @@ describe DXRubySDL::Image, '画像を表すクラス' do
92
93
  end
93
94
  end
94
95
 
96
+ describe '#[]' do
97
+ it "pixel is ARGB format" do
98
+ expect(logo_image[0, 0]).to eq([254, 255, 255, 255])
99
+ expect(logo_image[75, 144]).to eq([0, 255, 255, 255])
100
+ expect(logo_image[206, 191]).to eq([254, 229, 98, 108])
101
+ end
102
+ end
103
+
104
+ describe '#[]=' do
105
+ it "RGB" do
106
+ logo_image[0, 0] = [229, 98, 108]
107
+ expect(logo_image[0, 0]).to eq([255, 229, 98, 108])
108
+ end
109
+
110
+ it "ARGB" do
111
+ logo_image[0, 0] = [150, 229, 98, 108]
112
+ expect(logo_image[0, 0]).to eq([150, 229, 98, 108])
113
+ end
114
+ end
115
+
116
+ describe '#compare' do
117
+ it "RGB" do
118
+ expect(logo_image.compare(0, 0, [255, 255, 255])).to eq(false)
119
+ expect(logo_image.compare(75, 144, [255, 255, 255])).to eq(false)
120
+ expect(logo_image.compare(206, 191, [229, 98, 108])).to eq(false)
121
+ end
122
+
123
+ it "ARGB" do
124
+ expect(logo_image.compare(0, 0, [254, 255, 255, 255])).to eq(true)
125
+ expect(logo_image.compare(75, 144, [0, 255, 255, 255])).to eq(true)
126
+ expect(logo_image.compare(206, 191, [254, 229, 98, 108])).to eq(true)
127
+ end
128
+ end
129
+
95
130
  describe '#slice' do
96
131
  let(:image) { DXRubySDL::Image.load(fixture_path('logo.png')) }
97
132
 
@@ -81,7 +81,34 @@ describe DXRubySDL::Sound, '音を表すクラス' do
81
81
  end
82
82
  end
83
83
 
84
+ describe '#loop_count=' do
85
+ context 'WAVE file', wave: true do
86
+ it 'SDL::Waveのループ回数を変更する' do
87
+ wave =
88
+ sound.instance_variable_get('@sound').instance_variable_get('@wave')
89
+ expect(SDL::Mixer).to receive(:play_channel).with(-1, wave, 1)
90
+ sound.loop_count = 1
91
+ sound.play
92
+ end
93
+ end
94
+
95
+ context 'MIDI file', midi: true do
96
+ it 'SDL::Musicのループ回数を変更する' do
97
+ music =
98
+ sound.instance_variable_get('@sound').instance_variable_get('@music')
99
+ expect(SDL::Mixer).to receive(:play_music).with(music, 1)
100
+ sound.loop_count = 1
101
+ sound.play
102
+ end
103
+ end
104
+ end
105
+
84
106
  describe '#stop' do
107
+ it '再生していないサウンドを停止でエラーが発生しない' do
108
+ sound = DXRubySDL::Sound.new(fixture_path('sound.wav'))
109
+ expect { sound.stop }.to_not raise_error
110
+ end
111
+
85
112
  context 'WAVE file', wave: true do
86
113
  let(:path) { fixture_path('sound.wav') }
87
114
  let(:sound) { DXRubySDL::Sound.new(path) }
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.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-14 00:00:00.000000000 Z
11
+ date: 2017-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -244,7 +244,7 @@ files:
244
244
  - spec/lib/dxruby_spec.rb
245
245
  - spec/spec_helper.rb
246
246
  - spec/support/fixture_helper.rb
247
- homepage: https://github.com/takaokouji/dxruby-sdl
247
+ homepage: https://github.com/smalruby/dxruby-sdl
248
248
  licenses:
249
249
  - MIT
250
250
  metadata: {}
@@ -264,7 +264,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
264
264
  version: '0'
265
265
  requirements: []
266
266
  rubyforge_project:
267
- rubygems_version: 2.5.2
267
+ rubygems_version: 2.5.2.1
268
268
  signing_key:
269
269
  specification_version: 4
270
270
  summary: 2D graphics and game library