summaryrefslogtreecommitdiffhomepage
path: root/lib/ruby2d/image.rb
blob: 2691f787b651db02f4c3eca05f7f8013d2a17059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# image.rb

module Ruby2D
  class Image
    include Renderable

    attr_accessor :x, :y, :width, :height, :data
    attr_reader :path, :color

    def initialize(x, y, path, z=0)

      unless RUBY_ENGINE == 'opal'
        unless File.exists? path
          raise Error, "Cannot find image file `#{path}`"
        end
      end

      @type_id = 4
      @x, @y, @path = x, y, path
      @z = z
      @color = Color.new([1, 1, 1, 1])
      ext_image_init(path)
      add
    end

    def color=(c)
      @color = Color.new(c)
    end

    def contains?(x, y)
      @x < x and @x + @width > x and @y < y and @y + @height > y
    end
  end
end