Module: SFML::Assets

Defined in:
lib/sfml/assets.rb

Overview

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.

Defined Under Namespace

Classes: NotFound

Constant Summary collapse

TEXTURE_EXTS =

File extensions recognised as image / texture assets.

%w[.png .jpg .jpeg .bmp .gif .tga].freeze
SOUND_EXTS =

File extensions recognised as short-sound assets.

%w[.wav .ogg .flac .mp3].freeze
MUSIC_EXTS =

File extensions recognised as music (streamed) assets — same as SOUND_EXTS.

SOUND_EXTS
FONT_EXTS =

File extensions recognised as font assets.

%w[.ttf .otf].freeze

Class Method Summary collapse

Class Method Details

.add_search_path(path) ⇒ Object

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.



52
53
54
# 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

.clear ⇒ Object

Drop everything from the in-memory cache. New loads will re-read from disk and recreate textures, fonts, sound buffers.



58
59
60
61
# File 'lib/sfml/assets.rb', line 58

def clear
  @cache&.clear
  self
end

.font(name) ⇒ Object

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.



66
67
68
# File 'lib/sfml/assets.rb', line 66

def font(name)
  cache[[:font, name]] ||= load_font(name)
end

.music(name) ⇒ Object

Load a fresh SFML::Music by name. Not cached — each caller gets its own playback position.



82
83
84
85
86
# 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

.root=(path) ⇒ Object

Convenience for "use exactly this one directory" — shorthand for search_paths = [path].



45
46
47
# File 'lib/sfml/assets.rb', line 45

def root=(path)
  self.search_paths = [path]
end

.search_paths ⇒ Object

Current list of directories scanned by #font, #texture, etc. Defaults to <dir of $0>/assets/. Mutate via #root= / #add_search_path / #search_paths=.



32
33
34
# File 'lib/sfml/assets.rb', line 32

def search_paths
  @search_paths ||= [default_root]
end

.search_paths=(paths) ⇒ Object

Replace the entire search-path list. Resets the cache so the next load re-resolves from the new locations.



38
39
40
41
# File 'lib/sfml/assets.rb', line 38

def search_paths=(paths)
  @search_paths = Array(paths).map { |p| File.expand_path(p) }
  @cache&.clear
end

.sound(name) ⇒ Object

Load (or fetch from cache) an SFML::SoundBuffer by name.



76
77
78
# File 'lib/sfml/assets.rb', line 76

def sound(name)
  cache[[:sound, name]] ||= load_sound_buffer(name)
end

.texture(name) ⇒ Object

Load (or fetch from cache) an SFML::Texture by name.



71
72
73
# File 'lib/sfml/assets.rb', line 71

def texture(name)
  cache[[:texture, name]] ||= load_texture(name)
end