查看: 3074|回复: 1

[原创发布] 重写“半生烛光”

[复制链接]
梦石
0
星屑
6483
在线时间
119 小时
注册时间
2020-1-8
回帖
205
发表于 2022-11-23 09:52:18 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 RPGzh500223 于 2022-11-25 12:08 编辑

整体还是很鸡肋,效果基本没变化,懒得做gif示意图了。
烛火可以自定义颜色,支持不透明度变化实现明暗变化,API效率提升,代码是关于灯光效果的实现,关于交互自行修改添加吧。
Light.dll  VS2017 C语言编写

  1. class Rect
  2.   def to_int
  3.     self.__id__
  4.   end
  5. end

  6. class Bitmap
  7.   def to_int
  8.     self.__id__
  9.   end
  10. end


  11. module LIGHT
  12.   # 并不严谨的标志,但是方便
  13.   Flag = "set_light"
  14.   
  15.   Pic_Dir = "Graphics/Light_Textures"
  16.   
  17.   # 透明通道图片,有效值为alpha,填充的颜色能看清图形就好,建议黑色
  18.   Light_Type_Pic_Name = {
  19.     0 => "round.png"
  20.   }
  21.   
  22.   Light_Effect_Rect_Frame = 10
  23.   
  24.   dll = "Light.dll"
  25.   
  26.   # 源位图双线性插值缩放至目标位图(无混合)
  27.   # dst_bitmap_obj_id, src_bitmap_obj_id
  28.   Stretch = Win32API.new(dll, 'bitmap_stretch_move', 'LL', 'L')
  29.   
  30.   # ————灯光专用方法————
  31.   # 以纹理的alpha值进行颜色填充,再与黑色(0xFF000000)进行混合(Bitmap#blt)
  32.   # alpha: 0      <=   threshold[0..7]    <    threshold[8..15]     <=    255
  33.   # 颜色 :low_rgb24     |阈值下限|  HSL_gradient  |阈值上限|      high_rgb24  
  34.   # textutre_bitmap_obj_id
  35.   # high_rgb24    0..23位有效值
  36.   # low_rgb24     0..23位有效值   
  37.   #               该值为-1的话,单色(high_rgb24)填充,阈值无效
  38.   # alpha_threshold16   0..7位 阈值下限 (须小于) 8..15位 阈值上限  
  39.   Frgb2 = Win32API.new(dll, 'bitmap_full_rgb2_blt_black', 'LLLL', 'L')
  40.   
  41.   # 源位图区域与目标位图指定位置进行乘算叠加
  42.   # 计算:源RGB预乘opacity,反相后再与目标RGB乘算叠加,参数基本类似Btimap#blt
  43.   # dst_bitmap_obj_id, dx, dy,
  44.   # src_bitmap_obj_id, src_bitmap_rect_obj_id, opacity
  45.   Mul = Win32API.new(dll, 'bitmap_light_mul', 'LLLLLL', 'L')
  46.   
  47.   # 以指定alpha的白色对位图进行填充
  48.   # dst_bitmap_obj_id
  49.   # alpha8   0..7位有效值
  50.   Full = Win32API.new(dll, 'bitmap_full_white_alpha', 'LL', 'L')

  51.   
  52.   @@cache = {}
  53.   def self.cache(type)
  54.     @@cache[type] ||= Bitmap.new(
  55.       File.join(Pic_Dir, Light_Type_Pic_Name[type] || Light_Type_Pic_Name[0]))   
  56.   end
  57.    
  58.   def self.cache_clear
  59.     @@cache.values.each {|bitmap| bitmap.dispose}
  60.     @@cache.clear
  61.   end
  62.   
  63.   
  64.   module Macro
  65.     def rgb2int(r, g, b)
  66.       r << 16 | g << 8 | b
  67.     end  
  68.     alias RGB rgb2int
  69.     alias IXY rgb2int
  70.    
  71.     def argb2int(a, r, g, b)
  72.       a << 24 | r << 16 | g << 8 | b
  73.     end
  74.     alias DINX argb2int
  75.   end
  76.   
  77.   module Interface
  78.     def light_erase
  79.       @light_data ||= []
  80.       @light_data.each do |i|
  81.         $game_map.light_data.delete(i)
  82.         i.bitmap_dispose
  83.       end
  84.       @light_data.clear
  85.     end
  86.   
  87.     # type          纹理样式,参见 Light_Type_Pic_Name
  88.     # rgb24_high/low/threshold   颜色值相关参数,参见 Frgb2
  89.    
  90.     # zoom_x/y      缩放系数
  91.     # off_x/y       偏移
  92.     # center_type   对齐中心位置,参考小键盘数字的位置
  93.    
  94.     # alpha_delta/ini/min/max   不透明度相关参数,“一个参数”
  95.    
  96.     # cell_idx/xsize/ysize      帧动画的参数,“一个参数”   
  97.     #               效果参考 RMXP人物【停止动画】
  98.     #               单元序列,自左向右,从上到下,4 * 4如下所示
  99.     #                    0 —— (xsize-1)
  100.     #                0     0  1  2  3
  101.     #                |     4  5  6  7
  102.     #                |     8  9  A  B
  103.     #             ysize-1  C  D  E  F

  104.     def set_light(type = 0, high_rgb24 = 0xFFFFFF, low_rgb24 = -1,
  105.       rgb_threshold = 0xFF00, zoom_x = 1, zoom_y = 1, off_x = 0, off_y = 0,
  106.       center_type = 5, alpha_dinx = 0x00FFFFFF, cell_ixy = 0x000101)
  107.    
  108.       texture = LIGHT.cache(type)
  109.    
  110.       w, h = (texture.width * zoom_x).to_i, (texture.height * zoom_y).to_i
  111.       return if w < 2 || h < 2
  112.    
  113.       if w == texture.width && h == texture.height
  114.         bitmap = texture.clone
  115.       else
  116.         bitmap = Bitmap.new(w, h)
  117.         LIGHT::Stretch.call(bitmap, texture)
  118.       end
  119.    
  120.       LIGHT::Frgb2.call(bitmap, high_rgb24, low_rgb24, rgb_threshold)
  121.    
  122.       light = LightEffect.new(self, bitmap, off_x, off_y, center_type,
  123.         alpha_dinx, cell_ixy)
  124.    
  125.       @light_data << light
  126.       $game_map.light_data << light
  127.     end
  128.   end
  129. end


  130. class LightEffect
  131.   
  132.   def initialize(event, bitmap, offX, offY, center_type, alpha_dinx, cell_ixy)
  133.    
  134.     @event, @bitmap = event, bitmap
  135.    
  136.     cell_idx = (cell_ixy & 0xFF0000) >> 16
  137.     cell_xsize = (cell_ixy & 0xFF00) >> 8
  138.     cell_ysize = cell_ixy & 0xFF
  139.    
  140.     cell_x, cell_y = cell_idx % cell_xsize, cell_idx / cell_xsize
  141.     cell_w, cell_h = bitmap.width / cell_xsize, bitmap.height / cell_ysize
  142.     @src_rect = Rect.new(cell_x * cell_w, cell_y * cell_h, cell_w, cell_h)
  143.    
  144.     @off_x = offX + 16 - (center_type % 3 - 1) * cell_w / 2
  145.     @off_y = offY + 16 - (2 - center_type / 3) * cell_h / 2
  146.    
  147.     @rect_active = cell_xsize > 1   
  148.    
  149.     @delta_alpha = alpha_dinx >> 24
  150.     @alpha = (alpha_dinx & 0xFF0000) >> 16
  151.     @min_alpha = (alpha_dinx & 0xFF00) >> 8
  152.     @max_alpha = alpha_dinx & 0xFF
  153.     if @min_alpha > @max_alpha
  154.       @min_alpha, @max_alpha = @max_alpha, @min_alpha
  155.     end
  156.   end
  157.   
  158.   def display_at(target_bitmap)
  159.     self.alpha_update
  160.     return if @alpha == 0
  161.    
  162.     dx = (@event.real_x - $game_map.display_x + 3) / 4 + @off_x
  163.     return if dx >= target_bitmap.width            #left
  164.     return if dx + @src_rect.width - 1 <= 0        #right
  165.    
  166.     dy = (@event.real_y - $game_map.display_y + 3) / 4 + @off_y
  167.     return if dy >= target_bitmap.height           #top
  168.     return if dy + @src_rect.height - 1 <= 0       #bottom
  169.    
  170.     LIGHT::Mul.call(target_bitmap, dx, dy, @bitmap, @src_rect, @alpha)
  171.   end
  172.   
  173.   def alpha_update
  174.     if @delta_alpha != 0
  175.       @alpha += @delta_alpha
  176.       if @alpha > @max_alpha
  177.         @alpha, @delta_alpha = @max_alpha, -@delta_alpha
  178.       end
  179.       if @alpha < @min_alpha
  180.         @alpha, @delta_alpha = @min_alpha, -@delta_alpha
  181.       end
  182.     end
  183.   end
  184.   
  185.   def src_rect_update
  186.     if @rect_active == true
  187.       @src_rect.x += @src_rect.width
  188.       @src_rect.x = 0 if @src_rect.x >= @bitmap.width
  189.     end
  190.   end
  191.   
  192.   def bitmap_dispose
  193.     @bitmap.dispose
  194.   end
  195. end


  196. class LightRenderTarget
  197.   
  198.   def initialize(viewport)
  199.     @alpha = -1
  200.     self.alpha = 128
  201.    
  202.     return if $game_map.light_switch == false
  203.    
  204.     @rt = Sprite.new(viewport)
  205.     @target = Bitmap.new(viewport.rect.width, viewport.rect.height)
  206.     @rt.bitmap = @target
  207.     @rt.blend_type = 2
  208.     @rt.z = 1000
  209.   end
  210.   
  211.   def alpha=(new_alpha)
  212.     return if @alpha == new_alpha
  213.     @alpha = new_alpha < 0 ? 0 : (new_alpha > 255 ? 255 : new_alpha)
  214.   end
  215.   
  216.   def update
  217.     return if $game_map.light_switch == false
  218.     return if @alpha == 0
  219.    
  220.     if Graphics.frame_count % LIGHT::Light_Effect_Rect_Frame == 0
  221.       $game_map.light_data.each {|i| i.src_rect_update}  
  222.     end
  223.    
  224.     LIGHT::Full.call(@target, @alpha)   
  225.     $game_map.light_data.each {|i| i.display_at(@target)}
  226.   end
  227.   
  228.   def dispose
  229.     if @target != nil
  230.       @target.dispose
  231.       @rt.dispose
  232.       @target = nil
  233.     end
  234.    
  235.     LIGHT.cache_clear
  236.   end
  237. end

  238. class Interpreter
  239.   include LIGHT::Macro
  240. end

  241. class Game_Player
  242.   include LIGHT::Interface
  243.   
  244.   def light_on(*args)
  245.     @light_data ||= []
  246.     self.set_light(*args)
  247.   end
  248.   
  249.   # 其实类似手电筒,根据方向调整 center_type, off_x, off_y, cell_idx就可以实现
  250. end


  251. #———以下代码有大部分RMXP原代码,建议直接添加至原代码———

  252. class Game_Map
  253.   attr_reader   :light_data
  254.   attr_reader   :light_switch
  255.   #--------------------------------------------------------------------------
  256.   # ● 初始化条件
  257.   #--------------------------------------------------------------------------
  258.   def initialize
  259.     @map_id = 0
  260.     @display_x = 0
  261.     @display_y = 0
  262.    
  263.     @light_data = []
  264.     @light_switch = false
  265.   end
  266.   #--------------------------------------------------------------------------
  267.   # ● 设置
  268.   #     map_id : 地图 ID
  269.   #--------------------------------------------------------------------------
  270.   def setup(map_id)
  271.     # 地图 ID 记录到 @map_id
  272.     @map_id = map_id
  273.     # 地图文件装载后、设置到 @map
  274.     @map = load_data(sprintf("Data/Map%03d.rxdata", @map_id))
  275.     # 定义实例变量设置地图元件信息
  276.     tileset = $data_tilesets[@map.tileset_id]
  277.     @tileset_name = tileset.tileset_name
  278.     @autotile_names = tileset.autotile_names
  279.     @panorama_name = tileset.panorama_name
  280.     @panorama_hue = tileset.panorama_hue
  281.     @fog_name = tileset.fog_name
  282.     @fog_hue = tileset.fog_hue
  283.     @fog_opacity = tileset.fog_opacity
  284.     @fog_blend_type = tileset.fog_blend_type
  285.     @fog_zoom = tileset.fog_zoom
  286.     @fog_sx = tileset.fog_sx
  287.     @fog_sy = tileset.fog_sy
  288.     @battleback_name = tileset.battleback_name
  289.     @passages = tileset.passages
  290.     @priorities = tileset.priorities
  291.     @terrain_tags = tileset.terrain_tags
  292.     # 初始化显示坐标
  293.     @display_x = 0
  294.     @display_y = 0
  295.     # 清除刷新要求标志
  296.     @need_refresh = false
  297.      
  298.     # 释放上一地图的灯光对象
  299.     @light_data.each{|i| i.bitmap_dispose}
  300.     @light_data.clear
  301.    
  302.     # 设置地图事件数据
  303.     @events = {}
  304.     for i in @map.events.keys
  305.       @events[i] = Game_Event.new(@map_id, @map.events[i])
  306.     end
  307.    
  308.     @light_switch = !@light_data.empty?
  309.         
  310.     # 设置公共事件数据
  311.     @common_events = {}
  312.     for i in 1...$data_common_events.size
  313.       @common_events[i] = Game_CommonEvent.new(i)
  314.     end
  315.     # 初始化雾的各种信息
  316.     @fog_ox = 0
  317.     @fog_oy = 0
  318.     @fog_tone = Tone.new(0, 0, 0, 0)
  319.     @fog_tone_target = Tone.new(0, 0, 0, 0)
  320.     @fog_tone_duration = 0
  321.     @fog_opacity_duration = 0
  322.     @fog_opacity_target = 0
  323.     # 初始化滚动信息
  324.     @scroll_direction = 2
  325.     @scroll_rest = 0
  326.     @scroll_speed = 4
  327.   end
  328. end


  329. class Game_Event
  330.   include LIGHT::Macro
  331.   include LIGHT::Interface
  332.    
  333.   # !!注释即代码,不能乱写!!
  334.   # 只在“第一个指令块”有效。
  335.   # 标志:事件当前页的第一条指令set_light(...);
  336.   def refresh_light
  337.     if @list
  338.       if @list[0].code == 108
  339.         if (params = @list[0].parameters[0]).include?(LIGHT::Flag)
  340.           i, scripts = 1, params.dup
  341.          
  342.           while (@list[i].code == 408)
  343.             scripts << @list[i].parameters[0]
  344.             i += 1
  345.           end
  346.          
  347.           self.light_erase
  348.           eval(scripts)
  349.         end
  350.       end
  351.     end
  352.   end
  353.    
  354.   #--------------------------------------------------------------------------
  355.   # ● 刷新
  356.   #--------------------------------------------------------------------------
  357.   def refresh
  358.     # 初始化本地变量 new_page
  359.     new_page = nil
  360.     # 无法暂时消失的情况下
  361.     unless @erased
  362.       # 从编号大的事件页按顺序调查
  363.       for page in @event.pages.reverse
  364.         # 可以参考事件条件 c
  365.         c = page.condition
  366.         # 确认开关条件 1
  367.         if c.switch1_valid
  368.           if $game_switches[c.switch1_id] == false
  369.             next
  370.           end
  371.         end
  372.         # 确认开关条件 2
  373.         if c.switch2_valid
  374.           if $game_switches[c.switch2_id] == false
  375.             next
  376.           end
  377.         end
  378.         # 确认变量条件
  379.         if c.variable_valid
  380.           if $game_variables[c.variable_id] < c.variable_value
  381.             next
  382.           end
  383.         end
  384.         # 确认独立开关条件
  385.         if c.self_switch_valid
  386.           key = [@map_id, @event.id, c.self_switch_ch]
  387.           if $game_self_switches[key] != true
  388.             next
  389.           end
  390.         end
  391.         # 设置本地变量 new_page
  392.         new_page = page
  393.         # 跳出循环
  394.         break
  395.       end
  396.     end
  397.     # 与上次同一事件页的情况下
  398.     if new_page == @page
  399.       # 过程结束
  400.       return
  401.     end
  402.     # @page 设置为现在的事件页
  403.     @page = new_page
  404.     # 清除启动中标志
  405.     clear_starting
  406.     # 没有满足条件的页面的时候
  407.     if @page == nil
  408.       # 设置各实例变量
  409.       @tile_id = 0
  410.       @character_name = ""
  411.       @character_hue = 0
  412.       @move_type = 0
  413.       @through = true
  414.       @trigger = nil
  415.       @list = nil
  416.       @interpreter = nil
  417.       # 过程结束
  418.       return
  419.     end
  420.     # 设置各实例变量
  421.     @tile_id = @page.graphic.tile_id
  422.     @character_name = @page.graphic.character_name
  423.     @character_hue = @page.graphic.character_hue
  424.     if @original_direction != @page.graphic.direction
  425.       @direction = @page.graphic.direction
  426.       @original_direction = @direction
  427.       @prelock_direction = 0
  428.     end
  429.     if @original_pattern != @page.graphic.pattern
  430.       @pattern = @page.graphic.pattern
  431.       @original_pattern = @pattern
  432.     end
  433.     @opacity = @page.graphic.opacity
  434.     @blend_type = @page.graphic.blend_type
  435.     @move_type = @page.move_type
  436.     @move_speed = @page.move_speed
  437.     @move_frequency = @page.move_frequency
  438.     @move_route = @page.move_route
  439.     @move_route_index = 0
  440.     @move_route_forcing = false
  441.     @walk_anime = @page.walk_anime
  442.     @step_anime = @page.step_anime
  443.     @direction_fix = @page.direction_fix
  444.     @through = @page.through
  445.     @always_on_top = @page.always_on_top
  446.     @trigger = @page.trigger
  447.     @list = @page.list
  448.     @interpreter = nil
  449.         
  450.     self.refresh_light
  451.    
  452.     # 目标是 [并行处理] 的情况下
  453.     if @trigger == 4
  454.       # 生成并行处理用解释器
  455.       @interpreter = Interpreter.new
  456.     end
  457.     # 自动事件启动判定
  458.     check_event_trigger_auto
  459.   end
  460. end


  461. class Spriteset_Map
  462.   #--------------------------------------------------------------------------
  463.   # ● 初始化对像
  464.   #--------------------------------------------------------------------------
  465.   def initialize
  466.     # 生成显示端口
  467.     @viewport1 = Viewport.new(0, 0, 640, 480)
  468.     @viewport2 = Viewport.new(0, 0, 640, 480)
  469.     @viewport3 = Viewport.new(0, 0, 640, 480)
  470.     @viewport2.z = 200
  471.     @viewport3.z = 5000
  472.     # 生成元件地图
  473.     @tilemap = Tilemap.new(@viewport1)
  474.     @tilemap.tileset = RPG::Cache.tileset($game_map.tileset_name)
  475.     for i in 0..6
  476.       autotile_name = $game_map.autotile_names[i]
  477.       @tilemap.autotiles[i] = RPG::Cache.autotile(autotile_name)
  478.     end
  479.     @tilemap.map_data = $game_map.data
  480.     @tilemap.priorities = $game_map.priorities
  481.     # 生成远景平面
  482.     @panorama = Plane.new(@viewport1)
  483.     @panorama.z = -1000
  484.     # 生成雾平面
  485.     @fog = Plane.new(@viewport1)
  486.     @fog.z = 3000
  487.     # 生成角色活动块
  488.     @character_sprites = []
  489.     for i in $game_map.events.keys.sort
  490.       sprite = Sprite_Character.new(@viewport1, $game_map.events[i])
  491.       @character_sprites.push(sprite)
  492.     end
  493.     @character_sprites.push(Sprite_Character.new(@viewport1, $game_player))
  494.     # 生成天气
  495.     @weather = RPG::Weather.new(@viewport1)
  496.    
  497.    
  498.     @lights = LightRenderTarget.new(@viewport1)
  499.    
  500.    
  501.     # 生成图片
  502.     @picture_sprites = []
  503.     for i in 1..50
  504.       @picture_sprites.push(Sprite_Picture.new(@viewport2,
  505.         $game_screen.pictures[i]))
  506.     end
  507.     # 生成计时器块
  508.     @timer_sprite = Sprite_Timer.new
  509.     # 刷新画面
  510.     update
  511.   end
  512.   #--------------------------------------------------------------------------
  513.   # ● 释放
  514.   #--------------------------------------------------------------------------
  515.   def dispose
  516.     # 释放元件地图
  517.     @tilemap.tileset.dispose
  518.     for i in 0..6
  519.       @tilemap.autotiles[i].dispose
  520.     end
  521.     @tilemap.dispose
  522.     # 释放远景平面
  523.     @panorama.dispose
  524.     # 释放雾平面
  525.     @fog.dispose
  526.     # 释放角色活动块
  527.     for sprite in @character_sprites
  528.       sprite.dispose
  529.     end
  530.    
  531.     @lights.dispose
  532.    
  533.     # 释放天候
  534.     @weather.dispose
  535.     # 释放图片
  536.     for sprite in @picture_sprites
  537.       sprite.dispose
  538.     end
  539.     # 释放计时器块
  540.     @timer_sprite.dispose
  541.     # 释放显示端口
  542.     @viewport1.dispose
  543.     @viewport2.dispose
  544.     @viewport3.dispose
  545.   end
  546.   #--------------------------------------------------------------------------
  547.   # ● 刷新画面
  548.   #--------------------------------------------------------------------------
  549.   def update
  550.     # 远景与现在的情况有差异发生的情况下
  551.     if @panorama_name != $game_map.panorama_name or
  552.        @panorama_hue != $game_map.panorama_hue
  553.       @panorama_name = $game_map.panorama_name
  554.       @panorama_hue = $game_map.panorama_hue
  555.       if @panorama.bitmap != nil
  556.         @panorama.bitmap.dispose
  557.         @panorama.bitmap = nil
  558.       end
  559.       if @panorama_name != ""
  560.         @panorama.bitmap = RPG::Cache.panorama(@panorama_name, @panorama_hue)
  561.       end
  562.       Graphics.frame_reset
  563.     end
  564.     # 雾与现在的情况有差异的情况下
  565.     if @fog_name != $game_map.fog_name or @fog_hue != $game_map.fog_hue
  566.       @fog_name = $game_map.fog_name
  567.       @fog_hue = $game_map.fog_hue
  568.       if @fog.bitmap != nil
  569.         @fog.bitmap.dispose
  570.         @fog.bitmap = nil
  571.       end
  572.       if @fog_name != ""
  573.         @fog.bitmap = RPG::Cache.fog(@fog_name, @fog_hue)
  574.       end
  575.       Graphics.frame_reset
  576.     end
  577.     # 刷新元件地图
  578.     @tilemap.ox = $game_map.display_x / 4
  579.     @tilemap.oy = $game_map.display_y / 4
  580.     @tilemap.update
  581.     # 刷新远景平面
  582.     @panorama.ox = $game_map.display_x / 8
  583.     @panorama.oy = $game_map.display_y / 8
  584.     # 刷新雾平面
  585.     @fog.zoom_x = $game_map.fog_zoom / 100.0
  586.     @fog.zoom_y = $game_map.fog_zoom / 100.0
  587.     @fog.opacity = $game_map.fog_opacity
  588.     @fog.blend_type = $game_map.fog_blend_type
  589.     @fog.ox = $game_map.display_x / 4 + $game_map.fog_ox
  590.     @fog.oy = $game_map.display_y / 4 + $game_map.fog_oy
  591.     @fog.tone = $game_map.fog_tone
  592.     # 刷新角色活动块
  593.     for sprite in @character_sprites
  594.       sprite.update
  595.     end
  596.     # 刷新天候图形
  597.     @weather.type = $game_screen.weather_type
  598.     @weather.max = $game_screen.weather_max
  599.     @weather.ox = $game_map.display_x / 4
  600.     @weather.oy = $game_map.display_y / 4
  601.     @weather.update
  602.    
  603.    
  604.     @lights.update
  605.    
  606.    
  607.     # 刷新图片
  608.     for sprite in @picture_sprites
  609.       sprite.update
  610.     end
  611.     # 刷新计时器块
  612.     @timer_sprite.update
  613.     # 设置画面的色调与震动位置
  614.     @viewport1.tone = $game_screen.tone
  615.     @viewport1.ox = $game_screen.shake
  616.     # 设置画面的闪烁色
  617.     @viewport3.color = $game_screen.flash_color
  618.     # 刷新显示端口
  619.     @viewport1.update
  620.     @viewport3.update
  621.   end
  622. end
