summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2020-07-27 13:09:36 +0200
committerTom Barrett <tom@tombarrett.xyz>2020-07-27 13:09:36 +0200
commit4b1c0785a145b35affaf6ec4d6e35363e8fce383 (patch)
tree2024c21177be8ec8f5fcf73baf870b678c6a51a6
parent07504c1272b6b2296a90a68e3f38a3e9a5b16b19 (diff)
spider web
-rw-r--r--Cargo.lock133
-rw-r--r--Cargo.toml4
-rw-r--r--src/main.rs121
3 files changed, 256 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..0b0a810
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,133 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "c_vec"
+version = "1.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8a318911dce53b5f1ca6539c44f5342c632269f0fa7ea3e35f32458c27a7c30"
+
+[[package]]
+name = "cfg-if"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+
+[[package]]
+name = "getrandom"
+version = "0.1.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "libc"
+version = "0.2.73"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bd7d4bd64732af4bf3a67f367c27df8520ad7e230c5817b8ff485864d80242b9"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
+
+[[package]]
+name = "rand"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
+dependencies = [
+ "getrandom",
+ "libc",
+ "rand_chacha",
+ "rand_core",
+ "rand_hc",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "rand_hc"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
+name = "sdl2"
+version = "0.34.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "29fb006600d16da4f1f1e5c7b398f44af2f1b476ff5f2e555651bd78cf4c18d8"
+dependencies = [
+ "bitflags",
+ "c_vec",
+ "lazy_static",
+ "libc",
+ "sdl2-sys",
+]
+
+[[package]]
+name = "sdl2-sys"
+version = "0.34.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3ed17d6d46b62b7df12134513bcc4f071268963e8c9bc8bf7ad983fbfb2bc3cc"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "version-compare",
+]
+
+[[package]]
+name = "version-compare"
+version = "0.0.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1"
+
+[[package]]
+name = "wasi"
+version = "0.9.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
+
+[[package]]
+name = "web"
+version = "0.1.0"
+dependencies = [
+ "rand",
+ "sdl2",
+]
diff --git a/Cargo.toml b/Cargo.toml
index e795b37..b62923b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,5 @@
[package]
-name = "fun"
+name = "web"
version = "0.1.0"
authors = ["Tom Barrett <tom@tombarrett.xyz>"]
edition = "2018"
@@ -7,3 +7,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+sdl2 = {version = "0.34", features = ["gfx"]}
+rand = "0.7.3"
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..20b9c9c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,122 @@
+use std::thread;
+use std::time::Duration;
+
+extern crate rand;
+use rand::seq::SliceRandom;
+use rand::Rng;
+
+extern crate sdl2;
+use sdl2::event::Event;
+use sdl2::gfx::primitives::DrawRenderer;
+use sdl2::pixels::Color;
+
+fn find_origin() -> (i16, i16) {
+ let mut rng = rand::thread_rng();
+ (rng.gen_range(100, 400), rng.gen_range(100, 400))
+}
+
+struct Line {
+ x1: i16,
+ y1: i16,
+ x2: i16,
+ y2: i16,
+}
+
+struct Pixel {
+ x: i16,
+ y: i16,
+}
+
fn main() {
- println!("Hello, world!");
+ let context = sdl2::init().unwrap();
+ let video = context.video().unwrap();
+ let (height, width) = (500, 500);
+ let window = video.window("web", height, width).opengl().build().unwrap();
+ let mut canvas = window.into_canvas().build().unwrap();
+ let mut events = context.event_pump().unwrap();
+
+ canvas.set_draw_color(Color::RGB(0, 0, 0));
+ canvas.clear();
+ canvas.present();
+
+ let (x, y) = find_origin();
+ let delay = 50;
+ let mut lines = Vec::new();
+ for i in (0..(height as i16 + 1)).step_by(50) {
+ lines.push(Line {
+ x1: i,
+ y1: 0,
+ x2: x,
+ y2: y,
+ });
+ lines.push(Line {
+ x1: i,
+ y1: height as i16,
+ x2: x,
+ y2: y,
+ });
+ lines.push(Line {
+ x1: 0,
+ y1: i,
+ x2: x,
+ y2: y,
+ });
+ lines.push(Line {
+ x1: width as i16,
+ y1: i,
+ x2: x,
+ y2: y,
+ });
+ }
+
+ let mut rng = rand::thread_rng();
+ lines.shuffle(&mut rng);
+
+ let a = rng.gen_range(-10.0, 10.0);
+ let b = rng.gen_range(-10.0, 10.0);
+
+ let mut pixels = Vec::new();
+ for i in (1000..200000).step_by(10) {
+ let i = i as f64 * 0.001;
+ let x = (a + b * i) * i.cos() + x as f64;
+ let y = (a + b * i) * i.sin() + y as f64;
+ pixels.push(Pixel {
+ x: x as i16,
+ y: y as i16,
+ });
+ }
+
+ let mut i = 0;
+ let mut run = true;
+ while run {
+ for event in events.poll_iter() {
+ if let Event::Quit { .. } = event {
+ run = false
+ };
+ }
+
+ if i < lines.len() {
+ let line = &lines[i];
+ canvas
+ .line(
+ line.x1,
+ line.y1,
+ line.x2,
+ line.y2,
+ Color::RGB(255, 255, 255),
+ )
+ .unwrap();
+ canvas.present();
+ thread::sleep(Duration::from_millis(delay));
+ }
+ if i < pixels.len() {
+ let pixel = &pixels[i];
+ canvas
+ .pixel(pixel.x, pixel.y, Color::RGB(255, 255, 255))
+ .unwrap();
+ canvas.present();
+ }
+
+ i += 1;
+ }
}