Advertisement
mikb89

[VX] RM2k/2k3 Graphics v1.5

Jun 17th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.48 KB | None | 0 0
  1. # RM2k/2k3 Graphics v. 1.5
  2. # VX version
  3. # by mikb89
  4.  
  5. # Details:
  6. #  With this script you can use graphic files with a lower resolution, similar
  7. #   to the older RPG Maker. The size of the graphic will be doubled once drawn.
  8. #
  9. #  MidGraphics folder contains resized images, while Graphics is used by the
  10. #   application, as usually.
  11. #  The problem is that the tool CAN'T double images WHILE making your project,
  12. #   so we have to carry a double-copy of images, to use them in the project.
  13. #  If you use other images or modify those existing, insert them in MidGraphics.
  14. #  Next, do a copy of them which is double-sized and insert in Graphics at the
  15. #   same location. No matter quality, it's just for pratical purpuose.
  16. #  Once you release the game, get rid (but not permanently!) of Graphics
  17. #   let MidGraphics take his place by renaming in Graphics and you're ready.
  18. #  During test, in fact, images are taken from MidGraphics, but in normal
  19. #   playing (by opening Game.exe) they came from Graphics. This way you can
  20. #   release the project as usual also by menu File option and everything will
  21. #   work, included criptation for ones who care about image copyright.
  22.  
  23. # Configurations:
  24. module RM2K
  25.   FONT_TYPE = 1
  26.    # You can choose between:
  27.     # 0: Nothing to say, just tipical text.
  28.     # 1: Tipical of RPG Maker 2k/2k3. Color depends by EACH pixel in windowskin
  29.     #     color and characters come from the Font.png image inside System.
  30.     #     Characters max size is 8x8 but don't worry about the i: if there are
  31.     #     empty spaces before or after the character, they'll be trimmed.
  32.     #    For bold and italic the image is FontBI, for just bold it's FontB and
  33.     #     for italic, FontI.
  34.     # 2: Usual font. The only thing to know is that the text is stretched then
  35.     #     enlarged. That because the normal font (in my opinion) doesn't fit
  36.     #     with the doubled images style.
  37.     # 3: Characters in separated image files. Create inside System a Fonts
  38.     #     folder; inside this, insert the characters as f0, f1, f2 with the
  39.     #     number part representing the ASCII code (e.g. f49 is the zero).
  40.     #    For bold characters folder name will be B, I for italic, BI for both.
  41.     #     B, I, BI must be placed inside Fonts.
  42.     #    In this drawing type, characters width depend on image width.
  43.    # Other numbers: won't see anything... may somebody like it?
  44.  
  45.   BLUR_SCREEN_ON_MENU = false
  46.    # Background map bitmap is commonly blurred when accessing menus. Setting
  47.    #  false will remove this effect.
  48.  
  49.  
  50.   # Advanced settings. Don't touch unless you know what you're doing.
  51.  
  52.   STANDARD_FONT_SIZE = Font.default_size
  53.    # Needed for measuring the amount of resizing for character in images.
  54. end
  55.  
  56. #Codename: rm2k
  57.  
  58. ($imported ||= {})[:mikb89_rm2k] = true
  59.  
  60. # License:
  61. # - You can ask me to include support for other scripts as long as these scripts
  62. #   use the $imported[script] = true;
  63. # - You can modify and even repost my scripts, after having received a response
  64. #   by me. For reposting it, anyway, you must have done heavy edit or porting,
  65. #   you can't do a post with the script as is;
  66. # - You can use my scripts for whatever you want, from free to open to
  67. #   commercial games. I'd appreciate by the way if you let me know about what
  68. #   you're doing;
  69. # - You must credit me, if you use this script or part of it.
  70.  
  71. # Codice per codifica accentate
  72. class String
  73. #class String#def conv()
  74.   def conv # convert to a byte array
  75.     ns = []
  76.     add_n = false
  77.     self.each_byte {|b|
  78.       if add_n
  79.         ns.push(b+64)
  80.         add_n = false
  81.       elsif b < 128
  82.         ns.push(b)
  83.         add_n = false
  84.       else
  85.         add_n = true
  86.       end
  87.     }
  88.     ns
  89.   end
  90. end
  91.  
  92. class Fixnum
  93. #class Fixnum#def conv()
  94.   def conv # convert to a byte array
  95.     ns = []
  96.     self.to_s.each_byte {|b| ns << b}
  97.     ns
  98.   end
  99. end
  100.  
  101. # Codice vecchio
  102. class Scene_Base; unless RM2K::BLUR_SCREEN_ON_MENU
  103. #class Scene_Base#def snapshot_for_background() <- rewritten
  104.   def snapshot_for_background
  105.     $game_temp.background_bitmap.dispose
  106.     $game_temp.background_bitmap = Graphics.snap_to_bitmap
  107.   end
  108. end; end
  109.  
  110. class Font
  111.   attr_writer :font_type
  112. #class Font#def self.default_font_type()
  113.   def self.default_font_type
  114.     @default_font_type = RM2K::FONT_TYPE if @default_font_type == nil
  115.     @default_font_type
  116.   end
  117. #class Font#def self.default_font_type=(dm)
  118.   def self.default_font_type=(dm)
  119.     @default_font_type = dm
  120.   end
  121. #class Font#def font_type()
  122.   def font_type
  123.     @font_type = Font.default_font_type if @font_type == nil
  124.     @font_type
  125.   end
  126. end
  127.  
  128. class Bitmap
  129. #class Bitmap#def double()
  130.   def double
  131.     b = Bitmap.new(width*2, height*2)
  132.     b.stretch_blt(b.rect, self, rect)
  133.     b
  134.   end
  135. #class Bitmap#def tru_img()
  136.   def tru_img # get the image without trasparent left and right borders
  137.     sx, fx = width-1, 0
  138.     c = Color.new(0,0,0)
  139.     for x in 0...width
  140.       e = false
  141.       for y in 0...height
  142.         if get_pixel(x,y) == c
  143.           e = true
  144.           break
  145.         end
  146.       end
  147.       if e == true
  148.         sx = x
  149.         break
  150.       end
  151.     end
  152.     for x in 1...width
  153.       e = false
  154.       for y in 0...height
  155.         if get_pixel(width-x,y) == c
  156.           e = true
  157.           break
  158.         end
  159.       end
  160.       if e == true
  161.         fx = width-x
  162.         break
  163.       end
  164.     end
  165.     if fx+1 > sx
  166.       bit = Bitmap.new(fx-sx+1,height)
  167.       bit.blt(0,0,self,Rect.new(sx,0,fx-sx+1,height))
  168.       return bit
  169.     else
  170.       return Bitmap.new(width/2,height)
  171.     end
  172.   end
  173. #class Bitmap#def recolour(ind)
  174.   def recolour(ind) # color change from c to ind(ex) in Window.png/cache
  175.     c = Color.new(0,0,0)
  176.     dum = Bitmap.new(8,8)
  177.     if ind < 32
  178.       dum.blt(0,0,Cache.system("Window"),Rect.new(64+(ind%8)*8,96+(ind/8)*8,8,8))
  179.     else
  180.       dum.fill_rect(dum.rect, text_color(ind))
  181.     end
  182.     for x in 0...width
  183.       for y in 0...height
  184.         set_pixel(x,y,dum.get_pixel(x,y)) if get_pixel(x,y) == c
  185.       end
  186.     end
  187.   end
  188. #class Bitmap#def color_ind(col)
  189.   def color_ind(col) # get the index of color if found in palette or cache
  190.     for i in 0...32
  191.       return i if text_color(i).red == col.red &&
  192.                   text_color(i).green == col.green &&
  193.                   text_color(i).blue == col.blue
  194.     end
  195.     @@xtra_col ||= []
  196.     for c in 0...@@xtra_col.size
  197.       return 32+c if @@xtra_col[c].red == col.red &&
  198.                      @@xtra_col[c].green == col.green &&
  199.                      @@xtra_col[c].blue == col.blue
  200.     end
  201.     @@xtra_col.push(col)
  202.     return 31+@@xtra_col.size
  203.   end
  204. #class Bitmap#def text_color(n)
  205.   def text_color(n) # get color from palette or from cache
  206.     if n < 32
  207.       x = 64 + (n % 8) * 8
  208.       y = 96 + (n / 8) * 8
  209.       return Cache.system("Window").get_pixel(x, y)
  210.     end
  211.     return Color.new(0,0,0) if (@@xtra_col ||= []).size <= n-32
  212.     return @@xtra_col[n-32]
  213.   end
  214.   alias_method(:text_size_b4_rm2k, :text_size) unless method_defined?(:text_size_b4_rm2k)
  215. #class Bitmap#def text_size(str) <- aliased
  216.   def text_size(str)
  217.     m = font.font_type
  218.     case m
  219.     when 1,3
  220.       f = "Font" + ((m == 3) ? "s" : "")
  221.       f += "/" if m==3 && font.bold || font.italic
  222.       f += "B" if font.bold
  223.       f += "I" if font.italic
  224.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  225.       e = font.shadow ? 1 : 0
  226.       for b in str.conv
  227.         b = "f" + b.to_s if m==3
  228.         img = Cache.system(f, b)
  229.         e += img.width+2
  230.       end
  231.       return Rect.new(0,4-(fh-16)/2,e,fh)
  232.     when 0,2
  233.       return text_size_b4_rm2k(str)
  234.     end
  235.     return Rect.new(0,0,1,1)
  236.   end
  237.   alias_method(:draw_text_b4_rm2k, :draw_text) unless method_defined?(:draw_text_b4_rm2k)
  238. #class Bitmap#def draw_text(*t) <- aliased
  239.   def draw_text(*t)
  240.     case t.size
  241.     when 2,3
  242.       x,y,w,h,s,a = t[0].x,t[0].y,t[0].width,t[0].height,t[1],t.size==3 ? t[2] : 0
  243.     when 5,6
  244.       x,y,w,h,s,a = t[0],t[1],t[2],t[3],t[4],t.size==6 ? t[5] : 0
  245.     end
  246.     m = font.font_type
  247.     case m
  248.     when 0 # nessun cambiamento
  249.       draw_text_b4_rm2k(x,y,w,h,s,a)
  250.     when 1 # usa una tavolozza font 8x8
  251.       f = "Font"
  252.       f += "B" if font.bold
  253.       f += "I" if font.italic
  254.       plus = 0
  255.       ts = text_size(s)
  256.       plus = w/2 - ts.width/2 - 1 if a == 1
  257.       plus = w - ts.width if a == 2
  258.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  259.       e = 0
  260.       for b in s.conv
  261.         x_b = x+plus+e
  262.         y_b = y + h/2 - ts.height/2
  263.         if font.shadow
  264.           img = Cache.system(f, b)
  265.           iw = img.width
  266.           stretch_blt(Rect.new(x_b+2,y_b+2,iw,fh),img,Rect.new(0,0,iw,16))
  267.         end
  268.         img = Cache.system(f, b, color_ind(font.color))
  269.         iw = img.width
  270.         stretch_blt(Rect.new(x_b,y_b,iw,fh),img,Rect.new(0,0,iw,16), font.color.alpha)
  271.         e += iw+2
  272.       end
  273.     when 2 # usa font normale
  274.       b = Bitmap.new(w/2,h/2)
  275.       b.font = font.clone
  276.       b.font.size = font.size/2
  277.       b.draw_text_b4_rm2k(0,0,w/2,h/2,s,a)
  278.       stretch_blt(Rect.new(x,y,w,h), b, b.rect)
  279.       b.dispose
  280.     when 3 # usa file immagine Wx8 in cartelle (dove W è variabile)
  281.       f = "Fonts"
  282.       if font.bold
  283.         f += "/B"
  284.         f += "I" if font.italic
  285.       elsif font.italic
  286.         f += "/I"
  287.       end
  288.       plus = 0
  289.       ts = text_size(s)
  290.       plus = w/2 - ts.width/2 - 1 if a == 1
  291.       plus = w - ts.width if a == 2
  292.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  293.       e = 0
  294.       for b in s.conv
  295.         x_b = x+plus+e
  296.         y_b = y + h/2 - ts.height/2
  297.         if font.shadow
  298.           img = Cache.system(f, "f"+b.to_s)
  299.           iw = img.width
  300.           stretch_blt(Rect.new(x_b+2,y_b+2,iw,fh),img,Rect.new(0,0,iw,16))
  301.         end
  302.         img = Cache.system(f, "f"+b.to_s, color_ind(font.color))
  303.         iw = img.width
  304.         stretch_blt(Rect.new(x_b,y_b,iw,fh),img,Rect.new(0,0,iw,16), font.color.alpha)
  305.         e += iw+2
  306.       end
  307.     end
  308.   end
  309. end
  310.  
  311. module Cache
  312. #module Cache#def self.system(filename, ind, col) <- rewritten
  313.   def self.system(filename, ind = "", col = "")
  314.     load_bitmap("Graphics/System/", filename, 0, ind, col)
  315.   end
  316. #module Cache#def self.load_bitmap(folder_name, filename, hue, ind, col) <- rewritten  
  317.   def self.load_bitmap(folder_name, filename, hue = 0, ind = "", col = "")
  318.     @cache ||= {}
  319.     plus = $TEST ? "Mid" : ""
  320.     path = trupath = plus + folder_name + filename
  321.     path += ":" + ind.to_s + ":" + col.to_s if (ind != "" || col != "")
  322.     if not @cache.include?(path) or @cache[path].disposed?
  323.       if filename.empty?
  324.         @cache[path] = Bitmap.new(32, 32)
  325.       else
  326.         if ind != ""
  327.           if ind[0] != "f"[0]
  328.             bit = Bitmap.new(trupath)
  329.             buf = Bitmap.new(8,8)
  330.             px = (ind%16)*8
  331.             py = (ind/16)*8
  332.             buf.blt(0,0,bit,Rect.new(px,py,8,8))
  333.             bit = buf.tru_img
  334.             buf.dispose
  335.           else
  336.             if FileTest.exist?(trupath+"/"+ind+".png")
  337.               bit = Bitmap.new(trupath+"/"+ind)
  338.             else
  339.               bit = Bitmap.new(1,16)
  340.             end
  341.           end
  342.           bit.recolour(col) if col != ""
  343.         else
  344.           bit = Bitmap.new(trupath)
  345.         end
  346.         @cache[path] = bit.double
  347.       end
  348.     end
  349.     if hue == 0
  350.       return @cache[path]
  351.     else
  352.       key = [path, hue]
  353.       if not @cache.include?(key) or @cache[key].disposed?
  354.         @cache[key] = @cache[path].clone
  355.         @cache[key].hue_change(hue)
  356.       end
  357.       return @cache[key]
  358.     end
  359.   end
  360. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement