ruby2d 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ruby2d.rb CHANGED
@@ -1,17 +1,20 @@
1
1
  # ruby2d.rb
2
2
 
3
+ require 'ruby2d/exceptions'
4
+ require 'ruby2d/color'
3
5
  require 'ruby2d/window'
4
6
  require 'ruby2d/application'
5
- require 'ruby2d/color'
7
+ require 'ruby2d/dsl'
6
8
  require 'ruby2d/quad'
7
9
  require 'ruby2d/rectangle'
8
10
  require 'ruby2d/square'
9
11
  require 'ruby2d/triangle'
10
12
  require 'ruby2d/image'
13
+ require 'ruby2d/sprite'
11
14
  require 'ruby2d/text'
12
- require 'ruby2d/dsl'
13
- require 'ruby2d/exceptions'
15
+ require 'ruby2d/sound'
16
+ require 'ruby2d/music'
14
17
  require 'ruby2d/ruby2d' # load native extension
15
18
 
16
- include Ruby2D::DSL
17
19
  include Ruby2D
20
+ extend Ruby2D::DSL
@@ -12,8 +12,16 @@ module Ruby2D::Application
12
12
  @@window.set(opts)
13
13
  end
14
14
 
15
- def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc)
16
- @@window.on(mouse: mouse, key: key, key_down: key_down, controller: controller, &proc)
15
+ def on(args = {}, &proc)
16
+ @@window.on(args, &proc)
17
+ end
18
+
19
+ def on_key(&proc)
20
+ @@window.on_key(&proc)
21
+ end
22
+
23
+ def on_controller(&proc)
24
+ @@window.on_controller(&proc)
17
25
  end
18
26
 
19
27
  def add(o)
@@ -35,5 +43,9 @@ module Ruby2D::Application
35
43
  def show
36
44
  @@window.show
37
45
  end
46
+
47
+ def close
48
+ @@window.close
49
+ end
38
50
  end
39
51
  end
data/lib/ruby2d/color.rb CHANGED
@@ -5,26 +5,27 @@ module Ruby2D
5
5
 
6
6
  attr_reader :r, :g, :b, :a
7
7
 
8
+ # Based on clrs.cc
8
9
  @@colors = {
9
- 'black' => [ 0, 0, 0, 255],
10
- 'gray' => [170, 170, 170, 255],
11
- 'silver' => [221, 221, 221, 255],
12
- 'white' => [255, 255, 255, 255],
13
- 'navy' => [ 0, 31, 63, 255],
14
- 'blue' => [ 0, 116, 217, 255],
15
- 'aqua' => [127, 219, 255, 255],
16
- 'teal' => [ 57, 204, 204, 255],
17
- 'olive' => [ 61, 153, 112, 255],
18
- 'green' => [ 46, 204, 64, 255],
19
- 'lime' => [ 1, 255, 112, 255],
20
- 'yellow' => [255, 220, 0, 255],
21
- 'orange' => [255, 133, 27, 255],
22
- 'red' => [255, 65, 54, 255],
23
- 'maroon' => [133, 20, 75, 255],
24
- 'fuchsia' => [240, 18, 190, 255],
25
- 'purple' => [177, 13, 201, 255],
26
- 'brown' => [102, 51, 0, 255],
27
- 'random' => []
10
+ 'navy' => '#001F3F',
11
+ 'blue' => '#0074D9',
12
+ 'aqua' => '#7FDBFF',
13
+ 'teal' => '#39CCCC',
14
+ 'olive' => '#3D9970',
15
+ 'green' => '#2ECC40',
16
+ 'lime' => '#01FF70',
17
+ 'yellow' => '#FFDC00',
18
+ 'orange' => '#FF851B',
19
+ 'red' => '#FF4136',
20
+ 'brown' => '#663300',
21
+ 'fuchsia' => '#F012BE',
22
+ 'purple' => '#B10DC9',
23
+ 'maroon' => '#85144B',
24
+ 'white' => '#FFFFFF',
25
+ 'silver' => '#DDDDDD',
26
+ 'gray' => '#AAAAAA',
27
+ 'black' => '#111111',
28
+ 'random' => ''
28
29
  }
29
30
 
30
31
  def initialize(c)
@@ -34,39 +35,61 @@ module Ruby2D
34
35
  case c
35
36
  when String
36
37
  if c == 'random'
