Class: ResourceManager
Instance Method Summary collapse
-
#initialize ⇒ ResourceManager
constructor
A new instance of ResourceManager.
- #load_actor_image(actor) ⇒ Object
- #load_animation_set(actor, action) ⇒ Object
-
#load_font(name, size) ⇒ Object
loads fonts from the fonts dir and caches them for later.
- #load_frame_set(actor, action) ⇒ Object
- #load_image(file_name, tileable = false) ⇒ Object
- #load_music(full_name) ⇒ Object
- #load_sound(full_name) ⇒ Object
- #load_svg(file_name) ⇒ Object
- #load_tile_set(actor, action) ⇒ Object
- #load_tiles(filename, tile_width, tile_height) ⇒ Object
Constructor Details
#initialize ⇒ ResourceManager
Returns a new instance of ResourceManager.
8 9 10 11 12 13 |
# File 'lib/gamebox/core/resource_manager.rb', line 8 def initialize @loaded_images = {} @loaded_fonts = {} @loaded_svgs = {} @window = wrapped_screen.screen end |
Instance Method Details
#load_actor_image(actor) ⇒ Object
15 16 17 18 19 |
# File 'lib/gamebox/core/resource_manager.rb', line 15 def load_actor_image(actor) # use pngs only for now image_name = actor.do_or_do_not(:image_name) || actor.actor_type return load_image("#{image_name}.png") end |
#load_animation_set(actor, action) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/gamebox/core/resource_manager.rb', line 21 def load_animation_set(actor, action) using_tileset = File.exist?("#{Gamebox.configuration.gfx_path}#{actor.actor_type}/#{action}.png") if using_tileset load_tile_set(actor, action) else load_frame_set(actor, action) end end |
#load_font(name, size) ⇒ Object
loads fonts from the fonts dir and caches them for later
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/gamebox/core/resource_manager.rb', line 115 def load_font(name, size) config = Gamebox.configuration fonts_path = config.fonts_path gb_fonts_path = config.gb_fonts_path @loaded_fonts[name] ||= {} return @loaded_fonts[name][size] if @loaded_fonts[name][size] begin if name =~ /^\// and File.exists?(name) font = Font.new(@window, name, size) @loaded_fonts[name][size] = font else full_name = fonts_path + name if File.exist? full_name font = Font.new(@window, full_name, size) @loaded_fonts[name][size] = font else full_name = gb_fonts_path + name font = Font.new(@window, full_name, size) @loaded_fonts[name][size] = font end end return font rescue Exception => ex log "Cannot load font #{full_name}:#{ex}" end return nil end |
#load_frame_set(actor, action) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/gamebox/core/resource_manager.rb', line 64 def load_frame_set(actor, action) # use pngs only for now frames = Dir.glob("#{Gamebox.configuration.gfx_path}#{actor.actor_type}/#{action}/*.png") action_imgs = [] frames = frames.sort_by {|f| File.basename(f).to_i } for frame in frames rel_path = frame.slice(Gamebox.configuration.gfx_path.size,frame.size) action_imgs << load_image(rel_path) end action_imgs end |
#load_image(file_name, tileable = false) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/gamebox/core/resource_manager.rb', line 78 def load_image(file_name, tileable=false) cached_img = @loaded_images[file_name] if cached_img.nil? begin full_name = Gamebox.configuration.gfx_path + file_name if ! File.exist? full_name #check global gamebox location full_name = Gamebox.configuration.gb_gfx_path + file_name end cached_img = Image.new(@window, full_name, tileable) rescue Exception => ex # log "Cannot load image #{file_name}", :warn end @loaded_images[file_name] = cached_img end cached_img end |
#load_music(full_name) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/gamebox/core/resource_manager.rb', line 96 def load_music(full_name) begin music = Song.new(@window, full_name) return music rescue Exception => ex log "Cannot load music " + full_name + " : " + ex, :warn end end |
#load_sound(full_name) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/gamebox/core/resource_manager.rb', line 105 def load_sound(full_name) begin sound = Sample.new(@window, full_name) return sound rescue Exception => ex log "Cannot load sound " + full_name + " : " + ex.inspect, :warn end end |
#load_svg(file_name) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/gamebox/core/resource_manager.rb', line 143 def load_svg(file_name) # TODO create LEVEL_PATH in environment cached_svg = @loaded_svgs[file_name] if cached_svg.nil? #cached_svg = SvgDocument.new(File.open(File.expand_path(DATA_PATH + "levels/" + file_name + ".svg"))) cached_svg = SvgDocument.new(File.open(DATA_PATH + "levels/" + file_name + ".svg")) @loaded_svgs[file_name] = cached_svg end cached_svg end |
#load_tile_set(actor, action) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/gamebox/core/resource_manager.rb', line 43 def load_tile_set(actor, action) tileset_name = "#{actor.actor_type}/#{action}.png" tileset = load_image tileset_name action_imgs = [] w = tileset.width h = tileset.height if h > w # down num_frames = h/w action_imgs = Image.load_tiles @window, Gamebox.configuration.gfx_path+tileset_name, -1, -num_frames, true else # right num_frames = w/h action_imgs = Image.load_tiles @window, Gamebox.configuration.gfx_path+tileset_name, -num_frames, -1, true end action_imgs end |
#load_tiles(filename, tile_width, tile_height) ⇒ Object
154 155 156 |
# File 'lib/gamebox/core/resource_manager.rb', line 154 def load_tiles(filename, tile_width, tile_height) Image.load_tiles @window, Gamebox.configuration.gfx_path+filename, tile_width, tile_height, true end |