Struct cgl_rs::window::Window

source ·
pub struct Window { /* private fields */ }
Expand description

Represents a window, with a handle to the underlying CGL window and a flag indicating whether it has been destroyed.

Implementations§

source§

impl Window

source

pub fn new(title: &str, width: i32, height: i32) -> Result<Window, &'static str>

Creates a new window with the given title, width, and height.

Arguments
  • title - The title of the window.
  • width - The width of the window.
  • height - The height of the window.
Returns

Returns a Result containing the created Window if successful, or an error message if the window creation failed.

Panics

Panics if the width or height is not positive.

Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
window.destroy(); // we must destroy the window before shutting down
cgl_rs::shutdown();
source

pub fn new_undecorated( title: &str, width: i32, height: i32 ) -> Result<Window, &'static str>

Creates a new undecorated window with the given title, width, and height.

An undecorated window is a window without any borders or title bar.

Arguments
  • title - The title of the window.
  • width - The width of the window.
  • height - The height of the window.
Returns

Returns a Result containing the created Window if successful, or an error message if the window creation failed.

Panics

Panics if the width or height is not positive.

Example
cgl_rs::init();
{
    let mut window = cgl_rs::Window::new_undecorated("My Window", 800, 600).unwrap();
    // window.destroy(); // optional
}
cgl_rs::shutdown();
source

pub fn destroy(&mut self)

Destroys the window, freeing any resources associated with it.

NOTE: This is called automatically when the Window goes out of scope, so it is not necessary to call this manually. However, it is safe to call this multiple times, and the subsequent calls will have no effect. But one thing to be noted is that this function must be called before cgl_rs::shutdown() is called.

Example
cgl_rs::init();
{
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    window.destroy(); // optional
}
cgl_rs::shutdown();
source

pub fn register_for_events(&self)

Registers the window to receive events by setting the appropriate callbacks.

This function should be called after creating the window and before entering the event loop.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    window.register_for_events();
}
cgl_rs::shutdown();
source

pub fn attach_event_handler( &mut self, name: &str, handler: &'static EventFunction )

Attaches an named event handler to the window.

The name parameter is a string that identifies the event handler. This can be any string that is unique to the event handler.

The handler parameter is a reference to a function that will be called when the event occurs. The function must have the following signature:

fn my_event_handler(window: &cgl_rs::Window, event: &cgl_rs::Event) {
    println!("Event occurred!");
}
Example
cgl_rs::init();
{
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();

    // window.attach_event_handler("my_event", &my_event_handler);
}
cgl_rs::shutdown();
source

pub fn detach_event_handler(&mut self, name: &str)

Detaches a named event handler from the window.

The name parameter is a string that identifies the event handler. This should be the same string that was used to attach the event handler.

Example
cgl_rs::init();
{
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
     
    window.detach_event_handler("my_event");
}
cgl_rs::shutdown();
source

pub fn poll_events(&self)

Polls for events on the window.

This function should be called in a loop to continuously poll for events on the window.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();

    while true {
        window.poll_events();
        // Handle events here
        // ...
        break; // break out of the loop for testing purposes
    }

}
cgl_rs::shutdown();
source

pub fn swap_buffers(&self)

Swaps the front and back buffers of the window.

This function should be called after rendering to the back buffer to display the rendered image on the screen.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();

    while true {
        window.swap_buffers();
        // Handle events here
        // ...
        break; // break out of the loop for testing purposes
    }

}
cgl_rs::shutdown();
source

pub fn should_close(&self) -> bool

Returns whether or not the window should be closed.

This function should be called in a loop to continuously check if the window should be closed.

Returns

A boolean value indicating whether or not the window should be closed.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();

    while !window.should_close() {
        // ...
        break; // break out of the loop for testing purposes
    }

}
cgl_rs::shutdown();
source

pub fn set_title(&self, title: &str)

Sets the title of the window.

Arguments
  • title - A string slice containing the new title of the window.
Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    window.set_title("New Title");
}
cgl_rs::shutdown();
source

pub fn set_size(&self, width: i32, height: i32)

Sets the size of the window.

Arguments
  • width - The new width of the window.
  • height - The new height of the window.
Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    window.set_size(1024, 768);
}
cgl_rs::shutdown();
source

pub fn set_position(&self, x: i32, y: i32)

Sets the position of the window.

Arguments
  • x - The new x position of the window.
  • y - The new y position of the window.
Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    window.set_position(100, 100);
}
cgl_rs::shutdown();
source

pub fn set_hidden(&mut self, hidden: bool)

Sets the visibility of the window.

Arguments
  • hidden - A boolean indicating whether the window should be hidden or not.
Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    window.set_hidden(true);
}
cgl_rs::shutdown();
source

pub fn set_user_data<T>(&self, user_data: *mut T)

Sets the user data associated with the window.

Arguments
  • user_data - A pointer to the user data to associate with the window.
Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let user_data = Box::into_raw(Box::new(42));
    window.set_user_data(user_data);
}
cgl_rs::shutdown();
source

pub fn get_user_data<T>(&self) -> *mut T

Gets the user data associated with the window.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let user_data = Box::into_raw(Box::new(42));
    window.set_user_data(user_data);
    let retrieved_user_data = window.get_user_data();
    assert_eq!(retrieved_user_data, user_data);
}
cgl_rs::shutdown();
source

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

Gets the size of the window.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let (width, height) = window.get_size();
    assert_eq!(width, 800);
    assert_eq!(height, 600);
}
cgl_rs::shutdown();
source

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

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let (x, y) = window.get_position();
}
cgl_rs::shutdown();
source

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

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let (width, height) = window.get_framebuffer_size();
    assert_eq!(width, 800);
    assert_eq!(height, 600);
}
cgl_rs::shutdown();
source

pub fn rescure_callbacks(&self)

Rescues the callbacks of the window. This is usefule whn usiong cgl::window along with any third party library that internally uses glfw with cgl::window.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    window.rescure_callbacks();
}
cgl_rs::shutdown();
source

pub fn make_context_current(&self)

Makes the OpenGL context of the window current.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    window.make_context_current();
}
cgl_rs::shutdown();
source

pub fn get_glfw_handle(&self) -> *mut GLFWwindow

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let handle = window.get_glfw_handle();
}
cgl_rs::shutdown();
source

pub fn get_key(&self, key: Key) -> Action

Arguments
  • key - A Key enum value representing the key to get the state of.
Returns

An Action enum value representing the state of the key.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let action = window.get_key(cgl_rs::Key::A);
    match action {
        cgl_rs::Action::Release => println!("Key A was released"),
        cgl_rs::Action::Press => println!("Key A was pressed"),
        cgl_rs::Action::Repeat => println!("Key A was repeated"),
    }
}
cgl_rs::shutdown();
source

pub fn is_key_pressed(&self, key: Key) -> bool

Checks if a keyboard key of the window is pressed.

Arguments
  • key - A Key enum value representing the key to check.
Returns

A bool value representing if the key is pressed.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    if window.is_key_pressed(cgl_rs::Key::A) {
        println!("Key A is pressed");
    }
}
cgl_rs::shutdown();
source

pub fn get_mouse_button(&self, button: MouseButton) -> Action

Gets the state of a mouse button of the window.

Arguments
  • button - A MouseButton enum value representing the mouse button to get the state of.
Returns

An Action enum value representing the state of the mouse button.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let action = window.get_mouse_button(cgl_rs::MouseButton::Left);
    match action {
       cgl_rs::Action::Release => println!("Left mouse button was released"),
       cgl_rs::Action::Press => println!("Left mouse button was pressed"),
       cgl_rs::Action::Repeat => println!("Left mouse button was repeated"),
    }
}
cgl_rs::shutdown();
source

pub fn is_mouse_button_pressed(&self, button: MouseButton) -> bool

Checks if a mouse button of the window is pressed.

Arguments
  • button - A MouseButton enum value representing the mouse button to check.
Returns

A bool value representing if the mouse button is pressed.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    if window.is_mouse_button_pressed(cgl_rs::MouseButton::Left) {
        println!("Left mouse button is pressed");
    }
}
cgl_rs::shutdown();
source

pub fn get_mouse_position(&self) -> (f64, f64)

Gets the position of the mouse cursor relative to the top-left corner of the window.

Returns

A tuple containing the x and y coordinates of the mouse cursor.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let (x, y) = window.get_mouse_position();
    println!("Mouse position: ({}, {})", x, y);
}
cgl_rs::shutdown();

Trait Implementations§

source§

impl Clone for Window

source§

fn clone(&self) -> Self

Clones the current window instance.

NOTE: The new instance will have the same handle, has_been_destroyed flag and empty event_handlers map. This means that the new instance will not be able to receive events nor will the internal window handle be destroyed when the new instance is dropped. The internal window handle will be destroyed when the original instance is dropped.

Returns

A new Window instance with the same handle, has_been_destroyed flag and empty event_handlers map.

Example
cgl_rs::init();
{
    // window inside scope so that it is dropped before shutdown is called
    let mut window = cgl_rs::Window::new("My Window", 800, 600).unwrap();
    let mut window_clone = window.clone();
}
cgl_rs::shutdown();
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Drop for Window

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Window

§

impl !Send for Window

§

impl !Sync for Window

§

impl Unpin for Window

§

impl !UnwindSafe for Window

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.