Browse Source

fixed resize bug

master
Inderjit Gill 6 months ago
parent
commit
5a90708d7d
5 changed files with 31 additions and 19 deletions
  1. +2
    -3
      client/src/lib.rs
  2. +14
    -0
      client/src/renderer_webgl.rs
  3. +13
    -9
      core/src/game.rs
  4. +1
    -3
      core/src/geometry.rs
  5. +1
    -4
      www/index.html

+ 2
- 3
client/src/lib.rs View File

@@ -199,11 +199,10 @@ impl Sokoban {
pub fn event_resize(&mut self) {
let (width, height) =
resize_canvas_element(&mut self.config, &self.canvas_element_name).unwrap();
let curvature = self.game.get_curvature();

info!("event_resize: {}, {}", width, height);
self.renderer.resize(width, height).unwrap();
self.geometry.resize(width, height);
self.geometry.render(&self.renderer, curvature);
}

pub fn event_key_down(&mut self, key: &str) -> KeyEventReturn {
@@ -292,7 +291,7 @@ fn resize_canvas_element(config: &mut Config, canvas_element_name: &str) -> Resu
canvas_element.set_attribute("width", &config.canvas_width.to_string())?;
canvas_element.set_attribute("height", &config.canvas_height.to_string())?;

Ok((width, height))
Ok((config.canvas_width, config.canvas_height))
} else {
Err(Error::Web(String::from("no element with id of canvas")))
}

+ 14
- 0
client/src/renderer_webgl.rs View File

@@ -114,6 +114,20 @@ impl RendererWebGl {
}
}

pub fn resize(&mut self, canvas_width: i32, canvas_height: i32) -> Result<()> {
let projection_matrix = matrix_ortho(
0.0,
canvas_width as f32,
0.0,
canvas_height as f32,
10.0,
-10.0,
);
self.crt_shader.projection_matrix = projection_matrix;

Ok(())
}

pub fn event_load_texture(&mut self, image_element: HtmlImageElement, texture_unit: i32) {
let texture = load_texture(&self.gl, image_element, texture_unit).unwrap();
if texture_unit == 0 {

+ 13
- 9
core/src/game.rs View File

@@ -70,7 +70,7 @@ pub struct Game {
}

impl Game {
pub fn new(config: &Config, audio: &Audio) -> Game {
pub fn new<A: Audio>(config: &Config, audio: &A) -> Game {
let mut game = Game {
mode: GameMode::Playing,
game_state: GameState::ShowingProgress,
@@ -187,7 +187,7 @@ impl Game {
// }
}

fn paused(&mut self, controller: &Controller, audio: &Audio) -> error::Result<bool> {
fn paused<A: Audio>(&mut self, controller: &Controller, audio: &A) -> error::Result<bool> {
let updated = self.update_pause_menu(controller, audio);
if updated {
audio.play_sound(SoundEffect::MenuMove);
@@ -196,7 +196,11 @@ impl Game {
Ok(updated)
}

fn playing_in_level(&mut self, controller: &Controller, _audio: &Audio) -> error::Result<bool> {
fn playing_in_level<A: Audio>(
&mut self,
controller: &Controller,
_audio: &A,
) -> error::Result<bool> {
if controller.just_pressed(ControllerButton::Select) {
self.reset_level()?;
return Ok(false);
@@ -331,10 +335,10 @@ impl Game {
}
}

fn playing(
fn playing<A: Audio>(
&mut self,
controller: &Controller,
audio: &Audio,
audio: &A,
delta: f32,
_random: f32,
) -> error::Result<bool> {
@@ -344,10 +348,10 @@ impl Game {
}
}

pub fn tick(
pub fn tick<A: Audio>(
&mut self,
controller: &Controller,
audio: &Audio,
audio: &A,
delta: f32,
random: f32,
) -> bool {
@@ -400,7 +404,7 @@ impl Game {
}
}

fn update_pause_menu(&mut self, controller: &Controller, audio: &Audio) -> bool {
fn update_pause_menu<A: Audio>(&mut self, controller: &Controller, audio: &A) -> bool {
let mut update = if controller.just_pressed(ControllerButton::Down) {
self.menu_active_item += 1;
true
@@ -503,7 +507,7 @@ impl Game {
out
}

fn update_audio_volume(&mut self, audio: &Audio) {
fn update_audio_volume<A: Audio>(&mut self, audio: &A) {
let volume = self.menu_volume as f32 / self.menu_max_volume as f32;
audio.volume(volume);
}

+ 1
- 3
core/src/geometry.rs View File

@@ -83,11 +83,9 @@ impl Geometry {
}
}

pub fn render(&self, renderer: &Renderer, curvature: f32) {
pub fn render<R: Renderer>(&self, renderer: &R, curvature: f32) {
renderer.render_geometry_to_texture(
&self.geo[..],
// self.canvas_width,
// self.canvas_height,
self.render_texture_width,
self.render_texture_height,
);

+ 1
- 4
www/index.html View File

@@ -31,7 +31,6 @@
<canvas id="canvas" />
</div>
<script src='howler.core.min.js'></script>

<script>
// The `--no-modules`-generated JS from `wasm-bindgen` attempts to use
// `WebAssembly.instantiateStreaming` to instantiate the wasm module,
@@ -44,10 +43,8 @@
// hack when deploying over HTTP.
delete WebAssembly.instantiateStreaming;
</script>

<!-- this is the JS generated by the `wasm-bindgen` CLI tool -->
<script src='./sokoban.js'></script>

<script src='sokoban.js'></script>
<script src='index.js'></script>
</body>
</html>

Loading…
Cancel
Save