Class: Rubygame::MediaBag
- Inherits:
-
Object
- Object
- Rubygame::MediaBag
- Defined in:
- lib/rubygame/mediabag.rb
Overview
NOTE: MediaBag is DEPRECATED and will be removed in Rubygame 3.0! Use the NamedResource functionality of Music, Sound, and Surface instead.
NOTE: you must require ‘rubygame/mediabag’ manually to gain access to Rubygame::MediaBag. It is not imported with Rubygame by default!
A Hash-like class which will load and retain media files (images and sounds), so that the file can be loaded once, but used many times.
The first time a file is requested with the #[] method,that file will be loaded into memory. All subsequent requests for the same file will return a reference to the already-loaded version. Ideally, objects should not have to know whether or not the image has been loaded or not.
Constant Summary collapse
- @@image_ext =
%W{bmp gif jpg lbm pcx png pnm ppm pgm pbm tga tif xcf xpm}
- @@sound_ext =
%W{wav}
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return a reference to the stored value for key.
-
#force_load(filename) ⇒ Object
Forcibly (re)load the file, replacing the previous version in memory (if any).
-
#force_store(key, value) ⇒ Object
Forcibly store value as key, replacing the previous value (if any).
-
#initialize ⇒ MediaBag
constructor
A new instance of MediaBag.
-
#load(filename) ⇒ Object
Load the file, but only if it has not been previously loaded.
- #load_file(filename) ⇒ Object
- #load_image(filename) ⇒ Object
- #load_sound(filename) ⇒ Object
-
#store(key, value) ⇒ Object
Store value as key, but only if there is no previous value.
Constructor Details
#initialize ⇒ MediaBag
Returns a new instance of MediaBag.
48 49 50 |
# File 'lib/rubygame/mediabag.rb', line 48 def initialize() @media = Hash.new end |
Instance Method Details
#[](key) ⇒ Object
Return a reference to the stored value for key. If there is no value for key, automatically attempt to load key as a filename (guessing the file type based on its extension)
56 57 58 59 60 |
# File 'lib/rubygame/mediabag.rb', line 56 def [](key) @media[key] or load(key) rescue Rubygame::SDLError nil end |
#force_load(filename) ⇒ Object
Forcibly (re)load the file, replacing the previous version in memory (if any).
74 75 76 |
# File 'lib/rubygame/mediabag.rb', line 74 def force_load(filename) force_store( filename, load_file(filename) ) end |
#force_store(key, value) ⇒ Object
Forcibly store value as key, replacing the previous value (if any).
79 80 81 |
# File 'lib/rubygame/mediabag.rb', line 79 def force_store(key,value) @media[key] = value end |
#load(filename) ⇒ Object
Load the file, but only if it has not been previously loaded.
63 64 65 |
# File 'lib/rubygame/mediabag.rb', line 63 def load(filename) @media[filename] or store( filename, load_file(filename) ) end |
#load_file(filename) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rubygame/mediabag.rb', line 83 def load_file(filename) case File::extname(filename).downcase[1..-1] when *(@@image_ext) return load_image(filename) when *(@@sound_ext) return load_sound(filename) else raise(ArgumentError,"Unrecognized file extension `%s': %s"% [File::extname(filename), filename]) end end |
#load_image(filename) ⇒ Object
95 96 97 |
# File 'lib/rubygame/mediabag.rb', line 95 def load_image(filename) return Rubygame::Surface.load_image(filename) end |
#load_sound(filename) ⇒ Object
99 100 101 |
# File 'lib/rubygame/mediabag.rb', line 99 def load_sound(filename) return Rubygame::Mixer::Sample.load_audio(filename) end |
#store(key, value) ⇒ Object
Store value as key, but only if there is no previous value.
68 69 70 |
# File 'lib/rubygame/mediabag.rb', line 68 def store(key,value) @media[key] ||= value end |