Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support scaling #59

Merged
merged 4 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/hal/amethyst_be/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ impl SimpleConsoleBackend {
_tiles: &[crate::Tile],
_offset_x: f32,
_offset_y: f32,
_scale: f32,
_scale_center: (i32, i32),
) {
}

Expand Down Expand Up @@ -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],
) {
}
Expand Down
2 changes: 2 additions & 0 deletions src/hal/curses/simple_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions src/hal/curses/sparse_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/hal/dummy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ impl SimpleConsoleBackend {
_tiles: &[crate::Tile],
_offset_x: f32,
_offset_y: f32,
_scale: f32,
_scale_center: (i32, i32),
) {
}

Expand Down Expand Up @@ -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],
) {
}
Expand Down
10 changes: 6 additions & 4 deletions src/hal/native/simple_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions src/hal/native/sparse_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/hal/wasm/simple_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ impl SimpleConsoleBackend {
tiles: &Vec<Tile>,
offset_x: f32,
offset_y: f32,
_scale: f32,
_scale_center: (i32, i32),
) {
let gl = &platform.platform.gl;
unsafe {
Expand Down
2 changes: 2 additions & 0 deletions src/hal/wasm/sparse_console_backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ impl SparseConsoleBackend {
width: u32,
offset_x: f32,
offset_y: f32,
_scale: f32,
_scale_center: (i32, i32),
tiles: &Vec<SparseTile>,
) {
let gl = &platform.platform.gl;
Expand Down
22 changes: 22 additions & 0 deletions src/initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<S:ToString, T>(mut self, width: T, height: T, font: S) -> Self
where T: TryInto<u32>
{
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;
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/rltk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions src/simple_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub struct SimpleConsole {
offset_x: f32,
offset_y: f32,

scale: f32,
scale_center: (i32, i32),

backend: hal::SimpleConsoleBackend,
}

Expand All @@ -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),
};

Expand All @@ -51,6 +56,8 @@ impl SimpleConsole {
&self.tiles,
self.offset_x,
self.offset_y,
self.scale,
self.scale_center,
);
}
}
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions src/sparse_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct SparseConsole {
offset_x: f32,
offset_y: f32,

scale: f32,
scale_center: (i32, i32),

backend: hal::SparseConsoleBackend,
}

Expand All @@ -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),
};

Expand All @@ -49,6 +54,8 @@ impl SparseConsole {
self.width,
self.offset_x,
self.offset_y,
self.scale,
self.scale_center,
&self.tiles,
);
}
Expand Down Expand Up @@ -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
}
Expand Down