MACL - Method and Class Library 2.3

fill_rect is faster as set_pixel (for some reason)

ps: name your method negative! (because it change self)
and make a that dup a copy
Something random, doing randomizing things.
#==============================================================================
# ** Randomizing stuff
#------------------------------------------------------------------------------
#  Methods for Enumerable classes (like Array), for creating random entries and
#  random iteration. and a destructive shuffle! for class Array.
#==============================================================================

module Enumerable
 
  def shuffle
    array = self.entries
    return Array.new(self.size){
      array.delete_at(rand(array.size))
    }
  end
 
  def random_each
    for obj in self.shuffle
      yield obj
    end
  end
 
  def random_each_with_index
    self.shuffle.each_with_index do |obj, i|
      yield obj, i
    end
  end
 
end

class Array
  def shuffle!
    array = self.shuffle
    result = array == self ? nil : self
    clear()
    self.concat(array)
    return result
  end
end
Expand to see the code.
Shouldn't each be in the Array class, or at least each_with_index?   It behaves differently between different Enumerable classes (in Hash, each must yield two values, and each_with_index doesn't exist)

EDIT: I feel like sharing my personal modifications to Bitmap.draw_slant_bar. This gives the bar a pleasing gradient:

  #--------------------------------------------------------------------------
  # * Draw Slant Bar (by SephirothSpawn)
  #--------------------------------------------------------------------------
  def draw_slant_bar(x, y, min, max, width = 152, height = 6,
      bar_color = Color.new(150, 0, 0, 255),
      end_color = Color.new(255, 255, 60, 255))
    # Draw Border
    for i in 0..height
      self.fill_rect(x+i, y+height-i, width+1, 1, Color.new(50, 50, 50, 255))
    end
    # Draw Background
    for i in 1...(height - 1)
      r = 100 * (height - i) / height
      g = 100 * (height - i) / height
      b = 100 * (height - i) / height
      a = 255
      self.fill_rect(x + i, y + height - i, width, 1, Color.new(r, b, g, a))
    end
    # Draws Bar
    for i in 1...( (min.to_f / max.to_f) * width - 1).to_i
      r = bar_color.red   * (width - i) / width + end_color.red   * i / width
      g = bar_color.green * (width - i) / width + end_color.green * i / width
      b = bar_color.blue  * (width - i) / width + end_color.blue  * i / width
      a = bar_color.alpha * (width - i) / width + end_color.alpha * i / width
      for j in 1..(height - 1)
        r = r * (height - j) / height + (3 * r / 4) * j / height
        g = g * (height - j) / height + (3 * g / 4) * j / height
        b = b * (height - j) / height + (3 * b / 4) * j / height
        self.fill_rect(x + i + j, y + height - j, 1, 1, Color.new(r, g, b, a))
      end
    end
  end
Expand to see the code.
Until one of the mods update the first post, I've put the tree docs back online.

http://etheon.net/code/macl/

Enjoy.

Edit: thank lum for the quick update :)
Hello all,

Sorry for being away so long. So many changes in my life just keep me from being as active as I once was.

I have noticed it is extremely hard to keep track of everything and submitting sections, classes, modules and methods can be kinda hard, and a pain for me to update. Therefore, I created a wiki just for the MACL. I must approve members before they can add and modify the wiki, but once you register (please register with your name here so I know who you are) and I will approve you. This way, I can check modifications of the last month in the wiki and update everything much easier. I have learned php/mySQL so I am also working on a system that will allow this to be done with a php script and allow updates to be all taken care of online, but it'll probably be another month before I complete it.

I am working on going through everything again and updating the MACL to 3.0 this week (as long as nothing comes up). I am going to be revamping a lot of things to try to make updates easier. Any suggestions would be great. I have went through the topic and added everything I have seen. I will probably modify some of the codes, but original authors will receive credit.
Link to the Wiki? I'd be glad to help :)
Wow, I completely forgot to post the link. Lol

http://maclonline.pbwiki.com/

I'll give you admin privs as soon as you join. Feel free to change anything, as I am semi-new to wiki.
I have a little fix to the MACL, in RGSS Actor and party info part, arround the line 104, in the Game_Character refresh method puts:

  1.  
  2.   #--------------------------------------------------------------------------
  3.   # * Refresh
  4.   #--------------------------------------------------------------------------
  5.   def refresh
  6.     # Set character file name and hue
  7.     if @battler != nil
  8.       # If Dead
  9.       if @battler.dead?                
  10.         @character_name = ''            
  11.         @character_hue = 0              
  12.         return
  13.       end
  14.       @character_name = @battler.character_name
  15.       @character_hue = @battler.character_hue
  16.     end
  17.   end
Expand to see the code.


And refer to the original method, and the Logical display, must be:

  1.  #--------------------------------------------------------------------------
  2.   # * Refresh
  3.   #--------------------------------------------------------------------------
  4.   def refresh
  5.     # Set character file name and hue
  6.     if @battler != nil
  7.       # If Dead
  8.       if @battler.dead?              
  9.         @battler_name = ''            
  10.         @battler_hue = 0            
  11.         return
  12.       end
  13.       @character_name = @battler.character_name
  14.       @character_hue = @battler.character_hue
  15.     end
  16.   end
Expand to see the code.


So, in the original method, when the actors are all dead, there are no graphic for the character.

Well, that.
I was looking through the thread and I saw myself write something about a Data module. Well, I wrote it better, using Dir:

  1. class Data
  2.   # Load normal database
  3.   Dir.entries("Data").each do |filename|
  4.     if filename.include?(".rxdata")
  5.       name = filename.chomp(".rxdata")
  6.       eval(name + ' = load_data("Data/" + filename)')
  7.     end
  8.   end
  9.   if $BTEST
  10.     # Load battle test database
  11.     Dir.entries("Data").each do |filename|
  12.       if filename.include?("BT_")
  13.         name = filename.sub("BT_", '').chomp(".rxdata")
  14.         eval(name + ' = load_data("Data/" + filename)')
  15.       end
  16.     end
  17.   end
  18. end
Expand to see the code.


It would have been half as long if it weren't for $BTEST. It works though. You'll have, say, Data::System, Data::MapInfos, Data::Actors, Data::Scripts, Data::Map001, etc. All centralized. Not sure if having Scripts initialized is a good idea, but it's in there so it is. Another benefit of this is that if another "rxdata" file is added to the Data directory, it's automatically loaded.
It is an idea you have meustrus, but I don't like it since you load all of the map data into the memory. It can be a significant amount of data and you don't need it all at the same time. Note that it will not with encrypted projects since Dir for obvious reasons won't be able to give the filenames.
I like the idea of Data::System, Data::MapInfos and etc.
It doesn't really matter whether you have Data::Scripts or not since you have $RGSS_SCRIPTS. $RGSS_SCRIPTS contains both the compressed scripts and the raw scripts where as Data::Scripts will only have the compressed data.

*hugs*
- Zeriab
Hey I apologize if this is asking too much, but can someone put a mirror up for the MACL scripts?

The links on the wiki as well as this thread don't seem to work.

Thank you kindly!
On the first page: http://code.etheon.net/macl/scripts/macl_complete.txt
etheon wrote:


Aah, the site must have been down for a few moments earlier it seems!

