Skip to content

Commit

Permalink
add buttons for resetting the viewport in some demos
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitmel committed Jun 9, 2021
1 parent ac52e54 commit f9b329a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/game_of_life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ impl GameOfLife {
program_reflection,

prev_simulation_time: 0.0,
camera_pos: Vec2f::cast_from(GRID_SIZE) * CELL_SIZE * 0.5,
camera_zoom: 1.0,
camera_pos: Vec2f::ZERO,
camera_zoom: 0.0,

current_generation,
next_generation,
Expand All @@ -126,11 +126,17 @@ impl GameOfLife {
simulation_times: AverageTimeSampler::new(30),
texture_refill_times: AverageTimeSampler::new(30),
};
myself.reset_view();
myself.reset_simulation();
myself.refill_textures();
Ok(myself)
}

fn reset_view(&mut self) {
self.camera_pos = Vec2f::cast_from(GRID_SIZE) * CELL_SIZE * 0.5;
self.camera_zoom = 1.0;
}

fn reset_simulation(&mut self) {
for cell in &mut self.current_generation {
*cell = (self.globals.random.next_f32() < CELL_SPAWN_CHANCE) as u8;
Expand Down Expand Up @@ -254,6 +260,10 @@ impl GameOfLife {
};
self.camera_pos += camera_movement / self.camera_zoom;

if self.globals.input_state.is_key_pressed(Key::R) {
self.reset_view();
}

if self.globals.time >= self.prev_simulation_time + SIMULATION_INTERVAL {
self.prev_simulation_time = self.globals.time;
self.run_simulation();
Expand Down
17 changes: 14 additions & 3 deletions src/mandelbrot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ impl Mandelbrot {
Vertex { pos: vec2(-1.0, -1.0) },
]);

Ok(Self {
let mut myself = Self {
globals,
vertex_buf,
program,
program_reflection,
camera_pos: Vec2f::ZERO,
camera_zoom: 1.0,
})
camera_zoom: 0.0,
};
myself.reset_view();
Ok(myself)
}

fn reset_view(&mut self) {
self.camera_pos = Vec2f::ZERO;
self.camera_zoom = 1.0;
}

pub fn update(&mut self) {
Expand All @@ -88,6 +95,10 @@ impl Mandelbrot {
if self.globals.input_state.is_key_down(Key::MouseLeft) {
self.camera_pos -= self.globals.input_state.delta_mouse_pos / self.camera_zoom;
}

if self.globals.input_state.is_key_pressed(Key::R) {
self.reset_view();
}
}

pub fn render(&mut self) {
Expand Down

0 comments on commit f9b329a

Please sign in to comment.