Class: Gosu::Image

Inherits:
Object
  • Object
show all
Defined in:
rdoc/gosu.rb

Overview

Provides functionality for drawing rectangular images.

Instance Attribute Summary collapse

Creating and loading images collapse

Drawing an image collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Image #initialize(window, source, tileable = false) ⇒ Image #initialize(window, source, tileable, left, top, width, height) ⇒ Image

Note:

For Windows Bitmap (BMP) images, magenta (FF00FF, often called “magic pink” in this context) is treated as a chroma key and all pixels of that color are automatically rendered fully transparent.

Loads an image from a file or an RMagick image.

(Passing a Window reference is not necessary anymore, please use the first overload from now on.)

Parameters:

  • source (String, Magick::Image)

    the filename or RMagick image to load from.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :tileable (true, false) — default: false

    if true, the Image will not have soft edges when scaled

  • :retro (true, false) — default: false

    if true, the image will not be interpolated when it is scaled up or down. When :retro it set, :tileable has no effect.

  • :rect (Array) — default: [0, 0, image_width, image_height]

    the source rectangle in the image

See Also:



384
# File 'rdoc/gosu.rb', line 384

def initialize(source, options = {}); end

Instance Attribute Details

#heightInteger (readonly)

Returns the image’s height, in pixels.

Returns:

  • (Integer)

    the image’s height, in pixels.



360
361
362
# File 'rdoc/gosu.rb', line 360

def height
  @height
end

#widthInteger (readonly)

Returns the image’s width, in pixels.

Returns:

  • (Integer)

    the image’s width, in pixels.



356
357
358
# File 'rdoc/gosu.rb', line 356

def width
  @width
end

Class Method Details

.from_blob(width, height, rgba = "\0\0\0\0" * (width * height)) ⇒ Object

Creates a new image with the given dimensions and RGBA pixel data.

Parameters:

  • width (Integer)

    Width of the image in pixels.

  • height (Integer)

    Height of the image in pixels.

  • rgba (String) (defaults to: "\0\0\0\0" * (width * height))

    A string containing raw binary image data, with either one byte (‘uint8’) or four bytes (‘float’) per RGBA component.



392
# File 'rdoc/gosu.rb', line 392

def self.from_blob(width, height, rgba = "\0\0\0\0" * (width * height)); end

.from_markup(markup, line_height, options = {}) ⇒ Object

Like #from_text, but supports the following markup tags: ‘bold`, `italic`, `<c=rrggbb>colors</c>`.



424
# File 'rdoc/gosu.rb', line 424

def self.from_markup(markup, line_height, options = {}); end

.from_text(text, line_height, options = {}) ⇒ Gosu::Image .from_text(window, text, font_name, line_height) ⇒ Gosu::Image .from_text(window, text, font_name, line_height, line_spacing, width, align) ⇒ Gosu::Image

Note:

The text is always rendered in white. To draw it in a different color, use the color parameter of #draw, et al.

Creates a reusable image from one or more lines of text.

(Passing a Window reference is not necessary anymore, please use the first overload from now on.)

Parameters:

  • text (String)
  • line_height (Integer)

    the line height, in pixels.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :font (String) — default: Gosu::default_font_name

    the name of a system font, or a path to a TrueType Font (TTF) file. A path must contain at least one ‘/’ character to distinguish it from a system font.

  • :bold (bool) — default: false
  • :italic (bool) — default: false
  • :underline (bool) — default: false
  • :width (Integer)

    the width of the image, in pixels. Long lines will be automatically wrapped around to avoid overflow, but overlong words will be truncated. If this option is omitted, lines will not be wrapped, and :align and :spacing will be ignored as well.

  • :spacing (Integer) — default: 0

    the spacing between lines, in pixels.

  • :align (:left, :right, :center, :justify) — default: :left

    the text alignment.

  • :retro (true, false) — default: false

    if true, the image will not be interpolated when it is scaled up or down.

Returns:

See Also:



420
# File 'rdoc/gosu.rb', line 420

def self.from_text(text, line_height, options = {}); end

.load_tiles(source, tile_width, tile_height, options = {}) ⇒ Array<Gosu::Image> .load_tiles(window, source, tile_width, tile_height, tileable) ⇒ Array<Gosu::Image>

Note:

For Windows Bitmap (BMP) images, magenta (FF00FF, often called “magic pink” in this context) is treated as a chroma key and all pixels of that color are automatically rendered fully transparent.

Loads an image from a file or an RMagick image, then divides the image into an array of equal-sized tiles.

(Passing a Window reference is not necessary anymore, please use the first overload from now on.)

Parameters:

  • source (String, Magick::Image)
  • tile_width (Integer)

    If positive, this is the width of the individual tiles; if negative, the image is divided into -tile_width columns.

  • tile_height (Integer)

    If positive, this is the height of the individual tiles; if negative, the image is divided into -tile_height rows.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :tileable (true, false) — default: false

    if true, the Image will not have soft edges when scaled

  • :retro (true, false) — default: false

    if true, the image will not be interpolated when it is scaled up or down. When :retro it set, :tileable has no effect.

Returns:

See Also:



445
# File 'rdoc/gosu.rb', line 445

def self.load_tiles(source, tile_width, tile_height, options = {}); end

Instance Method Details

#draw(x, y, z = 0, scale_x = 1, scale_y = 1, color = 0xff_ffffff, mode = :default) ⇒ void

This method returns an undefined value.

Draws the image with its top left corner at (x, y).

