Skip to content

Commit

Permalink
Add view module
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Sep 30, 2023
1 parent 9fbbeb5 commit b437525
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 225 deletions.
17 changes: 16 additions & 1 deletion canvas/src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub struct Canvas {
brush_color: Color,
background_color: Color,
scale: Scale,
// TODO: fsm, frames, ticks
fps: Fps,
// TODO: fsm(or mode), frames, ticks
quit: bool,
}

Expand Down Expand Up @@ -47,6 +48,10 @@ impl Canvas {
self.scale.0
}

pub fn fps(&self) -> NonZeroU8 {
self.fps.0
}

pub fn quit(&self) -> bool {
self.quit
}
Expand All @@ -60,6 +65,7 @@ impl Canvas {
CanvasQueryValue::BackgroundColor(self.background_color)
}
CanvasQuery::Scale => CanvasQueryValue::Scale(self.scale.0),
CanvasQuery::Fps => CanvasQueryValue::Fps(self.fps.0),
}
}

Expand Down Expand Up @@ -101,3 +107,12 @@ impl Default for Scale {
Self(NonZeroU8::new(1).expect("unreachable"))
}
}

#[derive(Debug, Clone, Copy)]
struct Fps(NonZeroU8);

impl Default for Fps {
fn default() -> Self {
Self(NonZeroU8::new(30).expect("unreachable"))
}
}
2 changes: 2 additions & 0 deletions canvas/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum CanvasQuery {
BrushColor,
BackgroundColor,
Scale,
Fps,
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -20,4 +21,5 @@ pub enum CanvasQueryValue {
BrushColor(Color),
BackgroundColor(Color),
Scale(NonZeroU8),
Fps(NonZeroU8),
}
93 changes: 49 additions & 44 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
use crate::view::View;
use orfail::OrFail;
use pagurus::{event::Event, System};
use pagurus::{
event::{Event, TimeoutTag},
video::VideoFrame,
System,
};
use paticanvas::CanvasFile;
use std::time::Duration;

const TICK_TIMEOUT_TAG: TimeoutTag = TimeoutTag::new(0);

#[derive(Debug)]
pub struct Game {
model: CanvasFile,
view: View,

Check warning on line 16 in src/game.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

field `view` is never read

Check failure on line 16 in src/game.rs

View workflow job for this annotation

GitHub Actions / Lints (stable)

field `view` is never read

Check warning on line 16 in src/game.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

field `view` is never read

Check warning on line 16 in src/game.rs

View workflow job for this annotation

GitHub Actions / Test Suite (nightly)

field `view` is never read

Check warning on line 16 in src/game.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

field `view` is never read

Check warning on line 16 in src/game.rs

View workflow job for this annotation

GitHub Actions / Test Suite (beta)

field `view` is never read

Check warning on line 16 in src/game.rs

View workflow job for this annotation

GitHub Actions / Check (nightly)

field `view` is never read
video_frame: VideoFrame,
}

impl Game {
pub fn new(model: CanvasFile) -> Self {
Self { model }
Self {
model,
view: View::default(),
video_frame: VideoFrame::default(),
}
}

pub fn model(&self) -> &CanvasFile {
Expand All @@ -19,39 +33,51 @@ impl Game {
pub fn model_mut(&mut self) -> &mut CanvasFile {
&mut self.model
}

fn set_tick_timeout<S: System>(&mut self, system: &mut S, tick_time: Duration) {
let fps = self.model().canvas().fps();
let elapsed = system.clock_game_time() - tick_time;
let timeout = Duration::from_secs(1) / u32::from(fps.get());
let timeout = timeout.saturating_sub(elapsed);
system.clock_set_timeout(TICK_TIMEOUT_TAG, timeout);
}
}

impl<S: System> pagurus::Game<S> for Game {
fn initialize(&mut self, system: &mut S) -> pagurus::Result<()> {
todo!()
self.set_tick_timeout(system, system.clock_game_time());
Ok(())
}

fn handle_event(&mut self, system: &mut S, event: Event) -> pagurus::Result<bool> {
self.model.sync().or_fail()?;
todo!()
let ticked = match event {
Event::WindowResized(size) => {
self.video_frame = VideoFrame::new(system.video_init(size));
None
}
Event::Timeout(TICK_TIMEOUT_TAG) => {
let now = system.clock_game_time();
self.model.sync().or_fail()?;
// TODO: self.model.tick();
Some(now)
}
_ => {
// self.view
// .handle_event(system, &mut self.model, event)
// .or_fail()?;
None
}
};
if let Some(tick_time) = ticked {
// self.render(system);
self.set_tick_timeout(system, tick_time);
}
Ok(!self.model.canvas().quit())
}
}

// use crate::{
// command::Command,
// config::Config,
// model::Model,
// view::{View, WindowCanvas},
// };
// use pagurus::{
// event::{Event, TimeoutTag},
// failure::OrFail,
// image::Canvas,
// video::VideoFrame,
// Result, System,
// };
// use std::time::Duration;

// const TICK_TIMEOUT_TAG: TimeoutTag = TimeoutTag::new(0);

// #[derive(Debug, Default)]
// pub struct Game {
// video_frame: VideoFrame,
// view: View,
// model: Model,
// }
Expand Down Expand Up @@ -83,8 +109,6 @@ impl<S: System> pagurus::Game<S> for Game {
// system.video_draw(self.video_frame.as_ref());
// }

// fn set_tick_timeout<S: System>(&mut self, system: &mut S) {
// system.clock_set_timeout(TICK_TIMEOUT_TAG, Duration::from_secs(1) / self.model.fps());
// }
// }

Expand All @@ -96,24 +120,5 @@ impl<S: System> pagurus::Game<S> for Game {
// }

// fn handle_event(&mut self, system: &mut S, event: Event) -> Result<bool> {
// let mut set_timeout = false;
// match event {
// Event::WindowResized(size) => {
// self.video_frame = VideoFrame::new(system.video_init(size));
// }
// Event::Timeout(TICK_TIMEOUT_TAG) => {
// self.model.tick();
// set_timeout = true;
// }
// _ => {}
// }
// self.view
// .handle_event(system, &mut self.model, event)
// .or_fail()?;
// self.render(system);
// if set_timeout {
// self.set_tick_timeout(system);
// }
// Ok(!self.model.quit())
// }
// }
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub mod game;
// pub mod model; // TODO: delete
// pub mod query;
// pub mod remote;
// pub mod view;
pub mod view;
Loading

0 comments on commit b437525

Please sign in to comment.