diff --git a/public/usage-examples-images-gifs/graphics/draw_line_on_window_point_to_point-1-basic.gif b/public/usage-examples-images-gifs/graphics/draw_line_on_window_point_to_point-1-basic.gif new file mode 100644 index 000000000..593f71b25 Binary files /dev/null and b/public/usage-examples-images-gifs/graphics/draw_line_on_window_point_to_point-1-basic.gif differ diff --git a/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic-oop.cs b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic-oop.cs new file mode 100644 index 000000000..68ba5b49a --- /dev/null +++ b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic-oop.cs @@ -0,0 +1,90 @@ +// I am drawing lines on the window by clicking a start point and an end point (OOP form). +// I am left-clicking once for start; I am left-clicking again for end; I am pressing C to clear; I am pressing ESC to quit. + +using SplashKitSDK; +using System.Collections.Generic; + +namespace GraphicsExamples +{ + public class DrawLinePointToPointDemo + { + // I am keeping finished segments so I can redraw them every frame. + private readonly List<(double x1, double y1, double x2, double y2)> _segments + = new List<(double, double, double, double)>(); + + // I am remembering the current start point state and value. + private bool _hasStart = false; + private double _sx = 0.0, _sy = 0.0; + + public void Run() + { + // I am opening the window with an ASCII title and explicit size. + SplashKit.OpenWindow("Draw Line - point to point on window", 720, 405); + + while (!SplashKit.QuitRequested()) + { + SplashKit.ProcessEvents(); // I am polling inputs each frame. + + // I am handling quit and clear. + if (SplashKit.KeyTyped(KeyCode.EscapeKey)) + { + break; // I am exiting on ESC. + } + if (SplashKit.KeyTyped(KeyCode.CKey)) + { + _segments.Clear(); // I am clearing finished segments. + _hasStart = false; // I am cancelling the in-progress segment. + } + + // I am turning two clicks into one segment. + if (SplashKit.MouseClicked(MouseButton.LeftButton)) + { + double mx = SplashKit.MouseX(); + double my = SplashKit.MouseY(); + + if (!_hasStart) + { + // I am recording the start point on first click. + _hasStart = true; + _sx = mx; + _sy = my; + } + else + { + // I am saving the segment on second click. + _segments.Add((_sx, _sy, mx, my)); + _hasStart = false; + } + } + + // I am rendering the frame. + SplashKit.ClearScreen(SplashKit.ColorWhite()); + + // I am drawing all finished segments in navy. + foreach (var s in _segments) + { + SplashKit.DrawLine(SplashKit.ColorNavy(), s.x1, s.y1, s.x2, s.y2); + } + + // I am previewing the current segment in orange from start to mouse. + if (_hasStart) + { + double mx = SplashKit.MouseX(); + double my = SplashKit.MouseY(); + SplashKit.DrawLine(SplashKit.ColorOrangeRed(), _sx, _sy, mx, my); + SplashKit.FillCircle(SplashKit.ColorOrangeRed(), _sx, _sy, 3); // I am marking the start. + } + + // I am showing a small HUD with controls. + SplashKit.DrawText("Click: start/end C: clear ESC: quit", SplashKit.ColorBlack(), 16, 16); + + SplashKit.RefreshScreen(60); // I am pacing to ~60 FPS. + } + } + + public static void Main() + { + new DrawLinePointToPointDemo().Run(); + } + } +} \ No newline at end of file diff --git a/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.cpp b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.cpp new file mode 100644 index 000000000..9c10d942c --- /dev/null +++ b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.cpp @@ -0,0 +1,84 @@ +// I am drawing lines on the window by clicking a start point and an end point. +// I am left-clicking once for start; I am left-clicking again for end; I am pressing C to clear; I am pressing ESC to quit. + +#include "splashkit.h" +#include + +struct segment +{ + double x1, y1, x2, y2; +}; + +int main() +{ + // I am opening the window with a short ASCII title and explicit size. + open_window("Draw Line - point to point on window", 720, 405); + + // I am keeping finished segments so I can redraw them every frame. + std::vector segments; + + // I am remembering whether I have a start point for the current segment. + bool has_start = false; + double sx = 0.0, sy = 0.0; // I am storing the start point when I click the first time. + + while (!quit_requested()) + { + process_events(); // I am polling input each frame. + + // I am handling quit and clear. + if (key_typed(ESCAPE_KEY)) + { + break; // I am exiting the loop when ESC is typed. + } + if (key_typed(C_KEY)) + { + segments.clear(); // I am clearing the finished segments. + has_start = false; // I am cancelling an in-progress segment as well. + } + + // I am turning two clicks into one segment. + if (mouse_clicked(LEFT_BUTTON)) + { + const double mx = mouse_x(); + const double my = mouse_y(); + + if (!has_start) + { + // I am recording the start point on the first click. + has_start = true; + sx = mx; + sy = my; + } + else + { + // I am recording the end point on the second click and storing the segment. + segments.push_back({sx, sy, mx, my}); + has_start = false; + } + } + + // I am rendering the frame. + clear_screen(COLOR_WHITE); + + // I am drawing all finished segments in navy. + for (const segment &s : segments) + { + draw_line(COLOR_NAVY, s.x1, s.y1, s.x2, s.y2); + } + + // I am previewing the in-progress segment in orange from the start to the mouse. + if (has_start) + { + const double mx = mouse_x(); + const double my = mouse_y(); + draw_line(COLOR_ORANGE_RED, sx, sy, mx, my); + fill_circle(COLOR_ORANGE_RED, sx, sy, 3); // I am marking the start point. + } + + // I am showing a small HUD with controls. + draw_text("Click: start/end C: clear ESC: quit", COLOR_BLACK, 16, 16); + + refresh_screen(60); // I am pacing to ~60 FPS. + } + return 0; +} \ No newline at end of file diff --git a/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.cs b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.cs new file mode 100644 index 000000000..f51cf1bef --- /dev/null +++ b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.cs @@ -0,0 +1,76 @@ +// I am drawing lines on the window by clicking a start point and an end point. +// I am left-clicking once for start; I am left-clicking again for end; I am pressing C to clear; I am pressing ESC to quit. + +using SplashKitSDK; +using static SplashKitSDK.SplashKit; +using System.Collections.Generic; + +// I am opening the window with an ASCII title and explicit size. +OpenWindow("Draw Line - point to point on window", 720, 405); + +// I am keeping finished segments so I can redraw them every frame. +var segments = new List<(double x1, double y1, double x2, double y2)>(); + +// I am remembering whether I have a start point for the current segment. +bool hasStart = false; +double sx = 0.0, sy = 0.0; // I am storing the start point after the first click. + +while (!QuitRequested()) +{ + ProcessEvents(); // I am polling inputs each frame. + + // I am handling quit and clear. + if (KeyTyped(KeyCode.EscapeKey)) + { + break; // I am exiting the loop on ESC. + } + if (KeyTyped(KeyCode.CKey)) + { + segments.Clear(); // I am removing finished segments. + hasStart = false; // I am cancelling the partial segment as well. + } + + // I am turning two clicks into one segment. + if (MouseClicked(MouseButton.LeftButton)) + { + double mx = MouseX(); + double my = MouseY(); + + if (!hasStart) + { + // I am recording the start point on first click. + hasStart = true; + sx = mx; + sy = my; + } + else + { + // I am saving the full segment on second click. + segments.Add((sx, sy, mx, my)); + hasStart = false; + } + } + + // I am rendering the frame. + ClearScreen(ColorWhite()); + + // I am drawing all finished segments in navy. + foreach (var s in segments) + { + DrawLine(ColorNavy(), s.x1, s.y1, s.x2, s.y2); + } + + // I am previewing the current segment in orange from start to mouse. + if (hasStart) + { + double mx = MouseX(); + double my = MouseY(); + DrawLine(ColorOrangeRed(), sx, sy, mx, my); + FillCircle(ColorOrangeRed(), sx, sy, 3); // I am marking the start point. + } + + // I am showing a small HUD with controls. + DrawText("Click: start/end C: clear ESC: quit", ColorBlack(), 16, 16); + + RefreshScreen(60); // I am pacing to ~60 FPS. +} \ No newline at end of file diff --git a/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.py b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.py new file mode 100644 index 000000000..a8328f819 --- /dev/null +++ b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.py @@ -0,0 +1,56 @@ +# I am drawing lines on the window by clicking a start point and an end point. +# I am left-clicking once for start; I am left-clicking again for end; I am pressing C to clear; I am pressing ESC to quit. + +from splashkit import * +from splashkit import MouseButton, KeyCode + +# I am showing the controls in the title to avoid per-OS font issues. +open_window("Draw Line - point to point | Click start/end C clear ESC quit", 720, 405) + +# I am keeping finished segments so I can redraw them every frame. +segments: list[tuple[float, float, float, float]] = [] + +# I am remembering whether I have a start point for the current segment. +has_start = False +sx, sy = 0.0, 0.0 # I am storing the start point after the first click. + +while not quit_requested(): + process_events() # I am polling input every frame. + + # I am handling quit and clear. + if key_typed(KeyCode.escape_key): + break + if key_typed(KeyCode.c_key): + segments.clear() # I am clearing finished segments. + has_start = False # I am cancelling the partial segment. + + # I am turning two clicks into one segment. + if mouse_clicked(MouseButton.left_button): + mx = mouse_x() + my = mouse_y() + + if not has_start: + # I am recording the start point on the first click. + has_start = True + sx, sy = mx, my + else: + # I am recording the end point on the second click. + segments.append((sx, sy, mx, my)) + has_start = False + + # I am rendering the frame. + clear_screen(rgb_color(255, 255, 255)) + + # I am drawing all finished segments in navy. + for x1, y1, x2, y2 in segments: + draw_line(rgb_color(0, 0, 128), x1, y1, x2, y2) + + # I am previewing the current segment in orange from start to mouse. + if has_start: + mx = mouse_x() + my = mouse_y() + draw_line(rgb_color(255, 69, 0), sx, sy, mx, my) + fill_circle(rgb_color(255, 69, 0), sx, sy, 3) # I am marking the start point. + + refresh_screen() # I am matching ~60 FPS with the delay below. + delay(16) \ No newline at end of file diff --git a/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.txt b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.txt new file mode 100644 index 000000000..9a9f01334 --- /dev/null +++ b/src/assets/usage-examples-code/graphics/draw_line_on_window_point_to_point/draw_line_on_window_point_to_point-1-basic.txt @@ -0,0 +1 @@ +I am drawing lines on the window by clicking a start point and an end point. I am left-clicking once for start; I am left-clicking again for end; I am pressing C to clear; I am pressing ESC to quit. \ No newline at end of file