Struct Mlx

Source
pub struct Mlx { /* private fields */ }
Expand description

Api method holder.

Implementations§

Source§

impl Mlx

Source

pub fn new() -> Result<Self, MlxError>

Creates a new Mlx instance.

Calls the mlx_init C method under the hood.

Usage:

let mlx = Mlx::new().unwrap();
Examples found in repository?
examples/example.rs (line 7)
6fn main() {
7    let mlx = Mlx::new().unwrap();
8
9    let width = 1080;
10    let height = 720;
11    let window = mlx.new_window(width, height, "Mlx example").unwrap();
12
13    let image = match mlx.new_image(width, height) {
14        Ok(img) => img,
15        Err(e) => match e {
16            MlxError::Any(s) => return println!("{}", s),
17            _ => return,
18        },
19    };
20
21    let _bytes_per_pixel = image.bits_per_pixel / 8;
22    let offset = image.size_line / 2 + image.size_line * height / 2;
23    for i in 1..100 {
24        // we assume there are 32 bits per pixel here, because its just an example.
25        // color encoding can go from 8 bits to 48 bits.
26        let offset = offset + i * 4;
27        match image.endian {
28            Endian::Little => {
29                image.write_to(offset + 2, 0xff); // R
30                image.write_to(offset + 1, 0x0); // G
31                image.write_to(offset, 0x0); // B
32            }
33            Endian::Big => {
34                image.write_to(offset, 0xff); // R
35                image.write_to(offset + 1, 0x0); // G
36                image.write_to(offset + 2, 0x0); // B
37            }
38        }
39    }
40
41    window.key_hook(
42        move |keycode, _| {
43            // you can also check keycodes using the `xev` command
44            println!("{}", keycode);
45
46            // `q`
47            if keycode == 113 {
48                process::exit(0);
49            // Enter
50            } else if keycode == 97 {
51                let x = width / 2;
52                let y = height / 2;
53                let color = 0xffffff;
54                for i in 0..50 {
55                    mlx.pixel_put(&window, x + i, y + i, color);
56                }
57            } else if keycode == 98 {
58                mlx.put_image_to_window(&window, &image, 0, 0);
59            }
60        },
61        &(),
62    );
63
64    // this will loop forever
65    mlx.event_loop();
66}
Source

pub fn new_window( &self, size_x: i32, size_y: i32, title: &str, ) -> Result<MlxWindow, MlxError>

Creates a new window instance.

Usage:

 let image = mlx.new_window(1920, 1080, "mlx-example").unwrap();
Examples found in repository?
examples/example.rs (line 11)
6fn main() {
7    let mlx = Mlx::new().unwrap();
8
9    let width = 1080;
10    let height = 720;
11    let window = mlx.new_window(width, height, "Mlx example").unwrap();
12
13    let image = match mlx.new_image(width, height) {
14        Ok(img) => img,
15        Err(e) => match e {
16            MlxError::Any(s) => return println!("{}", s),
17            _ => return,
18        },
19    };
20
21    let _bytes_per_pixel = image.bits_per_pixel / 8;
22    let offset = image.size_line / 2 + image.size_line * height / 2;
23    for i in 1..100 {
24        // we assume there are 32 bits per pixel here, because its just an example.
25        // color encoding can go from 8 bits to 48 bits.
26        let offset = offset + i * 4;
27        match image.endian {
28            Endian::Little => {
29                image.write_to(offset + 2, 0xff); // R
30                image.write_to(offset + 1, 0x0); // G
31                image.write_to(offset, 0x0); // B
32            }
33            Endian::Big => {
34                image.write_to(offset, 0xff); // R
35                image.write_to(offset + 1, 0x0); // G
36                image.write_to(offset + 2, 0x0); // B
37            }
38        }
39    }
40
41    window.key_hook(
42        move |keycode, _| {
43            // you can also check keycodes using the `xev` command
44            println!("{}", keycode);
45
46            // `q`
47            if keycode == 113 {
48                process::exit(0);
49            // Enter
50            } else if keycode == 97 {
51                let x = width / 2;
52                let y = height / 2;
53                let color = 0xffffff;
54                for i in 0..50 {
55                    mlx.pixel_put(&window, x + i, y + i, color);
56                }
57            } else if keycode == 98 {
58                mlx.put_image_to_window(&window, &image, 0, 0);
59            }
60        },
61        &(),
62    );
63
64    // this will loop forever
65    mlx.event_loop();
66}
Source

pub fn clear_window(&self, window: &MlxWindow)

Clears the window with black.

Source

pub fn destroy_window(&self, window: &MlxWindow)

Destroys the window. This function also drops the window object.

Source

pub fn get_screen_size(&self) -> (i32, i32)

Get the actual screen size.

Source

pub fn pixel_put(&self, window: &MlxWindow, x: i32, y: i32, color: i32)

Put a pixel on the screen

You should encode the color as RGB on the three last bytes of the int.

0x00|ff(R)|ff(G)|ff(B)

Usage:

 let x = 200;
 let y = 300;
 let color = 0x0000ff; // blue
 mlx.pixel_put(&window, x, y, color);
