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 = [] ret = []
retstr = "" retstr = ""
for x in csplit for x in csplit
if x=="." if x == ".."
elsif x==".." if pos >= 0
ret.delete_at(pos) if pos>=0 ret.delete_at(pos)
pos -= 1 pos -= 1
else end
elsif x != "."
ret.push(x) ret.push(x)
pos += 1 pos += 1
end end
@@ -38,6 +39,7 @@ end
##################################################################### #####################################################################
# TODO: Delete this class in Ruby 2+.
class WeakRef class WeakRef
@@id_map = {} @@id_map = {}
@@id_rev_map = {} @@id_rev_map = {}
@@ -91,6 +93,7 @@ end
# TODO: Delete this class in Ruby 2+.
class WeakHashtable class WeakHashtable
include Enumerable include Enumerable
@@ -135,57 +138,13 @@ class WeakHashtable
end end
def []=(key, o) def []=(key, o)
if o!=nil o = WeakRef.new(o) if o != nil
o=WeakRef.new(o)
end
@hash[key] = o @hash[key] = o
end end
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 RPG
module Cache module Cache
def self.load_bitmap(folder_name, filename, hue = 0) def self.load_bitmap(folder_name, filename, hue = 0)
@@ -259,9 +218,6 @@ end
class BitmapWrapper < Bitmap class BitmapWrapper < Bitmap
attr_reader :refcount attr_reader :refcount
@@disposedBitmaps={}
@@keys={}
def dispose def dispose
return if self.disposed? return if self.disposed?
@refcount -= 1 @refcount -= 1
@@ -291,7 +247,9 @@ end
module BitmapCache module BitmapCache
# TODO: Replace this with the commented line in Ruby 2+.
@cache = WeakHashtable.new @cache = WeakHashtable.new
# @cache = ObjectSpace::WeakMap.new
def self.fromCache(i) def self.fromCache(i)
return nil if !@cache.include?(i) return nil if !@cache.include?(i)
@@ -321,15 +279,17 @@ module BitmapCache
def self.load_bitmap(path, hue = 0, failsafe = false) def self.load_bitmap(path, hue = 0, failsafe = false)
cached = true 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) objPath = fromCache(path)
if !objPath if !objPath
# TODO: Delete this in Ruby 2+.
@cleancounter = ((@cleancounter || 0) + 1) % 10 @cleancounter = ((@cleancounter || 0) + 1) % 10
if @cleancounter == 0 if @cleancounter == 0
for i in @cache.keys for i in @cache.keys
@cache.delete(i) if !fromCache(i) @cache.delete(i) if !fromCache(i)
end end
end end
# TODO: Up to here.
begin begin
bm = BitmapWrapper.new(path) bm = BitmapWrapper.new(path)
rescue Hangup rescue Hangup
@@ -421,10 +381,10 @@ module BitmapCache
key = [filename, tile_id, hue] key = [filename, tile_id, hue]
objKey = fromCache(key) objKey = fromCache(key)
if !objKey if !objKey
bitmap=BitmapWrapper.new(32, 32) bitmap = BitmapWrapper.new(Game_Map::TILE_WIDTH, Game_Map::TILE_HEIGHT)
x = (tile_id - 384) % 8 * 32 x = (tile_id - 384) % 8 * 32
y = (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) tileset = yield(filename)
bitmap.blt(0, 0, tileset, rect) bitmap.blt(0, 0, tileset, rect)
tileset.dispose tileset.dispose
@@ -442,7 +402,9 @@ module BitmapCache
end end
def self.clear def self.clear
@cache = {} # TODO: Replace this with the commented line in Ruby 2+.
@cache.clear
# @cache = ObjectSpace::WeakMap.new
GC.start GC.start
end end
end end