Class: AuthorEngine::Image

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/author_engine/image.rb

Constant Summary collapse

CACHE =
{}

Instance Method Summary collapse

Methods included from Support

#code_editor, #mouse_over?, #sprite_editor, #window

Constructor Details

#initialize(path, retro: true) ⇒ Image

Returns a new instance of Image.



6
7
8
9
# File 'lib/author_engine/image.rb', line 6

def initialize(path, retro: true)
  @retro = retro
  @image = image_from_cache(path)
end

Instance Method Details

#draw(*args) ⇒ Object



46
47
48
# File 'lib/author_engine/image.rb', line 46

def draw(*args)
  @image.draw(*args)
end

#draw_rot(*args) ⇒ Object



50
51
52
# File 'lib/author_engine/image.rb', line 50

def draw_rot(*args)
  @image.draw_rot(*args)
end

#heightObject



12
# File 'lib/author_engine/image.rb', line 12

def height; @image.height; end

#image_from_cache(path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/author_engine/image.rb', line 14

def image_from_cache(path)
  path = "#{File.expand_path("../../../", __FILE__)}/#{path}"
  image = nil
  if image = CACHE.dig(path)
    return image
  else
    _image = nil
    begin
      _image = Gosu::Image.new(path, retro: @retro)
    rescue RuntimeError => e
      if e.message.downcase.include?("cannot open file")
        warn e.message
        warn caller[0..2].map{|s| s = "  #{s}"}.reverse
        _image = image_missing
      else
        raise
      end
    end

    image = CACHE[path] = _image
  end

  return image
end

#image_missingObject



39
40
41
42
43
44
# File 'lib/author_engine/image.rb', line 39

def image_missing
  return Gosu.render(window.sprite_size, window.sprite_size) do
    Gosu.draw_rect(0, 0, window.sprite_size, window.sprite_size, Gosu::Color::YELLOW)
    Gosu.draw_rect(2, 2, window.sprite_size-4, window.sprite_size-4, Gosu::Color::RED)
  end
end

#widthObject



11
# File 'lib/author_engine/image.rb', line 11

def width; @image.width; end