Module: Tea::ImageSaving

Included in:
Bitmap, Screen
Defined in:
lib/tea/mix_image_saving.rb

Overview

This mixin allows SDL surface-backed classes to be saved as image files.

To use, include the mixin and implement an image_saving_buffer method:

include ImageSaving
def image_saving_buffer
  @my_sdl_buffer
end

Instance Method Summary collapse

Instance Method Details

#save(path) ⇒ Object

Save the object as an image file at the given path.

The format is determined by the extension at the end of the path. “.bmp” will save it as Windows Bitmap image.

May raise Tea::Error if the desired image file format can’t be determined, or isn’t supported.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tea/mix_image_saving.rb', line 28

def save(path)
  extension_match = /\..+$/.match(path)

  if extension_match
    ext = extension_match[0]
    case ext
    when '.bmp'
      image_saving_buffer.save_bmp path
    when '.png'
      image_saving_cheat_save path
    else
      raise Tea::Error, "can't determine image format '#{ext}' for saving", caller
    end
  else
    raise Tea::Error, "can't determine desired image file format for #{path}", caller
  end

rescue SDL::Error => e
  raise Tea::Error, e.message, e.backtrace
end