Parameters:

  • x (Float)

    the X coordinate.

  • y (Float)

    the Y coordinate.

  • z (Float) (defaults to: 0)

    the Z-order.

  • scale_x (Float) (defaults to: 1)

    the horizontal scaling factor.

  • scale_y (Float) (defaults to: 1)

    the vertical scaling factor.

  • color (Gosu::Color, Integer) (defaults to: 0xff_ffffff)
  • mode (:default, :additive) (defaults to: :default)

    the blending mode to use.

See Also:



479
# File 'rdoc/gosu.rb', line 479

def draw(x, y, z=0, scale_x=1, scale_y=1, color=0xff_ffffff, mode=:default); end

#draw_as_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, mode = :default) ⇒ void

This method returns an undefined value.

Draws the image as an arbitrary quad. This method can be used for advanced non-rectangular drawing techniques, e.g., faking perspective or isometric projection.

Parameters:

  • x1 (Float)

    the X coordinate of the first vertex.

  • y1 (Float)

    the Y coordinate of the first vertex.

  • c1 (Gosu::Color)

    the color of the first vertex.

  • x2 (Float)

    the X coordinate of the second vertex.

  • y2 (Float)

    the Y coordinate of the second vertex.

  • c2 (Gosu::Color)

    the color of the second vertex.

  • x3 (Float)

    the X coordinate of the third vertex.

  • y3 (Float)

    the Y coordinate of the third vertex.

  • c3 (Gosu::Color)

    the color of the third vertex.

  • x4 (Float)

    the X coordinate of the fourth vertex.

  • y4 (Float)

    the Y coordinate of the fourth vertex.

  • c4 (Gosu::Color)

    the color of the fourth vertex.

  • z (Float)

    the Z-order.

  • mode (:default, :additive) (defaults to: :default)

    the blending mode to use.

See Also:



506
# File 'rdoc/gosu.rb', line 506

def draw_as_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, mode=:default); end

#draw_rot(x, y, z = 0, angle = 0, center_x = 0.5, center_y = 0.5, scale_x = 1, scale_y = 1, color = 0xff_ffffff, mode = :default) ⇒ void

This method returns an undefined value.

Draws the image rotated, with its rotational center at (x, y).

Parameters:

  • angle (Float) (defaults to: 0)

    the angle to rotate, in degrees.

  • center_x (Float) (defaults to: 0.5)

    the relative horizontal rotation origin.

  • center_y (Float) (defaults to: 0.5)

    the relative vertical rotation origin.

  • x (Float)

    the X coordinate.

  • y (Float)

    the Y coordinate.

  • z (Float) (defaults to: 0)

    the Z-order.

  • scale_x (Float) (defaults to: 1)

    the horizontal scaling factor.

  • scale_y (Float) (defaults to: 1)

    the vertical scaling factor.

  • color (Gosu::Color, Integer) (defaults to: 0xff_ffffff)
  • mode (:default, :additive) (defaults to: :default)

    the blending mode to use.

See Also:



493
# File 'rdoc/gosu.rb', line 493

def draw_rot(x, y, z=0, angle=0, center_x=0.5, center_y=0.5, scale_x=1, scale_y=1, color=0xff_ffffff, mode=:default); end

#gl_tex_infoGosu::GLTexInfo?

Note:

Some images may be too large to fit on a single texture; this method returns nil in those cases.

Returns an object that holds information about the underlying OpenGL texture and UV coordinates of the image.

Returns:

See Also:



518
# File 'rdoc/gosu.rb', line 518

def gl_tex_info; end

#insert(source, x, y) ⇒ void

This method returns an undefined value.

Overwrites part of the image with the contents of another. If the source image is partially out of bounds, it will be clipped to fit.

This can be used to e.g. overwrite parts of a landscape.

Parameters:

  • source (String, Magick::Image)

    the filename or RMagick image to load from.

  • x (Integer)

    the X coordinate of the top left corner.

  • y (Integer)

    the Y coordinate of the top left corner.



541
# File 'rdoc/gosu.rb', line 541

def insert(source, x, y); end

#save(filename) ⇒ void

This method returns an undefined value.

Saves the image to a file. The file format is determined from the file extension.

Useful for, e.g., pre-rendering text on a development machine where the necessary fonts are known to be available.

Parameters:

  • filename (String)

    the path to save the file under.



550
# File 'rdoc/gosu.rb', line 550

def save(filename); end

#subimage(left, top, width, height) ⇒ Image?

Returns an image that is a smaller, rectangular view of this Gosu::Image.

This is a very fast operation, and no new textures will be allocated. If you update this Gosu::Image or the #subimage using #insert, the other Gosu::Image will be affected as well.

Caveats:

  • If you stretch or rotate a #subimage, the pixels adjacent to it might bleed into it, as Gosu does not manage the ‘tileability’ of subimages.

Returns:

  • (Image?)

    an image that represents a portion of the containing image



457
# File 'rdoc/gosu.rb', line 457

def subimage(left, top, width, height); end

#to_blobString

Returns the associated texture contents as binary string of packed RGBA values, useful for use with RMagick (Magick::Image.from_blob).

magick_image = Magick::Image.from_blob(image.to_blob) {
  self.format = "RGBA"
  self.size = "#{image.width}x#{image.height}"
  self.depth = 8
}.first

Returns:

  • (String)

    a binary string of packed RGBA values.



530
# File 'rdoc/gosu.rb', line 530

def to_blob; end