37
- @r, @g, @b, @a = rand(0..1.0), rand(0..1.0), rand(0..1.0), 1.0
38
+ @r, @g, @b, @a = rand, rand, rand, 1.0
39
+ elsif self.class.is_hex?(c)
40
+ @r, @g, @b, @a = hex_to_f(c)
38
41
  else
39
- @r, @g, @b, @a = to_f(@@colors[c])
42
+ @r, @g, @b, @a = hex_to_f(@@colors[c])
40
43
  end
41
44
  when Array
42
- @r, @g, @b, @a = to_f([c[0], c[1], c[2], c[3]])
45
+ @r, @g, @b, @a = [c[0], c[1], c[2], c[3]]
43
46
  end
44
47
  end
45
48
  end
46
49
 
47
- # Color must be String, like 'red', or Array, like [1.0, 0, 0, 1.0]
50
+ # Check if string is a proper hex value
51
+ def self.is_hex?(s)
52
+ # MRuby doesn't support regex, otherwise we'd do:
53
+ # !(/^#[0-9A-F]{6}$/i.match(a).nil?)
54
+ s.class == String && s[0] == '#' && s.length == 7
55
+ end
56
+
57
+ # Check if the color is valid
48
58
  def self.is_valid?(c)
49
- (c.class == String && @@colors.key?(c)) ||
50
- (c.class == Array && c.length == 4 &&
51
- c.all? { |el| el.is_a? Numeric } &&
52
- c.all? { |el| el.class == Fixnum && (0..255).include?(el) ||
53
- el.class == Float && (0.0..1.0).include?(el) })
59
+ @@colors.key?(c) || # keyword
60
+ self.is_hex?(c) || # hexadecimal value
61
+
62
+ # Array of Floats from 0.0..1.0
63
+ c.class == Array && c.length == 4 &&
64
+ c.all? { |el|
65
+ el.is_a?(Numeric) && (0.0..1.0).include?(el)
66
+ }
54
67
  end
55
68
 
56
69
  private
57
70
 
71
+ # TODO: Only `Number` supported in JS
58
72
  # Convert from Fixnum (0..255) to Float (0.0..1.0)
59
73
  def to_f(a)
60
74
  b = []
61
75
  a.each do |n|
62
- if n.class == Fixnum
63
- b.push(n / 255.0)
64
- else
65
- b.push(n)
66
- end
76
+ b.push(n / 255.0)
67
77
  end
68
78
  return b
69
79
  end
70
80
 
81
+ # Convert from hex value (e.g. #FFF000) to Float (0.0..1.0)
82
+ def hex_to_f(h)
83
+ h = (h[1..-1]).chars.each_slice(2).map(&:join)
84
+ a = []
85
+
86
+ h.each do |el|
87
+ a.push(el.to_i(16))
88
+ end
89
+
90
+ a.push(255)
91
+ return to_f(a)
92
+ end
93
+
71
94
  end
72
95
  end
data/lib/ruby2d/dsl.rb CHANGED
@@ -1,31 +1,40 @@
1
1
  # dsl.rb
2
2
 
3
3
  module Ruby2D::DSL
4
- def hello
5
- puts "hi"
6
- end
7
-
8
4
  def get(sym)
9
- Ruby2D::Application.get(sym)
5
+ Application.get(sym)
10
6
  end
11
7
 
12
8
  def set(opts)
13
- Ruby2D::Application.set(opts)
9
+ Application.set(opts)
10
+ end
11
+
12
+ def on(args = {}, &proc)
13
+ Application.on(args, &proc)
14
+ end
15
+
16
+ def on_key(&proc)
17
+ Application.on_key(&proc)
14
18
  end
15
19
 
16
- def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc)
17
- Ruby2D::Application.on(mouse: mouse, key: key, key_down: key_down, controller: controller, &proc)
20
+ def on_controller(&proc)
21
+ Application.on_controller(&proc)
18
22
  end
19
23
 
20
24
  def update(&proc)
21
- Ruby2D::Application.update(&proc)
25
+ Application.update(&proc)
26
+ end
27
+
28
+ def clear
29
+ Application.clear
22
30
  end
23
31
 
24
32
  def show
25
- Ruby2D::Application.show
33
+ Application.show
26
34
  end
27
35
 
28
- def clear
29
- Ruby2D::Application.clear
36
+ def close
37
+ Application.close
30
38
  end
39
+
31
40
  end
@@ -8,9 +8,8 @@ module Ruby2D
8
8
 
9
9
  def initialize(msg)
10
10
  super(msg)
