赞 | 68 |
VIP | 0 |
好人卡 | 0 |
积分 | 65 |
经验 | 0 |
最后登录 | 2023-7-2 |
在线时间 | 119 小时 |
Lv4.逐梦者
- 梦石
- 0
- 星屑
- 6483
- 在线时间
- 119 小时
- 注册时间
- 2020-1-8
- 帖子
- 234
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 RPGzh500223 于 2022-5-12 20:00 编辑
练习结构体与intrinsics
使用GDI进行描绘文本,修复alpha通道,模拟Bitmap#blt进行颜色混合,HSL颜色渐变。
多个字符情况下,虽然比Bitmap#draw_text快不少,但不考虑颜色混合效果的话,gdiplus都可以跑到3000+次/秒
bug:写着玩,只处理了 4 * n 部分宽度的像素,尽量文本尾部远离 位图右边界。
module TextBoard @@pBoard = 0 dll = "TextBoard.dll" TB_open = Win32API.new(dll, 'TB_open', 'V', 'L') TB_close = Win32API.new(dll, 'TB_close', 'L', 'L') TB_select_bitmap = Win32API.new(dll, 'TB_select_bitmap', 'LL', 'L') TB_set_font = Win32API.new(dll, 'TB_set_font', 'LLPLLLL', 'L') TB_draw = Win32API.new(dll, 'TB_draw', 'LPLLLLL', 'L') module_function def open @@pBoard = TB_open.call() if @@pBoard == 0 end def close if @@pBoard != 0 TB_close.call(@@pBoard) @@pBoard = 0 end end def select_bitmap(bitmap) TB_select_bitmap.call(@@pBoard, bitmap) if @@pBoard != 0 end # TB_open时字体默认设置下面的默认值,不改变字体参数的话可以不用调用此方法 # nWeight: 0..1000的整百数;700为粗体 def set_font(font_size = 22, font_name = "黑体", nWeight = 0, bItalic = 0, bUnderline = 0, bStrikeOut = 0) TB_set_font.call(@@pBoard, font_size, font_name, nWeight, bItalic,\ bUnderline, bStrikeOut) if @@pBoard != 0 end # mode: 0 => 水平渐变;非0 => 垂直渐变 def draw(text, dx, dy, colorA, colorB = nil, mode = 0) if @@pBoard != 0 TB_draw.call(@@pBoard, text, dx, dy, mode, colorA, colorB || colorA) end end end
module TextBoard
@@pBoard = 0
dll = "TextBoard.dll"
TB_open = Win32API.new(dll, 'TB_open', 'V', 'L')
TB_close = Win32API.new(dll, 'TB_close', 'L', 'L')
TB_select_bitmap = Win32API.new(dll, 'TB_select_bitmap', 'LL', 'L')
TB_set_font = Win32API.new(dll, 'TB_set_font', 'LLPLLLL', 'L')
TB_draw = Win32API.new(dll, 'TB_draw', 'LPLLLLL', 'L')
module_function
def open
@@pBoard = TB_open.call() if @@pBoard == 0
end
def close
if @@pBoard != 0
TB_close.call(@@pBoard)
@@pBoard = 0
end
end
def select_bitmap(bitmap)
TB_select_bitmap.call(@@pBoard, bitmap) if @@pBoard != 0
end
# TB_open时字体默认设置下面的默认值,不改变字体参数的话可以不用调用此方法
# nWeight: 0..1000的整百数;700为粗体
def set_font(font_size = 22, font_name = "黑体", nWeight = 0,
bItalic = 0, bUnderline = 0, bStrikeOut = 0)
TB_set_font.call(@@pBoard, font_size, font_name, nWeight, bItalic,\
bUnderline, bStrikeOut) if @@pBoard != 0
end
# mode: 0 => 水平渐变;非0 => 垂直渐变
def draw(text, dx, dy, colorA, colorB = nil, mode = 0)
if @@pBoard != 0
TB_draw.call(@@pBoard, text, dx, dy, mode, colorA, colorB || colorA)
end
end
end
# 简单的使用示例 bitmap = Bitmap.new(640, 480) text = "文字描绘测试123-ABCD" font_size = 22 font_name = "黑体" TextBoard.open TextBoard.select_bitmap(bitmap) TextBoard.set_font(font_size, font_name, 0) cA, cB = Color.new(255, 0, 0), Color.new(0, 255, 0) TextBoard.draw(text, 0, 100, cA, cB, 0) TextBoard.draw(text, 0, 200, cA, cB, 1) TextBoard.close
# 简单的使用示例
bitmap = Bitmap.new(640, 480)
text = "文字描绘测试123-ABCD"
font_size = 22
font_name = "黑体"
TextBoard.open
TextBoard.select_bitmap(bitmap)
TextBoard.set_font(font_size, font_name, 0)
cA, cB = Color.new(255, 0, 0), Color.new(0, 255, 0)
TextBoard.draw(text, 0, 100, cA, cB, 0)
TextBoard.draw(text, 0, 200, cA, cB, 1)
TextBoard.close
|
|