diff --git a/src/run-web.html b/src/run-web.html
index ac011df..cfe6dcb 100644
--- a/src/run-web.html
+++ b/src/run-web.html
@@ -1 +1 @@
-
uw8-run
\ No newline at end of file
+uw8-run
\ No newline at end of file
diff --git a/src/run_native.rs b/src/run_native.rs
index 29f0454..1449048 100644
--- a/src/run_native.rs
+++ b/src/run_native.rs
@@ -37,6 +37,7 @@ struct UW8Instance {
end_frame: TypedFunc<(), ()>,
update: Option>,
start_time: Instant,
+ next_frame: Instant,
module: Vec,
watchdog: Arc>,
sound: Option,
@@ -69,8 +70,7 @@ impl MicroW8 {
resize: true,
..Default::default()
};
- let mut window = Window::new("MicroW8", 320, 240, options)?;
- window.limit_update_rate(Some(std::time::Duration::from_micros(16666)));
+ let window = Window::new("MicroW8", 320, 240, options)?;
Ok(MicroW8 {
engine,
@@ -186,6 +186,7 @@ impl super::Runtime for MicroW8 {
end_frame,
update,
start_time: Instant::now(),
+ next_frame: Instant::now(),
module: module_data.into(),
watchdog,
sound,
@@ -197,7 +198,19 @@ impl super::Runtime for MicroW8 {
fn run_frame(&mut self) -> Result<()> {
let mut result = Ok(());
if let Some(mut instance) = self.instance.take() {
- let time = instance.start_time.elapsed().as_millis() as i32;
+ {
+ if let Some(sleep) = instance.next_frame.checked_duration_since(Instant::now()) {
+ std::thread::sleep(sleep);
+ }
+ }
+
+ let now = Instant::now();
+ let time = (now - instance.start_time).as_millis() as i32;
+ {
+ let offset = ((time as u32 as i64 * 6) % 100 - 50) / 6;
+ instance.next_frame = now + Duration::from_millis((16 - offset) as u64);
+ }
+
{
let mut gamepad: u32 = 0;
for key in self.window.get_keys() {
diff --git a/test/frame_time.cwa b/test/frame_time.cwa
new file mode 100644
index 0000000..a06ea97
--- /dev/null
+++ b/test/frame_time.cwa
@@ -0,0 +1,20 @@
+include "../examples/include/microw8-api.cwa"
+
+global mut pos = 0;
+global mut next = 0;
+
+export fn upd() {
+ let lazy t = 32!32;
+ let lazy tick = t * 6 / 100;
+ let lazy rel = t - tick * 100 / 6;
+
+ setBackgroundColor(select(tick == next, 0, select(tick < next, 0x35, 0x55)));
+ setCursorPosition(pos % 13 * 3, pos / 13 % 30);
+ if rel < 10 {
+ printChar(32);
+ }
+ printInt(rel);
+
+ pos = pos + 1;
+ next = tick + 1;
+}
\ No newline at end of file
diff --git a/tests/plot_ges.cwa b/test/plot_ges.cwa
similarity index 100%
rename from tests/plot_ges.cwa
rename to test/plot_ges.cwa
diff --git a/web/src/microw8.js b/web/src/microw8.js
index f73947d..a4fdbea 100644
--- a/web/src/microw8.js
+++ b/web/src/microw8.js
@@ -318,7 +318,7 @@ export default function MicroW8(screen, config = {}) {
if (restart) {
runModule(currentData);
} else {
- window.setTimeout(mainloop, Math.round(nextFrame - now))
+ window.setTimeout(mainloop, nextFrame - now)
}
} catch (err) {
config.setMessage(cartridgeSize, err.toString());