#============================================================================== # ■ VX-RGSS2-29 文字列キャッシュ [Ver.1.0.0] by Claimh #------------------------------------------------------------------------------ # 文字列用のビットマップキャッシュです。 # Bitmapクラスに以下のキャッシュ対応のメソッドが追加されます。 # ●Bitmap#draw_cache_text # 文字描画用のメソッドです。draw_textとだいたい同じです。 # ●Bitmap#draw_cache_num # 数字描画専用のメソッドです。 # 数字を1桁ずつ描画することでキャッシュのヒット率が上がってます。 #============================================================================== #============================================================================== # ■ Cache #============================================================================== module Cache #-------------------------------------------------------------------------- # ● 文字Bitmapキャッシュ #-------------------------------------------------------------------------- def self.text_clear @text_cache = {} if @text_cache == nil @text_cache.clear GC.start end #-------------------------------------------------------------------------- # ● 文字Bitmapキャッシュ #-------------------------------------------------------------------------- def self.draw_text(width, height, text, font, color) @text_cache = {} if @text_cache == nil # 文字情報からキャッシュIndexを生成 cache_index = text + width.to_s + height.to_s + color.red.to_s + color.green.to_s + color.blue.to_s + color.alpha.to_s # キャッシュ上にBitmapがない場合はBitmapを生成し、キャッシュに入れる if @text_cache[cache_index].nil? or @text_cache[cache_index].disposed? bitmap = Bitmap.new(width, height) bitmap.font = font bitmap.draw_text(0, 0, width, height, text, 0) @text_cache[cache_index] = bitmap end return @text_cache[cache_index] end end #============================================================================== # ■ Bitmap 拡張ライブラリ #============================================================================== class Bitmap #---------------------------------------------------------------------------- # ● キャッシュから文字列bitmapをブロック転送 #---------------------------------------------------------------------------- def blt_cache(x, y, width, height, text, align) cache_bitmap = Cache.draw_text(width, height, text, self.font, self.font.color) # alignによる表示位置計算 x_plus = uncache_align_exe(width, text, align) # Bitmapのブロック転送 blt(x + x_plus , y , cache_bitmap, Rect.new(0, 0, width, height)) end #---------------------------------------------------------------------------- # ● 文字表示開始位置の決定(キャッシュする必要のないalignへの対応) # 0:左揃え 1:中央揃え 2:右揃え #---------------------------------------------------------------------------- def uncache_align_exe(width, text, align) recter = self.text_size(text) case align when 0 # 左揃え x_index = 0 when 1 # 中央揃え x_index = (width - recter.width) / 2 when 2 # 右揃え x_index = width - recter.width else p "Bitmap.align_exe : align指定ミス" exit end return x_index end #---------------------------------------------------------------------------- # ● テキスト表示(キャッシュ機構対応) #---------------------------------------------------------------------------- def draw_cache_text(x, y, width, height, text, align = 0) blt_cache(x, y, width, height, text, align) end #---------------------------------------------------------------------------- # ● 数字分割表示(キャッシュ効率UP対応) #---------------------------------------------------------------------------- def draw_cache_num(x, y, w, h, text, align = 2) box = text.to_s.split(//) x += w - text_size(text).width for i in 0...box.size w = text_size(box[i]).width draw_cache_text(x, y, w, h, box[i], 0) x += w end end end