module SFML::Assets
Cached, search-path-driven asset loader. Use it to avoid repeating file paths and to load each asset exactly once.
font = SFML::Assets.font(“DejaVuSans”) tex = SFML::Assets.texture(“hero”) # finds hero.png/.jpg/.bmp blip = SFML::Assets.sound(“blip”) # finds blip.wav/.ogg/… music = SFML::Assets.music(“track”) # NOT cached (stateful)
By default the search root is <dir of $0>/assets/. Override:
SFML::Assets.root = File.expand_path(“data”, dir) SFML::Assets.add_search_path(“/usr/local/share/mygame”)
Cache survives until you call .clear or the process exits.
Constants
- FONT_EXTS
-
File extensions recognised as font assets.
- MUSIC_EXTS
-
File extensions recognised as music (streamed) assets — same as
SOUND_EXTS. - SOUND_EXTS
-
File extensions recognised as short-sound assets.
- TEXTURE_EXTS
-
File extensions recognised as image / texture assets.
Public Class Methods
Source
# File lib/sfml/assets.rb, line 52 def add_search_path(path) search_paths << File.expand_path(path) unless search_paths.include?(File.expand_path(path)) end
Append a directory to the end of search_paths unless it’s already present. Useful for adding mod / DLC directories without nuking the default search root.
Source
# File lib/sfml/assets.rb, line 58 def clear @cache&.clear self end
Drop everything from the in-memory cache. New loads will re-read from disk and recreate textures, fonts, sound buffers.
Source
# File lib/sfml/assets.rb, line 66 def font(name) cache[[:font, name]] ||= load_font(name) end
Load (or fetch from cache) an SFML::Font by name — searches search_paths for any of FONT_EXTS, falls back to system fonts via Font.find.
Source
# File lib/sfml/assets.rb, line 82 def music(name) path = locate(name, MUSIC_EXTS) or raise NotFound, "Music #{name.inspect} not found. Searched: #{search_paths.inspect}" Music.load(path) end
Load a fresh SFML::Music by name. Not cached — each caller gets its own playback position.
Source
# File lib/sfml/assets.rb, line 45 def root=(path) self.search_paths = [path] end
Convenience for “use exactly this one directory” — shorthand for search_paths = [path].
Source
# File lib/sfml/assets.rb, line 32 def search_paths @search_paths ||= [default_root] end
Current list of directories scanned by font, texture, etc. Defaults to <dir of $0>/assets/. Mutate via root= / add_search_path / search_paths=.
Source
# File lib/sfml/assets.rb, line 38 def search_paths=(paths) @search_paths = Array(paths).map { |p| File.expand_path(p) } @cache&.clear end
Replace the entire search-path list. Resets the cache so the next load re-resolves from the new locations.
Source
# File lib/sfml/assets.rb, line 76 def sound(name) cache[[:sound, name]] ||= load_sound_buffer(name) end
Load (or fetch from cache) an SFML::SoundBuffer by name.
Source
# File lib/sfml/assets.rb, line 71 def texture(name) cache[[:texture, name]] ||= load_texture(name) end
Load (or fetch from cache) an SFML::Texture by name.