fix double rendering issue

This commit is contained in:
Thilo Behnke
2022-06-19 15:35:47 +02:00
parent 1b3e29a00b
commit db62e6777d
4 changed files with 17 additions and 6 deletions

View File

@@ -22,6 +22,7 @@
engineCanvas.set(engineCanvas);
width.set(canvas.width);
height.set(canvas.height);
// field.set_dimensions(canvas.width, canvas.height);
// setup entities
listeners.forEach(async entity => {
@@ -68,8 +69,6 @@
function tick(dt) {
field.tick([], dt);
let objects = JSON.parse(field.objects());
render(objects, dt);
}
function render(objects, dt) {
@@ -93,8 +92,9 @@
}
function handleResize () {
width.set(window.innerWidth);
height.set(window.innerHeight);
// TODO: Resolution scaling needs to be implemented in wasm module.
// width.set(window.innerWidth);
// height.set(window.innerHeight);
pixelRatio.set(window.devicePixelRatio);
}
</script>

View File

@@ -3,8 +3,8 @@ import {getContext, onMount} from "svelte";
export const engineCanvas = writable();
export const engineCtx = writable();
export const width = writable(window.innerWidth);
export const height = writable(window.innerHeight);
export const width = writable(800);
export const height = writable(600);
export const pixelRatio = writable(window.devicePixelRatio);
// A more convenient store for grabbing all game props

View File

@@ -147,6 +147,12 @@ impl FieldWrapper {
let json = json!(objs);
serde_json::to_string(&json).unwrap()
}
pub fn set_dimensions(&mut self, width_js: JsValue, height_js: JsValue) {
let width = width_js.as_f64().unwrap();
let height = height_js.as_f64().unwrap();
self.field.set_dimensions(width as u16, height as u16);
}
}
#[derive(Clone)]

View File

@@ -219,6 +219,11 @@ impl Field {
pub fn objs(&self) -> Vec<&Rc<RefCell<Box<dyn GameObject>>>> {
self.objs.iter().collect()
}
pub fn set_dimensions(&mut self, width: u16, height: u16) {
self.width = width;
self.height = height;
}
}
impl DefaultGameObject {