Thank you kindly.
It's my site, but it may have been? My host has bouts from time to time. I don't know...I'll check and see if anything went wrong.
I don't know what's up with Seph right now, but etheon.net is down again. I was going to mirror the MACL, but I can't get a good cached copy (Google has one but it gets cut off) and I've broken my copy up/deleted some things already (I'm actually looking for the Random class, if anybody's got it). I would speculate that the website goes down because of bad PHP or Javascript that taxes the server until the server suspends the account (at least, that's what seemed to happen to me).
http://rapidshare.com/files/215652725/macl_tree.rar << the macl tree

edit:

  1. class Data
  2. # Load normal database
  3.  Dir.entries("Data").each do |filename|
  4.   if filename.include?(".rxdata")
  5.     name = filename.chomp(".rxdata")
  6.     self.const_set(name,load_data("Data/" + filename))
  7.   end
  8.  end
  9.  if $BTEST
  10.  # Load battle test database
  11.   Dir.entries("Data").each do |filename|
  12.     if filename.include?("BT_")
  13.      name = filename.sub("BT_", '').chomp(".rxdata")
  14.      self.const_set(name,load_data("Data/" + filename))
  15.     end
  16.   end
  17.  end
  18. end
Expand to see the code.


this is better and has no Evial
A simple method for the Dir class to recursively perform mkdir if any parent directories do not exist.

  1. class Dir
  2.   #-------------------------------------------------------------------------
  3.   # * Name         : Dir.mkdir_rec
  4.   #    Info         : Creates a directory and all its parent directories if they
  5.   #                      do not exist.
  6.   #    Author      : Raku
  7.   #    Call Info : path: The path to the directory to create
  8.   #-------------------------------------------------------------------------
  9.   def self.mkdir_rec(path)
  10.      begin
  11.         # Attempt to make the directory
  12.         Dir.mkdir(path)
  13.      rescue Errno::ENOENT
  14.         # Failed, so let's use recursion on the parent directory
  15.         parent_path = File.dirname(path)
  16.         mkdir_rec(parent_path)
  17.  
  18.         # Make the original directory
  19.         Dir.mkdir(path)
  20.      end
  21.   end
  22. end
Expand to see the code.
Does the Macl script have a licence ?
Can we use it in the games we want ? (crediting the authors)

Can we use it in commercial projects ? (crediting the authors too)
If this edits properly, I can't submit everything (roughly 1800 lines or so) so I posted it at pastebin and in a txt at mediafire since pastebin choked on it too o.O?

TONS of new stuff for...

Array
Game_Actor
Game_Actors
Game_Battler
Game_Enemy
Game_Party
Game_Troop
Game_Map
RPG::Cache
RPG::Item
RPG::Armor
RPG::Weapon
RPG::Skill

Enjoy :thumb:

I just read that you made a wiki for this. As soon as you approve me I'll be adding alot more stuff to the MACL.
I just switched hosts to a new VPS (which is still being set up), so the MACL docs are down right; if anyone wants to host them go ahead.
um link is broken, thanks
get ddl here:
http://rapidshare.com/files/215652725/macl_tree.rar << the macl tree

(i think it could on the first post)
Fixed. Link is fixed and you guys will soon have a couple more tools.
thanks =]
Notify me before you post an update of the MACL, I've got my own library of new stuff to submit but I'm just gonna wait 'til last minute so I can just submit it all.

EDIT: Speaking of which...

