Author Topic: Animation Delagger  (Read 1442 times)

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 20015
  • LV: 651
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Animation Delagger
« on: May 12, 2008, 06:38:30 PM »
This is NOT a script, it is but a mere random piece of code. In order to decrease lag during battle I changed the RPG::Sprite class in my game a little bit. If you want to use this, put it ABOVE ALL OF YOUR SCRIPTS!

What the script does: During animation 16 sprites are created (one for each cell), but mostly only a few are used. Empty sprites don't cause significant lag, but they do cause lag. This piece of code prevents the creation of unneeded sprites.

Use this code at your own risk, I cannot guarantee that it will work fine with your game as I took the code straight out of my own game. It is a mere edit of the "hidden" built-in script, I didn't make this script from scratch. In fact the edit is minor, but serving its purpose.

Code: [Select]
#==============================================================================
# RPG::Sprite
#==============================================================================

class RPG::Sprite
  
  alias init_delag initialize
  def initialize(viewport = nil)
    @_cells_max = 0
    @_loop_cells_max = 0
    init_delag(viewport)
  end
  
  def animation(animation, hit)
    dispose_animation
    @_animation = animation
    return if @_animation == nil
    @_animation_hit = hit
    @_animation_duration = @_animation.frame_max
    animation_name = @_animation.animation_name
    animation_hue = @_animation.animation_hue
    bitmap = RPG::Cache.animation(animation_name, animation_hue)
    if @@_reference_count.include?(bitmap)
      @@_reference_count[bitmap] += 1
    else
      @@_reference_count[bitmap] = 1
    end
    @_animation_sprites = []
    if @_animation.position != 3 || !@@_animations.include?(animation)
      @_cells_max = animation.max_cells
      (0..@_cells_max).each {|i|
          sprite = ::Sprite.new(self.viewport)
          sprite.bitmap = bitmap
          sprite.visible = false
          @_animation_sprites.push(sprite)}
      @@_animations.push(animation) unless @@_animations.include?(animation)
    end
    update_animation
  end
  
  def loop_animation(animation)
    return if animation == @_loop_animation
    dispose_loop_animation
    @_loop_animation = animation
    return if @_loop_animation == nil
    @_loop_animation_index = 0
    animation_name = @_loop_animation.animation_name
    animation_hue = @_loop_animation.animation_hue
    bitmap = RPG::Cache.animation(animation_name, animation_hue)
    if @@_reference_count.include?(bitmap)
      @@_reference_count[bitmap] += 1
    else
      @@_reference_count[bitmap] = 1
    end
    @_loop_animation_sprites = []
    @_loop_cells_max = animation.max_cells
    (0..@_loop_cells_max).each {|i|
        sprite = ::Sprite.new(self.viewport)
        sprite.bitmap = bitmap
        sprite.visible = false
        @_loop_animation_sprites.push(sprite)}
    update_loop_animation
  end

  def update_animation
    if @_animation_duration > 0
      frame_index = @_animation.frame_max - @_animation_duration
      cell_data = @_animation.frames[frame_index].cell_data
      position = @_animation.position
      animation_set_sprites(@_animation_sprites, cell_data, position, @_cells_max)
      @_animation.timings.each {|timing|
          if timing.frame == frame_index
            animation_process_timing(timing, @_animation_hit)
          end}
    else
      dispose_animation
    end
  end
  
  def update_loop_animation
    frame_index = @_loop_animation_index
    cell_data = @_loop_animation.frames[frame_index].cell_data
    position = @_loop_animation.position
    animation_set_sprites(@_loop_animation_sprites, cell_data, position, @_loop_cells_max)
    @_loop_animation.timings.each {|timing|
        if timing.frame == frame_index
          animation_process_timing(timing, true)
        end}
  end
  
  def animation_set_sprites(sprites, cell_data, position, max_cells)
    (0..max_cells).each {|i|
        sprite = sprites[i]
        pattern = cell_data[i, 0]
        if sprite == nil or pattern == nil or pattern == -1
          sprite.visible = false if sprite != nil
          next
        end
        sprite.visible = true
        sprite.src_rect.set(pattern % 5 * 192, pattern / 5 * 192, 192, 192)
        if position == 3
          if self.viewport != nil
            sprite.x = self.viewport.rect.width / 2
            sprite.y = self.viewport.rect.height - 160
          else
            sprite.x = 320
            sprite.y = 240
          end
        else
          sprite.x = self.x - self.ox + self.src_rect.width / 2
          sprite.y = self.y - self.oy + self.src_rect.height / 2
          sprite.y -= self.src_rect.height / 4 if position == 0
          sprite.y += self.src_rect.height / 4 if position == 2
        end
        sprite.x += cell_data[i, 1]
        sprite.y += cell_data[i, 2]
        sprite.z = 2000
        sprite.ox = 96
        sprite.oy = 96
        sprite.zoom_x = cell_data[i, 3] / 100.0
        sprite.zoom_y = cell_data[i, 3] / 100.0
        sprite.angle = cell_data[i, 4]
        sprite.mirror = (cell_data[i, 5] == 1)
        sprite.opacity = cell_data[i, 6] * self.opacity / 255.0
        sprite.blend_type = cell_data[i, 7]}
  end

  def x=(x)
    sx = x - self.x
    if sx != 0
      if @_animation_sprites != nil
        (0..@_cells_max).each {|i| @_animation_sprites[i].x += sx}
      end
      if @_loop_animation_sprites != nil
        (0..@_loop_cells_max).each {|i| @_loop_animation_sprites[i].x += sx}
      end
    end
    super
  end
  
  def y=(y)
    sy = y - self.y
    if sy != 0
      if @_animation_sprites != nil
        (0..@_cells_max).each {|i| @_animation_sprites[i].y += sy}
      end
      if @_loop_animation_sprites != nil
        (0..@_loop_cells_max).each {|i| @_loop_animation_sprites[i].y += sy}
      end
    end
    super
  end

end
« Last Edit: December 05, 2010, 12:20:13 PM by Blizzard »
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.