summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2026-08-13 01:56:09 -0700
committerKai Stevenson <kai@kaistevenson.com>2026-08-13 01:56:09 -0700
commitfeefeecec6c6050635b2c016452dfa1529575987 (patch)
tree024cc36c2c75e10b6bfd68f29c1494830bec37df /src/main.rs
parent7ce9781f86c56f932dabc3bc84b8d21d7be8fecc (diff)
big refactor for board
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs85
1 files changed, 40 insertions, 45 deletions
diff --git a/src/main.rs b/src/main.rs
index 02920d6..b048122 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,10 +7,7 @@ use egui::Id;
use egui_wgpu::{RendererOptions, ScreenDescriptor};
use egui_winit::egui::{self, Context};
use pixels::{Pixels, ScalingMode, SurfaceTexture};
-use std::{
- cmp::{max, min},
- time::{Duration, Instant},
-};
+use std::time::{Duration, Instant};
use winit::{
application::ApplicationHandler,
event::{
@@ -25,10 +22,7 @@ use winit::{
use crate::{
camera::Camera,
config::{PIXEL_BUFFER_HEIGHT, PIXEL_BUFFER_WIDTH, WINDOW_TITLE},
- sim::{
- board::Board, materials::MaterialId, overlay::create_compute_combined_overlay_offset,
- sim::sim_tick,
- },
+ sim::{cell::Cell, materials::MaterialId, sim::sim_tick, world::World},
ui::draw_egui,
};
@@ -70,7 +64,7 @@ struct App {
camera: Option<Camera>,
- board: Option<Board>,
+ world: Option<World>,
// sim state
// the last/current (not yet completed) seqno
@@ -110,10 +104,10 @@ impl Default for App {
camera: Some(Camera {
x: 0.0,
y: 0.0,
- zoom: 0.5,
+ zoom: 1.0,
}),
- board: Some(Board::empty()),
+ world: Some(World::from_default_size()),
sim_seqno: 0,
sim_paused: false,
@@ -182,7 +176,7 @@ impl ApplicationHandler for App {
&& let Some(egui_context) = &self.egui_context
&& let Some(egui_state) = &mut self.egui_state
&& let Some(egui_renderer) = &mut self.egui_renderer
- && let Some(board) = &mut self.board
+ && let Some(world) = &mut self.world
&& let Some(camera) = &mut self.camera
{
let egui_response = egui_state.on_window_event(window, &event);
@@ -207,7 +201,7 @@ impl ApplicationHandler for App {
KeyCode::KeyA => self.input.is_left_pressed = pressed,
KeyCode::KeyS => self.input.is_down_pressed = pressed,
KeyCode::KeyD => self.input.is_right_pressed = pressed,
- KeyCode::KeyC => self.board = Some(Board::empty()),
+ KeyCode::KeyC => self.world = Some(World::from_default_size()),
KeyCode::Space => {
if pressed {
self.sim_paused = !self.sim_paused
@@ -225,9 +219,9 @@ impl ApplicationHandler for App {
self.input.last_mouse_pos_on_screen = Some((position.x, position.y));
self.input.last_mouse_pos_on_board = pixels
.window_pos_to_pixel((position.x as f32, position.y as f32))
- .map(|v| camera.screen_position_to_board(board, v.0 as f64, v.1 as f64))
+ .map(|v| camera.screen_position_to_world(v.0 as f64, v.1 as f64))
.map(|v| (v.0 as i32, v.1 as i32))
- .ok()
+ .ok();
}
WindowEvent::MouseInput { state, button, .. } => {
if button == MouseButton::Left {
@@ -242,6 +236,9 @@ impl ApplicationHandler for App {
event_loop.exit();
}
WindowEvent::RedrawRequested => {
+ #[cfg(feature = "profiler")]
+ puffin::GlobalProfiler::lock().new_frame();
+ puffin::profile_scope!("redraw_requested");
// compute frame delta
let now = Instant::now();
let secs_since_last_frame = (now - self.last_frame_real).as_secs_f32();
@@ -259,43 +256,28 @@ impl ApplicationHandler for App {
frame.fill(0);
// --TEST DRAWING--
- if self.input.is_lmb_pressed && self.input.last_mouse_pos_on_board.is_some() {
+ if self.input.is_lmb_pressed
+ && let Some(lm) = self.input.last_mouse_pos_on_board
+ {
// start with the bounding box of the drawing brush circle + some margin
// clamp the bounding box to the board sie
// TODO better way to do this without unwrap?
- let bb_xl = max(
- self.input.last_mouse_pos_on_board.unwrap().0
- - self.config.brush_radius as i32,
- -((board.size_x / 2) as i32),
- );
- let bb_xu = min(
- self.input.last_mouse_pos_on_board.unwrap().0
- + self.config.brush_radius as i32,
- (board.size_x / 2) as i32,
- );
- let bb_yl = max(
- self.input.last_mouse_pos_on_board.unwrap().1
- - self.config.brush_radius as i32,
- -((board.size_y / 2) as i32),
- );
- let bb_yu = min(
- self.input.last_mouse_pos_on_board.unwrap().1
- + self.config.brush_radius as i32,
- (board.size_y / 2) as i32,
- );
+ let bb_xl = lm.0 - self.config.brush_radius as i32;
+ let bb_xu = lm.0 + self.config.brush_radius as i32;
+ let bb_yl = lm.1 - self.config.brush_radius as i32;
+ let bb_yu = lm.1 + self.config.brush_radius as i32;
// for each point, check if the distance is less than the brush size and write the pixel
for x in bb_xl..bb_xu {
for y in bb_yl..bb_yu {
// brush/selection
- if ((x - self.input.last_mouse_pos_on_board.unwrap().0).pow(2)
- + (y - self.input.last_mouse_pos_on_board.unwrap().1).pow(2))
+ if ((x - lm.0).pow(2) + (y - lm.1).pow(2))
< (self.config.brush_radius as i32).pow(2)
{
- board.set_cell_at_position(
+ world.set_cell_from_game_position(
x,
y,
- sim::board::Cell::from_material(self.config.brush_material),
+ Cell::from_material(self.config.brush_material),
);
}
}
@@ -305,14 +287,14 @@ impl ApplicationHandler for App {
// TODO check if we need to run another sim tick given the sim speed
// SIM logic
if !self.sim_paused || self.ignore_pause_next_tick {
- sim_tick(board, self.sim_seqno, delta_time);
+ sim_tick(world, self.sim_seqno);
self.sim_seqno += 1;
self.ignore_pause_next_tick = false;
}
- let get_overlay =
- create_compute_combined_overlay_offset(board, &self.config, &self.input);
- camera.write_frame_view(frame, board, get_overlay);
+ // let get_overlay =
+ // create_compute_combined_overlay_offset(&world, &self.config, &self.input);
+ camera.write_frame_view(frame, &world);
// egui logic
let raw_input = egui_state.take_egui_input(window);
@@ -410,8 +392,21 @@ impl ApplicationHandler for App {
}
}
+#[cfg(feature = "profiler")]
+fn start_profiler() {
+ let _server = puffin_http::Server::new("127.0.0.1:8585").unwrap();
+ puffin::set_scopes_on(true);
+ std::mem::forget(_server); // keep serving for the process lifetime
+
+ std::process::Command::new("puffin_viewer")
+ .args(["--url", "127.0.0.1:8585"])
+ .spawn()
+ .ok(); // don't die if it isn't installed
+}
+
fn main() -> Result<()> {
- env_logger::init();
+ #[cfg(feature = "profiler")]
+ start_profiler();
let event_loop = EventLoop::new()?;
event_loop.set_control_flow(ControlFlow::Poll);