Better management of window size

This commit is contained in:
Thomas Forgione 2022-08-02 17:44:34 +02:00
parent e5cf82ef32
commit 94f0cdffca

View File

@ -55,17 +55,6 @@ impl Game {
/// Creates a new game. /// Creates a new game.
pub fn new() -> Result<Game> { pub fn new() -> Result<Game> {
Ok(Game(Rc::new(RefCell::new(Engine::new()?)))) Ok(Game(Rc::new(RefCell::new(Engine::new()?))))
// let clone = inner.clone();
// let cb = Closure::<dyn FnMut(_)>::new(move |event: web_sys::GamepadEvent| {
// let mut inner = clone.borrow_mut();
// inner.add_gamepad(&event);
// });
// (*window)
// .add_event_listener_with_callback("gamepadconnected", cb.as_ref().unchecked_ref())?;
// cb.forget();
} }
/// Starts the game. /// Starts the game.
@ -135,6 +124,11 @@ pub struct Engine {
/// The web page document. /// The web page document.
pub document: web_sys::Document, pub document: web_sys::Document,
/// The canvas.
///
/// We keep it to easily know the size.
pub canvas: web_sys::HtmlCanvasElement,
/// The canvas rendering context. /// The canvas rendering context.
/// ///
/// We keep a reference so that we can easily render things. /// We keep a reference so that we can easily render things.
@ -172,6 +166,7 @@ impl Engine {
inputs: Inputs::new(&window)?, inputs: Inputs::new(&window)?,
window, window,
document, document,
canvas,
context, context,
performance, performance,
}) })
@ -231,13 +226,28 @@ impl Engine {
/// Performs the rendering of the engine. /// Performs the rendering of the engine.
pub fn render(&self) -> Result<()> { pub fn render(&self) -> Result<()> {
let window_width = unwrap!(self.window.inner_width()?.as_f64());
let window_height = unwrap!(self.window.inner_height()?.as_f64());
let window_width_u32 = window_width as u32;
let window_height_u32 = window_height as u32;
if window_width_u32 != self.canvas.width() {
self.canvas.set_width(window_width_u32);
}
if window_height_u32 != self.canvas.height() {
self.canvas.set_height(window_height_u32);
}
let view = self.scene.view().unwrap(); // TODO remove this unwrap let view = self.scene.view().unwrap(); // TODO remove this unwrap
// Clear render // Clear render
self.context.clear_rect(0.0, 0.0, 1920.0, 1080.0); self.context
.clear_rect(0.0, 0.0, window_width, window_height);
self.context self.context
.set_fill_style(&JsValue::from_str("rgb(135, 206, 235)")); .set_fill_style(&JsValue::from_str("rgb(135, 206, 235)"));
self.context.fill_rect(0.0, 0.0, 1920.0, 1080.0); self.context
.fill_rect(0.0, 0.0, window_width, window_height);
// Draw the scene // Draw the scene
let map = self.scene.map(); let map = self.scene.map();
@ -265,15 +275,18 @@ impl Engine {
/// Draw a drawable. /// Draw a drawable.
pub fn draw<D: Drawable>(&self, drawable: &D, view: Bbox) -> Result<()> { pub fn draw<D: Drawable>(&self, drawable: &D, view: Bbox) -> Result<()> {
let window_width = unwrap!(self.window.inner_width()?.as_f64());
let window_height = unwrap!(self.window.inner_height()?.as_f64());
let image = self.textures.get(drawable.texture()); let image = self.textures.get(drawable.texture());
let source = drawable.texture_rect(self.after_loop); let source = drawable.texture_rect(self.after_loop);
let mut dest = source.clone(); let mut dest = source.clone();
dest.position = drawable.position() - view.position; dest.position = drawable.position() - view.position;
dest.position.x *= 1920.0 / view.size.x; dest.position.x *= window_width / view.size.x;
dest.position.y *= 1080.0 / view.size.y; dest.position.y *= window_height / view.size.y;
dest.size.x *= 1920.0 / view.size.x; dest.size.x *= window_width / view.size.x;
dest.size.y *= 1080.0 / view.size.y; dest.size.y *= window_height / view.size.y;
image.render(source, dest, &self.context)?; image.render(source, dest, &self.context)?;
Ok(()) Ok(())