pub struct DwarfTerm { /* private fields */ }
Expand description
A terminal where each cell has a foreground, background, and sprite id.
Implementations§
Source§impl DwarfTerm
impl DwarfTerm
Sourcepub fn new<T>(
grid_width: u32,
grid_height: u32,
title: T,
) -> Result<Self, DwarfTermGliumError>
pub fn new<T>( grid_width: u32, grid_height: u32, title: T, ) -> Result<Self, DwarfTermGliumError>
Makes a new terminal of the given size and with the given title.
Examples found in repository?
18fn main() {
19 let mut term = DwarfTerm::new(TILE_GRID_WIDTH, TILE_GRID_HEIGHT, "Dwarf Term Test").expect("WHOOPS!");
20 // Add some fancy colors!
21 for (x, y, fg_mut) in term.layer_slices_mut().0.iter_mut() {
22 *fg_mut = rgb32!(
23 x as f32 / TILE_GRID_WIDTH as f32 * 255.0,
24 y as f32 / TILE_GRID_HEIGHT as f32 * 255.0,
25 (TILE_GRID_WIDTH - x as u32) as f32 / TILE_GRID_WIDTH as f32 * 255.0
26 );
27 }
28
29 // Main loop
30 let mut running = true;
31 let mut tab_held = false;
32 let mut watcher_position: (isize, isize) = (5, 5);
33 while running {
34 // Handle Input
35 term.poll_events(|event| match event {
36 Event::WindowEvent { event: win_event, .. } => match win_event {
37 WindowEvent::CloseRequested
38 | WindowEvent::KeyboardInput {
39 input:
40 KeyboardInput {
41 state: ElementState::Pressed,
42 virtual_keycode: Some(VirtualKeyCode::Escape),
43 ..
44 },
45 ..
46 } => {
47 running = false;
48 }
49 WindowEvent::KeyboardInput {
50 input: KeyboardInput {
51 state: ElementState::Pressed,
52 virtual_keycode: Some(key),
53 ..
54 },
55 ..
56 } => match key {
57 VirtualKeyCode::Up => {
58 watcher_position.1 += 1;
59 }
60 VirtualKeyCode::Down => {
61 watcher_position.1 -= 1;
62 }
63 VirtualKeyCode::Left => {
64 watcher_position.0 -= 1;
65 }
66 VirtualKeyCode::Right => {
67 watcher_position.0 += 1;
68 }
69 VirtualKeyCode::Tab => {
70 tab_held = true;
71 }
72 _ => {}
73 },
74 WindowEvent::KeyboardInput {
75 input: KeyboardInput {
76 state: ElementState::Released,
77 virtual_keycode: Some(key),
78 ..
79 },
80 ..
81 } => match key {
82 VirtualKeyCode::Tab => {
83 tab_held = false;
84 }
85 _ => {}
86 },
87 _ => {}
88 },
89 _ => {}
90 });
91
92 if tab_held {
93 let mut total = 0usize;
94 let (mut fgs, _bgs, mut ids) = term.layer_slices_mut();
95 for (_x, _y, ref_mut) in ids.iter_mut() {
96 *ref_mut = total as u8;
97 total += 1;
98 }
99 } else {
100 term.set_all_ids(b' ');
101 term
102 .get_id_mut((watcher_position.0 as usize, watcher_position.1 as usize))
103 .map(|mut_ref| *mut_ref = b'@');
104 }
105
106 let _ = term.clear_draw_swap().map_err(|e| eprintln!("There Was An Error: {:?}", e));
107 }
108}
Sourcepub fn set_all_foregrounds(&mut self, rgba: u32)
pub fn set_all_foregrounds(&mut self, rgba: u32)
Sets the foreground color of every cell to be the given value.
Sourcepub fn set_all_backgrounds(&mut self, rgba: u32)
pub fn set_all_backgrounds(&mut self, rgba: u32)
Sets the foreground color of every cell to be the given value.
Sourcepub fn set_all_ids(&mut self, tile_id: u8)
pub fn set_all_ids(&mut self, tile_id: u8)
Sets the tile id of every cell to be the given value.
Examples found in repository?
18fn main() {
19 let mut term = DwarfTerm::new(TILE_GRID_WIDTH, TILE_GRID_HEIGHT, "Dwarf Term Test").expect("WHOOPS!");
20 // Add some fancy colors!
21 for (x, y, fg_mut) in term.layer_slices_mut().0.iter_mut() {
22 *fg_mut = rgb32!(
23 x as f32 / TILE_GRID_WIDTH as f32 * 255.0,
24 y as f32 / TILE_GRID_HEIGHT as f32 * 255.0,
25 (TILE_GRID_WIDTH - x as u32) as f32 / TILE_GRID_WIDTH as f32 * 255.0
26 );
27 }
28
29 // Main loop
30 let mut running = true;
31 let mut tab_held = false;
32 let mut watcher_position: (isize, isize) = (5, 5);
33 while running {
34 // Handle Input
35 term.poll_events(|event| match event {
36 Event::WindowEvent { event: win_event, .. } => match win_event {
37 WindowEvent::CloseRequested
38 | WindowEvent::KeyboardInput {
39 input:
40 KeyboardInput {
41 state: ElementState::Pressed,
42 virtual_keycode: Some(VirtualKeyCode::Escape),
43 ..
44 },
45 ..
46 } => {
47 running = false;
48 }
49 WindowEvent::KeyboardInput {
50 input: KeyboardInput {
51 state: ElementState::Pressed,
52 virtual_keycode: Some(key),
53 ..
54 },
55 ..
56 } => match key {
57 VirtualKeyCode::Up => {
58 watcher_position.1 += 1;
59 }
60 VirtualKeyCode::Down => {
61 watcher_position.1 -= 1;
62 }
63 VirtualKeyCode::Left => {
64 watcher_position.0 -= 1;
65 }
66 VirtualKeyCode::Right => {
67 watcher_position.0 += 1;
68 }
69 VirtualKeyCode::Tab => {
70 tab_held = true;
71 }
72 _ => {}
73 },
74 WindowEvent::KeyboardInput {
75 input: KeyboardInput {
76 state: ElementState::Released,
77 virtual_keycode: Some(key),
78 ..
79 },
80 ..
81 } => match key {
82 VirtualKeyCode::Tab => {
83 tab_held = false;
84 }
85 _ => {}
86 },
87 _ => {}
88 },
89 _ => {}
90 });
91
92 if tab_held {
93 let mut total = 0usize;
94 let (mut fgs, _bgs, mut ids) = term.layer_slices_mut();
95 for (_x, _y, ref_mut) in ids.iter_mut() {
96 *ref_mut = total as u8;
97 total += 1;
98 }
99 } else {
100 term.set_all_ids(b' ');
101 term
102 .get_id_mut((watcher_position.0 as usize, watcher_position.1 as usize))
103 .map(|mut_ref| *mut_ref = b'@');
104 }
105
106 let _ = term.clear_draw_swap().map_err(|e| eprintln!("There Was An Error: {:?}", e));
107 }
108}
Sourcepub fn set_clear_color(&mut self, r: f32, g: f32, b: f32, a: f32)
pub fn set_clear_color(&mut self, r: f32, g: f32, b: f32, a: f32)
Sets the “clear” color that’s used.
This just passes the call along to OpenGL. Inputs should each be in the 0.0 to 1.0 (inclusive) range. Out of bounds inputs are automatically clamped by OpenGL.
Sourcepub fn get_foreground_mut(
&mut self,
position: (usize, usize),
) -> Option<&mut u32>
pub fn get_foreground_mut( &mut self, position: (usize, usize), ) -> Option<&mut u32>
gets a mutable reference to the foreground at the position specified.
Fails if the position would be out of bounds.
Sourcepub fn get_background_mut(
&mut self,
position: (usize, usize),
) -> Option<&mut u32>
pub fn get_background_mut( &mut self, position: (usize, usize), ) -> Option<&mut u32>
gets a mutable reference to the background at the position specified.
Fails if the position would be out of bounds.
Sourcepub fn get_id_mut(&mut self, position: (usize, usize)) -> Option<&mut u8>
pub fn get_id_mut(&mut self, position: (usize, usize)) -> Option<&mut u8>
gets a mutable reference to the tile id at the position specified.
Fails if the position would be out of bounds.
Examples found in repository?
18fn main() {
19 let mut term = DwarfTerm::new(TILE_GRID_WIDTH, TILE_GRID_HEIGHT, "Dwarf Term Test").expect("WHOOPS!");
20 // Add some fancy colors!
21 for (x, y, fg_mut) in term.layer_slices_mut().0.iter_mut() {
22 *fg_mut = rgb32!(
23 x as f32 / TILE_GRID_WIDTH as f32 * 255.0,
24 y as f32 / TILE_GRID_HEIGHT as f32 * 255.0,
25 (TILE_GRID_WIDTH - x as u32) as f32 / TILE_GRID_WIDTH as f32 * 255.0
26 );
27 }
28
29 // Main loop
30 let mut running = true;
31 let mut tab_held = false;
32 let mut watcher_position: (isize, isize) = (5, 5);
33 while running {
34 // Handle Input
35 term.poll_events(|event| match event {
36 Event::WindowEvent { event: win_event, .. } => match win_event {
37 WindowEvent::CloseRequested
38 | WindowEvent::KeyboardInput {
39 input:
40 KeyboardInput {
41 state: ElementState::Pressed,
42 virtual_keycode: Some(VirtualKeyCode::Escape),
43 ..
44 },
45 ..
46 } => {
47 running = false;
48 }
49 WindowEvent::KeyboardInput {
50 input: KeyboardInput {
51 state: ElementState::Pressed,
52 virtual_keycode: Some(key),
53 ..
54 },
55 ..
56 } => match key {
57 VirtualKeyCode::Up => {
58 watcher_position.1 += 1;
59 }
60 VirtualKeyCode::Down => {
61 watcher_position.1 -= 1;
62 }
63 VirtualKeyCode::Left => {
64 watcher_position.0 -= 1;
65 }
66 VirtualKeyCode::Right => {
67 watcher_position.0 += 1;
68 }
69 VirtualKeyCode::Tab => {
70 tab_held = true;
71 }
72 _ => {}
73 },
74 WindowEvent::KeyboardInput {
75 input: KeyboardInput {
76 state: ElementState::Released,
77 virtual_keycode: Some(key),
78 ..
79 },
80 ..
81 } => match key {
82 VirtualKeyCode::Tab => {
83 tab_held = false;
84 }
85 _ => {}
86 },
87 _ => {}
88 },
89 _ => {}
90 });
91
92 if tab_held {
93 let mut total = 0usize;
94 let (mut fgs, _bgs, mut ids) = term.layer_slices_mut();
95 for (_x, _y, ref_mut) in ids.iter_mut() {
96 *ref_mut = total as u8;
97 total += 1;
98 }
99 } else {
100 term.set_all_ids(b' ');
101 term
102 .get_id_mut((watcher_position.0 as usize, watcher_position.1 as usize))
103 .map(|mut_ref| *mut_ref = b'@');
104 }
105
106 let _ = term.clear_draw_swap().map_err(|e| eprintln!("There Was An Error: {:?}", e));
107 }
108}
Sourcepub fn poll_events<F>(&mut self, callback: F)
pub fn poll_events<F>(&mut self, callback: F)
Lets you poll for events on the window.
The DwarfTerm will automatically check for WindowEvent::Resized
events
and then keep itself up to date. However, it will also pass along such
events to your callback so that you can make any necessary changes on your
end as well.
Examples found in repository?
18fn main() {
19 let mut term = DwarfTerm::new(TILE_GRID_WIDTH, TILE_GRID_HEIGHT, "Dwarf Term Test").expect("WHOOPS!");
20 // Add some fancy colors!
21 for (x, y, fg_mut) in term.layer_slices_mut().0.iter_mut() {
22 *fg_mut = rgb32!(
23 x as f32 / TILE_GRID_WIDTH as f32 * 255.0,
24 y as f32 / TILE_GRID_HEIGHT as f32 * 255.0,
25 (TILE_GRID_WIDTH - x as u32) as f32 / TILE_GRID_WIDTH as f32 * 255.0
26 );
27 }
28
29 // Main loop
30 let mut running = true;
31 let mut tab_held = false;
32 let mut watcher_position: (isize, isize) = (5, 5);
33 while running {
34 // Handle Input
35 term.poll_events(|event| match event {
36 Event::WindowEvent { event: win_event, .. } => match win_event {
37 WindowEvent::CloseRequested
38 | WindowEvent::KeyboardInput {
39 input:
40 KeyboardInput {
41 state: ElementState::Pressed,
42 virtual_keycode: Some(VirtualKeyCode::Escape),
43 ..
44 },
45 ..
46 } => {
47 running = false;
48 }
49 WindowEvent::KeyboardInput {
50 input: KeyboardInput {
51 state: ElementState::Pressed,
52 virtual_keycode: Some(key),
53 ..
54 },
55 ..
56 } => match key {
57 VirtualKeyCode::Up => {
58 watcher_position.1 += 1;
59 }
60 VirtualKeyCode::Down => {
61 watcher_position.1 -= 1;
62 }
63 VirtualKeyCode::Left => {
64 watcher_position.0 -= 1;
65 }
66 VirtualKeyCode::Right => {
67 watcher_position.0 += 1;
68 }
69 VirtualKeyCode::Tab => {
70 tab_held = true;
71 }
72 _ => {}
73 },
74 WindowEvent::KeyboardInput {
75 input: KeyboardInput {
76 state: ElementState::Released,
77 virtual_keycode: Some(key),
78 ..
79 },
80 ..
81 } => match key {
82 VirtualKeyCode::Tab => {
83 tab_held = false;
84 }
85 _ => {}
86 },
87 _ => {}
88 },
89 _ => {}
90 });
91
92 if tab_held {
93 let mut total = 0usize;
94 let (mut fgs, _bgs, mut ids) = term.layer_slices_mut();
95 for (_x, _y, ref_mut) in ids.iter_mut() {
96 *ref_mut = total as u8;
97 total += 1;
98 }
99 } else {
100 term.set_all_ids(b' ');
101 term
102 .get_id_mut((watcher_position.0 as usize, watcher_position.1 as usize))
103 .map(|mut_ref| *mut_ref = b'@');
104 }
105
106 let _ = term.clear_draw_swap().map_err(|e| eprintln!("There Was An Error: {:?}", e));
107 }
108}
Sourcepub fn layer_slices(
&self,
) -> (ImageSlice<'_, u32>, ImageSlice<'_, u32>, ImageSlice<'_, u8>)
pub fn layer_slices( &self, ) -> (ImageSlice<'_, u32>, ImageSlice<'_, u32>, ImageSlice<'_, u8>)
Gives back all three layers at once in image slice form.
(fgs, bgs, ids).
Sourcepub fn layer_slices_mut(
&mut self,
) -> (ImageMutSlice<'_, u32>, ImageMutSlice<'_, u32>, ImageMutSlice<'_, u8>)
pub fn layer_slices_mut( &mut self, ) -> (ImageMutSlice<'_, u32>, ImageMutSlice<'_, u32>, ImageMutSlice<'_, u8>)
Gives back all three layers at once in image mutable slice form.
(fgs, bgs, ids).
Examples found in repository?
18fn main() {
19 let mut term = DwarfTerm::new(TILE_GRID_WIDTH, TILE_GRID_HEIGHT, "Dwarf Term Test").expect("WHOOPS!");
20 // Add some fancy colors!
21 for (x, y, fg_mut) in term.layer_slices_mut().0.iter_mut() {
22 *fg_mut = rgb32!(
23 x as f32 / TILE_GRID_WIDTH as f32 * 255.0,
24 y as f32 / TILE_GRID_HEIGHT as f32 * 255.0,
25 (TILE_GRID_WIDTH - x as u32) as f32 / TILE_GRID_WIDTH as f32 * 255.0
26 );
27 }
28
29 // Main loop
30 let mut running = true;
31 let mut tab_held = false;
32 let mut watcher_position: (isize, isize) = (5, 5);
33 while running {
34 // Handle Input
35 term.poll_events(|event| match event {
36 Event::WindowEvent { event: win_event, .. } => match win_event {
37 WindowEvent::CloseRequested
38 | WindowEvent::KeyboardInput {
39 input:
40 KeyboardInput {
41 state: ElementState::Pressed,
42 virtual_keycode: Some(VirtualKeyCode::Escape),
43 ..
44 },
45 ..
46 } => {
47 running = false;
48 }
49 WindowEvent::KeyboardInput {
50 input: KeyboardInput {
51 state: ElementState::Pressed,
52 virtual_keycode: Some(key),
53 ..
54 },
55 ..
56 } => match key {
57 VirtualKeyCode::Up => {
58 watcher_position.1 += 1;
59 }
60 VirtualKeyCode::Down => {
61 watcher_position.1 -= 1;
62 }
63 VirtualKeyCode::Left => {
64 watcher_position.0 -= 1;
65 }
66 VirtualKeyCode::Right => {
67 watcher_position.0 += 1;
68 }
69 VirtualKeyCode::Tab => {
70 tab_held = true;
71 }
72 _ => {}
73 },
74 WindowEvent::KeyboardInput {
75 input: KeyboardInput {
76 state: ElementState::Released,
77 virtual_keycode: Some(key),
78 ..
79 },
80 ..
81 } => match key {
82 VirtualKeyCode::Tab => {
83 tab_held = false;
84 }
85 _ => {}
86 },
87 _ => {}
88 },
89 _ => {}
90 });
91
92 if tab_held {
93 let mut total = 0usize;
94 let (mut fgs, _bgs, mut ids) = term.layer_slices_mut();
95 for (_x, _y, ref_mut) in ids.iter_mut() {
96 *ref_mut = total as u8;
97 total += 1;
98 }
99 } else {
100 term.set_all_ids(b' ');
101 term
102 .get_id_mut((watcher_position.0 as usize, watcher_position.1 as usize))
103 .map(|mut_ref| *mut_ref = b'@');
104 }
105
106 let _ = term.clear_draw_swap().map_err(|e| eprintln!("There Was An Error: {:?}", e));
107 }
108}
Sourcepub fn clear_draw_swap(&mut self) -> Result<(), DwarfTermGliumError>
pub fn clear_draw_swap(&mut self) -> Result<(), DwarfTermGliumError>
Clears the old screen, draws the new data, and then swaps the buffers.
This should hopefully never error! But it might. If it does, there’s a strong chance it’s my fault, not yours.
Examples found in repository?
18fn main() {
19 let mut term = DwarfTerm::new(TILE_GRID_WIDTH, TILE_GRID_HEIGHT, "Dwarf Term Test").expect("WHOOPS!");
20 // Add some fancy colors!
21 for (x, y, fg_mut) in term.layer_slices_mut().0.iter_mut() {
22 *fg_mut = rgb32!(
23 x as f32 / TILE_GRID_WIDTH as f32 * 255.0,
24 y as f32 / TILE_GRID_HEIGHT as f32 * 255.0,
25 (TILE_GRID_WIDTH - x as u32) as f32 / TILE_GRID_WIDTH as f32 * 255.0
26 );
27 }
28
29 // Main loop
30 let mut running = true;
31 let mut tab_held = false;
32 let mut watcher_position: (isize, isize) = (5, 5);
33 while running {
34 // Handle Input
35 term.poll_events(|event| match event {
36 Event::WindowEvent { event: win_event, .. } => match win_event {
37 WindowEvent::CloseRequested
38 | WindowEvent::KeyboardInput {
39 input:
40 KeyboardInput {
41 state: ElementState::Pressed,
42 virtual_keycode: Some(VirtualKeyCode::Escape),
43 ..
44 },
45 ..
46 } => {
47 running = false;
48 }
49 WindowEvent::KeyboardInput {
50 input: KeyboardInput {
51 state: ElementState::Pressed,
52 virtual_keycode: Some(key),
53 ..
54 },
55 ..
56 } => match key {
57 VirtualKeyCode::Up => {
58 watcher_position.1 += 1;
59 }
60 VirtualKeyCode::Down => {
61 watcher_position.1 -= 1;
62 }
63 VirtualKeyCode::Left => {
64 watcher_position.0 -= 1;
65 }
66 VirtualKeyCode::Right => {
67 watcher_position.0 += 1;
68 }
69 VirtualKeyCode::Tab => {
70 tab_held = true;
71 }
72 _ => {}
73 },
74 WindowEvent::KeyboardInput {
75 input: KeyboardInput {
76 state: ElementState::Released,
77 virtual_keycode: Some(key),
78 ..
79 },
80 ..
81 } => match key {
82 VirtualKeyCode::Tab => {
83 tab_held = false;
84 }
85 _ => {}
86 },
87 _ => {}
88 },
89 _ => {}
90 });
91
92 if tab_held {
93 let mut total = 0usize;
94 let (mut fgs, _bgs, mut ids) = term.layer_slices_mut();
95 for (_x, _y, ref_mut) in ids.iter_mut() {
96 *ref_mut = total as u8;
97 total += 1;
98 }
99 } else {
100 term.set_all_ids(b' ');
101 term
102 .get_id_mut((watcher_position.0 as usize, watcher_position.1 as usize))
103 .map(|mut_ref| *mut_ref = b'@');
104 }
105
106 let _ = term.clear_draw_swap().map_err(|e| eprintln!("There Was An Error: {:?}", e));
107 }
108}
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for DwarfTerm
impl !RefUnwindSafe for DwarfTerm
impl !Send for DwarfTerm
impl !Sync for DwarfTerm
impl Unpin for DwarfTerm
impl !UnwindSafe for DwarfTerm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.