Class: Ruby2D::SpriteSheet

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/sprite_sheet.rb

Overview

A texture atlas / sprite sheet loaded from a Sparrow XML or TexturePacker JSON file. Owns one shared Image so every Sprite built against the sheet draws from the same GPU texture.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SpriteSheet

Load a sprite sheet. path is the atlas file (.xml or .json); the texture image referenced inside the atlas is resolved relative to that file's directory.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby2d/sprite_sheet.rb', line 13

def initialize(path)
  @path = path.to_s
  raise Error, "SpriteSheet file `#{@path}` not found" unless File.exist?(@path)

  atlas = AtlasParser.parse_file(@path)
  @frames = atlas[:frames] || {}

  atlas_image = atlas[:image_path]
  raise Error, "SpriteSheet `#{@path}` does not specify an image path" unless atlas_image

  @image_path = resolve_image_path(@path, atlas_image)
  @texture = Image.new(@image_path, add: false)
end

Instance Attribute Details

#image_pathObject (readonly)

Returns the value of attribute image_path.



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

def image_path
  @image_path
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#textureObject (readonly)

Returns the value of attribute texture.



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

def texture
  @texture
end

Instance Method Details

#frame(name) ⇒ Object Also known as: []

Look up a frame's region by name. Always returns at least { x:, y:, width:, height: }. When the atlas carries trim or rotation metadata, the hash also includes source_width, source_height, trim_x, trim_y, and/or rotated: true. Returns nil if no such frame exists.



37
38
39
# File 'lib/ruby2d/sprite_sheet.rb', line 37

def frame(name)
  @frames[name.to_s]
end

#frame?(name) ⇒ Boolean

Whether the sheet contains a frame with the given name

Returns:

  • (Boolean)


43
44
45
# File 'lib/ruby2d/sprite_sheet.rb', line 43

def frame?(name)
  @frames.key?(name.to_s)
end

#frame_namesObject

All frame names, in declaration order



28
29
30
# File 'lib/ruby2d/sprite_sheet.rb', line 28

def frame_names
  @frames.keys
end