| Module | Joyau::BufferLibrary |
| In: |
ruby/site_ruby/joyau/buffer_library.rb
|
The BufferLibrary allows to register your own loader for graphical ressources (a.k.a Buffer). This allows to create a library of red buffers easily, whose sprites could be loaded simply with:
Joyau::Sprite.from_res_name("red:my_picture.png")
The matching regex being simply /^red:(.+)/.
Another example :
module MyLoader
include Joyau::BufferLibrary
@buffers = {}
@regex = /my_loader:(.+)/
def [](res_name)
@buffers[res_name] || create_buffer(res_name)
end
def create_buffer(res_name)
filename = nil
if res_name =~ regex
filename = $1
else
raise Joyau::InvalidRessourceName, ...
end
@buffers[res_name] = Joyau::Buffer[filename]
# modify the buffer.
return @buffers[res_name]
end
register_loader @regex
end
And now you can simply use your loader :
Joyau::Sprite.from_res_name("my_loader:picture.png")
Returns a buffer matching the ressource name. It uses either the loader whose regex match with the ressource name, or the built-in loader.
# File ruby/site_ruby/joyau/buffer_library.rb, line 75 def [](res_name) @@regexps.each do |regex, loader| if res_name =~ regex return loader[res_name] end end # If the string didn't match any of the previous regex, # we use the built-in loader. return Joyau::Buffer[res_name, false] end
Register an image loader, which must respond to []. Each ressource name matching with the regex will be given to that method, which should return a buffer.
# File ruby/site_ruby/joyau/buffer_library.rb, line 64 def register_loader(mod, regex) if mod.respond_to? :[] @@regexps[regex] = mod else raise InvalidLoader, "#{mod} is not a valid image Loader." end end