Files
2026-10-29 21:59:06 +04:00

664 lines
19 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
unless defined?(Ruby310Compat)
begin
compat_candidates = []
begin
compat_candidates << File.expand_path('000_Ruby310_Compat.rb', File.dirname(__FILE__))
rescue StandardError
end
begin
cwd = Dir.pwd.to_s
compat_candidates << File.expand_path('Data/Scripts/000_Ruby310_Compat.rb', cwd) unless cwd.empty?
compat_candidates << File.expand_path('000_Ruby310_Compat.rb', cwd) unless cwd.empty?
rescue StandardError
end
compat_candidates.compact.uniq.each do |compat_path|
next unless File.file?(compat_path)
load compat_path
break if defined?(Ruby310Compat)
end
rescue StandardError
end
end
#-------------------------------------------------------------------------------
# * [ACE] Khas Graphics Library
#-------------------------------------------------------------------------------
# * By Nilo K. (Khas)
# * Version: 2.0
# * Released on: 01/22/2017
#
# Blog: arcthunder.blogspot.com
# Facebook: facebook.com/khasarc
# Twitter: twitter.com/arcthunder
# Youtube: youtube.com/c/khasarc
#
#-------------------------------------------------------------------------------
# * Terms of Use
#-------------------------------------------------------------------------------
# When using KHAS GRAPHICS LIBRARY (the script), including its library, you
# agree with the following terms:
# 1. If you purchased a license to one of my scripts, credit is not required.
# Otherwise, you must give credit to Khas (if you want, a link to my blog is
# always appreciated);
# 2. You can use the script for free in both non-commercial and commercial
# projects;
# 4. You can edit the script for using in your own project. However, you are
# not allowed to share/distribute any modified version;
# 5. If you want to share a Khas script, don’t post the direct download link,
# redirect the user to my blog instead;
# 6. The script can not be ported to any RPG Maker version than VX Ace.
#
#-------------------------------------------------------------------------------
# * Updates
#-------------------------------------------------------------------------------
# Please check my blog for KGL updates!
#
#-------------------------------------------------------------------------------
# * Requirements
#-------------------------------------------------------------------------------
# 1. Khas Core Library
# Place the "Khas Core Library" script before KGL.
#
# 2. Khas Graphics Library file
# Place the "KGL2.klib" file in your game's System folder.
# You can customize the library location if you need.
#
#-------------------------------------------------------------------------------
# * Graphics Core (configuration)
#-------------------------------------------------------------------------------
module Graphics_Core
# LIBRARY FOLDER
# If you want to place the "KGL2.klib" file in another folder,
# please change the string below.
KGL_Folder = "System/"
# SCREEN RESOLUTION
# Change this if you are using a custom resolution script.
Screen_Width = Graphics.width
Screen_Height = Graphics.height
end
#-------------------------------------------------------------------------------
# * Requirements
#-------------------------------------------------------------------------------
unless $khas && $khas[:core]
warning = Sprite.new
warning.bitmap = Bitmap.new(Graphics.width,Graphics.height)
warning.bitmap.fill_rect(0,0,Graphics.width,Graphics.height,Color.new(255,255,255))
warning.bitmap.font = Font.new(System_Settings::MESSAGE_WINDOW_FONT_NAME, 32)
warning.bitmap.font.bold = true
warning.bitmap.font.outline = false
warning.bitmap.font.color.set(255,144,9)
warning.bitmap.draw_text(0,Graphics.height/2-32,Graphics.width,32,"PLEASE INSTALL",1)
warning.bitmap.draw_text(0,Graphics.height/2,Graphics.width,32,"KHAS CORE LIBRARY 1.03",1)
while !Input.trigger?(:C)
Input.update
Graphics.update
end
warning.bitmap.dispose
warning.dispose
exit
end
#-------------------------------------------------------------------------------
# * KGL
#-------------------------------------------------------------------------------
# mkxp-z port note:
# - prefers the embedded KGL2 library from the custom mkxp-z executable
# - falls back to the legacy System/KGL2.klib path on older builds
# - on mkxp-z, still falls back to a software/no-KGL path so the project can boot
module KGL
@@mkxp_z = defined?(System) && System.const_defined?(:VERSION)
@@software = false
@@software_reason = nil
@@framebuffer = nil
@@shadowbuffer = nil
@@light_blending = true
@@soft_shadows = false
@@native_bitmap_mode = false
@@native = {}
@@native_bitmap_handles = {}
@@native_dirty = {}
@@native_tmp = {}
@@downloadBufferRect = nil
@@clearBufferRectRGBA = nil
@@lightShaderBufferClip = nil
@@library_name = nil
def self.library_candidates
legacy_path = "#{Graphics_Core::KGL_Folder}KGL2.klib"
seen = {}
["KGL2", legacy_path].compact.map { |entry| entry.to_s }.reject { |entry| entry == "" }.select { |entry| seen[entry] ? false : (seen[entry] = true) }
rescue
["KGL2", "#{Graphics_Core::KGL_Folder}KGL2.klib"]
end
def self.bind_function(library_name, name, p="")
Ruby310Compat.native_api(library_name, name, p, "i")
end
def self.function(name, p="")
if @@library_name
return bind_function(@@library_name, name, p)
end
last_error = nil
library_candidates.each do |library_name|
begin
api = bind_function(library_name, name, p)
@@library_name = library_name
return api
rescue Exception => e
last_error = e
end
end
raise(last_error || LoadError.new("KGL2 library is unavailable"))
end
begin
@@version = KGL.function("kglVersion")
@@load = KGL.function("kglLoad")
@@blank = KGL.function("kglBlank", "i")
@@clear = KGL.function("kglClear", "ii")
@@invert = KGL.function("kglInvert", "i")
@@clone = KGL.function("kglClone", "ii")
@@bindFramebuffer = KGL.function("kglBindFramebuffer", "i")
@@bindShadowbuffer = KGL.function("kglBindShadowbuffer", "i")
@@unbindFramebuffer = KGL.function("kglUnbindFramebuffer")
@@unbindShadowbuffer = KGL.function("kglUnbindShadowbuffer")
@@clearFramebuffer = KGL.function("kglClearFramebuffer")
@@compressAlpha = KGL.function("kglCompressAlpha", "i")
@@lightBlending = KGL.function("kglLightBlending", "i")
@@lightShader = KGL.function("kglLightShader", "iiii")
@@softShadows = KGL.function("kglSoftShadows", "i")
@@shadowShaderH = KGL.function("kglShadowShaderH", "iii")
@@shadowShaderV = KGL.function("kglShadowShaderV", "iii")
@@shadowShaderW = KGL.function("kglShadowShaderW", "iii")
if @@mkxp_z
@@createBuffer = KGL.function("kglCreateBuffer", "ii")
@@destroyBuffer = KGL.function("kglDestroyBuffer", "i")
@@uploadBuffer = KGL.function("kglUploadBuffer", "ipi")
@@downloadBuffer = KGL.function("kglDownloadBuffer", "ipi")
@@downloadBufferRect = KGL.function("kglDownloadBufferRect", "ipiiiii")
@@blankBuffer = KGL.function("kglBlankBuffer", "i")
@@clearBufferRGBA = KGL.function("kglClearBufferRGBA", "ii")
@@clearBufferRectRGBA = KGL.function("kglClearBufferRectRGBA", "iiiiii")
@@cloneBuffer = KGL.function("kglCloneBuffer", "ii")
@@compressAlphaBuffer = KGL.function("kglCompressAlphaBuffer", "i")
@@bindFramebufferBuffer = KGL.function("kglBindFramebufferBuffer", "i")
@@bindShadowbufferBuffer = KGL.function("kglBindShadowbufferBuffer", "i")
@@lightShaderBuffer = KGL.function("kglLightShaderBuffer", "iiii")
@@lightShaderBufferClip = KGL.function("kglLightShaderBufferClip", "iiiiiiii")
@@native_bitmap_mode = true
end
rescue Exception => e
if @@mkxp_z
@@software = true
@@software_reason = "native mkxp-z raw_data backend unavailable: #{e.class}: #{e.message}"
puts "[KHAS] software fallback enabled: #{@@software_reason}"
else
raise
end
end
def self.software?
@@software
end
def self.native_bitmap_mode?
@@native_bitmap_mode && !@@software
end
def self.native_smoke_test
return true unless native_bitmap_mode?
begin
h = @@createBuffer.call(1, 1)
if h.nil? || h <= 0
@@software = true
@@software_reason = "native mkxp-z raw_data backend self-test failed (kglCreateBuffer returned #{h.inspect})"
return false
end
@@destroyBuffer.call(h)
true
rescue Exception => e
@@software = true
@@software_reason = "native mkxp-z raw_data backend self-test failed: #{e.class}: #{e.message}"
false
end
end
def self.run
if @@software
puts "[KHAS] software backend active#{@@software_reason ? ' (' + @@software_reason + ')' : ''}"
elsif native_bitmap_mode?
KGL.load
if native_smoke_test
puts "Khas Graphics Library #{KGL.version} (mkxp-z raw_data native backend)"
else
puts "[KHAS] software backend active (#{@@software_reason})"
end
else
KGL.load
puts "Khas Graphics Library #{KGL.version}"
end
end
def self.compressRGBA(color)
r = color.red.to_i
g = color.green.to_i
b = color.blue.to_i
a = color.alpha.to_i
r | (g << 8) | (b << 16) | (a << 24)
end
def self.compressBGRA(color)
r = color.red.to_i
g = color.green.to_i
b = color.blue.to_i
a = color.alpha.to_i
b | (g << 8) | (r << 16) | (a << 24)
end
def self.version
@@version.call().to_f / 100
end
def self.load
@@load.call()
end
def self.bitmap_key(bitmap)
bitmap.__id__
end
def self.allocate_buffer(bitmap)
key = bitmap_key(bitmap)
handle = @@native_bitmap_handles[key]
if handle && @@native[key] && @@native[key][0] == bitmap.width && @@native[key][1] == bitmap.height
return handle
end
release_bitmap(bitmap)
handle = @@createBuffer.call(bitmap.width, bitmap.height)
raise "KGL native buffer alloc failed for #{bitmap.width}x#{bitmap.height}" if handle.nil? || handle <= 0
@@native_bitmap_handles[key] = handle
@@native[key] = [bitmap.width, bitmap.height]
@@native_dirty[key] = true
handle
end
def self.ensure_buffer(bitmap)
return nil if bitmap.nil?
allocate_buffer(bitmap) unless @@native_bitmap_handles[bitmap_key(bitmap)]
key = bitmap_key(bitmap)
if !@@native[key] || @@native[key][0] != bitmap.width || @@native[key][1] != bitmap.height
allocate_buffer(bitmap)
end
if @@native_dirty[key]
data = bitmap.raw_data
@@uploadBuffer.call(@@native_bitmap_handles[key], data, data.bytesize)
@@native_dirty[key] = false
end
@@native_bitmap_handles[key]
end
def self.mark_native_dirty(bitmap)
return unless bitmap && native_bitmap_mode?
@@native_dirty[bitmap_key(bitmap)] = true if @@native_bitmap_handles[bitmap_key(bitmap)]
end
def self.release_bitmap(bitmap)
return unless bitmap && native_bitmap_mode?
key = bitmap_key(bitmap)
handle = @@native_bitmap_handles.delete(key)
@@destroyBuffer.call(handle) if handle && handle > 0
@@native.delete(key)
@@native_dirty.delete(key)
@@native_tmp.delete(key)
@@framebuffer = nil if @@framebuffer.equal?(bitmap)
@@shadowbuffer = nil if @@shadowbuffer.equal?(bitmap)
end
def self.normalize_rect(rect, max_w, max_h)
return nil if rect.nil?
x, y, w, h = rect
x = x.to_i
y = y.to_i
w = w.to_i
h = h.to_i
return nil if w <= 0 || h <= 0
x2 = x + w
y2 = y + h
x = 0 if x < 0
y = 0 if y < 0
x2 = max_w if x2 > max_w
y2 = max_h if y2 > max_h
w = x2 - x
h = y2 - y
return nil if w <= 0 || h <= 0
[x, y, w, h]
end
def self.commit(bitmap, rect = nil)
return unless bitmap && native_bitmap_mode?
key = bitmap_key(bitmap)
handle = @@native_bitmap_handles[key]
return unless handle && handle > 0
bytes = bitmap.width * bitmap.height * 4
data = @@native_tmp[key]
full_download = false
if data.nil? || data.bytesize != bytes
data = "\0" * bytes
@@native_tmp[key] = data
full_download = true
end
rect = normalize_rect(rect, bitmap.width, bitmap.height) unless rect.nil?
if !full_download && rect && @@downloadBufferRect
x, y, w, h = rect
@@downloadBufferRect.call(handle, data, bytes, x, y, w, h)
else
@@downloadBuffer.call(handle, data, bytes)
end
bitmap.raw_data = data
@@native_dirty[key] = false
end
def self.commit_if_bound(bitmap)
commit(bitmap) if native_bitmap_mode?
end
def self.bindFramebuffer(bitmap)
if @@software
@@framebuffer = bitmap
elsif native_bitmap_mode?
@@framebuffer = bitmap
@@bindFramebufferBuffer.call(ensure_buffer(bitmap))
else
@@bindFramebuffer.call(bitmap.__id__)
end
end
def self.bindShadowbuffer(bitmap)
if @@software
@@shadowbuffer = bitmap
elsif native_bitmap_mode?
@@shadowbuffer = bitmap
@@bindShadowbufferBuffer.call(ensure_buffer(bitmap))
else
@@bindShadowbuffer.call(bitmap.__id__)
end
end
def self.unbindFramebuffer()
if @@software
@@framebuffer = nil
else
@@unbindFramebuffer.call()
end
end
def self.unbindShadowbuffer()
if @@software
@@shadowbuffer = nil
else
@@unbindShadowbuffer.call()
end
end
def self.clearFramebuffer()
if @@software
blank(@@framebuffer) if @@framebuffer
else
@@clearFramebuffer.call()
end
end
def self.loadFramebuffer(bitmap, x, y)
# not used by this project
end
def self.blank(bitmap)
return unless bitmap
if @@software
bitmap.clear
elsif native_bitmap_mode?
@@blankBuffer.call(ensure_buffer(bitmap))
else
@@blank.call(bitmap.__id__)
end
end
def self.clear(bitmap, color)
return unless bitmap
if @@software
bitmap.clear
bitmap.fill_rect(0, 0, bitmap.width, bitmap.height, color)
elsif native_bitmap_mode?
@@clearBufferRGBA.call(ensure_buffer(bitmap), KGL.compressRGBA(color))
else
@@clear.call(bitmap.__id__, KGL.compressBGRA(color))
end
end
def self.clear_rect(bitmap, color, rect)
return clear(bitmap, color) if rect.nil?
if @@software || !native_bitmap_mode? || @@clearBufferRectRGBA.nil?
clear(bitmap, color)
else
rect = normalize_rect(rect, bitmap.width, bitmap.height)
return if rect.nil?
x, y, w, h = rect
@@clearBufferRectRGBA.call(ensure_buffer(bitmap), KGL.compressRGBA(color), x, y, w, h)
end
end
def self.invert(bitmap)
return unless bitmap
if @@software
# unused in this project
elsif native_bitmap_mode?
commit(bitmap)
bitmap.invert
mark_native_dirty(bitmap)
ensure_buffer(bitmap)
else
@@invert.call(bitmap.__id__)
end
end
def self.clone(target, source)
return unless target && source
if @@software
target.clear
target.blt(0, 0, source, source.rect, 255)
elsif native_bitmap_mode?
@@cloneBuffer.call(ensure_buffer(target), ensure_buffer(source))
else
@@clone.call(target.__id__, source.__id__)
end
end
def self.compressAlpha(bitmap)
return if bitmap.nil?
if @@software
y = 0
while y < bitmap.height
x = 0
while x < bitmap.width
c = bitmap.get_pixel(x, y)
a = c.alpha.to_i
if a <= 0
bitmap.set_pixel(x, y, Color.new(0, 0, 0, 255))
elsif a < 255
r = (c.red.to_i * a) >> 8
g = (c.green.to_i * a) >> 8
b = (c.blue.to_i * a) >> 8
bitmap.set_pixel(x, y, Color.new(r, g, b, 255))
else
bitmap.set_pixel(x, y, Color.new(c.red.to_i, c.green.to_i, c.blue.to_i, 255))
end
x += 1
end
y += 1
end
elsif native_bitmap_mode?
@@compressAlphaBuffer.call(ensure_buffer(bitmap))
else
@@compressAlpha.call(bitmap.__id__)
end
end
def self.lightBlending(b)
if @@software
@@light_blending = b
else
@@lightBlending.call(b ? 1 : 0)
end
end
def self.lightShader(light, tx, ty, opacity)
return if opacity <= 0 || light.nil?
if @@software
return unless @@framebuffer
amount = opacity.to_i
amount = 0 if amount < 0
amount = 255 if amount > 255
ox = tx.to_i
oy = ty.to_i
sx0 = ox < 0 ? -ox : 0
sy0 = oy < 0 ? -oy : 0
sx1 = light.width
sy1 = light.height
max_sx1 = @@framebuffer.width - ox
max_sy1 = @@framebuffer.height - oy
sx1 = max_sx1 if sx1 > max_sx1
sy1 = max_sy1 if sy1 > max_sy1
return if sx1 <= sx0 || sy1 <= sy0
y = sy0
while y < sy1
dy = oy + y
x = sx0
while x < sx1
dx = ox + x
sp = light.get_pixel(x, y)
sr = sp.red.to_i
sg = sp.green.to_i
sb = sp.blue.to_i
if sr != 0 || sg != 0 || sb != 0
if amount != 255
sr = (sr * amount) >> 8
sg = (sg * amount) >> 8
sb = (sb * amount) >> 8
end
dp = @@framebuffer.get_pixel(dx, dy)
dr = dp.red.to_i - sr
dg = dp.green.to_i - sg
db = dp.blue.to_i - sb
dr = 0 if dr < 0
dg = 0 if dg < 0
db = 0 if db < 0
@@framebuffer.set_pixel(dx, dy, Color.new(dr, dg, db, 255))
end
x += 1
end
y += 1
end
elsif native_bitmap_mode?
@@lightShaderBuffer.call(ensure_buffer(light), tx.to_i, ty.to_i, opacity.to_i)
else
@@lightShader.call(light.__id__, tx, ty, opacity)
end
end
def self.lightShaderClipped(light, tx, ty, opacity, rect)
return if opacity <= 0 || light.nil?
if native_bitmap_mode? && @@lightShaderBufferClip && rect
rect = normalize_rect(rect, @@framebuffer.width, @@framebuffer.height) if @@framebuffer
return if rect.nil?
x, y, w, h = rect
@@lightShaderBufferClip.call(ensure_buffer(light), tx.to_i, ty.to_i, opacity.to_i, x, y, w, h)
else
lightShader(light, tx, ty, opacity)
end
end
def self.softShadows(s)
if @@software
@@soft_shadows = s
else
@@softShadows.call(s ? 1 : 0)
end
end
def self.shadowShaderH(x1, x2, y)
@@shadowShaderH.call(x1, x2, y) unless @@software
end
def self.shadowShaderV(y1, y2, x)
@@shadowShaderV.call(y1, y2, x) unless @@software
end
def self.shadowShaderW(y1, y2, x)
@@shadowShaderW.call(y1, y2, x) unless @@software
end
end
class Bitmap
alias khas_kgl_native_dispose dispose
def dispose
KGL.release_bitmap(self) if defined?(KGL)
khas_kgl_native_dispose
end
end
KGL.run
#-------------------------------------------------------------------------------
# * Version
#-------------------------------------------------------------------------------
$khas_graphics[:core] = 2.0
$khas_graphics[:kgl] = KGL.version
#-------------------------------------------------------------------------------
# * Framebuffer
#-------------------------------------------------------------------------------
class Framebuffer < Sprite
include Graphics_Core
def initialize(viewport = nil, width = Screen_Width, height = Screen_Height)
super(viewport)
self.bitmap = Bitmap.new(width, height)
end
def bind
KGL.bindFramebuffer(self.bitmap)
end
def unbind
KGL.unbindFramebuffer()
end
def clear
KGL.clearFramebuffer()
end
def dispose
self.bitmap.dispose
super
end
end
#-------------------------------------------------------------------------------
# * End
#-------------------------------------------------------------------------------