Struct DwarfTerm

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

A terminal where each cell has a foreground, background, and sprite id.

Implementations§

Source§

impl DwarfTerm

Source

pub fn new<T>( grid_width: u32, grid_height: u32, title: T, ) -> Result<Self, DwarfTermGliumError>
where T: Into<String>,

Makes a new terminal of the given size and with the given title.

Examples found in repository?
examples/term_test.rs (line 19)
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}
Source

pub fn set_all_foregrounds(&mut self, rgba: u32)

Sets the foreground color of every cell to be the given value.

Source

pub fn set_all_backgrounds(&mut self, rgba: u32)

Sets the foreground color of every cell to be the given value.

Source

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?
examples/term_test.rs (line 100)
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}
Source

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.

Source

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.

Source

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.

Source

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?
examples/term_test.rs (line 102)
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}
Source

pub fn poll_events<F>(&mut self, callback: F)
where F: FnMut(Event),

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?
examples/term_test.rs (lines 35-90)
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}
Source

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).

Source

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?
examples/term_test.rs (line 21)
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}
Source

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?
examples/term_test.rs (line 106)
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§

Source§

impl Debug for DwarfTerm

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
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, 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.
Source§

impl<T> Erased for T