From effbf83aa3d9bd55ceb2398b5dce99847b26c743 Mon Sep 17 00:00:00 2001 From: Alex Clink Date: Wed, 23 Oct 2024 00:31:30 -0400 Subject: [PATCH] Minor updates, add line_intersect example --- .gitignore | 1 + examples/3d.cr | 1 - examples/cubic_bezier.cr | 30 +++++++++--------- examples/line_intersect.cr | 62 ++++++++++++++++++++++++++++++++++++++ shard.yml | 5 ++- src/audio/instrument.cr | 2 +- src/controller.cr | 2 -- src/game.cr | 3 ++ 8 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 examples/line_intersect.cr diff --git a/.gitignore b/.gitignore index 4ad01a1..fe39d5b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ # Libraries don't need dependency lock # Dependencies will be locked in applications that use them /shard.lock +/shard.override.yml /examples/build/* diff --git a/examples/3d.cr b/examples/3d.cr index 8c990f2..ff6938c 100644 --- a/examples/3d.cr +++ b/examples/3d.cr @@ -1,7 +1,6 @@ require "pf3d" require "../src/game" -require "../src/controller" require "../src/sprite" require "../src/pixel" require "../src/sprite" diff --git a/examples/cubic_bezier.cr b/examples/cubic_bezier.cr index f4ad078..4255a02 100644 --- a/examples/cubic_bezier.cr +++ b/examples/cubic_bezier.cr @@ -30,7 +30,7 @@ class CubicBezier < PF::Game if point = @selected_point point.value = cursor.to_f else - @hover_point = @curve.points.find { |p| cursor.distance(p.value) < 4 } + @hover_point = @curve.point_pointers.find { |p| cursor.distance(p.value) < 4 } end end @@ -50,32 +50,34 @@ class CubicBezier < PF::Game def draw clear + draw_line(0, height // 2, width, height // 2, PF::Pixel.new(25, 25, 25)) + draw_line(@curve.p0, @curve.p1, CTL_COLOR) draw_line(@curve.p3, @curve.p2, CTL_COLOR) draw_string("Length: " + @curve.length.round(2).to_s, 5, 5, @font, FONT_COLOR) - draw_rect(*@curve.rect.map(&.to_i), CTL_COLOR) + draw_rect(@curve.rect, CTL_COLOR) draw_curve(@curve, CURVE_COLOR) - fill_circle(@curve.p0.to_i, 2, POINT_COLOR) - fill_circle(@curve.p1.to_i, 2, POINT_COLOR) - fill_circle(@curve.p2.to_i, 2, POINT_COLOR) - fill_circle(@curve.p3.to_i, 2, POINT_COLOR) + @curve.horizontal_intersects(height // 2) do |p| + draw_circle(p.to_i, 3, PF::Pixel::Orange) + end - draw_string("P1 (#{@curve.p0.x.to_i}, #{@curve.p0.y.to_i})", @curve.p0, @font, FONT_COLOR) - draw_string("P2 (#{@curve.p1.x.to_i}, #{@curve.p1.y.to_i})", @curve.p1, @font, FONT_COLOR) - draw_string("P3 (#{@curve.p2.x.to_i}, #{@curve.p2.y.to_i})", @curve.p2, @font, FONT_COLOR) - draw_string("P4 (#{@curve.p3.x.to_i}, #{@curve.p3.y.to_i})", @curve.p3, @font, FONT_COLOR) + @curve.points.each do |p| + fill_circle(p.to_i, 2, POINT_COLOR) + end + + @curve.points.each_with_index do |p, i| + draw_string("P#{i} (#{p.x.to_i}, #{p.y.to_i})", p, @font, FONT_COLOR) + end if point = @hover_point draw_circle(point.value.to_i, 5, SEL_COLOR) end - @curve.extremities.each do |point| - point.try do |p| - draw_circle(p.to_i, 3, EXT_Y_COLOR) - end + @curve.extrema do |point| + draw_circle(point.to_i, 3, EXT_Y_COLOR) end end end diff --git a/examples/line_intersect.cr b/examples/line_intersect.cr new file mode 100644 index 0000000..9c01e91 --- /dev/null +++ b/examples/line_intersect.cr @@ -0,0 +1,62 @@ +require "../src/game" + +class LineIntersect < PF::Game + include PF2d + @line_1 : Line(Vec2(Float64)) + @line_2 : Line(Vec2(Float64)) + + @hover_point : Vec2(Float64)*? = nil + @selected_point : Vec2(Float64)*? = nil + + @font = Pixelfont::Font.new("#{__DIR__}/../lib/pixelfont/fonts/pixel-5x7.txt") + + def initialize(*args, **kwargs) + super + @line_1 = Line[Vec[5, 5].to_f64, Vec[width - 5, height - 5].to_f64] + @line_2 = Line[Vec[width - 5, 5].to_f64, Vec[5, height - 5].to_f64] + end + + def on_mouse_motion(cursor) + if point = @selected_point + point.value = cursor.to_f + else + @hover_point = {*@line_1.point_pointers, *@line_2.point_pointers}.find do |point| + point.value.distance(cursor) < 3 + end + end + end + + def on_mouse_button(event) + if event.button == 1 + if event.pressed? + @selected_point = @hover_point + else + @selected_point = nil + end + end + end + + def update(dt) + end + + def draw + clear + + draw_line(@line_1, PF::Pixel::Yellow) + draw_line(@line_2, PF::Pixel::Yellow) + fill_circle(@line_1.p1.to_i, 3, PF::Pixel::Red) + fill_circle(@line_1.p2.to_i, 3, PF::Pixel::Red) + fill_circle(@line_2.p1.to_i, 3, PF::Pixel::Red) + fill_circle(@line_2.p2.to_i, 3, PF::Pixel::Red) + + if point = @hover_point + draw_circle(point.value.to_i, 5, PF::Pixel::Blue) + end + + if point = @line_1.intersects?(@line_2) + fill_circle(point.to_i, 3, PF::Pixel::Green) + end + end +end + +LineIntersect.new(300, 300, 2).run! diff --git a/shard.yml b/shard.yml index 0175c6f..d93c43f 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: pixelfaucet -version: 0.0.8 +version: 0.0.9 authors: - Alex Clink @@ -14,7 +14,10 @@ dependencies: version: 0.1.0 pf2d: github: SleepingInsomniac/pf2d + version: 0.3.0 pf3d: github: SleepingInsomniac/pf3d + version: 0.1.1 pixelfont: github: SleepingInsomniac/pixelfont + version: 1.1.1 diff --git a/src/audio/instrument.cr b/src/audio/instrument.cr index 43f430d..f0a71e6 100644 --- a/src/audio/instrument.cr +++ b/src/audio/instrument.cr @@ -25,7 +25,7 @@ module PF sound.release!(time) spawn do - sleep @envelope.release.duration + sleep @envelope.release.duration.seconds @sounds.delete(sound) @notes.delete(note_id) end diff --git a/src/controller.cr b/src/controller.cr index 06409ec..0263aae 100644 --- a/src/controller.cr +++ b/src/controller.cr @@ -1,5 +1,3 @@ -require "./lib_sdl" - module PF alias Keys = LibSDL::Scancode alias KeyCodes = LibSDL::Keycode diff --git a/src/game.cr b/src/game.cr index b6dd9d6..cefd087 100644 --- a/src/game.cr +++ b/src/game.cr @@ -51,7 +51,10 @@ module PF end end + # Could be called multiple times per frame, used for updating the simulation abstract def update(dt : Float64) + + # Called to draw the current frame abstract def draw # Returns the width of the window and render area