summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: edf6f2e9a941755608822b6f09b63121e15bf474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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;

#[cfg(target_os = "emscripten")]
pub mod emscripten;

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() {
    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 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(-7.0, 7.0);
    let b = rng.gen_range(-7.0, 7.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 main_loop = || {
        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();
            }
            if i < pixels.len() {
                let pixel = &pixels[i];
                canvas
                    .pixel(pixel.x, pixel.y, Color::RGB(255, 255, 255))
                    .unwrap();
            }

            canvas.present();
            thread::sleep(Duration::from_millis(10));
            i += 1;
        }
    };

    #[cfg(target_os = "emscripten")]
    use emscripten::emscripten;

    #[cfg(target_os = "emscripten")]
    emscripten::set_main_loop_callback(main_loop);

    #[cfg(not(target_os = "emscripten"))]
    main_loop();
}