RGSS.BattleAction Show
  1. #===============================================================================
  2. # ** RGSS.BattleAction
  3. #-------------------------------------------------------------------------------
  4. # Description:
  5. # ------------
  6. #
  7. #   Makes setting battle actions faster and easier, from both the Game_Battler
  8. #   or directly to a Game_BattleAction object itself.
  9. #
  10. # Method List:
  11. # ------------
  12. #
  13. #   Game_BattleAction
  14. #   -----------------
  15. #   do_attack
  16. #   do_skill
  17. #   do_defend
  18. #   do_item
  19. #   do_escape
  20. #   do_nothing
  21. #
  22. #   Game_Battler
  23. #   ------------
  24. #   do_attack
  25. #   do_skill
  26. #   do_defend
  27. #   do_item
  28. #   do_escape
  29. #   do_nothing
  30. #===============================================================================
  31.  
  32. MACL::Loaded << 'RGSS.BattleAction'
  33.  
  34. #===============================================================================
  35. # ** Game_BattleAction
  36. #===============================================================================
  37.  
  38. class Game_BattleAction
  39.   #-----------------------------------------------------------------------------
  40.   # * Name      : Do Attack
  41.   #   Info      : Sets Battle Action for Attack
  42.   #   Author    : Kain Nobel
  43.   #   Call Info : Target (Optional) is a type of Game_Battler
  44.   #-----------------------------------------------------------------------------
  45.   def do_attack
  46.     @basic    = 0
  47.     @kind     = 0
  48.   end
  49.   #-----------------------------------------------------------------------------
  50.   # * Name      : Do Skill
  51.   #   Info      : Sets Battle Action for Skill
  52.   #   Author    : Kain Nobel
  53.   #   Call Info : Skill ID (Numeric) is ID of skill (Optional)
  54.   #               Target (Optional) is a type of Game_Battler
  55.   #-----------------------------------------------------------------------------
  56.   def do_skill(skill_id = 0)
  57.     unless $data_skills[skill_id].is_a?(RPG::Skill)
  58.       clear
  59.       return
  60.     end
  61.     @kind     = 1
  62.     @skill_id = skill_id
  63.   end
  64.   #-----------------------------------------------------------------------------
  65.   # * Name      : Do Defend
  66.   #   Info      : Sets Battle Action for Defend
  67.   #   Author    : Kain Nobel
  68.   #   Call Info : None
  69.   #-----------------------------------------------------------------------------
  70.   def do_defend
  71.     @basic    = 1
  72.     @kind     = 0
  73.   end
  74.   #-----------------------------------------------------------------------------
  75.   # * Name      : Do Item
  76.   #   Info      : Sets Battle Action for Item
  77.   #   Author    : Kain Nobel
  78.   #   Call Info : Item ID (Numeric) is ID of item (Optional)
  79.   #               Target (Optional) is a type of Game_Battler
  80.   #-----------------------------------------------------------------------------
  81.   def do_item(item_id = 0, target = nil)
  82.     unless $data_items[item_id].is_a?(RPG::Item)
  83.       clear
  84.       return
  85.     end
  86.     @kind     = 2
  87.     @skill_id = item_id
  88.   end
  89.   #-----------------------------------------------------------------------------
  90.   # * Name      : Do Escape
  91.   #   Info      : Sets Battle Action for Escape
  92.   #   Author    : Kain Nobel
  93.   #   Call Info : None
  94.   #-----------------------------------------------------------------------------
  95.   def do_escape
  96.     @kind   = 0
  97.     @basic  = 2
  98.   end
  99.   #-----------------------------------------------------------------------------
  100.   # * Name      : Do Nothing
  101.   #   Info      : Sets Battle Action for Nothing
  102.   #   Author    : Kain Nobel
  103.   #   Call Info : None
  104.   #-----------------------------------------------------------------------------
  105.   def do_nothing
  106.     @kind   = 0
  107.     @basic  = 3
  108.   end
  109. end
  110.  
  111. #===============================================================================
  112. # ** Game_Battler
  113. #===============================================================================
  114.  
  115. class Game_Battler
  116.   #-----------------------------------------------------------------------------
  117.   # * Name      : Do Attack
  118.   #   Info      : Current Action Does Attack and Selects Target
  119.   #   Author    : Kain Nobel
  120.   #   Call Info : None
  121.   #-----------------------------------------------------------------------------
  122.   def do_attack
  123.     self.current_action.do_attack
  124.     if $scene.respond_to?(:set_target_battlers)
  125.       $scene.set_target_battlers(1)
  126.     end
  127.   end
  128.   #-----------------------------------------------------------------------------
  129.   # * Name      : Do Skill
  130.   #   Info      : Current Action Does Skill and Selects Target
  131.   #   Author    : Kain Nobel
  132.   #   Call Info : Skill ID (Numeric) Representing Skill from Database
  133.   #-----------------------------------------------------------------------------
  134.   def do_skill(skill_id)
  135.     skill = $data_skills[skill_id]
  136.     self.current_action.do_skill(skill.id)
  137.     if $scene.respond_to?(:set_target_battlers) && !skill.nil?
  138.       $scene.set_target_battlers(skill.scope)
  139.     end
  140.   end
  141.   #-----------------------------------------------------------------------------
  142.   # * Name      : Do Defend
  143.   #   Info      : Current Action Does Defend
  144.   #   Author    : Kain Nobel
  145.   #   Call Info : None
  146.   #-----------------------------------------------------------------------------
  147.   def do_defend
  148.     self.current_action.do_defend
  149.     if $scene.respond_to?(:set_target_battlers)
  150.       $scene.set_target_battlers(0)
  151.     end
  152.   end
  153.   #-----------------------------------------------------------------------------
  154.   # * Name      : Do Item
  155.   #   Info      : Current Action Does Item and Selects Target
  156.   #   Author    : Kain Nobel
  157.   #   Call Info : Item ID (Numeric) Representing Item from Database
  158.   #-----------------------------------------------------------------------------
  159.   def do_item(item_id)
  160.     item = $data_items[item_id]
  161.     self.current_action.do_item(item.id)
  162.     if $scene.respond_to?(:set_target_battlers) && !item.nil?
  163.       $scene.set_target_battlers(item.scope)
  164.     end
  165.   end
  166.   #-----------------------------------------------------------------------------
  167.   # * Name      : Do Defend
  168.   #   Info      : Current Action Does Escape
  169.   #   Author    : Kain Nobel
  170.   #   Call Info : None
  171.   #-----------------------------------------------------------------------------
  172.   def do_escape
  173.     self.current_action.do_escape
  174.     if $scene.respond_to?(:set_target_battlers)
  175.       $scene.set_target_battlers(0)
  176.     end
  177.   end
  178.   #-----------------------------------------------------------------------------
  179.   # * Name      : Do Nothing
  180.   #   Info      : Current Action Does Nothing
  181.   #   Author    : Kain Nobel
  182.   #   Call Info : None
  183.   #-----------------------------------------------------------------------------
  184.   def do_nothing
  185.     self.current_action.do_nothing
  186.     if $scene.respond_to?(:set_target_battlers)
  187.       $scene.set_target_battlers(0)
  188.     end
  189.   end
  190. end
  191.  
  192. #===============================================================================
  193. # ** Scene_Battle
  194. #===============================================================================
  195.  
  196. class Scene_Battle
  197.   #-----------------------------------------------------------------------------
  198.   # * Public Instance Variables
  199.   #-----------------------------------------------------------------------------
  200.   attr_reader :target_battlers
  201.   #-----------------------------------------------------------------------------
  202.   # * Alias Listings
  203.   #-----------------------------------------------------------------------------
  204.   alias_method :macl_snbattle_settargetbattlers, :set_target_battlers
  205.   #-----------------------------------------------------------------------------
  206.   # * Set Target Battlers
  207.   #-----------------------------------------------------------------------------
  208.   def set_target_battlers(scope)
  209.     @target_battlers ||= Array.new
  210.     macl_snbattle_settargetbattlers(scope)
  211.   end
  212. end
  213.  
