Skip to content

Commit

Permalink
Add scale factor option for configuring video resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Sep 17, 2024
1 parent bf49090 commit d1c0868
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 90 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ edition = "2021"
bevy = { version = "0.14.2", features = ["wav"] }
bevy_dev_tools = "0.14.2"
rand = "0.8.5"
num-traits = "0.2"
num-derive = "0.4"
image = "0.25.2"
log = { version = "*", features = [
"max_level_debug",
Expand Down
2 changes: 2 additions & 0 deletions dist/style.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
body {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
Expand Down
1 change: 1 addition & 0 deletions src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pub enum AppState {
GameModeMenu,
LevelMenu,
Game,
ChangeScale,
}
226 changes: 137 additions & 89 deletions src/game_option_menu/plugin.rs

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use bevy::{
window::{PresentMode, WindowResolution},
};

#[macro_use]
extern crate num_derive;
extern crate num_traits;

mod app_state;
mod audio;
mod controller;
Expand All @@ -12,6 +16,7 @@ mod game_option_menu;
mod inputs;
mod level_menu;
mod logo;
mod scale;
mod splash;
mod utility;

Expand All @@ -24,8 +29,11 @@ fn main() {
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
resolution: WindowResolution::new(1280.0, 960.0),
resolution: WindowResolution::new(1920.0, 1080.0)
.with_scale_factor_override(1.0),
present_mode: PresentMode::AutoNoVsync,
position: WindowPosition::Centered(MonitorSelection::Primary),
fit_canvas_to_parent: true,
title: "TETRIS".into(),
..default()
}),
Expand Down Expand Up @@ -53,6 +61,7 @@ fn main() {
game_option_menu::plugin::setup,
level_menu::plugin::setup,
game::plugin::setup,
scale::plugin::setup,
))
.run();
}
Expand Down
1 change: 1 addition & 0 deletions src/scale/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod plugin;
65 changes: 65 additions & 0 deletions src/scale/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use bevy::prelude::*;
use num_traits::FromPrimitive;

use crate::{
app_state::AppState, game::transform::GameTransform,
game_option_menu::transform::GameOptionMenuTransform,
level_menu::transform::LevelMenuTransform, splash::transform::SplashTransform,
};

pub fn setup(app: &mut App) {
app.insert_resource(ScaleFactor::default())
.add_systems(OnEnter(AppState::ChangeScale), change_scale_system);
}

#[derive(Default, Clone, Copy, FromPrimitive, Resource)]
pub enum ScaleFactor {
S720 = 0,
#[default]
S1080,
S1440,
S1800,
S2160,
S3240,
S4320,
}

impl ScaleFactor {
pub fn mul(&self) -> f32 {
let base = 1080.0;

match self {
ScaleFactor::S720 => 720.0 / base,
ScaleFactor::S1080 => 1080.0 / base,
ScaleFactor::S1440 => 1440.0 / base,
ScaleFactor::S1800 => 1800.0 / base,
ScaleFactor::S2160 => 2160.0 / base,
ScaleFactor::S3240 => 3240.0 / base,
ScaleFactor::S4320 => 4320.0 / base,
}
}

pub fn prev(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 - 1).map(|n| std::mem::replace(self, n))
}

pub fn next(&mut self) -> Option<Self> {
FromPrimitive::from_i8(*self as i8 + 1).map(|n| std::mem::replace(self, n))
}
}

fn change_scale_system(
scale_factor: Res<ScaleFactor>,
mut splash_transform: ResMut<SplashTransform>,
mut game_option_menu_transform: ResMut<GameOptionMenuTransform>,
mut level_menu_transform: ResMut<LevelMenuTransform>,
mut game_transform: ResMut<GameTransform>,
mut app_state: ResMut<NextState<AppState>>,
) {
*splash_transform = SplashTransform::new(scale_factor.mul());
*game_option_menu_transform = GameOptionMenuTransform::new(scale_factor.mul());
*level_menu_transform = LevelMenuTransform::new(scale_factor.mul());
*game_transform = GameTransform::new(scale_factor.mul());

app_state.set(AppState::GameModeMenu);
}

0 comments on commit d1c0868

Please sign in to comment.