Advertisement
mikb89

[XP] RM2k/2k3 Graphics (320x240) v1.5

Jun 17th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.17 KB | None | 0 0
  1. # RM2k/2k3 Graphics (320x240) v. 1.5
  2. # XP 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. Characters come from the Font.png image
  29.     #     inside Graphics, in the subfolder you can choose next. Characters max
  30.     #     size is 8x8 but don't worry about the i: if there are empty spaces
  31.     #     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 Graphics the 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.   FONT_FOLDER = "Windowskins/"
  46.    # Where are the fonts IF method is 1? (Fonts in this case are the four images
  47.    #  Font, FontB, FontI and FontBI). Write "Fonts/" if you want to use the same
  48.    #  folder as the other method.
  49.  
  50.  
  51.   # Advanced settings. Don't touch unless you know what you're doing.
  52.  
  53.   STANDARD_FONT_SIZE = Font.default_size
  54.    # Needed for measuring the amount of resizing for character in images.
  55. end
  56.  
  57. #Codename: rm2k
  58.  
  59. ($imported ||= {})[:mikb89_rm2k] = true
  60.  
  61. # License:
  62. # - You can ask me to include support for other scripts as long as these scripts
  63. #   use the $imported[script] = true;
  64. # - You can modify and even repost my scripts, after having received a response
  65. #   by me. For reposting it, anyway, you must have done heavy edit or porting,
  66. #   you can't do a post with the script as is;
  67. # - You can use my scripts for whatever you want, from free to open to
  68. #   commercial games. I'd appreciate by the way if you let me know about what
  69. #   you're doing;
  70. # - You must credit me, if you use this script or part of it.
  71.  
  72. # Codice per codifica accentate
  73. class String
  74. #class String#def conv()
  75.   def conv # convert to a byte array
  76.     ns = []
  77.     add_n = false
  78.     self.each_byte {|b|
  79.       if add_n
  80.         ns.push(b+64)
  81.         add_n = false
  82.       elsif b < 128
  83.         ns.push(b)
  84.         add_n = false
  85.       else
  86.         add_n = true
  87.       end
  88.     }
  89.     ns
  90.   end
  91. end
  92.  
  93. class Fixnum
  94. #class Fixnum#def conv()
  95.   def conv # convert to a byte array
  96.     ns = []
  97.     self.to_s.each_byte {|b| ns << b}
  98.     ns
  99.   end
  100. end
  101.  
  102. # Codice vecchio
  103. class Font
  104.   attr_writer :font_type
  105. #class Font#def self.default_font_type()
  106.   def self.default_font_type
  107.     @default_font_type = RM2K::FONT_TYPE if @default_font_type == nil
  108.     @default_font_type
  109.   end
  110. #class Font#def self.default_font_type=(dm)
  111.   def self.default_font_type=(dm)
  112.     @default_font_type = dm
  113.   end
  114. #class Font#def font_type()
  115.   def font_type
  116.     @font_type = Font.default_font_type if @font_type == nil
  117.     @font_type
  118.   end
  119. end
  120.  
  121. class Bitmap
  122. #class Bitmap#def double()
  123.   def double
  124.     b = Bitmap.new(width*2, height*2)
  125.     b.stretch_blt(b.rect, self, rect)
  126.     b
  127.   end
  128. #class Bitmap#def tru_img()
  129.   def tru_img # get the image without trasparent left and right borders
  130.     sx, fx = width-1, 0
  131.     c = Color.new(0,0,0)
  132.     for x in 0...width
  133.       e = false
  134.       for y in 0...height
  135.         if get_pixel(x,y) == c
  136.           e = true
  137.           break
  138.         end
  139.       end
  140.       if e == true
  141.         sx = x
  142.         break
  143.       end
  144.     end
  145.     for x in 1...width
  146.       e = false
  147.       for y in 0...height
  148.         if get_pixel(width-x,y) == c
  149.           e = true
  150.           break
  151.         end
  152.       end
  153.       if e == true
  154.         fx = width-x
  155.         break
  156.       end
  157.     end
  158.     if fx+1 > sx
  159.       bit = Bitmap.new(fx-sx+1,height)
  160.       bit.blt(0,0,self,Rect.new(sx,0,fx-sx+1,height))
  161.       return bit
  162.     else
  163.       return Bitmap.new(width/2,height)
  164.     end
  165.   end
  166. #class Bitmap#def recolour(ind)
  167.   def recolour(ind) # color change from c to color ind(ex) in Window_Base/cache
  168.     c = Color.new(0,0,0)
  169.     p = text_color(ind)
  170.     for x in 0...width
  171.       for y in 0...height
  172.         set_pixel(x,y,p) if get_pixel(x,y) == c
  173.       end
  174.     end
  175.   end
  176. #class Bitmap#def color_ind(col)
  177.   def color_ind(col) # get the index of color if found in palette or cache
  178.     for i in 0...8
  179.       return i if text_color(i).red == col.red &&
  180.                   text_color(i).green == col.green &&
  181.                   text_color(i).blue == col.blue
  182.     end
  183.     @@xtra_col ||= []
  184.     for c in 0...@@xtra_col.size
  185.       return 8+c if @@xtra_col[c].red == col.red &&
  186.                     @@xtra_col[c].green == col.green &&
  187.                     @@xtra_col[c].blue == col.blue
  188.     end
  189.     @@xtra_col.push(col)
  190.     return 7+@@xtra_col.size
  191.   end
  192. #class Bitmap#def text_color(n)
  193.   def text_color(n) # get color from palette or from cache
  194.     if n < 8
  195.       w = Window_Base.new(-33,-33,33,33)
  196.       p = w.text_color(n)
  197.       w.dispose
  198.       return p
  199.     end
  200.     return Color.new(0,0,0) if (@@xtra_col ||= []).size <= n-8
  201.     return @@xtra_col[n-8]
  202.   end
  203.   alias_method(:text_size_b4_rm2k, :text_size) unless method_defined?(:text_size_b4_rm2k)
  204. #class Bitmap#def text_size(str) <- aliased
  205.   def text_size(str)
  206.     m = font.font_type
  207.     case m
  208.     when 1,3
  209.       f = "Font" + ((m == 1) ? "" : "s/")
  210.       f += "B" if font.bold
  211.       f += "I" if font.italic
  212.       f += "/" if m==3 && (font.bold || font.italic)
  213.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  214.       e = ((font.respond_to?(:shadow) && font.shadow) ? 1 : 0)
  215.       for b in str.conv
  216.         b = "f" + b.to_s if m==3
  217.         img = RPG::Cache.font(f, b)
  218.         e += img.width+2
  219.       end
  220.       return Rect.new(0,4-(fh-16)/2,e,fh)
  221.     when 0,2
  222.       return text_size_b4_rm2k(str)
  223.     end
  224.     return Rect.new(0,0,1,1)
  225.   end
  226.   alias_method(:draw_text_b4_rm2k, :draw_text) unless method_defined?(:draw_text_b4_rm2k)
  227. #class Bitmap#def draw_text(*t) <- aliased
  228.   def draw_text(*t)
  229.     case t.size
  230.     when 2,3
  231.       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
  232.     when 5,6
  233.       x,y,w,h,s,a = t[0],t[1],t[2],t[3],t[4],t.size==6 ? t[5] : 0
  234.     end
  235.     m = font.font_type
  236.     case m
  237.     when 0 # nessun cambiamento
  238.       draw_text_b4_rm2k(x,y,w,h,s,a)
  239.     when 1 # usa una tavolozza font 8x8
  240.       f = "Font"
  241.       f += "B" if font.bold
  242.       f += "I" if font.italic
  243.       plus = 0
  244.       ts = text_size(s)
  245.       plus = w/2 - ts.width/2 - 1 if a == 1
  246.       plus = w - ts.width if a == 2
  247.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  248.       e = 0
  249.       for b in s.conv
  250.         x_b = x+plus+e
  251.         y_b = y + h/2 - ts.height/2
  252.         #if font.shadow
  253.         #  img = RPG::Cache.font(f, b)
  254.         #  iw = img.width
  255.         #  stretch_blt(Rect.new(x_b+2,y_b+2,iw,fh),img,Rect.new(0,0,iw,16))
  256.         #end
  257.         img = RPG::Cache.font(f, b, color_ind(font.color))
  258.         iw = img.width
  259.         stretch_blt(Rect.new(x_b,y_b,iw,fh),img,Rect.new(0,0,iw,16), font.color.alpha)
  260.         e += iw+2
  261.       end
  262.     when 2 # usa font normale
  263.       b = Bitmap.new(w/2,h/2)
  264.       b.font = font.clone
  265.       b.font.size = font.size/2
  266.       b.draw_text_b4_rm2k(0,0,w/2,h/2,s,a)
  267.       stretch_blt(Rect.new(x,y,w,h), b, b.rect)
  268.       b.dispose
  269.     when 3 # usa file immagine Wx8 in cartelle (dove W è variabile)
  270.       f = "Fonts/"
  271.       f += "B" if font.bold
  272.       f += "I" if font.italic
  273.       f += "/" if font.bold || font.italic
  274.       plus = 0
  275.       ts = text_size(s)
  276.       plus = w/2 - ts.width/2 - 1 if a == 1
  277.       plus = w - ts.width if a == 2
  278.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  279.       e = 0
  280.       for b in s.conv
  281.         x_b = x+plus+e
  282.         y_b = y + h/2 - ts.height/2
  283.         #if font.shadow
  284.         #  img = RPG::Cache.font(f, "f"+b.to_s)
  285.         #  iw = img.width
  286.         #  stretch_blt(Rect.new(x_b+2,y_b+2,iw,fh),img,Rect.new(0,0,iw,16))
  287.         #end
  288.         img = RPG::Cache.font(f, "f"+b.to_s, color_ind(font.color))
  289.         iw = img.width
  290.         stretch_blt(Rect.new(x_b,y_b,iw,fh),img,Rect.new(0,0,iw,16), font.color.alpha)
  291.         e += iw+2
  292.       end
  293.     end
  294.   end
  295. end
  296.  
  297. module RPG::Cache
  298. #module RPG::Cache#def self.font(filename, ind, col)
  299.   def self.font(filename, ind = "", col = "")
  300.     fold = "Graphics/"
  301.     fold += RM2K::FONT_FOLDER if ind.is_a?(Numeric)
  302.     load_bitmap(fold, filename, 0, ind, col)
  303.   end
  304. #module RPG::Cache#def self.load_bitmap(folder_name, filename, hue, ind, col) <- rewritten  
  305.   def self.load_bitmap(folder_name, filename, hue = 0, ind = "", col = "")
  306.     plus = $DEBUG ? "Mid" : ""
  307.     path = trupath = plus + folder_name + filename
  308.     path += ":" + ind.to_s + ":" + col.to_s if (ind != "" || col != "")
  309.     if not @cache.include?(path) or @cache[path].disposed?
  310.       if filename.empty?
  311.         @cache[path] = Bitmap.new(32, 32)
  312.       else
  313.         if ind != ""
  314.           if ind[0] != "f"[0]
  315.             bit = Bitmap.new(trupath)
  316.             buf = Bitmap.new(8,8)
  317.             px = (ind%16)*8
  318.             py = (ind/16)*8
  319.             buf.blt(0,0,bit,Rect.new(px,py,8,8))
  320.             bit = buf.tru_img
  321.             buf.dispose
  322.           else
  323.             if FileTest.exist?(trupath+ind+".png")
  324.               bit = Bitmap.new(trupath+ind)
  325.             else
  326.               bit = Bitmap.new(1,16)
  327.             end
  328.           end
  329.           bit.recolour(col) if col != ""
  330.         else
  331.           bit = Bitmap.new(trupath)
  332.         end
  333.         @cache[path] = bit.double
  334.       end
  335.     end
  336.     if hue == 0
  337.       @cache[path]
  338.     else
  339.       key = [path, hue]
  340.       if not @cache.include?(key) or @cache[key].disposed?
  341.         @cache[key] = @cache[path].clone
  342.         @cache[key].hue_change(hue)
  343.       end
  344.       @cache[key]
  345.     end
  346.   end
  347. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement