Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Working vsync/framerate uncap example
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Oct 21, 2023
1 parent c7db3c6 commit f089f0c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# EXAMPLE=ecs_sprite
# EXAMPLE=ecs_topdown_game
# EXAMPLE=full_game_loop
EXAMPLE=framerate_vsync
# EXAMPLE=music
# EXAMPLE=lighting
# EXAMPLE=single_particle
Expand All @@ -21,7 +22,7 @@
# EXAMPLE=shapes
# EXAMPLE=sound
# EXAMPLE=text
EXAMPLE=timed_draw
# EXAMPLE=timed_draw
# EXAMPLE=y_sort

# default: build-examples
Expand Down
6 changes: 6 additions & 0 deletions comfy-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ pub struct GameConfig {
pub resolution: ResolutionConfig,
pub min_resolution: ResolutionConfig,

pub target_framerate: u32,
pub vsync_enabled: bool,

pub bloom_enabled: bool,
pub lighting: GlobalLightingParams,
pub lighting_enabled: bool,
Expand Down Expand Up @@ -115,6 +118,9 @@ impl Default for GameConfig {
resolution,
min_resolution,

target_framerate: 60,
vsync_enabled: true,

bloom_enabled: false,
lighting: GlobalLightingParams::default(),
lighting_enabled: false,
Expand Down
15 changes: 14 additions & 1 deletion comfy-wgpu/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,25 @@ impl WgpuRenderer {
#[cfg(not(feature = "record-pngs"))]
let surface_usage = wgpu::TextureUsages::RENDER_ATTACHMENT;

let desired_present_mode = if game_config().vsync_enabled {
wgpu::PresentMode::AutoVsync
} else {
wgpu::PresentMode::AutoNoVsync
};

let present_mode = if caps.present_modes.contains(&desired_present_mode)
{
desired_present_mode
} else {
caps.present_modes[0]
};

let config = wgpu::SurfaceConfiguration {
usage: surface_usage,
format: monitor_surface_format,
width: size.width,
height: size.height,
present_mode: caps.present_modes[0],
present_mode,
alpha_mode: caps.alpha_modes[0],
view_formats: vec![],
};
Expand Down
47 changes: 47 additions & 0 deletions comfy/examples/framerate_vsync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use comfy::*;

simple_game!(
"Framerate & VSync config options",
GameState,
config,
setup,
update
);

fn config(config: GameConfig) -> GameConfig {
GameConfig { vsync_enabled: false, target_framerate: 500, ..config }
}

pub struct GameState {}

impl GameState {
pub fn new(_c: &EngineContext) -> Self {
Self {}
}
}


fn setup(_state: &mut GameState, _c: &mut EngineContext) {
game_config_mut().dev.show_fps = true;
}

fn update(_state: &mut GameState, _c: &mut EngineContext) {
// Note the color is multiplied by 5.0 to make it brighter
// and glow with the bloom effect. This is possible because
// Comfy supports HDR.
draw_circle(vec2(0.0, 0.0), 0.5, RED * 5.0, 0);

let config = game_config();

draw_text(
&format!(
"VSync: {} ... Target FPS: {} ... Real FPS: {}",
config.vsync_enabled,
config.target_framerate,
get_fps()
),
vec2(0.0, -2.0),
WHITE,
TextAlign::Center,
);
}
4 changes: 2 additions & 2 deletions comfy/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub async fn run_comfy_main_async(mut game: impl GameLoop + 'static) {
let _tracy = maybe_setup_tracy();

#[cfg(not(target_arch = "wasm32"))]
let target_framerate = 60;
let target_framerate = game_config().target_framerate;

#[cfg(not(target_arch = "wasm32"))]
let mut loop_helper = spin_sleep::LoopHelper::builder()
Expand Down Expand Up @@ -117,7 +117,7 @@ pub async fn run_comfy_main_async(mut game: impl GameLoop + 'static) {
#[cfg(not(target_arch = "wasm32"))]
loop_helper.loop_sleep();
delta = frame_start.elapsed().as_secs_f32();
delta = delta.clamp(1.0 / 300.0, 1.0 / 15.0);
delta = delta.clamp(1.0 / 5000.0, 1.0 / 10.0);

#[cfg(feature = "tracy")]
tracy_client::frame_mark();
Expand Down

0 comments on commit f089f0c

Please sign in to comment.