Skip to content

Commit

Permalink
MinPixels
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed May 31, 2024
1 parent f3c816b commit 4491d51
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/anchors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/animated_sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::new(51, 35), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::new(51, 35), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/animated_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(64), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(64), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/animated_tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(64), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(64), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/filter_layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::new(64, 32), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::new(64, 32), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/palette_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
UVec2::splat(64),
// This is the palette that assets will be loaded with
// It is also the palette that assets will be displayed with, until changed
"palette/palette_1.palette.png".into(),
"palette/palette_1.palette.png",
),
))
.insert_resource(ClearColor(Color::BLACK))
Expand Down
2 changes: 1 addition & 1 deletion examples/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/sprite_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(32), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
}),
InputManagerPlugin::<Action>::default(),
StateMachinePlugin,
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(64), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(64), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/text_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(64), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(64), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
2 changes: 1 addition & 1 deletion examples/tilemap_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
}),
..default()
}),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png".into()),
PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, init)
Expand Down
6 changes: 3 additions & 3 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ fn update_cursor_position(
**position = None;
return;
};
let new_position = new_position / screen_tf.scale.truncate() * screen.size.as_vec2()
+ screen.size.as_vec2() / 2.;
let new_position = new_position / screen_tf.scale.truncate() * screen.computed_size.as_vec2()
+ screen.computed_size.as_vec2() / 2.;

**position = (new_position.cmpge(Vec2::ZERO).all()
&& new_position.cmplt(screen.size.as_vec2()).all())
&& new_position.cmplt(screen.computed_size.as_vec2()).all())
.then(|| new_position.as_uvec2());
}

Expand Down
2 changes: 1 addition & 1 deletion src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ macro_rules! align_to_screen {
};

**pos = align.as_uvec2().as_ivec2()
* (screen.size.as_ivec2() - size.as_ivec2())
* (screen.computed_size.as_ivec2() - size.as_ivec2())
+ anchor.pos(size).as_ivec2();
});
}
Expand Down
74 changes: 54 additions & 20 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,55 @@ pub(crate) fn screen_plugin<L: PxLayer>(size: ScreenSize) -> impl FnOnce(&mut Ap
(
update_screen,
(
(
clear_screen,
(apply_deferred, draw_screen::<L>)
.chain()
.in_set(PxSet::Draw),
)
.chain(),
resize_screen,
update_screen_palette,
),
(clear_screen, resize_screen),
draw_screen::<L>.in_set(PxSet::Draw),
)
.chain(),
update_screen_palette,
),
);
}
}

/// Size of the image which `seldom_pixel` draws to
#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug)]
pub enum ScreenSize {
/// The screen will have the given dimensions, which is scaled up to fit the window, preserving
/// the given dimensions' aspect ratio
Fixed(UVec2),
/// The screen will match the aspect ratio of the window, with dimensions with at least as many
/// The screen will match the aspect ratio of the window, with an area of at least as many
/// pixels as given
MinPixels(u32),
}

impl From<UVec2> for ScreenSize {
fn from(value: UVec2) -> Self {
Self::Fixed(value)
}
}

impl ScreenSize {
fn compute(self, window_size: Vec2) -> UVec2 {
use ScreenSize::*;

match self {
Fixed(size) => size,
MinPixels(pixels) => {
let pixels = pixels as f32;
let width = (window_size.x * pixels / window_size.y).sqrt();
let height = pixels / width;

UVec2::new(width as u32, height as u32)
}
}
}
}

#[derive(Clone, Resource)]
pub(crate) struct Screen {
pub(crate) image: Handle<Image>,
pub(crate) size: ScreenSize,
pub(crate) computed_size: UVec2,
}

#[derive(Component)]
Expand Down Expand Up @@ -106,10 +125,7 @@ fn insert_screen(
) -> impl Fn(ResMut<Assets<Image>>, Query<&Window, With<PrimaryWindow>>, Commands) {
move |mut images, windows, mut commands| {
let window = windows.single();
let computed_size = size.compute(UVec2::new(
window.physical_width(),
window.physical_height(),
));
let computed_size = size.compute(Vec2::new(window.width(), window.height()));

commands.insert_resource(Screen {
image: images.add(Image {
Expand Down Expand Up @@ -162,8 +178,11 @@ fn init_screen(
}

let (entity, window) = windows.single();
let calculated_screen_scale =
screen_scale(screen.size, Vec2::new(window.width(), window.height())).extend(1.);
let calculated_screen_scale = screen_scale(
screen.computed_size,
Vec2::new(window.width(), window.height()),
)
.extend(1.);

commands.spawn((
MaterialMesh2dBundle {
Expand Down Expand Up @@ -192,13 +211,28 @@ fn init_screen(
fn resize_screen(
mut window_resized: EventReader<WindowResized>,
mut screens: Query<&mut Transform, With<ScreenMarker>>,
screen: Res<Screen>,
mut screen: ResMut<Screen>,
mut images: ResMut<Assets<Image>>,
) {
if let Some(window_resized) = window_resized.read().last() {
let window_size = Vec2::new(window_resized.width, window_resized.height);
let computed_size = screen.size.compute(window_size);

if computed_size != screen.computed_size {
let image = images.get_mut(&screen.image).unwrap();
image.data = vec![0; (computed_size.x * computed_size.y) as usize];

let size = &mut image.texture_descriptor.size;
size.width = computed_size.x;
size.height = computed_size.y;
}

screen.computed_size = computed_size;

let mut transform = screens.single_mut();

transform.scale = screen_scale(
screen.size,
computed_size,
Vec2::new(window_resized.width, window_resized.height),
)
.extend(1.);
Expand Down

0 comments on commit 4491d51

Please sign in to comment.