Closed DuckDuckWhale closed 2 years ago
After some more testing:
loop {
buffer = get_new_buffer();
window.update_with_buffer(&buffer, width, width).unwrap();
while !window.is_key_pressed(Key::Space, KeyRepeat::Yes) {
window.update();
}
}
Results in the key not being repeated, while
loop {
buffer = get_new_buffer();
window.update_with_buffer(&buffer, width, width).unwrap();
while !window.is_key_pressed(Key::Space, KeyRepeat::Yes) {
window.update_with_buffer(&buffer, width, width).unwrap();
}
}
...works fine. I couldn't figure out why this happens from reading the docs for update
and update_with_buffer
.
So the docs can be clarified, but if you look at all the examples the are structured like this:
while window.is_open() && !window.is_key_down(Key::Escape) {
let new_size = window.get_size();
// .. more code here
// read somes keys here
window.get_keys().iter().for_each(|key| match key {
Key::W => println!("holding w!"),
Key::T => println!("holding t!"),
_ => (),
});
// .. update here
window
.update_with_buffer(&buffer, new_size.0, new_size.1)
.unwrap();
}
So user code is expected to follow a similar pattern.
Thanks for the response. In my case I only care about one key, so iterating over all possible keys seem a bit verbose. Am I using is_key_pressed
wrong? And why would update_with_buffer
work yet update
doesn't? (I don't change the image in the loop so updating with buffer seems unnecessary here.)
Should only ever call update_with_buffer
or update
during each loop, they should never be called twice.
Something like this (untested) should work fine
while window.is_open() {
if window.is_key_down(Key::Space) {
println!("space!");
}
window.update();
}
docs has now been updated to state that only one of update_with_buffer
and update
should be called for a single window.
Closing this, if you still have issues post (here) again.
Should only ever call
update_with_buffer
or update during each loop, they should never be called twice.
Does this mean that they shouldn't be called consecutively twice without checking for keys etc. (which I was doing)?
docs has now been updated to state that only one of
update_with_buffer
andupdate
should be called for a single window.
Does this mean that for a given window, only one of the two methods can be called, i.e. once update_with_buffer
is called, it is now invalid to call update
on the same window, in the next iteration, and only update_with_buffer
is valid to call? If so I don't really understand the point. Under the scenario where the window content (framebuffer) is only set once in the very beginning and not changed and only the key presses are of interest, isn't it unnecessary to update the framebuffer again?
Usually applications pick one of the other.
update_with_buffer
regardless of the buffer been updated or not.update
Switching between them isn't directly support but should work, something like this should do the trick, but is really untested.
while window.is_open() {
if window.is_key_down(Keys::Space) {
...
}
if needs_to_update_buffer {
window.update_with_buffer(...);
} else {
window.update();
}
}
I just wrote a bug by mistakingupdate_with_buffer
as a superset ofupdate
, thinking that it also takes in keyboard events when it does not, leading to the window not responding after the first key press. Maybe a better name likeset_framebuffer
could avoid the confusion, considering the crate is not yet 1.0?It apparently does and it turns out that I didn't update the window before checking for keyboard event in the while loop condition. Still, it might be a good idea to make it explicit in the docs? Also, found a weird behavior with key repeat below. I can make a separate issue for it if you'd like.