Skip to main content

simple/
simple.rs

1// SPDX-License-Identifier: MIT
2
3use std::time::Instant;
4
5use orbclient::{Color, EventOption, GraphicsPath, Mode, Renderer, Window, WindowFlag};
6
7fn main() {
8    let (width, height) = orbclient::get_display_size().unwrap();
9
10    let mut window = Window::new_flags(
11        (width as i32) / 4,
12        (height as i32) / 4,
13        width / 2,
14        height / 2,
15        "TITLE",
16        &[WindowFlag::Transparent],
17    )
18    .unwrap();
19
20    let (win_w, win_h) = (width / 2, height / 2);
21
22    let start = Instant::now();
23    // top left -> bottom right
24    window.linear_gradient(
25        0,
26        0,
27        win_w / 3,
28        win_h,
29        0,
30        0,
31        (win_w / 3) as i32,
32        (win_h / 2) as i32,
33        Color::rgb(128, 128, 128),
34        Color::rgb(255, 255, 255),
35    );
36    // horizontal gradient
37    window.linear_gradient(
38        (win_w / 3) as i32,
39        0,
40        win_w / 3,
41        win_h,
42        (win_w / 3) as i32,
43        0,
44        (2 * win_w / 3) as i32,
45        0,
46        Color::rgb(128, 255, 255),
47        Color::rgb(255, 255, 255),
48    );
49    // vertical gradient
50    window.linear_gradient(
51        (2 * win_w / 3) as i32,
52        0,
53        win_w / 3,
54        win_h,
55        (2 * win_w / 3) as i32,
56        0,
57        (2 * win_w / 3) as i32,
58        win_h as i32,
59        Color::rgb(0, 128, 0),
60        Color::rgb(255, 255, 255),
61    );
62    window.arc(100, 100, -25, 1 << 0 | 1 << 2, Color::rgb(0, 0, 255));
63    window.arc(100, 100, -25, 1 << 1 | 1 << 3, Color::rgb(0, 255, 255));
64    window.arc(100, 100, -25, 1 << 4 | 1 << 6, Color::rgb(255, 0, 255));
65    window.arc(100, 100, -25, 1 << 5 | 1 << 7, Color::rgb(255, 255, 0));
66    window.circle(100, 100, 25, Color::rgb(0, 0, 0));
67    window.circle(100, 101, -25, Color::rgb(0, 255, 0));
68    window.circle(220, 220, -100, Color::rgba(128, 128, 128, 80));
69    window.wu_circle(150, 220, 100, Color::rgba(255, 0, 0, 255));
70    window.line(0, 0, 200, 200, Color::rgb(255, 0, 0));
71    window.line(0, 200, 200, 0, Color::rgb(128, 255, 0));
72    // vertical and horizontal line test
73    window.line(100, 0, 100, 200, Color::rgb(0, 0, 255));
74    window.line(0, 100, 200, 100, Color::rgb(255, 255, 0));
75    window.wu_line(100, 220, 400, 250, Color::rgba(255, 0, 0, 255));
76    window.line(100, 230, 400, 260, Color::rgba(255, 0, 0, 255));
77
78    // path and bezier curve example draw a cloud
79    let mut cloud_path = GraphicsPath::new();
80    cloud_path.move_to(170, 80);
81    cloud_path.bezier_curve_to(130, 100, 130, 150, 230, 150);
82    cloud_path.bezier_curve_to(250, 180, 320, 180, 340, 150);
83    cloud_path.bezier_curve_to(420, 150, 420, 120, 390, 100);
84    cloud_path.bezier_curve_to(430, 40, 370, 30, 340, 50);
85    cloud_path.bezier_curve_to(320, 5, 250, 20, 250, 50);
86    cloud_path.bezier_curve_to(200, 5, 150, 20, 170, 80);
87    window.draw_path_stroke(cloud_path, Color::rgb(0, 0, 255));
88
89    // path and quadratic curve example draw a balloon
90    let mut balloon_path = GraphicsPath::new();
91    balloon_path.move_to(75, 25);
92    balloon_path.quadratic_curve_to(25, 25, 25, 62);
93    balloon_path.quadratic_curve_to(25, 100, 50, 100);
94    balloon_path.quadratic_curve_to(50, 120, 30, 125);
95    balloon_path.quadratic_curve_to(60, 120, 65, 100);
96    balloon_path.quadratic_curve_to(125, 100, 125, 62);
97    balloon_path.quadratic_curve_to(125, 25, 75, 25);
98    window.draw_path_stroke(balloon_path, Color::rgb(0, 0, 255));
99
100    window.char(200, 200, '═', Color::rgb(0, 0, 0));
101    window.char(208, 200, '═', Color::rgb(0, 0, 0));
102
103    // testing for non existent x,y position : does not panic but returns Color(0,0,0,0)
104    let _non_existent_pixel = window.getpixel(width as i32 + 10, height as i32 + 10);
105
106    // testing PartialEq for Color
107    if Color::rgb(11, 2, 3) == Color::rgba(1, 2, 3, 100) {
108        println!("Testing colors: they are the same!")
109    } else {
110        println!("Testing colors: they are NOT the same!")
111    }
112
113    //Draw a transparent rectangle over window content
114    // default mode is Blend
115    window.rect(250, 200, 80, 80, Color::rgba(100, 100, 100, 100));
116
117    //Draw an opaque rectangle replacing window content
118    window.mode().set(Mode::Overwrite); // set window drawing mode to Overwrite from now on
119    window.rect(300, 220, 80, 80, Color::rgb(100, 100, 100));
120
121    //Draw a hole in the window replacing alpha channel (Only in Orbital, not in SDL2)
122    window.rect(300, 100, 80, 80, Color::rgba(10, 10, 10, 1));
123
124    //Draw a transparent rectangle over window content
125    window.mode().set(Mode::Blend); //set mode to Blend fron now on
126    window.rect(200, 230, 80, 80, Color::rgba(100, 100, 100, 100));
127
128    //Draw a blured box over window content
129    window.box_blur(170, 100, 150, 150, 10);
130
131    //Draw a shadow around a box
132    window.box_shadow(170, 100, 150, 150, 0, 0, 20, Color::rgba(0, 0, 0, 255));
133
134    println!(
135        "Drawing took {}ms",
136        (start.elapsed().as_micros() / 100) as f32 / 10.0
137    );
138
139    window.sync();
140
141    'events: loop {
142        for event in window.events() {
143            match event.to_option() {
144                EventOption::Quit(_quit_event) => break 'events,
145                EventOption::Mouse(evt) => println!(
146                    "At position {:?} pixel color is : {:?}",
147                    (evt.x, evt.y),
148                    window.getpixel(evt.x, evt.y)
149                ),
150                event_option => println!("{:?}", event_option),
151            }
152        }
153    }
154}