diff --git a/src/console.rs b/src/console.rs index 7bc2da68..44735379 100644 --- a/src/console.rs +++ b/src/console.rs @@ -103,6 +103,10 @@ pub trait Console { /// drawing walls between tiles. fn set_offset(&mut self, x: f32, y: f32); + /// Specify a scale of the scale. A scale above 1.0 will make the text larger. + /// The center of the scale is at character position (center_x, center_y). + fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32); + // Produces the implementor as an Any that can be matched to determine type and access // natively. fn as_any(&self) -> &dyn Any; diff --git a/src/hal/amethyst_be/dummy.rs b/src/hal/amethyst_be/dummy.rs index 33774d2f..46953f9a 100644 --- a/src/hal/amethyst_be/dummy.rs +++ b/src/hal/amethyst_be/dummy.rs @@ -17,6 +17,8 @@ impl SimpleConsoleBackend { _tiles: &[crate::Tile], _offset_x: f32, _offset_y: f32, + _scale: f32, + _scale_center: (i32, i32), ) { } @@ -45,6 +47,8 @@ impl SparseConsoleBackend { _width: u32, _offset_x: f32, _offset_y: f32, + _scale: f32, + _scale_center: (i32, i32), _tiles: &[crate::sparse_console::SparseTile], ) { } diff --git a/src/hal/curses/simple_console_backing.rs b/src/hal/curses/simple_console_backing.rs index ddb2211a..2b4cbe2b 100644 --- a/src/hal/curses/simple_console_backing.rs +++ b/src/hal/curses/simple_console_backing.rs @@ -21,6 +21,8 @@ impl SimpleConsoleBackend { tiles: &[crate::Tile], _offset_x: f32, _offset_y: f32, + _scale: f32, + _scale_center: (i32, i32), ) { self.tiles.clear(); for t in tiles.iter() { diff --git a/src/hal/curses/sparse_console_backing.rs b/src/hal/curses/sparse_console_backing.rs index 2bacfa4d..68dd9e2c 100644 --- a/src/hal/curses/sparse_console_backing.rs +++ b/src/hal/curses/sparse_console_backing.rs @@ -24,6 +24,8 @@ impl SparseConsoleBackend { width: u32, _offset_x: f32, _offset_y: f32, + _scale: f32, + _scale_center: (i32, i32), _tiles: &[crate::sparse_console::SparseTile], ) { self.width = width; diff --git a/src/hal/dummy/mod.rs b/src/hal/dummy/mod.rs index cd9a08f9..bf3d712f 100644 --- a/src/hal/dummy/mod.rs +++ b/src/hal/dummy/mod.rs @@ -88,6 +88,8 @@ impl SimpleConsoleBackend { _tiles: &[crate::Tile], _offset_x: f32, _offset_y: f32, + _scale: f32, + _scale_center: (i32, i32), ) { } @@ -116,6 +118,8 @@ impl SparseConsoleBackend { _width: u32, _offset_x: f32, _offset_y: f32, + _scale: f32, + _scale_center: (i32, i32), _tiles: &[crate::sparse_console::SparseTile], ) { } diff --git a/src/hal/native/simple_console_backing.rs b/src/hal/native/simple_console_backing.rs index 8ebd95e1..01b41803 100644 --- a/src/hal/native/simple_console_backing.rs +++ b/src/hal/native/simple_console_backing.rs @@ -153,19 +153,21 @@ impl SimpleConsoleBackend { tiles: &[Tile], offset_x: f32, offset_y: f32, + scale: f32, + scale_center: (i32, i32), ) { self.vertex_counter = 0; self.index_counter = 0; let glyph_size_x: f32 = 1.0f32 / 16.0f32; let glyph_size_y: f32 = 1.0f32 / 16.0f32; - let step_x: f32 = 2.0f32 / width as f32; - let step_y: f32 = 2.0f32 / height as f32; + let step_x: f32 = scale * 2.0f32 / width as f32; + let step_y: f32 = scale * 2.0f32 / height as f32; let mut index_count: i32 = 0; - let mut screen_y: f32 = -1.0f32; + let mut screen_y: f32 = -1.0 * scale + 2.0 * (scale_center.1 - height as i32/2) as f32 * (scale - 1.0) / height as f32; for y in 0..height { - let mut screen_x: f32 = -1.0f32; + let mut screen_x: f32 = -1.0 * scale - 2.0 * (scale_center.0 - width as i32/2) as f32 * (scale - 1.0) / width as f32; for x in 0..width { let fg = tiles[((y * width) + x) as usize].fg; let bg = tiles[((y * width) + x) as usize].bg; diff --git a/src/hal/native/sparse_console_backing.rs b/src/hal/native/sparse_console_backing.rs index 309aee9b..1f8751bf 100644 --- a/src/hal/native/sparse_console_backing.rs +++ b/src/hal/native/sparse_console_backing.rs @@ -101,6 +101,8 @@ impl SparseConsoleBackend { width: u32, offset_x: f32, offset_y: f32, + scale: f32, + scale_center: (i32, i32), tiles: &[SparseTile], ) { if tiles.is_empty() { @@ -113,16 +115,18 @@ impl SparseConsoleBackend { let glyph_size_x: f32 = 1.0 / 16.0; let glyph_size_y: f32 = 1.0 / 16.0; - let step_x: f32 = 2.0 / width as f32; - let step_y: f32 = 2.0 / height as f32; + let step_x: f32 = scale * 2.0 / width as f32; + let step_y: f32 = scale * 2.0 / height as f32; let mut index_count: i32 = 0; + let screen_x_start: f32 = -1.0 * scale - 2.0 * (scale_center.0 - width as i32/2) as f32 * (scale - 1.0) / width as f32; + let screen_y_start: f32 = -1.0 * scale + 2.0 * (scale_center.1 - height as i32/2) as f32 * (scale - 1.0) / height as f32; for t in tiles.iter() { let x = t.idx % width as usize; let y = t.idx / width as usize; - let screen_x = ((step_x * x as f32) - 1.0) + offset_x; - let screen_y = ((step_y * y as f32) - 1.0) + offset_y; + let screen_x = ((step_x * x as f32) + screen_x_start) + offset_x; + let screen_y = ((step_y * y as f32) + screen_y_start) + offset_y; let fg = t.fg; let bg = t.bg; let glyph = t.glyph; diff --git a/src/hal/wasm/simple_console_backing.rs b/src/hal/wasm/simple_console_backing.rs index 5d81325f..d9a79aec 100644 --- a/src/hal/wasm/simple_console_backing.rs +++ b/src/hal/wasm/simple_console_backing.rs @@ -113,6 +113,8 @@ impl SimpleConsoleBackend { tiles: &Vec, offset_x: f32, offset_y: f32, + _scale: f32, + _scale_center: (i32, i32), ) { let gl = &platform.platform.gl; unsafe { diff --git a/src/hal/wasm/sparse_console_backing.rs b/src/hal/wasm/sparse_console_backing.rs index e0fb1032..2c15806b 100644 --- a/src/hal/wasm/sparse_console_backing.rs +++ b/src/hal/wasm/sparse_console_backing.rs @@ -112,6 +112,8 @@ impl SparseConsoleBackend { width: u32, offset_x: f32, offset_y: f32, + _scale: f32, + _scale_center: (i32, i32), tiles: &Vec, ) { let gl = &platform.platform.gl; diff --git a/src/initializer.rs b/src/initializer.rs index 298560d5..1f12a6e4 100644 --- a/src/initializer.rs +++ b/src/initializer.rs @@ -20,6 +20,11 @@ enum ConsoleType { height: u32, font: String, }, + SparseConsoleNoBg { + width: u32, + height: u32, + font: String + } } /// Provides a builder mechanism for initializing RLTK. You can chain builders together, @@ -248,6 +253,18 @@ impl RltkBuilder { self } + /// Adds a sparse console with no bg rendering layer to the RLTK builder. + pub fn with_sparse_console_no_bg(mut self, width: T, height: T, font: S) -> Self + where T: TryInto + { + self.consoles.push(ConsoleType::SparseConsoleNoBg{ + width : width.try_into().ok().unwrap(), + height : height.try_into().ok().unwrap(), + font : font.to_string() + }); + self + } + /// Enables you to override the vsync default for native rendering. pub fn with_vsync(mut self, vsync: bool) -> Self { self.platform_hints.vsync = vsync; @@ -308,6 +325,11 @@ impl RltkBuilder { font_id, ); } + ConsoleType::SparseConsoleNoBg{width, height, font} => { + let font_path = format!("{}/{}", self.resource_path, font); + let font_id = font_map[&font_path]; + context.register_console_no_bg(SparseConsole::init(*width, *height, &context.backend), font_id); + } } } diff --git a/src/rltk.rs b/src/rltk.rs index 430f9e01..7cb84402 100644 --- a/src/rltk.rs +++ b/src/rltk.rs @@ -355,6 +355,10 @@ impl Console for Rltk { fn set_offset(&mut self, x: f32, y: f32) { self.consoles[self.active_console].console.set_offset(x, y); } + fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32) { + self.consoles[self.active_console].console.set_scale(scale, center_x, center_y); + } + fn as_any(&self) -> &dyn Any { self } diff --git a/src/simple_console.rs b/src/simple_console.rs index dce5d305..3b0263c9 100644 --- a/src/simple_console.rs +++ b/src/simple_console.rs @@ -13,6 +13,9 @@ pub struct SimpleConsole { offset_x: f32, offset_y: f32, + scale: f32, + scale_center: (i32, i32), + backend: hal::SimpleConsoleBackend, } @@ -37,6 +40,8 @@ impl SimpleConsole { is_dirty: true, offset_x: 0.0, offset_y: 0.0, + scale: 1.0, + scale_center: (width as i32 / 2, height as i32 / 2), backend: hal::SimpleConsoleBackend::new(platform, width as usize, height as usize), }; @@ -51,6 +56,8 @@ impl SimpleConsole { &self.tiles, self.offset_x, self.offset_y, + self.scale, + self.scale_center, ); } } @@ -272,6 +279,11 @@ impl Console for SimpleConsole { self.offset_y = y * (2.0 / self.height as f32); } + fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32) { + self.scale = scale; + self.scale_center = (center_x, center_y); + } + fn as_any(&self) -> &dyn Any { self } diff --git a/src/sparse_console.rs b/src/sparse_console.rs index c56a1387..940fd19a 100644 --- a/src/sparse_console.rs +++ b/src/sparse_console.rs @@ -22,6 +22,9 @@ pub struct SparseConsole { offset_x: f32, offset_y: f32, + scale: f32, + scale_center: (i32, i32), + backend: hal::SparseConsoleBackend, } @@ -36,6 +39,8 @@ impl SparseConsole { is_dirty: true, offset_x: 0.0, offset_y: 0.0, + scale: 1.0, + scale_center: (width as i32 / 2, height as i32 / 2), backend: hal::SparseConsoleBackend::new(platform, width as usize, height as usize), }; @@ -49,6 +54,8 @@ impl SparseConsole { self.width, self.offset_x, self.offset_y, + self.scale, + self.scale_center, &self.tiles, ); } @@ -277,6 +284,11 @@ impl Console for SparseConsole { self.offset_y = y * (2.0 / self.height as f32); } + fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32) { + self.scale = scale; + self.scale_center = (center_x, center_y); + } + fn as_any(&self) -> &dyn Any { self }