Advertisement
Guest User

RGSS3 Plane v1.2.1

a guest
Sep 10th, 2014
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.34 KB | None | 0 0
  1. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  2. # RGSS3 Plane v1.2.1
  3. # FenixFyreX
  4. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  5. # This is a rewrite of RGSS3's Plane class, mainly for use with custom resolution
  6. # alterations of RPG Maker VXAce. This allows Plane to function properly on
  7. # larger resolutions than the hard-coded 640px x 480px in the dll.
  8. #
  9. # It also correctly displays ox and oy offset and such, just like the original.
  10. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  11. # CHANGELOG
  12. #   - Added in caching of plane bitmaps, to stop the lag from persisting.
  13. #   - Fixed potential bug where when the viewport was set, the bitmap would
  14. #     retile incorrectly.
  15. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  16. # To remove any artifacts / conflicts with the original, we alias it then remove
  17. # the original tie.
  18. RGSS3Plane = Plane
  19. Object.send(:remove_const, :Plane)
  20.  
  21. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  22. # Bitmap
  23. #   Saves a bitmap's name, for future reference.
  24. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  25. class Bitmap
  26.     alias fyx_initialize_save_name initialize
  27.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  28.   # initialize
  29.   #   Instantiate a bitmap's name, if given one.
  30.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  31.     def initialize(*argv, &argb)
  32.     @name = ''
  33.         if name = argv.find {|arg| arg.is_a?(String) }
  34.             @name = name
  35.         end
  36.         fyx_initialize_save_name(*argv, &argb)
  37.     end
  38.   attr_reader :name
  39. end
  40.  
  41. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  42. # Cache
  43. #   Add in Plane caching, to speed up processing at the slight cost of memory.
  44. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  45. module Cache
  46.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  47.   # Cache::plane_cache
  48.   #   Convenience method, to not have to type it out in the below methods.
  49.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  50.   def self.plane_cache
  51.     @plane_cache ||= {}
  52.   end
  53.  
  54.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  55.   # Cache::plane
  56.   #   Get a cached plane bitmap.
  57.   #   key : Object  ( most likely an Array e.g. [Rect, String] )
  58.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  59.     def self.plane(key)
  60.     return plane_cache[key]
  61.   end
  62.  
  63.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  64.   # Cache::add_plane
  65.   #   Add a tiled plane bitmap to the cache.
  66.   #   key : Object ( see above )
  67.   #   bmp : Bitmap
  68.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  69.   def self.add_plane(key, bmp)
  70.     plane_cache[key] = bmp
  71.   end
  72.  
  73.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  74.   # Cache::has_plane?
  75.   #   Check for a cached plane bitmap.
  76.   #   key : Object ( see above )
  77.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  78.   def self.has_plane?(key)
  79.     plane_cache[key].is_a?(Bitmap)
  80.   end
  81.  
  82.   class << self; alias clear_b4_fyx_plane_cache clear; end
  83.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  84.   # Cache::clear
  85.   #   See original documentation.
  86.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  87.   def self.clear
  88.     plane_cache.each_value {|v| v.dispose unless v.nil? || v.disposed? }
  89.     plane_cache.clear
  90.     clear_b4_fyx_plane_cache
  91.   end
  92. end
  93.  
  94. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  95. # Plane
  96. #   Tiles a bitmap across either the window rect, or a given viewport's rect.
  97. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  98. class Plane
  99.  
  100.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  101.   # initialize
  102.   #   Setup an allocated instance of Plane.
  103.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  104.   def initialize(v = nil)
  105.     @sprite = Sprite.new(v)
  106.     @bitmap = nil
  107.   end
  108.  
  109.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  110.   # dispose
  111.   #   Free an instance of Plane.
  112.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  113.   def dispose
  114.     b1 = @sprite.nil? || @sprite.disposed?
  115.     b2 = b1 ? false : @sprite.bitmap.nil? || @sprite.bitmap.disposed?
  116.     @sprite.bitmap.dispose if b2
  117.     @sprite.dispose if b1
  118.     return nil
  119.   end
  120.  
  121.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  122.   # disposed?
  123.   #   Check whether this instance of Plane has been freed.
  124.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  125.   def disposed?
  126.     @sprite.nil? || @sprite.disposed?
  127.   end
  128.  
  129.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  130.   # ox=
  131.   #   Set the offset x of this instance of Plane.
  132.   #   val : Integer
  133.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  134.   def ox=(val)
  135.     @sprite.ox = (val % (@bitmap.nil? ? 1 : @bitmap.width))
  136.   end
  137.  
  138.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  139.   # oy=
  140.   #   Set the offset y of this instance of Plane.
  141.   #   val : Integer
  142.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  143.   def oy=(val)
  144.     @sprite.oy = (val % (@bitmap.nil? ? 1 : @bitmap.height))
  145.   end
  146.  
  147.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  148.   # bitmap
  149.   #   Get the tile bitmap of this instance of Plane.
  150.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  151.   def bitmap
  152.     @bitmap
  153.   end
  154.  
  155.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  156.   # viewport=
  157.   #   Set the viewport, and refresh if the vrect has changed.
  158.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  159.   def viewport=(v)
  160.     r = v.nil? ? Rect.new(0, 0, Graphics.width, Graphics.height) : v.rect
  161.     b = r != vrect
  162.     ret = @sprite.viewport = v
  163.     self.bitmap = @bitmap if b
  164.     return ret
  165.   end
  166.  
  167.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  168.   # bitmap=
  169.   #   Set the tile bitmap of this instance of Plane.
  170.   #   bmp : Bitmap
  171.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  172.   def bitmap=(bmp)
  173.     w, h = vrect.width, vrect.height
  174.    
  175.     nw = bmp.width <= 100 ? 2 : 3
  176.     nh = bmp.height <= 100 ? 2 : 3
  177.    
  178.     dx = [(w / bmp.width).ceil, 1].max * nw
  179.     dy = [(h / bmp.height).ceil, 1].max * nh
  180.  
  181.     bw = dx * bmp.width
  182.     bh = dy * bmp.height
  183.  
  184.     @bitmap = bmp
  185.     key = [vrect.clone, bmp.name]
  186.     if Cache.has_plane?(key)
  187.       @sprite.bitmap = Cache.plane(key)
  188.     else
  189.       @sprite.bitmap = Bitmap.new(bw, bh)
  190.      
  191.       dx.times do |x|
  192.         dy.times do |y|
  193.           @sprite.bitmap.blt(x * bmp.width, y * bmp.height, @bitmap, @bitmap.rect)
  194.         end
  195.       end
  196.       Cache.add_plane(key, @sprite.bitmap)
  197.     end
  198.   end
  199.  
  200.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  201.   # method_missing
  202.   #   Here we let any methods not found in this class be redirected to our
  203.   #   underlying sprite.
  204.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  205.   def method_missing(sym, *argv, &argb)
  206.     if @sprite.respond_to?(sym)
  207.       return @sprite.send(sym, *argv, &argb)
  208.     end
  209.     super(sym, *argv, &argb)
  210.   end
  211.  
  212.   # private methods from here down
  213.   private
  214.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  215.   # vrect
  216.   #   Get the view rect of this instance of Plane, which depends on if the
  217.   #   viewport has been set or not.
  218.   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  219.   def vrect
  220.     @sprite.viewport.nil? ? Rect.new(0, 0, Graphics.width, Graphics.height) :
  221.     @sprite.viewport.rect
  222.   end
  223. end
  224. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  225. # SCRIPT END
  226. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement