Advertisement
rpgmakergraphics

[VX] RM2k/2k3 Graphics v1.4

Apr 12th, 2012
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.25 KB | None | 0 0
  1. # RM2k/2k3 Graphics v. 1.4
  2. # VX version
  3. # by mikb89
  4.  
  5. # Scriverò un po' italiano e un po' inglese, spero di non urtarvi ^^.
  6. # I'll write both in italian & english, I hope this won't get you angry ^^
  7.  
  8. # Guida italiano
  9.  
  10.   # Questo script permette di utilizzare grafica nello stile 2k/2k3 in quanto
  11.   # ogni immagine visualizzata viene ingrandita del 2x, come avveniva nei vecchi
  12.   # RPG Maker.
  13.  
  14.   # Per importare DIRETTAMENTE i charset del 2k/2k3 inserite
  15.   # il simbolo # davanti al nome del file. Esempio: '#Chara1'.
  16.   # Così facendo lo script mostrerà correttamente le direzioni dei
  17.   # charset (i charset del 2k/2k3 hanno infatti posizioni diverse).
  18.   # Ovviamente nell'importarli dovrete settare il colore trasparente.
  19.  
  20.   # La cartella MidGraphics contiene le immagini ridimensionate, mentre la Graphics
  21.   # serve per vedere le immagini nel programma.
  22.   # Il problema è che, anche se dagli script le immagini vengono raddoppiate, il vx
  23.   # non lo fa, perciò vi serve una copia delle immagini in dimensioni doppie.
  24.   # Se usate le RTP è già tutto funzionante.
  25.   # Se importate immagini vostre o ne modificate, mettetele in MidGraphics.
  26.   # Dopodiché, fatene una copia a dimensioni raddoppiate e mettetela in Graphics.
  27.   # Non importa la qualità, serve solo momentaneamente per gestirla correttamente
  28.   # nel progetto.
  29.   # Una volta che rilasciate il gioco, togliete (magari temporaneamente) la cartella
  30.   # Graphics, rinominate MidGraphics in Graphics e distribuite.
  31.   # Durante il test infatti le immagini vengono prese da MidGraphics, altrimenti da
  32.   # Graphics. Inoltre se distribuite tramite il comando del menu File in questo
  33.   # modo le immagini verranno normalmente criptate, se settate l'opzione.
  34.  
  35. # English guide
  36.  
  37.   # With this script you can use graphic files with a lower resolution, similar
  38.   # to the older RPG Maker. The size of the graphic will be doubled once drawn.
  39.  
  40.   # Old RPG Maker charsets can be used DIRECTLY by inserting a '#' in the file
  41.   # name. Ex: '#Chara1'.
  42.   # This way the script will correctly show the directions of the charsets
  43.   # (indeed charsets from the 2k/2k3 have different positions).
  44.   # You have to set the transparent colour when importing though.
  45.  
  46.   # MidGraphics folder contains resized images, while Graphics is used by the
  47.   # application, as usually.
  48.   # The problem is that vx CAN'T double images WHILE making your project, so
  49.   # we have to carry a double-copy of images, to use them in the project.
  50.   # If you use RTP you can go on, it already works.
  51.   # If you use other images or modify those existing, insert them in MidGraphics.
  52.   # Next, do a copy of them which is double-sized and insert in Graphics at the
  53.   # same location. No matter quality, it's just for pratical purpuose.
  54.   # Once you release the game, get rid (but not permanently!) of Graphics
  55.   # let MidGraphics take his place by renaming in Graphics and you're ready.
  56.   # During test, in fact, images are taken from MidGraphics, but in normal playing
  57.   # (by opening Game.exe) they came from Graphics. In this way you can release the
  58.   # project as usual also by menu File option and everything will work, included
  59.   # criptation for ones who cares about image copyright.
  60.  
  61. module RM2K
  62.   FONT_TYPE = 1
  63.   # Puoi scegliere fra:  //  You can choose between:
  64.  
  65.   # 0: Niente da dire, testo normale.
  66.  
  67.   # 0: Nothing to say, just tipical text.
  68.  
  69.   # 1: Metodo tipico RPG Maker 2k/2k3. Il colore dipende da OGNI pixel del
  70.   #    quadratino colore nella windowskin e il testo viene preso da una
  71.   #    palette bitmap chiamata Font.png dentro System. Ogni carattere ha
  72.   #    dimensioni max 8x8 ma non preoccupatevi per le i: se il carattere ha
  73.   #    spazi vuoti vengono riconosciuti ed eliminati.
  74.   #    Per corsivo e grassetto l'immagine è FontBI, per grassetto è FontB e
  75.   #    per corsivo è FontI.
  76.  
  77.   # 1: Tipical of RPG Maker 2k/2k3. Color depends by EACH pixel in windowskin
  78.   #    color and characters come from the Font.png image inside System.
  79.   #    Characters max size is 8x8 but don't worry about i: if there are empty
  80.   #    spaces before or after the character, they'll be (brutally) trimmed.
  81.   #    For bold and italic the image is FontBI, for just bold it's FontB and
  82.   #    for italic, FontI.
  83.  
  84.   # 2: Font normale. L'unica cosa è che viene prima dimezzato e poi raddoppiato,
  85.   #    con ovvia perdità di qualità. Potevo non fare questo giro ma il testo
  86.   #    normale mi pare che stoni.
  87.  
  88.   # 2: Usual font. The only thing to know is that the text is stretched and
  89.   #    enlarged to obtain a vintage (see also: awful) effect. That because the
  90.   #    normal font (in my opinion) doesn't fit with the doubled images style.
  91.  
  92.   # 3: Caratteri in immagini separate. Create dentro System la cartella Font;
  93.   #     dentro questa mettete i caratteri come f0, f1, f2 con il numero che
  94.   #     rappresenta il codice ASCII del carattere (es. f49 sarà lo zero).
  95.   #     Per il grassetto bisogna creare la cartella B, I per il corsivo e BI per
  96.   #     la combinazione delle due. B, I, BI stanno dentro Font.
  97.   #     In questa modalità la larghezza dei caratteri è decisa dall'immagine.
  98.  
  99.   # 3: Characters in separated image files. Create inside System the Font folder;
  100.   #     inside this, insert the characters as f0, f1, f2 with the number part
  101.   #     representing the ASCII code (f49 is the zero, for example).
  102.   #     For bold characters folder name will be B, I for italic, BI for both.
  103.   #     B, I, BI must be placed inside Font.
  104.   #     In this drawing type, characters width depend on image width.
  105.  
  106.   # Numero a casaccio: Non vi scriverà niente.
  107.  
  108.   # Random number: Won't see anything, somebody may like it...
  109.  
  110.   BLUR_SCREEN_ON_MENU = false
  111.   # Quando si accede in un menu l'immagine della mappa viene sfocata. Con false
  112.   # questo effetto viene rimosso.
  113.  
  114.   # Background map bitmap is commonly blurred when accessing menus. Setting false
  115.   # will remove this effect.
  116.  
  117.  
  118.   # Opzioni avanzate. Non toccare se non sai quello che fai.
  119.  
  120.   # Advanced settings. Don't touch unless you know what you're doing.
  121.  
  122.   STANDARD_FONT_SIZE = Font.default_size
  123.   # Questo serve come metro di paragone per ridimensionare i caratteri da immagine.
  124.  
  125.   # Needed for measuring the amount of resizing for character in images.
  126.  
  127. # Da qui in avanti è tutto script.  //  From now on it's just script.
  128.  
  129. end
  130.  
  131. #Codename: rm2k
  132.  
  133. ($imported ||= {})[:mikb89_rm2k] = true
  134.  
  135. # Codice per codifica accentate
  136. class String
  137.   def conv
  138.     ns = []
  139.     add_n = false
  140.     self.each_byte {|b|
  141.       if add_n
  142.         ns.push(b+64)
  143.         add_n = false
  144.       elsif b < 128
  145.         ns.push b
  146.         add_n = false
  147.       else
  148.         add_n = true
  149.       end
  150.     }
  151.     return ns
  152.   end
  153. end
  154.  
  155. class Fixnum
  156.   def conv
  157.     ns = []
  158.     self.to_s.each_byte {|b|
  159.       ns.push b
  160.     }
  161.     return ns
  162.   end
  163. end
  164.  
  165. # Codice vecchio
  166. class Font
  167.   attr_writer :font_type
  168.   def self.default_font_type
  169.     @default_font_type = RM2K::FONT_TYPE if @default_font_type == nil
  170.     return @default_font_type
  171.   end
  172.   def font_type
  173.     @font_type = Font.default_font_type if @font_type == nil
  174.     return @font_type
  175.   end
  176.   def self.default_font_type=(dm)
  177.     @default_font_type = dm
  178.   end
  179. end
  180.  
  181. class Bitmap
  182.   def double
  183.     b = Bitmap.new(width*2, height*2)
  184.     b.stretch_blt(b.rect, self, rect)
  185.     return b
  186.   end
  187.  
  188.   def tru_img
  189.     sx, fx = width-1, 0
  190.     c = Color.new(0,0,0)
  191.     for x in 0...width
  192.       e = false
  193.       for y in 0...height
  194.         if get_pixel(x,y) == c
  195.           e = true
  196.           break
  197.         end
  198.       end
  199.       if e == true
  200.         sx = x
  201.         break
  202.       end
  203.     end
  204.     for x in 1...width
  205.       e = false
  206.       for y in 0...height
  207.         if get_pixel(width-x,y) == c
  208.           e = true
  209.           break
  210.         end
  211.       end
  212.       if e == true
  213.         fx = width-x
  214.         break
  215.       end
  216.     end
  217.     if fx+1 > sx
  218.       bit = Bitmap.new(fx-sx+1,height)
  219.       bit.blt(0,0,self,Rect.new(sx,0,fx-sx+1,height))
  220.       return bit
  221.     else
  222.       return Bitmap.new(width/2,height)
  223.     end
  224.   end
  225.  
  226.   def recolour(ind)
  227.     c = Color.new(0,0,0)
  228.     dum = Bitmap.new(8,8)
  229.     dum.blt(0,0,Cache.system("Window"),Rect.new(64+(ind%8)*8,96+(ind/8)*8,8,8))
  230.     for x in 0...width
  231.       for y in 0...height
  232.         set_pixel(x,y,dum.get_pixel(x,y)) if get_pixel(x,y) == c
  233.       end
  234.     end
  235.   end
  236.  
  237.   def color_ind(col)
  238.     for i in 0...32
  239.       return i if text_color(i).red == col.red &&
  240.                   text_color(i).green == col.green &&
  241.                   text_color(i).blue == col.blue
  242.     end
  243.     @@xtra_col ||= []
  244.     for c in 0...@@xtra_col.size
  245.       return 32+c if @@xtra_col[c].red == col.red &&
  246.                      @@xtra_col[c].green == col.green &&
  247.                      @@xtra_col[c].blue == col.blue
  248.     end
  249.     @@xtra_col.push(col)
  250.     return 31+@@xtra_col.size
  251.   end
  252.  
  253.   def text_color(n)
  254.     if n < 32
  255.       x = 64 + (n % 8) * 8
  256.       y = 96 + (n / 8) * 8
  257.       return Cache.system("Window").get_pixel(x, y)
  258.     end
  259.     return Color.new(0,0,0) if (@@xtra_col ||= []).size <= n-32
  260.     return @@xtra_col[n-32]
  261.   end
  262.  
  263.   alias_method :text_size_b4_rm2k, :text_size unless method_defined?(:text_size_b4_rm2k)
  264.   def text_size(str)
  265.     m = font.font_type
  266.     case m
  267.     when 1,3
  268.       f = "Font"
  269.       f += "/" if m==3 && font.bold || font.italic
  270.       f += "B" if font.bold
  271.       f += "I" if font.italic
  272.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  273.       e = font.shadow ? 1 : 0
  274.       for b in str.conv
  275.         b = "f" + b.to_s if m==3
  276.         img = Cache.system(f, b)
  277.         e += img.width+2
  278.       end
  279.       return Rect.new(0,4-(fh-16)/2,e,fh)
  280.     when 0,2
  281.       return text_size_b4_rm2k(str)
  282.     end
  283.     return Rect.new(0,0,1,1)
  284.   end
  285.  
  286.   alias_method :draw_text_b4_rm2k, :draw_text unless method_defined?(:draw_text_b4_rm2k)
  287.   def draw_text(*t)
  288.     case t.size
  289.     when 2,3
  290.       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
  291.     when 5,6
  292.       x,y,w,h,s,a = t[0],t[1],t[2],t[3],t[4], t.size==6? t[5] : 0
  293.     end
  294.     m = font.font_type
  295.     case m
  296.     when 0 # nessun cambiamento
  297.       draw_text_b4_rm2k(x,y,w,h,s,a)
  298.     when 1 # usa una tavolozza font 8x8
  299.       f = "Font"
  300.       f += "B" if font.bold
  301.       f += "I" if font.italic
  302.       plus = 0
  303.       ts = text_size(s)
  304.       plus = w/2 - ts.width/2 - 1 if a == 1
  305.       plus = w - ts.width if a == 2
  306.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  307.       e = 0
  308.       for b in s.conv
  309.         x_b = x+plus+e
  310.         y_b = y + h/2 - ts.height/2
  311.         if font.shadow
  312.           img = Cache.system(f, b)
  313.           iw = img.width
  314.           stretch_blt(Rect.new(x_b+2,y_b+2,iw,fh),img,Rect.new(0,0,iw,16))
  315.         end
  316.         img = Cache.system(f, b, color_ind(font.color))
  317.         iw = img.width
  318.         stretch_blt(Rect.new(x_b,y_b,iw,fh),img,Rect.new(0,0,iw,16), font.color.alpha)
  319.         e += iw+2
  320.       end
  321.     when 2 # usa font normale
  322.       b = Bitmap.new(w/2,h/2)
  323.       b.font = font.clone
  324.       b.font.size = font.size/2
  325.       b.draw_text_b4_rm2k(0,0,w/2,h/2,s,a)
  326.       stretch_blt(Rect.new(x,y,w,h), b, b.rect)
  327.       b.dispose
  328.     when 3 # usa file immagine Wx8 in cartelle (dove W è variabile)
  329.       f = "Font"
  330.       if font.bold
  331.         f += "/B"
  332.         f += "I" if font.italic
  333.       elsif font.italic
  334.         f += "/I"
  335.       end
  336.       plus = 0
  337.       ts = text_size(s)
  338.       plus = w/2 - ts.width/2 - 1 if a == 1
  339.       plus = w - ts.width if a == 2
  340.       fh = (font.size*16)/RM2K::STANDARD_FONT_SIZE
  341.       e = 0
  342.       for b in s.conv
  343.         x_b = x+plus+e
  344.         y_b = y + h/2 - ts.height/2
  345.         if font.shadow
  346.           img = Cache.system(f, "f"+b.to_s)
  347.           iw = img.width
  348.           stretch_blt(Rect.new(x_b+2,y_b+2,iw,fh),img,Rect.new(0,0,iw,16))
  349.         end
  350.         img = Cache.system(f, "f"+b.to_s, color_ind(font.color))
  351.         iw = img.width
  352.         stretch_blt(Rect.new(x_b,y_b,iw,fh),img,Rect.new(0,0,iw,16), font.color.alpha)
  353.         e += iw+2
  354.       end
  355.     end
  356.   end
  357. end
  358.  
  359. module Cache
  360.   def self.system(filename, ind = "", col = "")
  361.     load_bitmap("Graphics/System/", filename, 0, ind, col)
  362.   end
  363.  
  364.   def self.load_bitmap(folder_name, filename, hue = 0, ind = "", col = "")
  365.     @cache ||= {}
  366.     plus = $TEST? "Mid" : ""
  367.     path = trupath = plus + folder_name + filename
  368.     path += ":" + ind.to_s + ":" + col.to_s if (ind != "" || col != "")
  369.     if not @cache.include?(path) or @cache[path].disposed?
  370.       if filename.empty?
  371.         @cache[path] = Bitmap.new(32, 32)
  372.       else
  373.         if ind != ""
  374.           if ind[0] != "f"[0]
  375.             bit = Bitmap.new(trupath)
  376.             buf = Bitmap.new(8,8)
  377.             px = (ind%16)*8
  378.             py = (ind/16)*8
  379.             buf.blt(0,0,bit,Rect.new(px,py,8,8))
  380.             bit = buf.tru_img
  381.             buf.dispose
  382.           else
  383.             if FileTest.exist?(trupath+"/"+ind+".png")
  384.               bit = Bitmap.new(trupath+"/"+ind)
  385.             else
  386.               bit = Bitmap.new(1,16)
  387.             end
  388.           end
  389.           bit.recolour(col) if col != ""
  390.         else
  391.           bit = Bitmap.new(trupath)
  392.         end
  393.         @cache[path] = bit.double
  394.       end
  395.     end
  396.     if hue == 0
  397.       return @cache[path]
  398.     else
  399.       key = [path, hue]
  400.       if not @cache.include?(key) or @cache[key].disposed?
  401.         @cache[key] = @cache[path].clone
  402.         @cache[key].hue_change(hue)
  403.       end
  404.       return @cache[key]
  405.     end
  406.   end
  407. end
  408.  
  409. class Sprite_Character < Sprite_Base
  410.    def update_src_rect
  411.     if @tile_id == 0
  412.       index = @character.character_index
  413.       pattern = @character.pattern < 3 ? @character.pattern : 1
  414.       sx = (index % 4 * 3 + pattern) * @cw
  415.       dir = 0
  416.       if @character_name.include?("#")
  417.         case @character.direction
  418.         when 2; dir = 2
  419.         when 4; dir = 3
  420.         when 6; dir = 1
  421.         when 8; dir = 0
  422.         end
  423.       else
  424.         dir = (@character.direction - 2) / 2
  425.       end
  426.       sy = (index / 4 * 4 + dir) * @ch
  427.       self.src_rect.set(sx, sy, @cw, @ch)
  428.     end
  429.   end
  430. end
  431.  
  432. class Scene_Base
  433.   def snapshot_for_background
  434.     $game_temp.background_bitmap.dispose
  435.     $game_temp.background_bitmap = Graphics.snap_to_bitmap
  436.     $game_temp.background_bitmap.blur if RM2K::BLUR_SCREEN_ON_MENU
  437.   end
  438. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement