Tweaks to BitmapCache

This commit is contained in:
Maruno17
2021-02-08 19:25:54 +00:00
parent 0c7aab530d
commit 8b5b9d5fc4

View File

@@ -19,11 +19,12 @@ def canonicalize(c)
ret = []
retstr = ""
for x in csplit
if x=="."
elsif x==".."
ret.delete_at(pos) if pos>=0
if x == ".."
if pos >= 0
ret.delete_at(pos)
pos -= 1
else
end
elsif x != "."
ret.push(x)
pos += 1
end
@@ -38,6 +39,7 @@ end
#####################################################################
# TODO: Delete this class in Ruby 2+.
class WeakRef
@@id_map = {}
@@id_rev_map = {}
@@ -91,6 +93,7 @@ end
# TODO: Delete this class in Ruby 2+.
class WeakHashtable
include Enumerable
@@ -135,57 +138,13 @@ class WeakHashtable
end
def []=(key, o)
if o!=nil
o=WeakRef.new(o)
end
o = WeakRef.new(o) if o != nil
@hash[key] = o
end
end
# Cache from RPG Maker VX library
module Cache
def self.system(x,hue=0)
BitmapCache.load_bitmap("Graphics/System/"+x,hue, true)
end
def self.character(x,hue=0)
BitmapCache.load_bitmap("Graphics/Characters/"+x,hue, true)
end
def self.picture(x,hue=0)
BitmapCache.load_bitmap("Graphics/Pictures/"+x,hue, true)
end
def self.animation(x,hue=0)
BitmapCache.load_bitmap("Graphics/Animations/"+x,hue, true)
end
def self.battler(x,hue=0)
BitmapCache.load_bitmap("Graphics/Battlers/"+x,hue, true)
end
def self.face(x,hue=0)
BitmapCache.load_bitmap("Graphics/Faces/"+x,hue, true)
end
def self.parallax(x,hue=0)
BitmapCache.load_bitmap("Graphics/Parallaxes/"+x,hue, true)
end
def self.clear
BitmapCache.clear()
end
def self.load_bitmap(dir,name,hue=0)
BitmapCache.load_bitmap(dir+name,hue, true)
end
end
# RPG::Cache from RPG Maker XP library
module RPG
module Cache
def self.load_bitmap(folder_name, filename, hue = 0)
@@ -259,9 +218,6 @@ end
class BitmapWrapper < Bitmap
attr_reader :refcount
@@disposedBitmaps={}
@@keys={}
def dispose
return if self.disposed?
@refcount -= 1
@@ -291,7 +247,9 @@ end
module BitmapCache
# TODO: Replace this with the commented line in Ruby 2+.
@cache = WeakHashtable.new
# @cache = ObjectSpace::WeakMap.new
def self.fromCache(i)
return nil if !@cache.include?(i)
@@ -321,15 +279,17 @@ module BitmapCache
def self.load_bitmap(path, hue = 0, failsafe = false)
cached = true
path = canonicalize(path)
path = -canonicalize(path) # Creates a frozen string from the path, to ensure identical paths are treated as identical.
objPath = fromCache(path)
if !objPath
# TODO: Delete this in Ruby 2+.
@cleancounter = ((@cleancounter || 0) + 1) % 10
if @cleancounter == 0
for i in @cache.keys
@cache.delete(i) if !fromCache(i)
end
end
# TODO: Up to here.
begin
bm = BitmapWrapper.new(path)
rescue Hangup
@@ -421,10 +381,10 @@ module BitmapCache
key = [filename, tile_id, hue]
objKey = fromCache(key)
if !objKey
bitmap=BitmapWrapper.new(32, 32)
bitmap = BitmapWrapper.new(Game_Map::TILE_WIDTH, Game_Map::TILE_HEIGHT)
x = (tile_id - 384) % 8 * 32
y = (tile_id - 384) / 8 * 32
rect = Rect.new(x, y, 32, 32)
rect = Rect.new(x, y, Game_Map::TILE_WIDTH, Game_Map::TILE_HEIGHT)
tileset = yield(filename)
bitmap.blt(0, 0, tileset, rect)
tileset.dispose
@@ -442,7 +402,9 @@ module BitmapCache
end
def self.clear
@cache = {}
# TODO: Replace this with the commented line in Ruby 2+.
@cache.clear
# @cache = ObjectSpace::WeakMap.new
GC.start
end
end