Skip to content

Commit

Permalink
Window now scales when resized, resizing is not locked anymore. Shoul…
Browse files Browse the repository at this point in the history
…d help resolve issue #8 .
  • Loading branch information
thebracket committed Oct 8, 2019
1 parent fdd2ecc commit 9441dbe
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub trait Console {
/// Gets the dimensions of the console in characters
fn get_char_size(&self) -> (u32, u32);

// Resizes the viewport
fn resize_pixels(&mut self, width: u32, height: u32);

/// Tells the console to draw itself via OpenGL.
fn gl_draw(&mut self, font: &Font, shader: &Shader, gl: &glow::Context);

Expand Down
12 changes: 6 additions & 6 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ impl Font {
unsafe {
texture = gl.create_texture().unwrap();
gl.bind_texture(glow::TEXTURE_2D, Some(texture));
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::REPEAT as i32); // set texture wrapping to gl::REPEAT (default wrapping method)
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::REPEAT as i32);
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE as i32); // set texture wrapping to gl::REPEAT (default wrapping method)
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE as i32);
// set texture filtering parameters
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_MIN_FILTER,
glow::LINEAR as i32,
glow::NEAREST as i32,
);
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_MAG_FILTER,
glow::LINEAR as i32,
glow::NEAREST as i32,
);