复制代码



更新:bitmap_light_mul 优化计算精度,效率较之前损失10%左右,目前是tktk_bitmap.dll 类似效果API的 13-15倍。
          新增 RMXP图书馆 【移植】烛光系统(VA -> XP) 脚本 tktk_bitmap.dll 替换成 Light.dll,原工程自行下载。
  1. =begin
  2.   HN_Light version 1.0.1.0 for VX Ace
  3.         by 半生
  4. [url]http://www.tktkgame.com[/url]

  5.   要HN_RG_BITMAP(ver 0.1.2.1以降)

  6. 2012/01/08 ver 1.0.1.2
  7.  队列歩行の仲间に対応

  8. 2015/04/25 Xp移植(ver 1.0)
  9.   By RyanBern
  10.   
  11. 2015/12/20 ver 1.1
  12.   Z 坐标修正 && 战斗测试 BUG 修复

  13. 2018/02/11 ver 1.2
  14.   增加控制黑暗不透明度的变量

  15. =end


  16. # 注释掉的为原代码,替换代码都比原代码快


  17. class Bitmap; def to_int; self.__id__; end; end
  18. class Rect; def to_int; self.__id__; end; end

  19. # ----- 在这里设定-----
  20. module HN_Light
  21.   # 简略化0:(精细)~2:(粗暴,负荷轻)
  22.   SIMPLIFY = 2
  23.   
  24.   # 表示黑暗处的不透明度的变量号码
  25.   # 变量的数值为0-255,越大则不可见度越高
  26.   DARK_OPACITY_VAR = 11
  27.   
  28.   # 玩家的烛光类型使用的变量号码
  29.   PLAYER_LIGHT_TYPE = 12
  30.   
  31.   # 黑暗判断上使用的开关
  32.   DARK_SWITCH = 11

  33.   # 烛光事件识别用的正规表达式
  34.   REGEX_LIGHT = /@LIGHT(\d+)/

  35.   # 烛光图像的目录
  36.   LIGHT_IMG_DIR = "Graphics/Pictures/"

  37.   # 烛光Bitmap设定
  38.   LIGHTS = [
  39.   # [文件名  ,单元数, 放大率,  Y偏移值, 色相]
  40.     ["light1",     1,    2.0,        0,    0],
  41.     ["light2",     1,    1.0,        0,    0],
  42.     ["light3",     1,    0.8,        0,    0],
  43.     ["light4",     1,    1.0,      -16,    0],
  44.     ["light5",     1,    2.0,        0,    0],
  45.     ["light6",     1,    1.0,        0,    0],
  46.     ["light7",     1,    3.0,      -16,    0],
  47.     ["light1",     1,    0.5,        0,    0],
  48.     ["light6",     1,    2.0,        0,    0],
  49.   ]
  50.   
  51.   dll = "Light.dll"
  52.   Stretch = Win32API.new(dll, 'bitmap_stretch_move', 'LL', 'L')
  53.   Mul = Win32API.new(dll, 'bitmap_light_mul', 'LLLLLL', 'L')
  54.   Full = Win32API.new(dll, 'bitmap_full_white_alpha', 'LL', 'L')
  55. end
  56. #  ----- 在这里设定 -----

  57. module HN_Light
  58.   # 事件mix-in用
  59.   module LightEvent
  60.     attr_reader :light_type
  61.     def initialize
  62.       super()
  63.       @light_type = 0
  64.     end

  65.     def check_light
  66.       @light_type = 0
  67.       return if @list.nil?
  68.       @list.each do |command|
  69.         break if @light_type > 0
  70.         if command.code == 108 or command.code == 408
  71.           command.parameters.each do |line|
  72.             if line =~ REGEX_LIGHT
  73.               @light_type = $1.to_i
  74.               break
  75.             end
  76.           end
  77.         end
  78.       end # END @list.each
  79.     end

  80.   end # END module LightEvent


  81.   class Light
  82.     attr_reader :bitmap
  83.     attr_reader :cells
  84.     attr_reader :width
  85.     attr_reader :height
  86.     attr_reader :ox
  87.     attr_reader :oy
  88.     def initialize(light_type, s_zoom = 1)
  89.       light = LIGHTS[light_type - 1]
  90.       if light.nil?
  91.         # 本来不应该来这里
  92.         @bitmap = Bitmap.new(32, 32)
  93.         @cells = 1
  94.         @zoom = 1.0
  95.         @oy = 16
  96.         @ox = 16
  97.         @width  = 32
  98.         @height = 32
  99.       else
  100.         @bitmap = Bitmap.new(LIGHT_IMG_DIR + light[0])
  101.         #@bitmap.invert()
  102.         @cells = light[1].to_i
  103.         @cells = 1 if (@cells < 1 or @cells > @bitmap.width)
  104.         @zoom = light[2].to_f
  105.         @zoom = 1.0 if @zoom <= 0.0
  106.         @zoom /= s_zoom
  107.         @width  = @bitmap.width / @cells
  108.         @height = @bitmap.height

  109.         # 缩放处理
  110.         if @zoom != 1.0
  111.           new_width  = (@width * @zoom).round
  112.           new_height = (@height * @zoom).round
  113.           if new_width * new_height < 1
  114.             @zoom = 1.0
  115.           else
  116.             @width = new_width
  117.             @height = new_height
  118.             new_bitmap = Bitmap.new(@width * @cells, @height)
  119.             #new_bitmap.stretch_blt(new_bitmap.rect,@bitmap, @bitmap.rect)
  120.             HN_Light::Stretch.call(new_bitmap, @bitmap)
  121.             @bitmap.dispose
  122.             @bitmap = new_bitmap
  123.           end
  124.         end
  125.         @ox = @width / 2
  126.         @oy = @height / 2 - light[3].to_i / s_zoom

  127.         # 色相変换
  128.         if ( (hue = light[4].to_i) != 0)
  129.           @bitmap.hue_change(hue)
  130.         end
  131.       end
  132.     end # End def initialize

  133.     # 色调转换
  134.     def dispose
  135.       @bitmap.dispose
  136.       @bitmap = nil
  137.     end
  138.   end

  139. end

  140. class Game_Event
  141.   include HN_Light::LightEvent
  142.   alias :_hn_light__setup :refresh unless method_defined?(:_hn_light__setup)
  143.   def refresh
  144.     _hn_light__setup
  145.     check_light()
  146.   end
  147. end

  148. class Game_Player
  149.   def light_type
  150.     return $game_variables[HN_Light::PLAYER_LIGHT_TYPE]
  151.   end
  152. end

  153. class Game_Map
  154.   attr_reader :light_events

  155.   # 更新烛光事件列表
  156.   def refresh_lights
  157.     @light_events = []
  158.     @events.values.each do |event|
  159.       if (event.light_type > 0)
  160.         @light_events.push(event)
  161.       end
  162.     end
  163.   end

  164.   alias :_hn_light__setup_events :setup unless method_defined?(:_hn_light__setup_events)
  165.   def setup(map_id)
  166.     _hn_light__setup_events(map_id)
  167.     refresh_lights() if @map_id > 0
  168.   end

  169.   alias :_hn_light__refresh :refresh unless method_defined?(:_hn_light__refresh)
  170.   def refresh
  171.     _hn_light__refresh()
  172.     refresh_lights() if @map_id > 0
  173.   end
  174. end

  175. class Sprite_Dark < Sprite
  176.   @@base_color = Color.new(255,255,255,192)

  177.   def initialize(viewport = nil)
  178.     super(viewport)
  179.     @width = 640
  180.     @height = 480

  181.     case HN_Light::SIMPLIFY
  182.     when 1
  183.       @zoom = 2
  184.     when 2
  185.       @zoom = 4
  186.     else
  187.       @zoom = 1
  188.     end
  189.     @width /= @zoom
  190.     @height /= @zoom
  191.     self.zoom_x = @zoom.to_f
  192.     self.zoom_y = @zoom.to_f

  193.     self.bitmap = Bitmap.new(@width, @height)
  194.     self.bitmap.fill_rect(self.bitmap.rect, @@base_color)
  195.     self.blend_type = 2 # 混合型(减算)
  196.     self.z = 800
  197.     self.visible = false
  198.     @light_cache = {}
  199.   end

  200.   # 追加烛光
  201.   def add_light(charactor)
  202.     return if charactor.nil?
  203.     light_type = charactor.light_type
  204.     return if (light_type < 1 or light_type > HN_Light::LIGHTS.size)
  205.     unless @light_cache.key?(light_type)
  206.       @light_cache[light_type] = HN_Light::Light.new(light_type, @zoom)
  207.     end
  208.     light = @light_cache[light_type]

  209.     # 画面外什麽都不做
  210.     if @zoom == 1
  211.       return if (charactor.screen_x < 0  - light.width + light.ox)
  212.       return if (charactor.screen_x > @width + light.ox)
  213.       return if (charactor.screen_y < 0 - light.height + light.oy)
  214.       return if (charactor.screen_y > @height + light.oy)
  215.     else
  216.       return if (charactor.screen_x < 0  - (light.width + light.ox) * @zoom)
  217.       return if (charactor.screen_x > (@width + light.ox)  * @zoom)
  218.       return if (charactor.screen_y < 0 - (light.height + light.oy) * @zoom)
  219.       return if (charactor.screen_y > (@height + light.oy) * @zoom)
  220.     end

  221.     # 动画判定
  222.     if light.cells > 1
  223.       index = (Graphics.frame_count / 4) % light.cells
  224.       rect = Rect.new(index * light.width , 0, light.width, light.height)
  225.     else
  226.       rect = light.bitmap.rect
  227.     end
  228.     if @zoom != 1
  229.       p_x = charactor.screen_x / @zoom - light.ox
  230.       p_y = (charactor.screen_y - 16) / @zoom - light.oy
  231.     else
  232.       p_x = charactor.screen_x - light.ox
  233.       p_y = charactor.screen_y - 16 - light.oy
  234.     end

  235.     # 乗算合成(3)
  236.     #self.bitmap.blend_blt(p_x, p_y, light.bitmap, rect, 3)
  237.     HN_Light::Mul.call(self.bitmap, p_x, p_y, light.bitmap, rect, 255)
  238.   end


  239.   def refresh
  240.     #self.bitmap.fill_rect(self.bitmap.rect, @@base_color)
  241.     HN_Light::Full.call(self.bitmap, @@base_color.alpha.to_i)
  242.     $game_map.light_events.each do |event|
  243.       next if HN_Light::LIGHTS[event.light_type - 1].nil?
  244.       add_light(event)
  245.     end
  246.     add_light($game_player)
  247.   end

  248.   # 更新
  249.   def update
  250.     super
  251.     #@@base_color = Color.new(255, 255, 255, $game_variables[HN_Light::DARK_OPACITY_VAR])
  252.     @@base_color.alpha = $game_variables[HN_Light::DARK_OPACITY_VAR]
  253.     refresh()
  254.   end

  255.   #--------------------------------------------------------------------------
  256.   # ● 解放
  257.   #--------------------------------------------------------------------------
  258.   def dispose
  259.     self.bitmap.dispose
  260.     @light_cache.values.each do |light|
  261.       light.dispose
  262.     end
  263.     super
  264.   end
  265. end


  266. class Spriteset_Map  
  267.   # 动画判定
  268.   def create_dark
  269.     @dark_sprite = Sprite_Dark.new(@viewport1)
  270.   end

  271.   # 更新黑暗Sprite
  272.   def update_dark
  273.     if (@dark_sprite.visible = $game_switches[HN_Light::DARK_SWITCH])
  274.       @dark_sprite.update
  275.     end
  276.   end

  277.   # 破弃黑暗Sprite
  278.   def dispose_dark
  279.     @dark_sprite.dispose
  280.   end

  281.   # 初期化
  282.   alias :_dark__initialize :initialize unless private_method_defined?(:_dark__initialize)
  283.   def initialize
  284.     _dark__initialize()
  285.     create_dark()
  286.     update_dark()
  287.   end

  288.   # 更新
  289.   alias :_dark__update :update unless method_defined?(:_dark__update)
  290.   def update
  291.     _dark__update()
  292.     update_dark() if !@dark_sprite.nil? and !@dark_sprite.disposed?
  293.   end

  294.   # 结束处理
  295.   alias :_dark__dispose :dispose unless method_defined?(:_dark__dispose)
  296.   def dispose
  297.     dispose_dark()
  298.     _dark__dispose()
  299.   end
  300. end
复制代码





round.png
tmp.png

Light.zip

6.32 KB, 下载次数: 18

Light.dll

梦石
0
星屑
7306
在线时间
1244 小时
注册时间
2015-8-15
回帖
473
发表于 2022-11-23 10:18:59 | 显示全部楼层
大神解决一下楼下问题 如何在角色HP变量值<90窗口10的图片透明=0或关闭窗口
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部