dxruby_sdl 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,178 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module DXRubySDL
4
+ class Sprite
5
+ attr_accessor :x
6
+ attr_accessor :y
7
+ attr_reader :image
8
+ attr_accessor :z
9
+ attr_accessor :angle
10
+ attr_accessor :scale_x
11
+ attr_accessor :scale_y
12
+ attr_accessor :center_x
13
+ attr_accessor :center_y
14
+ attr_accessor :alpha
15
+ attr_accessor :blend
16
+ attr_accessor :shader
17
+ attr_accessor :target
18
+ attr_accessor :collision
19
+ attr_accessor :collision_enable
20
+ attr_accessor :collision_sync
21
+ attr_accessor :visible
22
+
23
+ class << self
24
+ def check(o_sprites, d_sprites, shot = :shot, hit = :hit)
25
+ res = false
26
+ o_sprites = [o_sprites].flatten.select { |s| s.is_a?(self) }
27
+ d_sprites = [d_sprites].flatten.select { |s| s.is_a?(self) }
28
+ discards = []
29
+ o_sprites.each do |o_sprite|
30
+ if discards.include?(o_sprite)
31
+ next
32
+ end
33
+ d_sprites.each do |d_sprite|
34
+ if discards.include?(o_sprite)
35
+ break
36
+ end
37
+ if discards.include?(d_sprite)
38
+ next
39
+ end
40
+ if o_sprite === d_sprite
41
+ res = true
42
+ discard = false
43
+ if o_sprite.respond_to?(shot) && shot
44
+ if o_sprite.send(shot, d_sprite) == :discard
45
+ discard = true
46
+ end
47
+ end
48
+ if d_sprite.respond_to?(hit) && hit
49
+ if d_sprite.send(hit, o_sprite) == :discard
50
+ discard = true
51
+ end
52
+ end
53
+ if discard
54
+ discards << o_sprite
55
+ discards << d_sprite
56
+ end
57
+ end
58
+ end
59
+ end
60
+ return res
61
+ end
62
+
63
+ def update(sprites)
64
+ sprites.flatten.each do |s|
65
+ if s.respond_to?(:update)
66
+ s.update
67
+ end
68
+ end
69
+ end
70
+
71
+ def draw(sprites)
72
+ sprites.flatten.each do |s|
73
+ if s.respond_to?(:draw)
74
+ s.draw
75
+ end
76
+ end
77
+ end
78
+
79
+ def clean(sprites)
80
+ return [sprites].flatten.compact.reject(&:vanished?)
81
+ end
82
+ end
83
+
84
+ def initialize(x = 0, y = 0, image = nil)
85
+ @x = x
86
+ @y = y
87
+ @image = image
88
+
89
+ @collision_enable = true
90
+ @collision_sync = true
91
+ @visible = true
92
+ @vanished = false
93
+
94
+ calc_center
95
+ end
96
+
97
+ def image=(val)
98
+ @image = val
99
+ calc_center
100
+ end
101
+
102
+ def draw
103
+ if !@visible || vanished?
104
+ return
105
+ end
106
+ [:target, :blend, :shader].each do |method|
107
+ if send(method)
108
+ raise NotImplementedError, "Sprite#draw with #{method}"
109
+ end
110
+ end
111
+ options = {}
112
+ if angle
113
+ options[:angle] = angle
114
+ end
115
+ if scale_x
116
+ options[:scale_x] = scale_x
117
+ end
118
+ if scale_y
119
+ options[:scale_y] = scale_y
120
+ end
121
+ if center_x
122
+ options[:center_x] = center_x
123
+ end
124
+ if center_y
125
+ options[:center_y] = center_y
126
+ end
127
+ Window.draw_ex(x, y, image, options)
128
+ end
129
+
130
+ def ===(other)
131
+ if !@collision_enable || vanished? ||
132
+ !other.collision_enable || other.vanished? ||
133
+ !other.image && !other.collision
134
+ return false
135
+ end
136
+ if @collision_enable && @collision
137
+ x, y, width, height =
138
+ @collision[0] + @x, @collision[1] + @y, @collision[2], @collision[3]
139
+ else
140
+ x, y, width, height =
141
+ @x, @y, @image.width, @image.height
142
+ end
143
+ if other.collision_enable && other.collision
144
+ other_x, other_y, other_width, other_height =
145
+ other.collision
146
+ else
147
+ other_x, other_y, other_width, other_height =
148
+ other.x, other.y, other.image.width, other.image.height
149
+ end
150
+ return other_x + other_width > x && other_x < x + width &&
151
+ other_y + other_height > y && other_y < y + height
152
+ end
153
+
154
+ def check(sprites)
155
+ return [sprites].flatten.select { |s| self === s }
156
+ end
157
+
158
+ def vanish
159
+ @vanished = true
160
+ end
161
+
162
+ def vanished?
163
+ return @vanished
164
+ end
165
+
166
+ private
167
+
168
+ def calc_center
169
+ if @image
170
+ @center_x = @image.width / 2
171
+ @center_y = @image.height / 2
172
+ else
173
+ @center_x = 0
174
+ @center_y = 0
175
+ end
176
+ end
177
+ end
178
+ end
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module DXRubySDL
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
@@ -1,7 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'dxruby_sdl/window/fpstimer'
4
- require 'set'
5
4
 
6
5
  module DXRubySDL
7
6
  module Window
@@ -18,7 +17,7 @@ module DXRubySDL
18
17
  end
19
18
 
20
19
  def scale
21
- @scale ||= 1
20
+ @scale ||= DEFAULTS[:scale]
22
21
  return @scale
23
22
  end
24
23
 
@@ -44,6 +43,8 @@ module DXRubySDL
44
43
  case event
45
44
  when SDL::Event::Quit
46
45
  exit
46
+ when SDL::Event::KeyDown, SDL::Event::KeyUp
47
+ Input.send(:handle_key_event, event)
47
48
  end
48
49
  end
49
50
 
@@ -51,7 +52,7 @@ module DXRubySDL
51
52
 
52
53
  yield
53
54
 
54
- screen.update_rect(0, 0, 0, 0)
55
+ screen.flip
55
56
 
56
57
  Input.send(:store_last_state)
57
58
  end
@@ -59,19 +60,54 @@ module DXRubySDL
59
60
  end
60
61
 
61
62
  def draw(x, y, image, z = 0)
63
+ if z != 0
64
+ raise NotImplementedError, 'Window.draw(x, y, image, z != 0)'
65
+ end
66
+ image._surface_display_format_alpha
62
67
  screen.put(image._surface, x, y)
63
68
  end
64
69
 
70
+ def draw_ex(x, y, image, hash = {})
71
+ if hash[:z] && hash[:z] != 0
72
+ raise NotImplementedError, 'Window.draw_ex(x, y, image, z: != 0)'
73
+ end
74
+ image._surface_display_format_alpha
75
+ option = {
76
+ angle: 0,
77
+ scale_x: 1,
78
+ scale_y: 1,
79
+ center_x: 0,
80
+ center_y: 0,
81
+ }.merge(hash)
82
+ SDL::Surface.transform_blit(image._surface, screen,
83
+ option[:angle],
84
+ option[:scale_x], option[:scale_y],
85
+ option[:center_x], option[:center_y],
86
+ x + option[:center_x], y + option[:center_y],
87
+ SDL::Surface::TRANSFORM_SAFE)
88
+ end
89
+
65
90
  def draw_font(x, y, string, font, hash = {})
91
+ if string.empty?
92
+ return
93
+ end
66
94
  if hash[:color]
67
95
  r, g, b = *hash[:color]
68
96
  else
69
97
  r, g, b = 255, 255, 255
70
98
  end
71
- font._ttf.draw_blended_utf8(screen, string, x, y, r, g, b)
99
+ h = font._ttf.height + 1
100
+ string.lines.each.with_index do |line, i|
101
+ line.chomp!
102
+ if line.empty?
103
+ next
104
+ end
105
+ font._ttf.draw_blended_utf8(screen, line, x, y + i * h, r, g, b)
106
+ end
72
107
  end
73
108
 
74
109
  def open_filename(filter, title)
110
+ # :nocov:
75
111
  if /darwin/ =~ RUBY_PLATFORM
76
112
  apple_script = <<-EOS
77
113
  on run
@@ -87,24 +123,25 @@ end run
87
123
  else
88
124
  raise NotImplementedError, 'Window.open_filename'
89
125
  end
126
+ # :nocov:
90
127
  end
91
128
 
92
- # rubocop:disable SymbolName
93
129
  class << self
94
130
  attr_writer :width
95
131
  attr_writer :height
96
132
  attr_writer :scale
97
133
 
134
+ alias_method :drawEx, :draw_ex
98
135
  alias_method :drawFont, :draw_font
99
136
  alias_method :openFilename, :open_filename
100
137
  end
101
- # rubocop:enable SymbolName
102
138
 
103
139
  private
104
140
 
105
141
  DEFAULTS = {
106
142
  width: 640,
107
143
  height: 480,
144
+ scale: 1,
108
145
  background_color: [0, 0, 0],
109
146
  }
110
147
  private_constant :DEFAULTS
@@ -116,7 +153,9 @@ end run
116
153
  def screen
117
154
  return SDL::Screen.get
118
155
  rescue SDL::Error
119
- return SDL::Screen.open(width, height, 16, SDL::SWSURFACE)
156
+ flags =
157
+ SDL::HWSURFACE | SDL::ASYNCBLIT | SDL::HWPALETTE | SDL::DOUBLEBUF
158
+ return SDL::Screen.open(width, height, 0, flags)
120
159
  end
121
160
  end
122
161
  end
data/lib/dxruby_sdl.rb CHANGED
@@ -152,6 +152,8 @@ module DXRubySDL
152
152
  ].each.with_index do |dik_name, i|
153
153
  const_set(dik_name.sub(/\ADI/, '').to_sym, i)
154
154
  end
155
+ K_BACKSPACE = K_BACK
156
+ K_NUMPADSTAR = K_MULTIPLY
155
157
 
156
158
  %w[
157
159
  P_LEFT
@@ -220,5 +222,6 @@ require 'dxruby_sdl/font'
220
222
  require 'dxruby_sdl/input'
221
223
  require 'dxruby_sdl/sound'
222
224
  require 'dxruby_sdl/sound_effect'
225
+ require 'dxruby_sdl/sprite'
223
226
 
224
227
  SDL.init(SDL::INIT_EVERYTHING)
@@ -25,8 +25,8 @@ describe DXRubySDL::Color, 'カラーを変換するモジュール' do
25
25
  context '引数が3つの要素の配列の場合' do
26
26
  let(:color) { [0, 125, 255] }
27
27
 
28
- it '常に255を返す' do
29
- should eq(255)
28
+ it '常にnilを返す' do
29
+ should eq(nil)
30
30
  end
31
31
  end
32
32
 
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe DXRubySDL::Font, 'フォントを表すクラス' do
5
+ describe '.new' do
6
+ given = 32
7
+ context "大きさ(#{given.inspect})を指定した場合" do
8
+ subject { described_class.new(given) }
9
+
10
+ its(:size) { should eq(given) }
11
+ end
12
+ end
13
+
14
+ shared_context '#get_width' do
15
+ let(:font) { described_class.new(32) }
16
+ let(:method) { :get_width }
17
+
18
+ subject { font.send(method, 'やあ') }
19
+
20
+ it { should be_kind_of(Integer) }
21
+
22
+ it 'SDL::TTF#text_size(\'やあ\')を呼び出す' do
23
+ expect_any_instance_of(SDL::TTF)
24
+ .to receive(:text_size).with('やあ').once.and_return([62, 32])
25
+ subject
26
+ end
27
+ end
28
+
29
+ describe '#get_width', '文字列の描画幅を返す' do
30
+ include_context '#get_width'
31
+
32
+ describe 'alias' do
33
+ describe '#getWidth' do
34
+ it_behaves_like '#get_width' do
35
+ let(:method) { :getWidth }
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -14,10 +14,7 @@ describe DXRubySDL::Image, '画像を表すクラス' do
14
14
  end
15
15
 
16
16
  describe '.load' do
17
- subject {
18
- path = File.expand_path("../../fixtures/#{filename}", __FILE__)
19
- DXRubySDL::Image.load(path)
20
- }
17
+ subject { DXRubySDL::Image.load(fixture_path(filename)) }
21
18
 
22
19
  context 'PNG形式のファイルの場合' do
23
20
  let(:filename) { 'logo.png' }
@@ -198,4 +195,50 @@ describe DXRubySDL::Image, '画像を表すクラス' do
198
195
  end
199
196
  end
200
197
  end
198
+
199
+ shared_context '#draw_font' do
200
+ let!(:font) { DXRubySDL::Font.new(32) }
201
+ let(:args) { [0, 0, 'やあ', font] }
202
+
203
+ include_context 'draw methods'
204
+
205
+ context '引数が空文字列の場合' do
206
+ let(:args) { [0, 0, '', font] }
207
+
208
+ it 'なにもしない' do
209
+ expect { subject }.not_to raise_error
210
+ end
211
+ end
212
+
213
+ context '引数が複数行の場合' do
214
+ let(:args) { [0, 0, "やあ\n\nこんにちは\n\nHello\n", font] }
215
+
216
+ it 'なにもしない' do
217
+ expect { subject }.not_to raise_error
218
+ end
219
+ end
220
+
221
+ color = [255, 0, 0]
222
+ context "第5引数の色(#{color.inspect})を指定した場合" do
223
+ let(:args) { [0, 0, 'やあ', font, color] }
224
+
225
+ it '文字列を描画する' do
226
+ subject
227
+ end
228
+ end
229
+ end
230
+
231
+ describe '#draw_font' do
232
+ subject { image.draw_font(*args) }
233
+
234
+ include_context '#draw_font'
235
+
236
+ describe 'alias' do
237
+ describe '#drawFont' do
238
+ it_behaves_like '#draw_font' do
239
+ subject { image.drawFont(*args) }
240
+ end
241
+ end
242
+ end
243
+ end
201
244
  end