11
- puts error("\nRuby 2D Error:") << " " << msg
12
- puts bold("Occurred in:")
13
- puts bold(" " + caller.last), "\n"
11
+ puts error("\nRuby 2D Error:") << " #{msg}" <<
12
+ bold("\nOccurred in:\n #{caller.last}\n")
14
13
  end
15
14
  end
16
15
  end
data/lib/ruby2d/image.rb CHANGED
@@ -3,25 +3,38 @@
3
3
  module Ruby2D
4
4
  class Image
5
5
 
6
- attr_accessor :x, :y
6
+ attr_accessor :x, :y, :width, :height, :data
7
+ attr_reader :path, :color
7
8
 
8
9
  def initialize(x, y, path)
9
10
 
10
- unless File.exists? path
11
- raise Error, "Cannot find image file `#{path}`"
12
- end
11
+ # TODO: Check if file exists
12
+ # `File.exists?` is not available in MRuby
13
+ # Ideally would do:
14
+ # unless File.exists? path
15
+ # raise Error, "Cannot find image file `#{path}`"
16
+ # end
13
17
 
14
18
  @type_id = 3
15
19
  @x, @y, @path = x, y, path
16
-
17
- if defined? Ruby2D::DSL
18
- Ruby2D::Application.add(self)
20
+ @color = Color.new([1, 1, 1, 1])
21
+ init(path)
22
+ add
23
+ end
24
+
25
+ def color=(c)
26
+ @color = Color.new(c)
27
+ end
28
+
29
+ def add
30
+ if Module.const_defined? :DSL
31
+ Application.add(self)
19
32
  end
20
33
  end
21
34
 
22
35
  def remove
23
- if defined? Ruby2D::DSL
24
- Ruby2D::Application.remove(self)
36
+ if Module.const_defined? :DSL
37
+ Application.remove(self)
25
38
  end
26
39
  end
27
40
 
@@ -0,0 +1,17 @@
1
+ # music.rb
2
+
3
+ module Ruby2D
4
+ class Music
5
+
6
+ attr_accessor :data, :loop
7
+ attr_reader :path
8
+
9
+ def initialize(path)
10
+ # TODO: Check if file exists
11
+ init(path)
12
+ @path = path
13
+ @loop = false
14
+ end
15
+
16
+ end
17
+ end
data/lib/ruby2d/quad.rb CHANGED
@@ -19,10 +19,7 @@ module Ruby2D
19
19
  @x1, @y1, @x2, @y2, @x3, @y3, @x4, @y4 = x1, y1, x2, y2, x3, y3, x4, y4
20
20
  @color = c
21
21
  update_color(c)
22
-
23
- if defined? Ruby2D::DSL
24
- Ruby2D::Application.add(self)
25
- end
22
+ add
26
23
  end
27
24
 
28
25
  def color=(c)
@@ -30,9 +27,15 @@ module Ruby2D
30
27
  update_color(c)
31
28
  end
32
29
 
30
+ def add
31
+ if Module.const_defined? :DSL
32
+ Application.add(self)
33
+ end
34
+ end
35
+
33
36
  def remove
34
- if defined? Ruby2D::DSL
35
- Ruby2D::Application.remove(self)
37
+ if Module.const_defined? :DSL
38
+ Application.remove(self)
36
39
  end
37
40
  end
38
41
 
@@ -42,21 +45,27 @@ module Ruby2D
42
45
 
43
46
  # If a valid color, use it for each vertex
44
47
  if Color.is_valid? c
45
- @c1 = Ruby2D::Color.new(c)
46
- @c2 = Ruby2D::Color.new(c)
47
- @c3 = Ruby2D::Color.new(c)
48
- @c4 = Ruby2D::Color.new(c)
49
-
48
+ @c1 = Color.new(c)
49
+ @c2 = Color.new(c)
50
+ @c3 = Color.new(c)
51
+ @c4 = Color.new(c)
52
+
53
+ elsif c.class == Array && c.length < 4
54
+ raise Error, "Quads require 4 colors, one for each vertex. Only " <<
55
+ "#{c.length} were given."
56
+
50
57
  # If a valid array of colors, assign them to each vertex, respectively
51
58
  elsif c.all? { |el| Color.is_valid? el }
52
- @c1 = Ruby2D::Color.new(c[0])
53
- @c2 = Ruby2D::Color.new(c[1])
54
- @c3 = Ruby2D::Color.new(c[2])
55
- @c4 = Ruby2D::Color.new(c[3])
59
+ @c1 = Color.new(c[0])
60
+ @c2 = Color.new(c[1])
61
+ @c3 = Color.new(c[2])
62
+ @c4 = Color.new(c[3])
63
+
56
64
  else
