Sha256: 82e197822b16cc54292b2e3cb247bd5b00a6899e4208b9fc3cef8c138713e053

Contents?: true

Size: 1.26 KB

Versions: 9

Compression:

Stored size: 1.26 KB

Contents

class AuthorEngine
  class Image
    include AuthorEngine::Support
    CACHE = {}

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

    def width; @image.width; end
    def height; @image.height; end

    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

    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

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
author_engine-0.9.0 lib/author_engine/image.rb
author_engine-0.8.0 lib/author_engine/image.rb
author_engine-0.7.0 lib/author_engine/image.rb
author_engine-0.6.1 lib/author_engine/image.rb
author_engine-0.6.0 lib/author_engine/image.rb
author_engine-0.5.0 lib/author_engine/image.rb
author_engine-0.4.0 lib/author_engine/image.rb
author_engine-0.3.1 lib/author_engine/image.rb
author_engine-0.3.0 lib/author_engine/image.rb