TheoAllen

Anim Precache

Jan 9th, 2014
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.61 KB | None | 0 0
  1. #==============================================================================
  2. # ** Theo - Animations Precaching
  3. #------------------------------------------------------------------------------
  4. #  Untuk memproses animasi sebelum masuk game. Seperti loading dalam game.
  5. # Proses precaching dilakukan agar game tidak mengalami banyak lag. Namun
  6. # timpal baliknya adalah game akan memakan memori lebih banyak
  7. #==============================================================================
  8.  
  9.   AnimID_Start  = 111 # ID animasi start
  10.   AnimID_End    = 170 # ID animasi end
  11.  
  12. #==============================================================================
  13. # End of Config ~
  14. #==============================================================================
  15. class Sprite_Base < Sprite
  16.   def dispose_animation
  17.     if @ani_bitmap1
  18.       @@_reference_count[@ani_bitmap1] -= 1
  19.       if @@_reference_count[@ani_bitmap1] == 0
  20. #~         @ani_bitmap1.dispose
  21.       end
  22.     end
  23.     if @ani_bitmap2
  24.       @@_reference_count[@ani_bitmap2] -= 1
  25.       if @@_reference_count[@ani_bitmap2] == 0
  26. #~         @ani_bitmap2.dispose
  27.       end
  28.     end
  29.     if @ani_sprites
  30.       @ani_sprites.each {|sprite| sprite.dispose }
  31.       @ani_sprites = nil
  32.       @animation = nil
  33.     end
  34.     @ani_bitmap1 = nil
  35.     @ani_bitmap2 = nil
  36.   end
  37. end
  38.  
  39. class Sprite_Loading < Sprite
  40.   attr_accessor :rate
  41.  
  42.   def initialize
  43.     super
  44.     @rate = 0.0
  45.     self.bitmap = Bitmap.new(400, 10)
  46.     col1 = Color.new(128,128,128)
  47.     col2 = Color.new(255,255,255)
  48.     bitmap.gradient_fill_rect(bitmap.rect, col1, col2)
  49.     self.x = (Graphics.width - width) / 2
  50.     self.y = (Graphics.height - height) / 2
  51.     update_src_rect
  52.   end
  53.  
  54.   def update_src_rect
  55.     src_rect.width = bitmap.width * rate
  56.   end
  57.  
  58.   def update
  59.     super
  60.     update_src_rect
  61.   end
  62.  
  63.   def dispose
  64.     bitmap.dispose
  65.     super
  66.   end
  67.  
  68. end
  69.  
  70. class SPR_Progress < Sprite
  71.  
  72.   def initialize
  73.     super
  74.     @count = 0
  75.     self.bitmap = Bitmap.new(100,24)
  76.     self.x = (Graphics.width - width) / 2
  77.     self.y = (Graphics.height - height) / 2
  78.     self.y += 30
  79.     draw_progress
  80.   end
  81.  
  82.   def count=(count)
  83.     if @count != count
  84.       @count = count
  85.       draw_progress
  86.     end
  87.   end
  88.  
  89.   def dispose
  90.     bitmap.dispose
  91.     super
  92.   end
  93.  
  94.   def draw_progress
  95.     bitmap.clear
  96.     text = "#{@count}%"
  97.     bitmap.draw_text(bitmap.rect, text, 1)
  98.   end
  99.  
  100. end
  101.  
  102. $data_animations = load_data("Data/Animations.rvdata2")
  103. @anim_count =  AnimID_End - AnimID_Start
  104. @loaded = 0
  105. @loading = Sprite_Loading.new
  106. # ----------------------------------
  107. @text = Sprite.new
  108. @text.bitmap = Bitmap.new(400, 24)
  109. @text.x = (Graphics.width - @text.width) / 2
  110. @text.y = (Graphics.height - @text.height) / 2
  111. @text.y -= 40
  112. txt = "Precaching animation. Please wait ..."
  113. @text.bitmap.draw_text(@text.bitmap.rect, txt, 1)
  114. # ----------------------------------
  115. @progress = SPR_Progress.new
  116.  
  117. def loading(anim_id)
  118.   rate = @loaded / @anim_count.to_f
  119.   @loading.rate = rate
  120.   @loading.update
  121.   @progress.count = (rate * 100).to_i
  122.   data = $data_animations[anim_id]
  123.   Cache.animation(data.animation1_name, data.animation1_hue)
  124.   Cache.animation(data.animation2_name, data.animation2_hue)
  125.   @loaded += 1
  126.   Graphics.update
  127.   Input.update
  128. end
  129.  
  130. for anim_id in AnimID_Start..AnimID_End
  131.   loading(anim_id)
  132.   break if Input.press?(:C) || $TEST || $BTEST
  133. end
  134.  
  135. class << Cache
  136.   def size
  137.     @cache.keys.size
  138.   end
  139. end
  140.  
  141. puts Cache.size
  142.  
  143. @loading.dispose
  144. @text.bitmap.dispose
  145. @text.dispose
  146. @progress.dispose
  147. @loading = nil
  148. @text = nil
  149. @progress = nil
  150. @anim_count = nil
  151. @loaded = nil
Advertisement
Add Comment
Please, Sign In to add comment