Class: Ruby2D::PixmapAtlas

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

Overview

A pixmap is a 2D grid of pixel data of fixed width and height. A pixmap atlas is a dictionary of named pixmaps where each named pixmap is loaded once and can be re-used multiple times without the overhead of repeated loading and multiple copies.

Instance Method Summary collapse

Constructor Details

#initializePixmapAtlas

Returns a new instance of PixmapAtlas.



11
12
13
# File 'lib/ruby2d/pixmap_atlas.rb', line 11

def initialize
  @atlas = {} # empty map
end

Instance Method Details

#[](name) ⇒ Pixmap?

Find a previosly loaded Pixmap in the atlas by name

Parameters:

Returns:



25
26
27
# File 'lib/ruby2d/pixmap_atlas.rb', line 25

def [](name)
  @atlas[name]
end

#clearObject

Empty the atlas, eventually releasing all the loaded pixmaps (except for any that are referenced elsewhere)



17
18
19
# File 'lib/ruby2d/pixmap_atlas.rb', line 17

def clear
  @atlas.clear
end

#countObject

Get the number of Pixmaps in the atlas



30
31
32
# File 'lib/ruby2d/pixmap_atlas.rb', line 30

def count
  @atlas.count
end

#each(&block) ⇒ Object

Iterate through each Pixmap in the atlas



35
36
37
# File 'lib/ruby2d/pixmap_atlas.rb', line 35

def each(&block)
  @atlas.each(&block)
end

#load_and_keep_image(file_path, as: nil) ⇒ Pixmap

Load a pixmap from an image file_path and store it as named for later lookup.

Parameters:

  • file_path (String)

    The path to the image file to load as a Pixmap

  • as (String) (defaults to: nil)

    The name to associated with the Pixmap; if nil then file_path is used as the name

Returns:

  • (Pixmap)

    the new (or existing) Pixmap

Raises:

  • (Pixmap::InvalidImageFileError)

    if the image file cannot be loaded



46
47
48
49
50
51
52
53
54
# File 'lib/ruby2d/pixmap_atlas.rb', line 46

def load_and_keep_image(file_path, as: nil)
  name = as || file_path
  if @atlas.member? name
    @atlas[name]
  else
    pixmap = Pixmap.new file_path
    @atlas[name] = pixmap
  end
end