Class: Ruby2D::Image

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/image.rb

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#add, #contains?, #remove

Constructor Details

#initialize(path, opts = {}) ⇒ Image

Returns a new instance of Image.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby2d/image.rb', line 18

def initialize(path, opts = {})
  @path = path

  @texture = Texture.new(*Image.load_image(@path))
  @width = opts[:width] || @texture.width
  @height = opts[:height] || @texture.height

  @x = opts[:x] || 0
  @y = opts[:y] || 0
  @z = opts[:z] || 0
  @rotate = opts[:rotate] || 0
  self.color = opts[:color] || 'white'
  self.color.opacity = opts[:opacity] if opts[:opacity]

  unless opts[:show] == false then add end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/ruby2d/image.rb', line 8

def data
  @data
end

#heightObject

Returns the value of attribute height.



8
9
10
# File 'lib/ruby2d/image.rb', line 8

def height
  @height
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/ruby2d/image.rb', line 7

def path
  @path
end

#rotateObject

Returns the value of attribute rotate.



8
9
10
# File 'lib/ruby2d/image.rb', line 8

def rotate
  @rotate
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/ruby2d/image.rb', line 8

def width
  @width
end

#xObject

Returns the value of attribute x.



8
9
10
# File 'lib/ruby2d/image.rb', line 8

def x
  @x
end

#yObject

Returns the value of attribute y.



8
9
10
# File 'lib/ruby2d/image.rb', line 8

def y
  @y
end

Class Method Details

.load_image(path) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/ruby2d/image.rb', line 10

def self.load_image(path)
  unless File.exist? path
    raise Error, "Cannot find image file `#{path}`"
  end

  ext_load_image(path)
end

Instance Method Details

#draw(opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby2d/image.rb', line 35

def draw(opts = {})
  Window.render_ready_check

  opts[:width] = opts[:width] || @width
  opts[:height] = opts[:height] || @height
  opts[:rotate] = opts[:rotate] || @rotate
  unless opts[:color]
    opts[:color] = [1.0, 1.0, 1.0, 1.0]
  end

  render(x: opts[:x], y: opts[:y], width: opts[:width], height: opts[:height], color: Color.new(opts[:color]), rotate: opts[:rotate])
end