### Home Classes Methods

### In Files

- lib/rubygame/mediabag.rb

### Parent

Object

### Methods

- ::new
- #[]
- #force_load
- #force_store
- #load
- #load_file
- #load_image
- #load_sound
- #store

### Files

- CREDITS
- LICENSE
- NEWS
- README
- ROADMAP
- custom_sdl_load_paths.rdoc
- getting_started.rdoc
- keyboard_symbols.rdoc
- macosx_install.rdoc
- managing_framerate.rdoc
- surface_palettes.rdoc
- windows_install.rdoc

### Class/Module Index

Quicksearch

- Rubygame
- Rubygame::ActiveEvent
- Rubygame::Clock
- Rubygame::Color
- Rubygame::Color::ColorBase
- Rubygame::Color::ColorHSL
- Rubygame::Color::ColorHSV
- Rubygame::Color::ColorRGB
- Rubygame::Color::Palette
- Rubygame::Event
- Rubygame::EventActions
- Rubygame::EventActions::BlockAction
- Rubygame::EventActions::MethodAction
- Rubygame::EventActions::MultiAction
- Rubygame::EventHandler
- Rubygame::EventHandler::HasEventHandler
- Rubygame::EventHook
- Rubygame::EventQueue
- Rubygame::EventTriggers
- Rubygame::EventTriggers::AndTrigger
- Rubygame::EventTriggers::AttrTrigger
- Rubygame::EventTriggers::BlockTrigger
- Rubygame::EventTriggers::InstanceOfTrigger
- Rubygame::EventTriggers::KeyPressTrigger
- Rubygame::EventTriggers::KeyReleaseTrigger
- Rubygame::EventTriggers::KindOfTrigger
- Rubygame::EventTriggers::MouseMoveTrigger
- Rubygame::EventTriggers::MousePressTrigger
- Rubygame::EventTriggers::MouseReleaseTrigger
- Rubygame::EventTriggers::OrTrigger
- Rubygame::EventTriggers::TickTrigger
- Rubygame::EventTriggers::YesTrigger
- Rubygame::Events
- Rubygame::Events::ClockTicked
- Rubygame::Events::InputFocusGained
- Rubygame::Events::InputFocusLost
- Rubygame::Events::JoystickAxisMoved
- Rubygame::Events::JoystickBallMoved
- Rubygame::Events::JoystickButtonEvent
- Rubygame::Events::JoystickButtonPressed
- Rubygame::Events::JoystickButtonReleased
- Rubygame::Events::JoystickHatMoved
- Rubygame::Events::KeyPressed
- Rubygame::Events::KeyReleased
- Rubygame::Events::KeyboardEvent
- Rubygame::Events::MouseButtonEvent
- Rubygame::Events::MouseFocusGained
- Rubygame::Events::MouseFocusLost
- Rubygame::Events::MouseMoved
- Rubygame::Events::MousePressed
- Rubygame::Events::MouseReleased
- Rubygame::Events::QuitRequested
- Rubygame::Events::WindowExposed
- Rubygame::Events::WindowMinimized
- Rubygame::Events::WindowResized
- Rubygame::Events::WindowUnminimized
- Rubygame::ExposeEvent
- Rubygame::Ftor
- Rubygame::GL
- Rubygame::Hotspot
- Rubygame::JoyAxisEvent
- Rubygame::JoyBallEvent
- Rubygame::JoyDownEvent
- Rubygame::JoyHatEvent
- Rubygame::JoyUpEvent
- Rubygame::Joystick
- Rubygame::Key
- Rubygame::KeyDownEvent
- Rubygame::KeyUpEvent
- Rubygame::MailQueue
- Rubygame::MediaBag
- Rubygame::Mixer
- Rubygame::Mixer::Music
- Rubygame::Mixer::Sample
- Rubygame::Mouse
- Rubygame::MouseDownEvent
- Rubygame::MouseMotionEvent
- Rubygame::MouseUpEvent
- Rubygame::Music
- Rubygame::NamedResource
- Rubygame::NamedResource::NamedResourceClassMethods
- Rubygame::QuitEvent
- Rubygame::Rect
- Rubygame::ResizeEvent
- Rubygame::SDLError
- Rubygame::SFont
- Rubygame::Screen
- Rubygame::Sound
- Rubygame::Sprites
- Rubygame::Sprites::DepthSortGroup
- Rubygame::Sprites::Group
- Rubygame::Sprites::LimitGroup
- Rubygame::Sprites::Sprite
- Rubygame::Sprites::UpdateGroup
- Rubygame::Surface
- Rubygame::TTF

No matching classes.

# Rubygame::MediaBag

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.

### Public Class Methods

new() click to toggle source

    # File lib/rubygame/mediabag.rb, line 48
    def initialize()
      @media = Hash.new
    end

### Public Instance Methods

[](key) click to toggle source

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)

    # File lib/rubygame/mediabag.rb, line 56
    def [](key)
      @media[key] or load(key)
    rescue Rubygame::SDLError
      nil
    end

force_load(filename) click to toggle source

Forcibly (re)load the file, replacing the previous version in memory (if any).

    # File lib/rubygame/mediabag.rb, line 74
    def force_load(filename)
      force_store( filename, load_file(filename) )
    end

force_store(key,value) click to toggle source

Forcibly store value as key, replacing the previous value (if any).

    # File lib/rubygame/mediabag.rb, line 79
    def force_store(key,value)
      @media[key] = value
    end

load(filename) click to toggle source

Load the file, but only if it has not been previously loaded.

    # File lib/rubygame/mediabag.rb, line 63
    def load(filename)
      @media[filename] or store( filename, load_file(filename) )
    end

load_file(filename) click to toggle source

    # 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) click to toggle source

    # File lib/rubygame/mediabag.rb, line 95
    def load_image(filename)
      return Rubygame::Surface.load_image(filename)
    end

load_sound(filename) click to toggle source

    # File lib/rubygame/mediabag.rb, line 99
    def load_sound(filename)
      return Rubygame::Mixer::Sample.load_audio(filename)
    end

store(key,value) click to toggle source

Store value as key, but only if there is no previous value.

    # File lib/rubygame/mediabag.rb, line 68
    def store(key,value)
      @media[key] ||= value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.