Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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 <vector>

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<segment> 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;
}
Original file line number Diff line number Diff line change
@@ -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.
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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.