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
use glfw::Context;
/// Represents a window in a GLFW context.
pub struct Window {
pwindow: glfw::PWindow,
glfw: glfw::Glfw,
title: String,
}
impl Window {
/// Creates a new window with the specified title and dimensions.
///
/// # Arguments
///
/// * `title` - A string representing the title of the window.
/// * `width` - The width of the window.
/// * `height` - The height of the window.
///
/// # Returns
///
/// A `Window` object with the specified properties.
///
/// # Panics
///
/// This function will panic if GLFW initialization or window creation fails.
pub fn new(title: &str, width: u32, height: u32) -> Window {
let title = String::from(title);
let mut glfw = glfw::init(glfw::fail_on_errors).expect("Couldn't init GLFW!");
// Set window hints for customization
glfw.window_hint(glfw::WindowHint::X11ClassName(Some(title.clone())));
glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3));
glfw.window_hint(glfw::WindowHint::Floating(true));
// Create the window and capture events
let (mut pwindow, events) = glfw
.create_window(width, height, &title, glfw::WindowMode::Windowed)
.expect("Couldn't create GLFW Window");
// Load OpenGL functions
gl::load_with(|s| pwindow.get_proc_address(s));
// Enable key polling and make the window current
pwindow.set_key_polling(true);
pwindow.make_current();
Window {
pwindow,
title,
glfw,
}
}
/// Updates the window, swapping buffers and polling for events.
///
/// This function should be called each frame to maintain window responsiveness.
pub fn update(&mut self) {
self.pwindow.swap_buffers();
self.glfw.poll_events();
}
/// Clears the window with the specified color.
///
/// # Arguments
///
/// * `red` - Red component of the clear color.
/// * `green` - Green component of the clear color.
/// * `blue` - Blue component of the clear color.
/// * `alpha` - Alpha component of the clear color.
///
/// # Safety
///
/// This function calls OpenGL directly, which is unsafe.
pub fn clear(&self, red: f32, green: f32, blue: f32, alpha: f32) {
unsafe {
gl::ClearColor(red, green, blue, alpha);
gl::Clear(gl::COLOR_BUFFER_BIT);
}
}
/// Returns the current title of the window.
///
/// # Returns
///
/// A reference to the title of the window.
pub fn get_title(&self) -> &str {
&self.title
}
/// Sets a new title for the window.
///
/// # Arguments
///
/// * `title` - The new title to set for the window.
pub fn set_title(&mut self, title: &str) {
self.title = String::from(title);
self.pwindow.set_title(title);
}
/// Checks if the window should close.
///
/// # Returns
///
/// `true` if the window should close, `false` otherwise.
pub fn should_close(&self) -> bool {
self.pwindow.should_close()
}
/// Sets whether the window should close or not.
///
/// # Arguments
///
/// * `value` - `true` if the window should close, `false` otherwise.
pub fn set_should_close(&mut self, value: bool) {
self.pwindow.set_should_close(value);
}
}