Expand to see the code.


...I just happened to develop this 'on accident' kind of when making my FFXII-ish Gambit system, it speeds up setting common methods for battlers and their actions (do_attack, do_skill, do_escape, do_wait, etc...)

I still need to add an escape method to Game_Actor class, because simply calling actor.do_escape won't hold any merit, only for enemies would that work. Basically, what I mean is methods which will make actors able to escape idependantly rather than all at once. I'll add that and update the script later this week if I have time, but everything else works.
Well, nobody answered my post. :(

Quote:
Does the Macl script have a licence ?
Can we use it in the games we want ? (crediting the authors)

Can we use it in commercial projects ? (crediting the authors too)



I'm using part of Macl in all of my projects :

Image

If the authors don't agree, just tell me. ;)
I am afraid MACL does not have a license which contributors have to agree to, so it would be normal copyright rules for each part of MACL. Joint copyright where applicable.
The intention has definitely been to allow commercial use, but intention doesn't hold in court.

I give permission to use any part I have copyright for however you see fit and allow you to modify and distribute out however you like naturally without you being able to hold me accountable in any way.

*hugs*
I'm using only one function, so I don't really care about licence (I could delete this function).
But MACL have a very good purpose (prevent other scripters from "reinventing the wheel"), so I decided to include its Ruby part in my scripts.

If I'm not using MACL, people who are using my scripts could be interested by it. ;)


And thanks for your permission. :D
I've made modifications to the Png_File class. It was using gigantic amounts of memory writing to two separate temporary files, reading back the entirety of those files, then writing them again to the final file. I have also used the Zlib::Deflate class more effectively as a stream rather than a single function, so that instead of caching the data to hoge.gz in method 0, it simply streams it into the deflater (using deflater << [...].pack("C*")). I did all of this because Bitmap#make_png was sometimes choking on large images, encountering a NoMemoryError at the point where make_bitmap_data0 reads from hoge.gz (and getting that error more frequently when using mode 1 instead).

  1. #==============================================================================
  2. # ** RGSS.Bitmap.misc
  3. #------------------------------------------------------------------------------
  4. # Description:
  5. # ------------
  6. # Miscellaneous New stuff for the Bitmap class.
  7. #
  8. # Method List:
  9. # ------------
  10. # make_png
  11. #==============================================================================
  12.  
  13. #==============================================================================
  14. # ** Bitmap
  15. #==============================================================================
  16.  
  17. class Bitmap
  18.   #-------------------------------------------------------------------------
  19.   # * Name      : Make PNG
  20.   #   Info      : Saves Bitmap to File
  21.   #   Author    : ??? - http://www.66rpg.com/htm/news624.htm
  22.   #   Call Info : Zero to Three Arguments
  23.   #               Name : Name of filenam
  24.   #               Path : Directory in Game Folder
  25.   #               Mode : Mode of Writing
  26.   #-------------------------------------------------------------------------
  27.   def make_png(name = 'like', path = '', mode = 0)
  28.     Dir.make_dir(path) if path != ''
  29.     f = Png_File.new(path + name + '.png', 'wb')
  30.     f.make_png(self)
  31.     f.close
  32.   end
  33. end
  34.  
  35. #==============================================================================
  36. # ** Class Png_File
  37. #------------------------------------------------------------------------------
  38. # Description:
  39. # ------------
  40. # Adds PNG_File class to save Bitmap's to PNG Files
  41. #
  42. # Class List:
  43. # -----------
  44. # Png_File
  45. #==============================================================================
  46.  
  47. #==============================================================================
  48. # ** Png_File
  49. #==============================================================================
  50.  
  51. class Png_File < File
  52.   #--------------------------------------------------------------------------
  53.   # * Make PNG
  54.   #--------------------------------------------------------------------------
  55.   def make_png(bitmap)
  56.     # Save Bitmap & Mode
  57.     @bitmap = bitmap
  58.     # Create & Save PNG
  59.     self.write(make_header)
  60.     self.write(make_ihdr)
  61.     self.write(make_idat)
  62.     self.write(make_iend)
  63.   end
  64.   #--------------------------------------------------------------------------
  65.   # * Make Header
  66.   #--------------------------------------------------------------------------
  67.   def make_header
  68.     return [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a].pack('C*')
  69.   end
  70.   #--------------------------------------------------------------------------
  71.   # * Make IHDR
  72.   #--------------------------------------------------------------------------
  73.   def make_ihdr
  74.     ih_size               = [13].pack("N")
  75.     ih_sign               = 'IHDR'
  76.     ih_width              = [@bitmap.width].pack('N')
  77.     ih_height             = [@bitmap.height].pack('N')
  78.     ih_bit_depth          = [8].pack('C')
  79.     ih_color_type         = [6].pack('C')
  80.     ih_compression_method = [0].pack('C')
  81.     ih_filter_method      = [0].pack('C')
  82.     ih_interlace_method   = [0].pack('C')
  83.     string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
  84.              ih_compression_method + ih_filter_method + ih_interlace_method
  85.     ih_crc = [Zlib.crc32(string)].pack('N')
  86.     return ih_size + string + ih_crc
  87.   end
  88.   #--------------------------------------------------------------------------
  89.   # * Make IDAT
  90.   #--------------------------------------------------------------------------
  91.   def make_idat
  92.     header  = "\x49\x44\x41\x54"
  93.     data    = make_bitmap_data
  94.     size    = [data.length].pack('N')
  95.     crc     = [Zlib.crc32(header + data)].pack('N')
  96.     return size + header + data + crc
  97.   end
  98.   #--------------------------------------------------------------------------
  99.   # * Make bitmap data
  100.   #--------------------------------------------------------------------------
  101.   def make_bitmap_data
  102.     deflater = Zlib::Deflate.new(8)
  103.     t_Fx = 0
  104.     for y in 0...@bitmap.height
  105.       deflater << [0].pack("C*")
  106.       for x in 0...@bitmap.width
  107.         Graphics.update if (t_Fx += 1) % 10000 == 0
  108.         color = @bitmap.get_pixel(x, y)
  109.         deflater << [color.red, color.green, color.blue, color.alpha].pack("C*")
  110.       end
  111.     end
  112.     return deflater.flush(Zlib::FINISH)
  113.   end
  114.   #--------------------------------------------------------------------------
  115.   # * Make IEND
  116.   #--------------------------------------------------------------------------
  117.   def make_iend
  118.     ie_size = [0].pack('N')
  119.     ie_sign = 'IEND'
  120.     ie_crc  = [Zlib.crc32(ie_sign)].pack('N')
  121.     return ie_size + ie_sign + ie_crc
  122.   end
  123. end
Expand to see the code.


EDIT: I improved my modifications further, removing data = [] completely and streaming the color data directly into the deflater.
Here's a new function for the Bitmap class. plane_blt functions just like stretch_blt (where it calls blt instead if the width and height are the same on both dest_rect and src_rect) except that if it comes to the edge of the source bitmap, it wraps around. You could use this to, say, draw a fog to a very large bitmap, starting at any x,y offset. If bmp has dimensions 50x50, and src_rect = Rect.new(25, 25, 100, 200) then the bmp will tile over those dimensions, resulting in something like this:

  1. ,.,
  2. |M|
  3. |M|
  4. `*`
Expand to see the code.


I made this function for the Map Image Maker to draw panoramas and fogs to the full size bitmap, as well as small 32x32 tiles offset the correct amount so that if you drew the tiles individually, then pieced them together, the plane bitmap continuously loops as if you had only called the function once (though of course calling the function many times instead of once is not optimal, but in this case only done when necessary)

  1. class Bitmap
  2.   #-----------------------------------------------------------------------------
  3.   # * Draw bitmap to self; if rect goes outside, loop around
  4.   #-----------------------------------------------------------------------------
  5.   def plane_blt(dest_rect, bmp, src_rect = bmp.rect, op = 255)
  6.     # Flag to stretch bitmap or not
  7.     stretch = (dest_rect.width != src_rect.width or
  8.                dest_rect.height != src_rect.height)
  9.     # Starting coordinates
  10.     sx = src_rect.x % bmp.width
  11.     dx = dest_rect.x
  12.     # Source width remaining
  13.     swr = src_rect.width
  14.     while (swr > 0)
  15.       # Starting coordinates
  16.       sy = src_rect.y % bmp.height
  17.       dy = dest_rect.y
  18.       # Source height remaining
  19.       shr = src_rect.height
  20.       # Width for this column
  21.       sw = [bmp.width - sx, swr].min
  22.       dw = dest_rect.width * sw / src_rect.width
  23.       while (shr > 0)
  24.         # Height for this column
  25.         sh = [bmp.height - sy, shr].min
  26.         dh = dest_rect.height * sh / src_rect.height
  27.         # Draw bitmap using stretch function if necessary
  28.         if stretch
  29.           self.stretch_blt(Rect.new(dx,dy,dw,dh), bmp, Rect.new(sx,sy,sw,sh),op)
  30.         else
  31.           self.blt(dx, dy, bmp, Rect.new(sx, sy, sw, sh), op)
  32.         end
  33.         sy = 0    # Set Y value to wrap around
  34.         shr -= sh # Reduce height remaining
  35.         dy += dh  # Increase destination coordinate
  36.       end
  37.       sx = 0    # Set X value to wrap around
  38.       swr -= sw # Reduce width remaining
  39.       dx += dw  # Increase destination coordinate
  40.     end
  41.   end
  42. end
Expand to see the code.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=9417