Skip to content

Instantly share code, notes, and snippets.

@girasquid
Last active October 13, 2023 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girasquid/236c5b98bd14cd947fdfa04c0787ff73 to your computer and use it in GitHub Desktop.
Save girasquid/236c5b98bd14cd947fdfa04c0787ff73 to your computer and use it in GitHub Desktop.
This is a helper method that makes you a box filled with diagonal stripes. You can pass in the spacing between stripes, as well as any extras you want applied to the border or lines (ex. colors)
def striped_box(x, y, w, h, spacing=10, border_extra={}, line_extra={})
primitives = []
border_box = {
x: x,
y: y,
w: w,
h: h,
}
primitives << border_box.merge(primitive_marker: :border).merge(border_extra)
top_corner_x = x
top_corner_y = y + h # DR inverted Y
# TODO the spacing/2 is a cheat to make sure we cover the entire box,
# but also is why we need the 'next if's inside the loop below.
# can they be removed?
1.upto(border_box[:w]/(spacing/2)).each do |line_index|
y1 = top_corner_y
x1 = top_corner_x + (line_index * spacing)
if x1 > border_box[:x] + border_box[:w]
x1 = border_box[:x] + border_box[:w]
y1 = top_corner_y - (spacing * (line_index - (border_box[:w]/spacing)))
end
x2 = top_corner_x
y2 = top_corner_y - line_index * spacing
if y2 < y
y2 = y
x2 = top_corner_x + (spacing * (line_index - (border_box[:h]/spacing)))
end
next if x2 > border_box[:x] + border_box[:w]
# there's a single offset corner that crops up, we need to ban it
next if y1 < border_box[:y] + 1
primitives << {
primitive_marker: :line,
x: x1 - 1,
y: y1,
x2: x2,
y2: y2 + 1,
}.merge(line_extra)
end
primitives
end

The box in the sample screenshot was generated using:

args.outputs.primitives << striped_box(100, 100, 400, 200, 10, {}, {r: 255})
@amirrajan
Copy link

here's an example using render targets:

def tick args
  if args.state.tick_count == 0 || args.inputs.keyboard.key_down.r
    args.outputs[:stripes_10_spacing].w = 1280
    args.outputs[:stripes_10_spacing].h = 1280
    args.outputs[:stripes_10_spacing].background_color = [0, 0, 0, 0]
    args.outputs[:sprites_10_spacing].borders << (1280.idiv(10)).map_with_index do |i|
      { x: i * 10, y: 0, w: 1280, h: 1280, r: 255, g: 255, b: 255 }
    end
  end

  args.outputs[:stripes_10_angle].w = 1280
  args.outputs[:stripes_10_angle].h = 1280
  args.outputs[:stripes_10_angle].transient!
  args.outputs[:stripes_10_angle].background_color = [0, 0, 0, 0]
  args.outputs[:sprites_10_angle].sprites << { x: 0, y: 0, w: 1280, h: 1280, path: :sprites_10, angle: 400.fdiv(200).to_degrees, anchor_x: 0.5, anchor_y: 0.5 }


  args.outputs.sprites << { x: 640,
                            y: 360,
                            w: 400,
                            h: 200,
                            path: :sprites_10_angle,
                            r: 255,
                            g: 0,
                            b: 0,
                            source_y: args.state.tick_count % 100,
                            source_w: 400,
                            source_h: 200,
                            anchor_x: 0.5,
                            anchor_y: 0.5 }

  args.outputs.borders << { x: 640, y: 360, w: 400, h: 200, r: 0, g: 0, b: 0, anchor_x: 0.5, anchor_y: 0.5 }
end

$gtk.reset
CleanShot.2023-10-12.at.19.19.49-converted.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment