Module: RPG::Cache

Defined in:
lib/rpg/cache.rb

Overview

A module that loads each of RPGXP's graphic formats, creates a Bitmap object, and retains it.

To speed up load times and conserve memory, this module holds the created Bitmap object in an internal hash, allowing the program to return preexisting objects when the same bitmap is requested again.

This feature means you must take care not to use Bitmap#dispose to free the bitmaps specified in the origins of other sprites. If those objects have already been freed when a bitmap is requested, the objects will automatically be recreated, so it is safe to free the object if you are certain it is not referenced elsewhere. Bitmaps consume a large amount of memory, so make it a practice to free bitmaps you use only rarely.

If the specified file name is an empty string, the module will create and return a 32 x 32 pixel bitmap. This ensures compatibility with RPGXP's “(None)” designation.

Class Method Summary collapse

Class Method Details

.animation(filename, hue) ⇒ Bitmap

Gets an animation graphic. Use hue to adjust its hue values.


48
49
50
# File 'lib/rpg/cache.rb', line 48

def self.animation(filename, hue)
  self.load_bitmap("Graphics/Animations/", filename, hue)
end

.autotile(filename) ⇒ Bitmap

Gets an autotile graphic.


54
55
56
# File 'lib/rpg/cache.rb', line 54

def self.autotile(filename)
  self.load_bitmap("Graphics/Autotiles/", filename)
end

.battleback(filename) ⇒ Bitmap

Gets a battle background graphic.


60
61
62
# File 'lib/rpg/cache.rb', line 60

def self.battleback(filename)
  self.load_bitmap("Graphics/Battlebacks/", filename)
end

.battler(filename, hue) ⇒ Bitmap

Gets a battler graphic. Use hue to adjust its hue values.


66
67
68
# File 'lib/rpg/cache.rb', line 66

def self.battler(filename, hue)
  self.load_bitmap("Graphics/Battlers/", filename, hue)
end

.character(filename, hue) ⇒ Bitmap

Gets a character graphic. Use hue to adjust its hue values.


72
73
74
# File 'lib/rpg/cache.rb', line 72

def self.character(filename, hue)
  self.load_bitmap("Graphics/Characters/", filename, hue)
end

.clearObject

Empties the cache.


145
146
147
148
# File 'lib/rpg/cache.rb', line 145

def self.clear
  @cache = {}
  GC.start
end

.fog(filename, hue) ⇒ Bitmap

Gets a fog graphic. Use hue to adjust its hue values.


78
79
80
# File 'lib/rpg/cache.rb', line 78

def self.fog(filename, hue)
  self.load_bitmap("Graphics/Fogs/", filename, hue)
end

.gameover(filename) ⇒ Bitmap

Gets a “Game Over” graphic.


84
85
86
# File 'lib/rpg/cache.rb', line 84

def self.gameover(filename)
  self.load_bitmap("Graphics/Gameovers/", filename)
end

.icon(filename) ⇒ Bitmap

Gets an icon graphic.


90
91
92
# File 'lib/rpg/cache.rb', line 90

def self.icon(filename)
  self.load_bitmap("Graphics/Icons/", filename)
end

.load_bitmap(folder_name, filename, hue = 0) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rpg/cache.rb', line 25

def self.load_bitmap(folder_name, filename, hue = 0)
  path = folder_name + filename
  if not @cache.include?(path) or @cache[path].disposed?
    if filename != ""
      @cache[path] = Bitmap.new(path)
    else
      @cache[path] = Bitmap.new(32, 32)
    end
  end
  if hue == 0
    @cache[path]
  else
    key = [path, hue]
    if not @cache.include?(key) or @cache[key].disposed?
      @cache[key] = @cache[path].clone
      @cache[key].hue_change(hue)
    end
    @cache[key]
  end
end

.panorama(filename, hue) ⇒ Bitmap

Gets a panorama graphic. Use hue to adjust its hue values.


96
97
98
# File 'lib/rpg/cache.rb', line 96

def self.panorama(filename, hue)
  self.load_bitmap("Graphics/Panoramas/", filename, hue)
end

.picture(filename) ⇒ Bitmap

Gets a picture graphic.


102
103
104
# File 'lib/rpg/cache.rb', line 102

def self.picture(filename)
  self.load_bitmap("Graphics/Pictures/", filename)
end

.tile(filename, tile_id, hue) ⇒ Bitmap

Gets only a specified tile from a tileset. Use tile_id to specify the ID of the tile to retrieve and hue to adjust its hue values.

Used when a tile is specified in an event graphic (Event::Page::Graphic).


131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rpg/cache.rb', line 131

def self.tile(filename, tile_id, hue)
  key = [filename, tile_id, hue]
  if not @cache.include?(key) or @cache[key].disposed?
    @cache[key] = Bitmap.new(32, 32)
    x = (tile_id - 384) % 8 * 32
    y = (tile_id - 384) / 8 * 32
    rect = Rect.new(x, y, 32, 32)
    @cache[key].blt(0, 0, self.tileset(filename), rect)
    @cache[key].hue_change(hue)
  end
  @cache[key]
end

.tileset(filename) ⇒ Bitmap

Gets a tileset graphic.


108
109
110
# File 'lib/rpg/cache.rb', line 108

def self.tileset(filename)
  self.load_bitmap("Graphics/Tilesets/", filename)
end

.title(filename) ⇒ Bitmap

Gets a title graphic.


114
115
116
# File 'lib/rpg/cache.rb', line 114

def self.title(filename)
  self.load_bitmap("Graphics/Titles/", filename)
end

.windowskin(filename) ⇒ Bitmap

Gets a window skin graphic.


120
121
122
# File 'lib/rpg/cache.rb', line 120

def self.windowskin(filename)
  self.load_bitmap("Graphics/Windowskins/", filename)
end