Examples found in repository?
examples/example.rs (line 55)
6fn main() {
7    let mlx = Mlx::new().unwrap();
8
9    let width = 1080;
10    let height = 720;
11    let window = mlx.new_window(width, height, "Mlx example").unwrap();
12
13    let image = match mlx.new_image(width, height) {
14        Ok(img) => img,
15        Err(e) => match e {
16            MlxError::Any(s) => return println!("{}", s),
17            _ => return,
18        },
19    };
20
21    let _bytes_per_pixel = image.bits_per_pixel / 8;
22    let offset = image.size_line / 2 + image.size_line * height / 2;
23    for i in 1..100 {
24        // we assume there are 32 bits per pixel here, because its just an example.
25        // color encoding can go from 8 bits to 48 bits.
26        let offset = offset + i * 4;
27        match image.endian {
28            Endian::Little => {
29                image.write_to(offset + 2, 0xff); // R
30                image.write_to(offset + 1, 0x0); // G
31                image.write_to(offset, 0x0); // B
32            }
33            Endian::Big => {
34                image.write_to(offset, 0xff); // R
35                image.write_to(offset + 1, 0x0); // G
36                image.write_to(offset + 2, 0x0); // B
37            }
38        }
39    }
40
41    window.key_hook(
42        move |keycode, _| {
43            // you can also check keycodes using the `xev` command
44            println!("{}", keycode);
45
46            // `q`
47            if keycode == 113 {
48                process::exit(0);
49            // Enter
50            } else if keycode == 97 {
51                let x = width / 2;
52                let y = height / 2;
53                let color = 0xffffff;
54                for i in 0..50 {
55                    mlx.pixel_put(&window, x + i, y + i, color);
56                }
57            } else if keycode == 98 {
58                mlx.put_image_to_window(&window, &image, 0, 0);
59            }
60        },
61        &(),
62    );
63
64    // this will loop forever
65    mlx.event_loop();
66}
Source

pub fn string_put( &self, window: &MlxWindow, x: i32, y: i32, color: i32, s: &str, ) -> Result<(), MlxError>

Writes a string on the screen

Color is encoded in rgb as well. Usage:

 let x = 200;
 let y = 300;
 let color = 0xff0000; // red
 mlx.string_put(&window, x, y, color, "Hello World");
Source

pub fn new_image(&self, width: i32, height: i32) -> Result<MlxImage, MlxError>

Creates a new image.

Examples found in repository?
examples/example.rs (line 13)
6fn main() {
7    let mlx = Mlx::new().unwrap();
8
9    let width = 1080;
10    let height = 720;
11    let window = mlx.new_window(width, height, "Mlx example").unwrap();
12
13    let image = match mlx.new_image(width, height) {
14        Ok(img) => img,
15        Err(e) => match e {
16            MlxError::Any(s) => return println!("{}", s),
17            _ => return,
18        },
19    };
20
21    let _bytes_per_pixel = image.bits_per_pixel / 8;
22    let offset = image.size_line / 2 + image.size_line * height / 2;
23    for i in 1..100 {
24        // we assume there are 32 bits per pixel here, because its just an example.
25        // color encoding can go from 8 bits to 48 bits.
26        let offset = offset + i * 4;
27        match image.endian {
28            Endian::Little => {
29                image.write_to(offset + 2, 0xff); // R
30                image.write_to(offset + 1, 0x0); // G
31                image.write_to(offset, 0x0); // B
32            }
33            Endian::Big => {
34                image.write_to(offset, 0xff); // R
35                image.write_to(offset + 1, 0x0); // G
36                image.write_to(offset + 2, 0x0); // B
37            }
38        }
39    }
40
41    window.key_hook(
42        move |keycode, _| {
43            // you can also check keycodes using the `xev` command
44            println!("{}", keycode);
45
46            // `q`
47            if keycode == 113 {
48                process::exit(0);
49            // Enter
50            } else if keycode == 97 {
51                let x = width / 2;
52                let y = height / 2;
53                let color = 0xffffff;
54                for i in 0..50 {
55                    mlx.pixel_put(&window, x + i, y + i, color);
56                }
57            } else if keycode == 98 {
58                mlx.put_image_to_window(&window, &image, 0, 0);
59            }
60        },
61        &(),
62    );
63
64    // this will loop forever
65    mlx.event_loop();
66}
Source

pub fn xpm_to_image(&self, xpm_data: Vec<String>) -> Result<MlxImage, MlxError>

Creates a new image from xpm data.

Note that the minilibx does not use the standard Xpm library. You may not be able to read all types of xpm images.

It however handles transparency.

Source

pub fn xpm_file_to_image(&self, filename: &str) -> Result<MlxImage, MlxError>

Creates a new image from an xpm file.

Source

pub fn destroy_image(&self, image: &MlxImage)

Destroy the image. Also drops the image instance.

Source

pub fn put_image_to_window( &self, window: &MlxWindow, image: &MlxImage, x: i32, y: i32, )

Draws an image to the window

Usage:

 let x = 200;
 let y = 200;
 mlx.put_image_to_window(&window, &image, x, y);
Examples found in repository?
examples/example.rs (line 58)
6fn main() {
7    let mlx = Mlx::new().unwrap();
8
9    let width = 1080;
10    let height = 720;
11    let window = mlx.new_window(width, height, "Mlx example").unwrap();
12
13    let image = match mlx.new_image(width, height) {
14        Ok(img) => img,
15        Err(e) => match e {
16            MlxError::Any(s) => return println!("{}", s),
17            _ => return,
18        },
19    };
20
21    let _bytes_per_pixel = image.bits_per_pixel / 8;
22    let offset = image.size_line / 2 + image.size_line * height / 2;
23    for i in 1..100 {
24        // we assume there are 32 bits per pixel here, because its just an example.
25        // color encoding can go from 8 bits to 48 bits.
26        let offset = offset + i * 4;
27        match image.endian {
28            Endian::Little => {
29                image.write_to(offset + 2, 0xff); // R
30                image.write_to(offset + 1, 0x0); // G
31                image.write_to(offset, 0x0); // B
32            }
33            Endian::Big => {
34                image.write_to(offset, 0xff); // R
35                image.write_to(offset + 1, 0x0); // G
36                image.write_to(offset + 2, 0x0); // B
37            }
38        }
39    }
40
41    window.key_hook(
42        move |keycode, _| {
43            // you can also check keycodes using the `xev` command
44            println!("{}", keycode);
45
46            // `q`
47            if keycode == 113 {
48                process::exit(0);
49            // Enter
50            } else if keycode == 97 {
51                let x = width / 2;
52                let y = height / 2;
53                let color = 0xffffff;
54                for i in 0..50 {
55                    mlx.pixel_put(&window, x + i, y + i, color);
56                }
57            } else if keycode == 98 {
58                mlx.put_image_to_window(&window, &image, 0, 0);
59            }
60        },
61        &(),
62    );
63
64    // this will loop forever
65    mlx.event_loop();
66}
Source

pub fn get_color_value(&self, color: i32) -> u32

Transforms an RGB color parameter into a u32 value.

This returns a bits_per_pixel value of the rgb value.

You can use this to write into an image

Source

pub fn do_key_autorepeaton(&self)

Enables key autorepeat when pressing a key

Source

pub fn do_key_autorepeatoff(&self)

Disables key autorepeat when pressing a key

Source

pub fn mouse_move(&self, window: &MlxWindow, x: i32, y: i32)

Moves the mouse cursor

Source

pub fn mouse_show(&self, window: &MlxWindow)

Shows the mouse cursor

Source

pub fn mouse_hide(&self, window: &MlxWindow)

Hides the mouse cursor

Source

pub fn event_loop(&self)

Run the event loop.

This is running an infinite loop which launches hooks when receiving events.

Examples found in repository?
examples/example.rs (line 65)
6fn main() {
7    let mlx = Mlx::new().unwrap();
8
9    let width = 1080;
10    let height = 720;
11    let window = mlx.new_window(width, height, "Mlx example").unwrap();
12
13    let image = match mlx.new_image(width, height) {
14        Ok(img) => img,
15        Err(e) => match e {
16            MlxError::Any(s) => return println!("{}", s),
17            _ => return,
18        },
19    };
20
21    let _bytes_per_pixel = image.bits_per_pixel / 8;
22    let offset = image.size_line / 2 + image.size_line * height / 2;
23    for i in 1..100 {
24        // we assume there are 32 bits per pixel here, because its just an example.
25        // color encoding can go from 8 bits to 48 bits.
26        let offset = offset + i * 4;
27        match image.endian {
28            Endian::Little => {
29                image.write_to(offset + 2, 0xff); // R
30                image.write_to(offset + 1, 0x0); // G
31                image.write_to(offset, 0x0); // B
32            }
33            Endian::Big => {
34                image.write_to(offset, 0xff); // R
35                image.write_to(offset + 1, 0x0); // G
36                image.write_to(offset + 2, 0x0); // B
37            }
38        }
39    }
40
41    window.key_hook(
42        move |keycode, _| {
43            // you can also check keycodes using the `xev` command
44            println!("{}", keycode);
45
46            // `q`
47            if keycode == 113 {
48                process::exit(0);
49            // Enter
50            } else if keycode == 97 {
51                let x = width / 2;
52                let y = height / 2;
53                let color = 0xffffff;
54                for i in 0..50 {
55                    mlx.pixel_put(&window, x + i, y + i, color);
56                }
57            } else if keycode == 98 {
58                mlx.put_image_to_window(&window, &image, 0, 0);
59            }
60        },
61        &(),
62    );
63
64    // this will loop forever
65    mlx.event_loop();
66}

Trait Implementations§

Source§

impl Clone for Mlx

Source§

fn clone(&self) -> Mlx

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Mlx

Auto Trait Implementations§

§

impl Freeze for Mlx

§

impl RefUnwindSafe for Mlx

§

impl !Send for Mlx

§

impl !Sync for Mlx

§

impl Unpin for Mlx

§

impl UnwindSafe for Mlx

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.