summaryrefslogtreecommitdiff
path: root/src/systems/draw.rs
diff options
context:
space:
mode:
authorArne Dußin2021-05-07 18:06:02 +0200
committerArne Dußin2021-05-07 18:06:02 +0200
commit6de8cfc84edbc80196ad144f2886031a898f5ed7 (patch)
treeb51d5f147dacce69bbb70bf363067a2528a2601f /src/systems/draw.rs
parentf3178df0a92fb3b87087e78cad7b9313f947be6a (diff)
downloadpmd_coop-main.tar.gz
pmd_coop-main.zip
Add player movementHEADmain
Diffstat (limited to 'src/systems/draw.rs')
-rw-r--r--src/systems/draw.rs40
1 files changed, 35 insertions, 5 deletions
diff --git a/src/systems/draw.rs b/src/systems/draw.rs
index 1d5ee6b..fd0185e 100644
--- a/src/systems/draw.rs
+++ b/src/systems/draw.rs
@@ -1,4 +1,8 @@
+use std::thread;
+use std::time::{Duration, Instant};
+
use sdl2::pixels::Color;
+use sdl2::rect::Rect;
use sdl2::render::WindowCanvas;
use sdl2::video::{Window, WindowContext};
use specs::prelude::*;
@@ -6,8 +10,14 @@ use specs::prelude::*;
use crate::components::{Pos, StaticDrawable};
use crate::texture_manager::TextureManager;
+/// The minimum time in micro a frame should take to limit the number of
+/// frames drawn to the screen. 5000 microseconds for a maximum of 200 frames
+/// per second.
+pub const FRAME_TIME_MIN_US: u64 = (1_000_000. / 200.) as u64;
+
pub struct SysDraw
{
+ last_frame_time: Instant,
canvas: WindowCanvas,
texture_manager: TextureManager<WindowContext>,
}
@@ -25,10 +35,20 @@ impl SysDraw
let texture_manager = TextureManager::new(canvas.texture_creator());
Self {
+ last_frame_time: Instant::now(),
canvas,
texture_manager,
}
}
+
+ pub fn delay_for_min_frametime(&mut self)
+ {
+ let frame_duration_us = (Instant::now() - self.last_frame_time).as_micros() as u64;
+ if frame_duration_us < FRAME_TIME_MIN_US {
+ thread::sleep(Duration::from_micros(FRAME_TIME_MIN_US - frame_duration_us));
+ }
+ self.last_frame_time = Instant::now();
+ }
}
impl<'a> System<'a> for SysDraw
@@ -39,20 +59,30 @@ impl<'a> System<'a> for SysDraw
{
self.canvas.clear();
- // XXX: This is so slow.. yaaaawn. Replace with ids and texture manager
- // lifetime?
- for (_pos, drawable) in (&pos, &drawable).join() {
+ /* XXX: This is so slow.. yaaaawn. Replace with ids and texture manager
+ * lifetime? */
+ for (pos, drawable) in (&pos, &drawable).join() {
let texture = self
.texture_manager
.get(&drawable.texture_name)
.expect("Unable to load texture");
- // let texture_info = texture.query();
self.canvas
- .copy(&texture, None, None)
+ .copy(
+ &texture,
+ Some(drawable.source_rect),
+ Rect::new(
+ pos.0.x as i32,
+ pos.0.y as i32,
+ drawable.source_rect.width(),
+ drawable.source_rect.height(),
+ ),
+ )
.expect("Unable to draw texture");
}
+ self.delay_for_min_frametime();
+
self.canvas.present();
}
}