Class: Ruby2D::Image

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

Overview

Images in many popular formats can be drawn in the window. To draw an image in the window, use the following, providing the image file path: Image.new(‘star.png’)

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, atlas: nil, width: nil, height: nil, x: 0, y: 0, z: 0, rotate: 0, color: nil, colour: nil, opacity: nil, show: true) ⇒ Image

Create an Image

Parameters:

  • path (#to_s)

    The location of the file to load as an image.

  • width (Numeric) (defaults to: nil)

    The width of image, or default is width from image file

  • height (Numeric) (defaults to: nil)

    The height of image, or default is height from image file

  • x (Numeric) (defaults to: 0)
  • y (Numeric) (defaults to: 0)
  • z (Numeric) (defaults to: 0)
  • rotate (Numeric) (defaults to: 0)

    Angle, default is 0

  • color (Numeric) (defaults to: nil)

    (or colour) Tint the image when rendering

  • opacity (Numeric) (defaults to: nil)

    Opacity of the image when rendering

  • show (Boolean) (defaults to: true)

    If true the image is added to Window automatically.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby2d/image.rb', line 39

def initialize(path, atlas: nil,
               width: nil, height: nil, x: 0, y: 0, z: 0,
               rotate: 0, color: nil, colour: nil,
               opacity: nil, show: true)
  @path = path.to_s

  # Consider input pixmap atlas if supplied to load image file
  @texture = Image.load_image_as_texture @path, atlas: atlas
  @width = width || @texture.width
  @height = height || @texture.height

  @x = x
  @y = y
  @z = z
  @rotate = rotate
  self.color = color || colour || 'white'
  self.color.opacity = opacity unless opacity.nil?

  add if show
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def data
  @data
end

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def height
  @height
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/ruby2d/image.rb', line 12

def path
  @path
end

#rotateObject

Returns the value of attribute rotate.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def rotate
  @rotate
end

#widthObject

Returns the value of attribute width.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def width
  @width
end

#xObject

Returns the value of attribute x.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def x
  @x
end

#yObject

Returns the value of attribute y.



13
14
15
# File 'lib/ruby2d/image.rb', line 13

def y
  @y
end

Class Method Details

.load_image_as_texture(path, atlas:) ⇒ Texture

Load an image path and return a Texture, using a pixmap atlas if provided

Parameters:

  • path (#to_s)

    The location of the file to load as an image.

  • atlas (PixmapAtlas)

    Pixmap atlas to use to manage the image file

Returns:



19
20
21
22
23
24
25
26
# File 'lib/ruby2d/image.rb', line 19

def self.load_image_as_texture(path, atlas:)
  pixmap = if atlas
             atlas.load_and_keep_image(path, as: path)
           else
             Pixmap.new path
           end
  pixmap.texture
end

Instance Method Details

#draw(x: 0, y: 0, width: nil, height: nil, rotate: 0, color: nil, colour: nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/ruby2d/image.rb', line 60

def draw(x: 0, y: 0, width: nil, height: nil, rotate: 0, color: nil, colour: nil)
  Window.render_ready_check

  render(x: x, y: y, width:
    width || @width, height: height || @height,
         color: Color.new(color || colour || 'white'),
         rotate: rotate || @rotate)
end