let img_orig = Font::load_image(&self.bitmap_file);
Expand Down Expand Up @@ -106,8 +106,8 @@ impl Font {
unsafe {
texture = gl.create_texture().unwrap();
gl.bind_texture(glow::TEXTURE_2D, Some(texture));
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::REPEAT as i32); // set texture wrapping to gl::REPEAT (default wrapping method)
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::REPEAT as i32);
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE as i32); // set texture wrapping to gl::REPEAT (default wrapping method)
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE as i32);
// set texture filtering parameters
gl.tex_parameter_i32(
glow::TEXTURE_2D,
Expand Down
30 changes: 15 additions & 15 deletions src/platform_specific.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::{framebuffer::Framebuffer, quadrender, shader_strings, GameState, Rltk, Shader};


use super::{framebuffer::Framebuffer, quadrender, shader_strings, GameState, Rltk, Shader, Console};

#[cfg(not(target_arch = "wasm32"))]
use glutin::{
Expand Down Expand Up @@ -32,14 +30,6 @@ pub fn init_raw<S: ToString>(width_pixels: u32, height_pixels: u32, window_title
let el = EventLoop::new();
let wb = WindowBuilder::new()
.with_title(window_title.to_string())
.with_max_inner_size(LogicalSize::new(
f64::from(width_pixels),
f64::from(height_pixels),
))
.with_min_inner_size(LogicalSize::new(
f64::from(width_pixels),
f64::from(height_pixels),
))
.with_inner_size(LogicalSize::new(
f64::from(width_pixels),
f64::from(height_pixels),
Expand Down Expand Up @@ -119,6 +109,9 @@ const TICK_TYPE : ControlFlow = ControlFlow::Poll;
// Glutin version of main loop
#[cfg(not(target_arch = "wasm32"))]
pub fn main_loop<GS: GameState>(mut rltk: Rltk, mut gamestate: GS) {
unsafe {
rltk.gl.viewport(0, 0, rltk.width_pixels as i32, rltk.height_pixels as i32);
}
let now = Instant::now();
let mut prev_seconds = now.elapsed().as_secs();
let mut prev_ms = now.elapsed().as_millis();
Expand Down Expand Up @@ -160,10 +153,16 @@ pub fn main_loop<GS: GameState>(mut rltk: Rltk, mut gamestate: GS) {
}
Event::LoopDestroyed => (),
Event::WindowEvent { ref event, .. } => match event {
WindowEvent::Resized(_logical_size) => {
WindowEvent::Resized(logical_size) => {
// Commenting out to see if it helps the Linux world
//let dpi_factor = wc.window().hidpi_factor();
//wc.resize(logical_size.to_physical(dpi_factor));
let dpi_factor = wc.window().hidpi_factor();
let physical = logical_size.to_physical(dpi_factor);
wc.resize(physical);
rltk.resize_pixels(physical.width as u32, physical.height as u32);
unsafe {
rltk.gl.viewport(0, 0, physical.width as i32, physical.height as i32);
}
rltk.backing_buffer = Framebuffer::build_fbo(&rltk.gl, physical.width as i32, physical.height as i32);
}
WindowEvent::RedrawRequested => {
//tock(&mut rltk, &mut gamestate, &mut frames, &mut prev_seconds, &mut prev_ms, &now);
Expand Down Expand Up @@ -241,7 +240,7 @@ fn tock<GS: GameState>(

// Clear the screen
unsafe {
rltk.gl.clear_color(0.2, 0.3, 0.3, 1.0);
rltk.gl.clear_color(0.0, 0.0, 0.0, 1.0);
rltk.gl.clear(glow::COLOR_BUFFER_BIT);
}

Expand Down Expand Up @@ -315,6 +314,7 @@ fn tock<GS: GameState>(

// Clear the screen
unsafe {
rltk.gl.viewport(0, 0, rltk.width_pixels, rltk.height_pixels);
rltk.gl.clear_color(0.2, 0.3, 0.3, 1.0);
rltk.gl.clear(glow::COLOR_BUFFER_BIT);
}
Expand Down
9 changes: 9 additions & 0 deletions src/rltk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ impl Console for Rltk {
self.consoles[self.active_console].console.get_char_size()
}

fn resize_pixels(&mut self, width: u32, height: u32) {
self.width_pixels = width;
self.height_pixels = height;

for c in self.consoles.iter_mut() {
c.console.resize_pixels(width, height);
}
}

// Implement pass-through to active console

fn at(&self, x: i32, y: i32) -> usize {
Expand Down
18 changes: 11 additions & 7 deletions src/simple_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl SimpleConsole {
fn push_point(&mut self, x: f32, y: f32, fg: RGB, bg: RGB, ux: f32, uy: f32) {
self.vertex_buffer[self.vertex_counter] = x + self.offset_x;
self.vertex_buffer[self.vertex_counter + 1] = y + self.offset_y;
self.vertex_buffer[self.vertex_counter + 2] = 0.0;
self.vertex_buffer[self.vertex_counter + 2] = 0.0f32;
self.vertex_buffer[self.vertex_counter + 3] = fg.r;
self.vertex_buffer[self.vertex_counter + 4] = fg.g;
self.vertex_buffer[self.vertex_counter + 5] = fg.b;
Expand All @@ -253,16 +253,16 @@ impl SimpleConsole {
fn rebuild_vertices(&mut self, gl: &glow::Context) {
self.vertex_counter = 0;
self.index_counter = 0;
let glyph_size_x: f32 = 1.0 / 16.0;
let glyph_size_y: f32 = 1.0 / 16.0;
let glyph_size_x: f32 = 1.0f32 / 16.0f32;
let glyph_size_y: f32 = 1.0f32 / 16.0f32;

let step_x: f32 = 2.0 / self.width as f32;
let step_y: f32 = 2.0 / self.height as f32;
let step_x: f32 = 2.0f32 / self.width as f32;
let step_y: f32 = 2.0f32 / self.height as f32;

let mut index_count: i32 = 0;
let mut screen_y: f32 = -1.0;
let mut screen_y: f32 = -1.0f32;
for y in 0..self.height {
let mut screen_x: f32 = -1.0;
let mut screen_x: f32 = -1.0f32;
for x in 0..self.width {
let fg = self.tiles[((y * self.width) + x) as usize].fg;
let bg = self.tiles[((y * self.width) + x) as usize].bg;
Expand Down Expand Up @@ -339,6 +339,10 @@ impl Console for SimpleConsole {
(self.width, self.height)
}

fn resize_pixels(&mut self, _width: u32, _height: u32) {
self.is_dirty = true;
}

/// Sends the console to OpenGL.
fn gl_draw(&mut self, font: &Font, shader: &Shader, gl: &glow::Context) {
unsafe {
Expand Down
4 changes: 4 additions & 0 deletions src/sparse_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ impl Console for SparseConsole {
(self.width, self.height)
}

fn resize_pixels(&mut self, _width: u32, _height: u32) {
self.is_dirty = true;
}

/// Draws the console to OpenGL.
fn gl_draw(&mut self, font: &Font, shader: &Shader, gl: &glow::Context) {
unsafe {
Expand Down

0 comments on commit 9441dbe

Please sign in to comment.