Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Created January 4, 2024 17:01
Show Gist options
  • Save vaiorabbit/c5f14ccbd0cd2caf7ea17b36e1748e04 to your computer and use it in GitHub Desktop.
Save vaiorabbit/c5f14ccbd0cd2caf7ea17b36e1748e04 to your computer and use it in GitHub Desktop.
Testing Ruby bindings for Blend2D ( https://blend2d.com )
require_relative '../lib/blend2d.rb'
require_relative 'util'
include Blend2D
if __FILE__ == $PROGRAM_NAME
load_blend2d_lib()
img = BLImageCore.new
r = blImageInitAs(img.pointer, 480, 480, BL_FORMAT_PRGB32)
ctx = BLContextCore.new
r = blContextInitAs(ctx.pointer, img.pointer, nil)
blContextClearAll(ctx.pointer)
# First shape
radial = BLGradientCore.new
radialValues = BLRadialGradientValues.new
radialValues[:x0] = 180.0
radialValues[:y0] = 180.0
radialValues[:x1] = 180.0
radialValues[:y1] = 180.0
radialValues[:r0] = 180.0
blGradientInitAs(radial.pointer, BL_GRADIENT_TYPE_RADIAL, radialValues.pointer, BL_EXTEND_MODE_PAD, nil, 0, nil)
blGradientAddStopRgba32(radial.pointer, 0.0, 0xFFFFFFFF)
blGradientAddStopRgba32(radial.pointer, 1.0, 0xFFFF6F3F)
circle = BLCircle.new
circle[:cx] = 180.0
circle[:cy] = 180.0
circle[:r] = 160.0
blContextFillGeometryExt(ctx.pointer, BL_GEOMETRY_TYPE_CIRCLE, circle.pointer, radial.pointer)
blGradientDestroy(radial.pointer)
radial = nil
# Second shape
linear = BLGradientCore.new
linearValues = BLLinearGradientValues.new
linearValues[:x0] = 195.0
linearValues[:y0] = 195.0
linearValues[:x1] = 470.0
linearValues[:y1] = 470.0
blGradientInitAs(linear.pointer, BL_GRADIENT_TYPE_LINEAR, linearValues.pointer, BL_EXTEND_MODE_PAD, nil, 0, nil)
blGradientAddStopRgba32(linear.pointer, 0.0, 0xFFFFFFFF)
blGradientAddStopRgba32(linear.pointer, 1.0, 0xFF3F9FFF)
# blContextSetCompOp(ctx.pointer, BL_COMP_OP_DIFFERENCE) # cause of blContextFillGeometryExt '65543 NOT_IMPLEMENTED' ?
# Ref.:
# - https://github.com/blend2d/blend2d/issues/182
# - https://github.com/blend2d/blend2d/issues/181
blContextSetCompOp(ctx.pointer, BL_COMP_OP_PLUS)
roundRect = BLRoundRect.new
roundRect[:x] = 195.0
roundRect[:y] = 195.0
roundRect[:w] = 270.0
roundRect[:h] = 270.0
roundRect[:rx] = 25.0
roundRect[:ry] = 25.0
p blContextFillGeometryExt(ctx.pointer, BL_GEOMETRY_TYPE_ROUND_RECT, roundRect.pointer, linear.pointer)
blGradientDestroy(linear.pointer)
linear = nil
blContextDestroy(ctx.pointer)
ctx = nil
codec = BLImageCodecCore.new
blImageCodecInitByName(codec.pointer, "PNG", "PNG".chars.length, nil)
blImageWriteToFile(img.pointer, "bl_sample_capi.png", codec.pointer)
blImageCodecDestroy(codec.pointer)
codec = nil
blImageDestroy(img.pointer)
img = nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment