Skip to content

Commit 39ab744

Browse files
VSync option
1 parent c58fedc commit 39ab744

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

docs/project-setup.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ All paths mentioned are relative to the `project.toml` file.
4949
- `debug_options`: A list of debug options to enable. Only available options are `show_fps` and `show_mouse_pos`
5050
- `show_fps`: Shows the current frames per second (FPS)
5151
- `show_mouse_pos`: Shows the current mouse position on the screen (World coordinates, not screen coordinates)
52+
- `vsync`: Whether to enable vertical synchronization (VSync). Defaults to `true`. If set to `false`, the game will run as fast as possible, which may cause screen tearing.
5253
- `[font]`: The font configuration. Defaults to the default Crust font.
5354
- `file`: The path to the bitmap font file. The file must be an image file.
5455
- `first_char`: The first character in the font.

src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ async fn main() {
7979
window.set_cursor_pos_polling(true);
8080
window.make_current();
8181
gl::load_with(|symbol| window.get_proc_address(symbol) as *const std::os::raw::c_void);
82-
glfw.set_swap_interval(glfw::SwapInterval::Sync(1));
82+
83+
let mut runtime = utils::Runtime::new(&project_file, args.additional_args, &window);
84+
println!("Loaded project: {}", project_file);
85+
86+
glfw.set_swap_interval(if runtime.vsync {
87+
glfw::SwapInterval::Sync(1)
88+
} else {
89+
glfw::SwapInterval::None
90+
});
8391
unsafe {
8492
gl::Enable(gl::CULL_FACE);
8593
gl::CullFace(gl::BACK);
@@ -111,8 +119,6 @@ async fn main() {
111119
}]);
112120
}
113121

114-
let mut runtime = utils::Runtime::new(&project_file, args.additional_args, &window);
115-
println!("Loaded project: {}", project_file);
116122
let shader_program = ShaderProgram::new(VERT_SHADER, FRAG_SHADER);
117123
runtime
118124
.run(&mut window, &events, &shader_program, &mut glfw)

src/utils/runtime.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct TagConfig {
5656
#[derive(Deserialize, Debug)]
5757
struct ProjectConfig {
5858
debug_options: Option<Vec<String>>,
59+
vsync: Option<bool>,
5960
font: Option<FontConfig>,
6061
stage: Option<StageConfig>,
6162
sprites: Vec<SpriteConfig>,
@@ -175,6 +176,7 @@ pub struct Runtime {
175176
pub project: Project,
176177
pub audio_manager: AudioManager<DefaultBackend>,
177178
pub font: BitmapFont,
179+
pub vsync: bool,
178180
debug_options: Vec<String>,
179181
}
180182

@@ -349,6 +351,7 @@ impl Runtime {
349351
project,
350352
audio_manager,
351353
font,
354+
vsync: config.vsync.unwrap_or(true),
352355
debug_options: config.debug_options.unwrap_or(vec![]),
353356
}
354357
}

0 commit comments

Comments
 (0)