57
65
  raise Error, "Not a valid color for #{self.class}"
58
66
  end
59
67
 
60
68
  end
69
+
61
70
  end
62
71
  end
@@ -1,7 +1,7 @@
1
1
  # rectangle.rb
2
2
 
3
3
  module Ruby2D
4
- class Rectangle < Ruby2D::Quad
4
+ class Rectangle < Quad
5
5
 
6
6
  attr_reader :x, :y, :width, :height
7
7
 
@@ -10,10 +10,7 @@ module Ruby2D
10
10
  @x, @y, @width, @height, @color = x, y, w, h, c
11
11
  update_coords(x, y, w, h)
12
12
  update_color(c)
13
-
14
- if defined? Ruby2D::DSL
15
- Ruby2D::Application.add(self)
16
- end
13
+ add
17
14
  end
18
15
 
19
16
  def x=(x)
data/lib/ruby2d/sound.rb CHANGED
@@ -3,16 +3,13 @@
3
3
  module Ruby2D
4
4
  class Sound
5
5
 
6
- def initialize(window, path)
7
- unless File.exists? path
8
- raise Error, "Cannot find sound file!"
9
- end
10
- window.create_audio(self, path)
11
- @window, @path = window, path
12
- end
6
+ attr_accessor :data
7
+ attr_reader :path
13
8
 
14
- def play
15
- @window.play_audio(self)
9
+ def initialize(path)
10
+ # TODO: Check if file exists
11
+ init(path)
12
+ @path = path
16
13
  end
17
14
 
18
15
  end
@@ -0,0 +1,91 @@
1
+ # sprite.rb
2
+
3
+ module Ruby2D
4
+ class Sprite
5
+
6
+ attr_accessor :x, :y, :clip_x, :clip_y, :clip_w, :clip_h, :data
7
+
8
+ def initialize(x, y, path)
9
+
10
+ # unless File.exists? path
11
+ # raise Error, "Cannot find image file `#{path}`"
12
+ # end
13
+
14
+ @type_id = 4
15
+ @x, @y, @path = x, y, path
16
+ @clip_x, @clip_y, @clip_w, @clip_h = 0, 0, 0, 0
17
+ @default = nil
18
+ @animations = {}
19
+ @current_animation = nil
20
+ @current_frame = 0
21
+ @current_frame_time = 0
22
+
23
+ init(path)
24
+ if Module.const_defined? :DSL
25
+ Application.add(self)
26
+ end
27
+ end
28
+
29
+ def start(x, y, w, h)
30
+ @default = [x, y, w, h]
31
+ clip(x, y, w, h)
32
+ end
33
+
34
+ def add(animations)
35
+ @animations.merge!(animations)
36
+ end
37
+
38
+ def animate(animation)
39
+ if @current_animation != animation
40
+ @current_frame = 0
41
+ @current_frame_time = 0
42
+ @current_animation = animation
43
+ end
44
+ animate_frames(@animations[animation])
45
+ end
46
+
47
+ def reset
48
+ clip(@default[0], @default[1], @default[2], @default[3])
49
+ @current_animation = nil
50
+ end
51
+
52
+ # TODO: Sprite already has an `add` method, have to reconsile
53
+ # def add
54
+ # if Module.const_defined? :DSL
55
+ # Application.add(self)
56
+ # end
57
+ # end
58
+
59
+ def remove
60
+ if Module.const_defined? :DSL
61
+ Application.remove(self)
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def clip(x, y, w, h)
68
+ @clip_x, @clip_y, @clip_w, @clip_h = x, y, w, h
69
+ end
70
+
71
+ def animate_frames(frames)
72
+ if @current_frame_time < frames[@current_frame][4]
73
+ clip_with_current_frame(frames)
74
+ @current_frame_time += 1
75
+ else
76
+ @current_frame += 1
77
+ if @current_frame == frames.length
78
+ @current_frame = 0
79
+ end
80
+ clip_with_current_frame(frames)
81
+ @current_frame_time = 0
82
+ end
83
+ end
84
+
85
+ def clip_with_current_frame(frames)
86
+ clip(frames[@current_frame][0], frames[@current_frame][1],
87
+ frames[@current_frame][2], frames[@current_frame][3])
88
+ end
89
+
90
+ end
91
+ end