Advertisement
FlipelyFlip

Map PNG

Oct 20th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.85 KB | None | 0 0
  1. # =============================================================================
  2. # Map Saver
  3. # Version 0.1
  4. # Author: Omegas7.   Site: http://www.myrpgmaker.com
  5. # Credit: Woratana for PNG Saver script.
  6. # =============================================================================
  7. # Save a whole map into a .png file by taking multiple screenshots and finally
  8. # combining them. Exporting the resulting bitmap to a .png image file using
  9. # Woratana's PNG Saver script.
  10. # =============================================================================
  11. # Instructions:
  12. #   Go to Paint or whatever image editing software you want.
  13. #   Create a new file, width and height will be the same as your map, but
  14. #   multiplied by 32. (If map's width is 20, the image's width will be 640).
  15. #   Name the image "Blank", and save it on the System folder of your game.
  16. #   Go to your game map, and put the player starting position on the
  17. #   bottom left part of the map.
  18. #   Now make a new event, autorun, with script call:
  19. #   $saver = Map_Saver.new
  20. #   Then turn self-switch A ON.
  21. #   Go to the event's second page, put as condition self-switch A, then put
  22. #   script call:
  23. #   $saver.update
  24. #   Finally make the second page to be autorun as well.
  25. #   Run the game, you will see the map will start scrolling. Let it be.
  26. #   At the end the script will start creating the image file, this process
  27. #   may take several minutes depending on your map's size.
  28. #   Sure, it may take very long, but it saves HOURS if you planned to make
  29. #   your map manually, so do not complain ;).
  30. # =============================================================================
  31. # Notes:
  32. #   The final result will be an image file located on your game's main folder.
  33. #   The image will be exactly what appears on the screen, so if you got a HUD,
  34. #   it will appear as well.
  35. #   If you notice a problem with the final result, like with tiles etc,
  36. #   try activating "weird mode" below.
  37. #   If the final result is still not fixed, try to get support.
  38. # =============================================================================
  39.  
  40. class Map_Saver
  41.   def initialize
  42.  
  43.     # If the resulting image seems to be wrongly made, try turning this value
  44.     # to "true". Normally, this should be false.
  45.     @weird_mode = false
  46.  
  47.     @result = Bitmap.new('Graphics/System/Blank')
  48.     @bitmaps = []
  49.     @moving = true
  50.     @last_place = 'none'
  51.     @finishing = false
  52.     @pause_time = 30
  53.     @scrolling_x = 13
  54.   end
  55.   def update
  56.     if !(@finishing)
  57.       if !($game_map.scrolling?)
  58.         if @moving
  59.           execute_image
  60.         else
  61.           if can_move?(8)
  62.             $game_map.start_scroll(8, @scrolling_x, 7)
  63.             @moving = true
  64.             @last_place = 'down'
  65.           elsif can_move?(6) && @last_place == 'down'
  66.             $game_map.start_scroll(6, 17, 7)
  67.             @last_place = 'left'
  68.           elsif can_move?(2) && @last_place == 'left'
  69.             $game_map.start_scroll(2, $game_map.height, 9)
  70.             @last_place = 'up'
  71.             @moving = true
  72.           else
  73.             @finishing = true
  74.             for i in 0...@bitmaps.size
  75.               y = @bitmaps[i][2]*32
  76.               if @bitmaps[i][3] == true
  77.                 y += 16 if @weird_mode
  78.               end
  79.               @result.blt(@bitmaps[i][1]*32,y,@bitmaps[i][0],
  80.               Rect.new(0,0,Graphics.width,Graphics.height))
  81.             end
  82.             @result.make_png('Map','')
  83.             print "Map image production is over!"
  84.             exit
  85.           end
  86.         end
  87.       end
  88.     end
  89.   end
  90.   def execute_image
  91.     Graphics.freeze
  92.     @bitmaps.push([Graphics.snap_to_bitmap,
  93.     $game_map.display_x,
  94.     $game_map.display_y,
  95.     nil])
  96.     @moving = false
  97.     Graphics.transition(@pause_time)
  98.     if can_move?(8) && can_move?(2)
  99.       @bitmaps[@bitmaps.size - 1][3] = true
  100.     end
  101.   end
  102.   def can_move?(direction)
  103.     case direction
  104.     when 6
  105.       return false if ($game_map.display_x) == ($game_map.width - Graphics.width / 32)
  106.       return true
  107.     when 2
  108.       return false if ($game_map.display_y) == ($game_map.height - Graphics.height / 32)
  109.       return true
  110.     when 4
  111.       return false if ($game_map.display_x == 0)
  112.       return true
  113.     when 8
  114.       return false if ($game_map.display_y == 0)
  115.       return true
  116.     end
  117.   end
  118. end
  119.  
  120. # =============================================================================
  121. # PNG Saver by Woratana.
  122. # =============================================================================
  123. module Zlib
  124.   class Png_File < GzipWriter
  125.     def make_png(bitmap, mode = 0)
  126.       @bitmap, @mode = bitmap, mode
  127.       create_loader
  128.       self.write(make_header)
  129.       self.write(make_ihdr)
  130.       self.write(make_idat)
  131.       self.write(make_iend)
  132.     end
  133.     def create_loader
  134.       w = @bitmap.width
  135.       h = @bitmap.height
  136.       @calculated = [0,nil]
  137.       @calculated[1] = (w*h).to_f
  138.       @window = Window_Base.new(0,150,544,116)
  139.       @window.contents.font.size = 16
  140.       @update = ((@bitmap.width*@bitmap.height)/3000.0).floor.to_i
  141.       refresh_loader
  142.     end
  143.     def refresh_loader
  144.       Graphics.update
  145.       @window.contents.clear
  146.       text = ['Creating image file, please wait...',
  147.       percent.to_s + '% done...']
  148.       for i in 0...text.size
  149.         @window.contents.draw_text(0,20*i,520,20,text[i])
  150.       end
  151.       Graphics.update
  152.     end
  153.     def percent
  154.       return ((100/@calculated[1])*@calculated[0]).floor
  155.     end
  156.     def make_header
  157.       return [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a].pack('C*')
  158.     end
  159.     def make_ihdr
  160.       ih_size               = [13].pack('N')
  161.       ih_sign               = 'IHDR'
  162.       ih_width              = [@bitmap.width].pack('N')
  163.       ih_height             = [@bitmap.height].pack('N')
  164.       ih_bit_depth          = [8].pack('C')
  165.       ih_color_type         = [6].pack('C')
  166.       ih_compression_method = [0].pack('C')
  167.       ih_filter_method      = [0].pack('C')
  168.       ih_interlace_method   = [0].pack('C')
  169.       string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
  170.                ih_compression_method + ih_filter_method + ih_interlace_method
  171.       ih_crc = [Zlib.crc32(string)].pack('N')
  172.       return ih_size + string + ih_crc
  173.     end
  174.     def make_idat
  175.       header  = "\x49\x44\x41\x54"
  176.       data    = @mode == 0 ? make_bitmap_data0 : make_bitmap_data1
  177.       data    = Zlib::Deflate.deflate(data, 8)
  178.       crc     = [Zlib.crc32(header + data)].pack('N')
  179.       size    = [data.length].pack('N')
  180.       return size + header + data + crc
  181.     end
  182.     def make_bitmap_data0
  183.       gz = Zlib::GzipWriter.open('png2.tmp')
  184.       t_Fx = 0
  185.       w = @bitmap.width
  186.       h = @bitmap.height
  187.       data = []
  188.       for y in 0...h
  189.         data.push(0)
  190.         for x in 0...w
  191.           t_Fx += 1
  192.           if t_Fx % 10000 == 0
  193.             Graphics.update
  194.           end
  195.           if t_Fx % 100000 == 0
  196.             s = data.pack('C*')
  197.             gz.write(s)
  198.             data.clear
  199.           end
  200.           color = @bitmap.get_pixel(x, y)
  201.           red = color.red
  202.           green = color.green
  203.           blue = color.blue
  204.           alpha = color.alpha
  205.           data.push(red)
  206.           data.push(green)
  207.           data.push(blue)
  208.           data.push(alpha)
  209.           @calculated[0] += 1
  210.           if @calculated[0] % @update == 0
  211.             refresh_loader
  212.           end
  213.         end
  214.       end
  215.       s = data.pack('C*')
  216.       gz.write(s)
  217.       gz.close  
  218.       data.clear
  219.       gz = Zlib::GzipReader.open('png2.tmp')
  220.       data = gz.read
  221.       gz.close
  222.       File.delete('png2.tmp')
  223.       return data
  224.     end
  225.     def make_bitmap_data1
  226.       w = @bitmap.width
  227.       h = @bitmap.height
  228.       data = []
  229.       for y in 0...h
  230.         data.push(0)
  231.         for x in 0...w
  232.           color = @bitmap.get_pixel(x, y)
  233.           red = color.red
  234.           green = color.green
  235.           blue = color.blue
  236.           alpha = color.alpha
  237.           data.push(red)
  238.           data.push(green)
  239.           data.push(blue)
  240.           data.push(alpha)
  241.           @calculated[0] += 1
  242.           if @calculated[0] % @update == 0
  243.             refresh_loader
  244.           end
  245.         end
  246.       end
  247.       return data.pack('C*')
  248.     end
  249.     def make_iend
  250.       ie_size = [0].pack('N')
  251.       ie_sign = 'IEND'
  252.       ie_crc  = [Zlib.crc32(ie_sign)].pack('N')
  253.       return ie_size + ie_sign + ie_crc
  254.     end
  255.   end
  256. end
  257. #=============================================================================
  258. # ** Bitmap
  259. #=============================================================================
  260. class Bitmap
  261.   def make_png(name = 'like', path = '', mode = 0)
  262.     Zlib::Png_File.open('png.tmp')   { |gz| gz.make_png(self, mode) }
  263.     Zlib::GzipReader.open('png.tmp') { |gz| $read = gz.read }
  264.     f = File.open(path + name + '.png', 'wb')
  265.     f.write($read)
  266.     f.close
  267.     File.delete('png.tmp')
  268.   end
  269. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement