From b76a53aff4e7e32265128f50deae2309866d7c7b Mon Sep 17 00:00:00 2001 From: Sebastian Andersson Date: Wed, 29 Jan 2020 21:58:57 +0100 Subject: [PATCH 1/3] Create consoles without bg from the builder --- src/initializer.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/initializer.rs b/src/initializer.rs index 19664406..09f4c60b 100644 --- a/src/initializer.rs +++ b/src/initializer.rs @@ -11,7 +11,8 @@ struct BuilderFont { /// Internal enum defining a console to be loaded. enum ConsoleType { SimpleConsole{ width: u32, height: u32, font: String }, - SparseConsole{ width: u32, height: u32, font: String } + SparseConsole{ width: u32, height: u32, font: String }, + SparseConsoleNoBg{ width: u32, height: u32, font: String } } /// Provides a builder mechanism for initializing RLTK. You can chain builders together, @@ -228,6 +229,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; @@ -268,6 +281,11 @@ impl RltkBuilder { let font_id = font_map[&font_path]; context.register_console(SparseConsole::init(*width, *height, &context.backend), 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); + } } } From 592a8996ec723bb50160200982716d7099f067cf Mon Sep 17 00:00:00 2001 From: Sebastian Andersson Date: Wed, 29 Jan 2020 21:59:32 +0100 Subject: [PATCH 2/3] Scale texts to support zooming --- src/console.rs | 3 +++ src/hal/amethyst_be/dummy.rs | 2 ++ src/hal/curses/simple_console_backing.rs | 1 + src/hal/curses/sparse_console_backing.rs | 1 + src/hal/dummy/mod.rs | 2 ++ src/hal/native/simple_console_backing.rs | 9 +++++---- src/hal/native/sparse_console_backing.rs | 9 +++++---- src/hal/wasm/simple_console_backing.rs | 1 + src/hal/wasm/sparse_console_backing.rs | 1 + src/rltk.rs | 4 ++++ src/simple_console.rs | 8 ++++++++ src/sparse_console.rs | 8 ++++++++ 12 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/console.rs b/src/console.rs index 4fc15a8c..c5cd7478 100644 --- a/src/console.rs +++ b/src/console.rs @@ -103,6 +103,9 @@ 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. + fn set_scale(&mut self, scale: f32); + // 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..2d5bce81 100644 --- a/src/hal/amethyst_be/dummy.rs +++ b/src/hal/amethyst_be/dummy.rs @@ -17,6 +17,7 @@ impl SimpleConsoleBackend { _tiles: &[crate::Tile], _offset_x: f32, _offset_y: f32, + _scale: f32, ) { } @@ -45,6 +46,7 @@ impl SparseConsoleBackend { _width: u32, _offset_x: f32, _offset_y: f32, + _scale: f32, _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..12a61dca 100644 --- a/src/hal/curses/simple_console_backing.rs +++ b/src/hal/curses/simple_console_backing.rs @@ -21,6 +21,7 @@ impl SimpleConsoleBackend { tiles: &[crate::Tile], _offset_x: f32, _offset_y: f32, + _scale: f32, ) { 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..b75dbbb9 100644 --- a/src/hal/curses/sparse_console_backing.rs +++ b/src/hal/curses/sparse_console_backing.rs @@ -24,6 +24,7 @@ impl SparseConsoleBackend { width: u32, _offset_x: f32, _offset_y: f32, + _scale: f32, _tiles: &[crate::sparse_console::SparseTile], ) { self.width = width; diff --git a/src/hal/dummy/mod.rs b/src/hal/dummy/mod.rs index 604a8b9e..a74f7ea7 100644 --- a/src/hal/dummy/mod.rs +++ b/src/hal/dummy/mod.rs @@ -87,6 +87,7 @@ impl SimpleConsoleBackend { _tiles: &[crate::Tile], _offset_x: f32, _offset_y: f32, + _scale: f32, ) { } @@ -115,6 +116,7 @@ impl SparseConsoleBackend { _width: u32, _offset_x: f32, _offset_y: f32, + _scale: f32, _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..824d146f 100644 --- a/src/hal/native/simple_console_backing.rs +++ b/src/hal/native/simple_console_backing.rs @@ -153,19 +153,20 @@ impl SimpleConsoleBackend { tiles: &[Tile], offset_x: f32, offset_y: f32, + scale: f32, ) { 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.0f32 * scale; for y in 0..height { - let mut screen_x: f32 = -1.0f32; + let mut screen_x: f32 = -1.0f32 * scale; 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..2828094c 100644 --- a/src/hal/native/sparse_console_backing.rs +++ b/src/hal/native/sparse_console_backing.rs @@ -101,6 +101,7 @@ impl SparseConsoleBackend { width: u32, offset_x: f32, offset_y: f32, + scale: f32, tiles: &[SparseTile], ) { if tiles.is_empty() { @@ -113,16 +114,16 @@ 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; 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) - 1.0 * scale) + offset_x; + let screen_y = ((step_y * y as f32) - 1.0 * scale) + 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..2b06f81e 100644 --- a/src/hal/wasm/simple_console_backing.rs +++ b/src/hal/wasm/simple_console_backing.rs @@ -113,6 +113,7 @@ impl SimpleConsoleBackend { tiles: &Vec, offset_x: f32, offset_y: f32, + _scale: f32, ) { 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..4af359ae 100644 --- a/src/hal/wasm/sparse_console_backing.rs +++ b/src/hal/wasm/sparse_console_backing.rs @@ -112,6 +112,7 @@ impl SparseConsoleBackend { width: u32, offset_x: f32, offset_y: f32, + _scale: f32, tiles: &Vec, ) { let gl = &platform.platform.gl; diff --git a/src/rltk.rs b/src/rltk.rs index e701e45b..2051db7d 100644 --- a/src/rltk.rs +++ b/src/rltk.rs @@ -341,6 +341,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) { + self.consoles[self.active_console].console.set_scale(scale); + } + fn as_any(&self) -> &dyn Any { self } diff --git a/src/simple_console.rs b/src/simple_console.rs index 57de19cd..2d293bc2 100644 --- a/src/simple_console.rs +++ b/src/simple_console.rs @@ -13,6 +13,8 @@ pub struct SimpleConsole { offset_x: f32, offset_y: f32, + scale: f32, + backend: hal::SimpleConsoleBackend, } @@ -37,6 +39,7 @@ impl SimpleConsole { is_dirty: true, offset_x: 0.0, offset_y: 0.0, + scale: 1.0, backend: hal::SimpleConsoleBackend::new(platform, width as usize, height as usize), }; @@ -51,6 +54,7 @@ impl SimpleConsole { &self.tiles, self.offset_x, self.offset_y, + self.scale, ); } } @@ -272,6 +276,10 @@ impl Console for SimpleConsole { self.offset_y = y * (2.0 / self.height as f32); } + fn set_scale(&mut self, scale: f32) { + self.scale = scale; + } + fn as_any(&self) -> &dyn Any { self } diff --git a/src/sparse_console.rs b/src/sparse_console.rs index 786501b9..0ed29016 100644 --- a/src/sparse_console.rs +++ b/src/sparse_console.rs @@ -22,6 +22,8 @@ pub struct SparseConsole { offset_x: f32, offset_y: f32, + scale: f32, + backend: hal::SparseConsoleBackend, } @@ -36,6 +38,7 @@ impl SparseConsole { is_dirty: true, offset_x: 0.0, offset_y: 0.0, + scale: 1.0, backend: hal::SparseConsoleBackend::new(platform, width as usize, height as usize), }; @@ -49,6 +52,7 @@ impl SparseConsole { self.width, self.offset_x, self.offset_y, + self.scale, &self.tiles, ); } @@ -263,6 +267,10 @@ impl Console for SparseConsole { self.offset_y = y * (2.0 / self.height as f32); } + fn set_scale(&mut self, scale: f32) { + self.scale = scale; + } + fn as_any(&self) -> &dyn Any { self } From b6fb15249845353d49c5547a09e77f9209e8b428 Mon Sep 17 00:00:00 2001 From: Sebastian Andersson Date: Thu, 30 Jan 2020 22:34:56 +0100 Subject: [PATCH 3/3] Added center of scale as well --- src/console.rs | 3 ++- src/hal/amethyst_be/dummy.rs | 2 ++ src/hal/curses/simple_console_backing.rs | 1 + src/hal/curses/sparse_console_backing.rs | 1 + src/hal/dummy/mod.rs | 2 ++ src/hal/native/simple_console_backing.rs | 5 +++-- src/hal/native/sparse_console_backing.rs | 7 +++++-- src/hal/wasm/simple_console_backing.rs | 1 + src/hal/wasm/sparse_console_backing.rs | 1 + src/rltk.rs | 4 ++-- src/simple_console.rs | 6 +++++- src/sparse_console.rs | 6 +++++- 12 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/console.rs b/src/console.rs index c5cd7478..44524f37 100644 --- a/src/console.rs +++ b/src/console.rs @@ -104,7 +104,8 @@ pub trait Console { 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. - fn set_scale(&mut self, scale: f32); + /// 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. diff --git a/src/hal/amethyst_be/dummy.rs b/src/hal/amethyst_be/dummy.rs index 2d5bce81..46953f9a 100644 --- a/src/hal/amethyst_be/dummy.rs +++ b/src/hal/amethyst_be/dummy.rs @@ -18,6 +18,7 @@ impl SimpleConsoleBackend { _offset_x: f32, _offset_y: f32, _scale: f32, + _scale_center: (i32, i32), ) { } @@ -47,6 +48,7 @@ impl SparseConsoleBackend { _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 12a61dca..2b4cbe2b 100644 --- a/src/hal/curses/simple_console_backing.rs +++ b/src/hal/curses/simple_console_backing.rs @@ -22,6 +22,7 @@ impl SimpleConsoleBackend { _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 b75dbbb9..68dd9e2c 100644 --- a/src/hal/curses/sparse_console_backing.rs +++ b/src/hal/curses/sparse_console_backing.rs @@ -25,6 +25,7 @@ impl SparseConsoleBackend { _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 a74f7ea7..d2bc1eda 100644 --- a/src/hal/dummy/mod.rs +++ b/src/hal/dummy/mod.rs @@ -88,6 +88,7 @@ impl SimpleConsoleBackend { _offset_x: f32, _offset_y: f32, _scale: f32, + _scale_center: (i32, i32), ) { } @@ -117,6 +118,7 @@ impl SparseConsoleBackend { _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 824d146f..01b41803 100644 --- a/src/hal/native/simple_console_backing.rs +++ b/src/hal/native/simple_console_backing.rs @@ -154,6 +154,7 @@ impl SimpleConsoleBackend { offset_x: f32, offset_y: f32, scale: f32, + scale_center: (i32, i32), ) { self.vertex_counter = 0; self.index_counter = 0; @@ -164,9 +165,9 @@ impl SimpleConsoleBackend { let step_y: f32 = scale * 2.0f32 / height as f32; let mut index_count: i32 = 0; - let mut screen_y: f32 = -1.0f32 * scale; + 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 * scale; + 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 2828094c..1f8751bf 100644 --- a/src/hal/native/sparse_console_backing.rs +++ b/src/hal/native/sparse_console_backing.rs @@ -102,6 +102,7 @@ impl SparseConsoleBackend { offset_x: f32, offset_y: f32, scale: f32, + scale_center: (i32, i32), tiles: &[SparseTile], ) { if tiles.is_empty() { @@ -118,12 +119,14 @@ impl SparseConsoleBackend { 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 * scale) + offset_x; - let screen_y = ((step_y * y as f32) - 1.0 * scale) + 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 2b06f81e..d9a79aec 100644 --- a/src/hal/wasm/simple_console_backing.rs +++ b/src/hal/wasm/simple_console_backing.rs @@ -114,6 +114,7 @@ impl SimpleConsoleBackend { 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 4af359ae..2c15806b 100644 --- a/src/hal/wasm/sparse_console_backing.rs +++ b/src/hal/wasm/sparse_console_backing.rs @@ -113,6 +113,7 @@ impl SparseConsoleBackend { offset_x: f32, offset_y: f32, _scale: f32, + _scale_center: (i32, i32), tiles: &Vec, ) { let gl = &platform.platform.gl; diff --git a/src/rltk.rs b/src/rltk.rs index 2051db7d..162cd755 100644 --- a/src/rltk.rs +++ b/src/rltk.rs @@ -341,8 +341,8 @@ 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) { - self.consoles[self.active_console].console.set_scale(scale); + 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 { diff --git a/src/simple_console.rs b/src/simple_console.rs index 2d293bc2..32354125 100644 --- a/src/simple_console.rs +++ b/src/simple_console.rs @@ -14,6 +14,7 @@ pub struct SimpleConsole { offset_y: f32, scale: f32, + scale_center: (i32, i32), backend: hal::SimpleConsoleBackend, } @@ -40,6 +41,7 @@ impl SimpleConsole { 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), }; @@ -55,6 +57,7 @@ impl SimpleConsole { self.offset_x, self.offset_y, self.scale, + self.scale_center, ); } } @@ -276,8 +279,9 @@ impl Console for SimpleConsole { self.offset_y = y * (2.0 / self.height as f32); } - fn set_scale(&mut self, scale: 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 { diff --git a/src/sparse_console.rs b/src/sparse_console.rs index 0ed29016..9fe657b7 100644 --- a/src/sparse_console.rs +++ b/src/sparse_console.rs @@ -23,6 +23,7 @@ pub struct SparseConsole { offset_y: f32, scale: f32, + scale_center: (i32, i32), backend: hal::SparseConsoleBackend, } @@ -39,6 +40,7 @@ impl SparseConsole { 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), }; @@ -53,6 +55,7 @@ impl SparseConsole { self.offset_x, self.offset_y, self.scale, + self.scale_center, &self.tiles, ); } @@ -267,8 +270,9 @@ impl Console for SparseConsole { self.offset_y = y * (2.0 / self.height as f32); } - fn set_scale(&mut self, scale: 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 {