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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
extern crate glfw;
use glu_sys::glu::*;
use glfw::{Action, Context, Key};
#[derive(Debug)]
pub enum UfbError {
InitError(glfw::InitError),
Internal(UfbErrorKind),
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum UfbErrorKind {
InvalidFormat,
}
impl std::error::Error for UfbError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl std::fmt::Display for UfbError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
UfbError::InitError(ref err) => write!(f, "A glfw init error occured {:?}", err),
UfbError::Internal(ref err) => write!(f, "An internal error occured {:?}", err),
}
}
}
impl From<glfw::InitError> for UfbError {
fn from(err: glfw::InitError) -> UfbError {
UfbError::InitError(err)
}
}
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum ColorDepth {
L8 = 1,
La8 = 2,
Rgb8 = 3,
Rgba8 = 4,
}
pub struct Window {
glfw: glfw::Glfw,
win: glfw::Window,
w: u32,
h: u32,
visual: ColorDepth,
events: std::sync::mpsc::Receiver<(f64, glfw::WindowEvent)>,
frame: Vec<u8>,
}
impl Window {
pub fn new(w: u32, h: u32, visual: ColorDepth, title: &str) -> Result<Self, UfbError> {
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS)?;
let (mut window, events) = glfw
.create_window(w, h, title, glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window.");
window.set_resizable(false);
window.make_current();
glfw.set_swap_interval(glfw::SwapInterval::Sync(1));
Ok(Self {
glfw,
win: window,
w,
h,
visual,
events,
frame: vec![0u8; (w * h * visual as u32) as usize],
})
}
pub fn get_frame(&mut self) -> &mut [u8] {
&mut self.frame
}
pub fn buffer_index_at(&self, x: u32, y: u32) -> Option<usize> {
if x >= self.w || y >= self.h {
None
} else {
Some((y as usize * self.w as usize + x as usize) * self.visual as usize)
}
}
pub fn show(&mut self) {
while !self.win.should_close() {
let gl_enum = match self.visual {
ColorDepth::L8 => GL_LUMINANCE,
ColorDepth::La8 => GL_LUMINANCE_ALPHA,
ColorDepth::Rgb8 => GL_RGB,
ColorDepth::Rgba8 => GL_RGBA,
};
unsafe {
glRasterPos2i(-1, 1);
glPixelZoom(1., -1.);
glDrawPixels(
self.w as _,
self.h as _,
gl_enum,
GL_UNSIGNED_BYTE,
self.frame.as_ptr() as _,
);
}
self.win.swap_buffers();
self.glfw.poll_events();
for (_, event) in glfw::flush_messages(&self.events) {
if let glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) = event {
self.win.set_should_close(true)
}
}
}
}
pub fn shown(&mut self) -> bool {
let gl_enum = match self.visual {
ColorDepth::L8 => GL_LUMINANCE,
ColorDepth::La8 => GL_LUMINANCE_ALPHA,
ColorDepth::Rgb8 => GL_RGB,
ColorDepth::Rgba8 => GL_RGBA,
};
unsafe {
glRasterPos2i(-1, 1);
glPixelZoom(1., -1.);
glDrawPixels(
self.w as _,
self.h as _,
gl_enum,
GL_UNSIGNED_BYTE,
self.frame.as_ptr() as _,
);
}
self.win.swap_buffers();
self.glfw.poll_events();
for (_, event) in glfw::flush_messages(&self.events) {
if let glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) = event {
self.win.set_should_close(true)
}
}
!self.win.should_close()
}
}