Make viewport the same size as the window

This commit is contained in:
crumblingstatue 2023-04-06 23:02:45 +02:00
parent 4fb622989b
commit e2a38e80d7
3 changed files with 27 additions and 27 deletions

View file

@ -15,7 +15,7 @@ use sfml::{
use crate::{ use crate::{
debug::DebugState, debug::DebugState,
game::{for_each_tile_on_screen, Biome, GameState}, game::{for_each_tile_on_screen, Biome, GameState},
graphics::{self, ScreenPos, ScreenPosScalar, NATIVE_RESOLUTION}, graphics::{self, ScreenPos, ScreenPosScalar},
input::Input, input::Input,
math::{center_offset, px_per_frame_to_km_h, WorldPos, M_PER_PX, TILE_SIZE}, math::{center_offset, px_per_frame_to_km_h, WorldPos, M_PER_PX, TILE_SIZE},
res::Res, res::Res,
@ -45,8 +45,9 @@ impl App {
res.surf_music.set_looping(true); res.surf_music.set_looping(true);
res.surf_music.set_volume(10.0); res.surf_music.set_volume(10.0);
res.surf_music.play(); res.surf_music.play();
let rt = RenderTexture::new(NATIVE_RESOLUTION.w.into(), NATIVE_RESOLUTION.h.into()) let rw_size = rw.size();
.context("Failed to create render texture")?; let rt =
RenderTexture::new(rw_size.x, rw_size.y).context("Failed to create render texture")?;
Ok(Self { Ok(Self {
rw, rw,
should_quit: false, should_quit: false,
@ -77,6 +78,7 @@ impl App {
match ev { match ev {
Event::Closed => self.should_quit = true, Event::Closed => self.should_quit = true,
Event::Resized { width, height } => { Event::Resized { width, height } => {
self.rt = RenderTexture::new(width, height).unwrap();
let view = View::from_rect(Rect::new(0., 0., width as f32, height as f32)); let view = View::from_rect(Rect::new(0., 0., width as f32, height as f32));
self.rw.set_view(&view); self.rw.set_view(&view);
} }
@ -87,6 +89,7 @@ impl App {
fn do_update(&mut self) { fn do_update(&mut self) {
self.debug.update(&self.input); self.debug.update(&self.input);
let rt_size = self.rt.size();
if self.debug.freecam { if self.debug.freecam {
self.do_freecam(); self.do_freecam();
} else { } else {
@ -115,7 +118,7 @@ impl App {
.vspeed .vspeed
.clamp(-terminal_velocity, terminal_velocity); .clamp(-terminal_velocity, terminal_velocity);
let mut on_screen_tile_ents = Vec::new(); let mut on_screen_tile_ents = Vec::new();
for_each_tile_on_screen(self.game.camera_offset, |tp, _sp| { for_each_tile_on_screen(self.game.camera_offset, self.rt.size(), |tp, _sp| {
let tid = self.game.world.tile_at_mut(tp, &self.game.worldgen).mid; let tid = self.game.world.tile_at_mut(tp, &self.game.worldgen).mid;
if tid == Tile::EMPTY { if tid == Tile::EMPTY {
return; return;
@ -158,13 +161,11 @@ impl App {
}); });
self.game.player.vspeed += self.game.gravity; self.game.player.vspeed += self.game.gravity;
let (x, y, _w, _h) = self.game.player.col_en.en.xywh(); let (x, y, _w, _h) = self.game.player.col_en.en.xywh();
self.game.camera_offset.x = self.game.camera_offset.x = (x - rt_size.x as i32 / 2).try_into().unwrap_or(0);
(x - NATIVE_RESOLUTION.w as i32 / 2).try_into().unwrap_or(0); self.game.camera_offset.y = (y - rt_size.y as i32 / 2).try_into().unwrap_or(0);
self.game.camera_offset.y =
(y - NATIVE_RESOLUTION.h as i32 / 2).try_into().unwrap_or(0);
} }
let mut loc = self.input.mouse_down_loc; let mut loc = self.input.mouse_down_loc;
let vco = viewport_center_offset(self.rw.size(), self.rt.size(), self.scale); let vco = viewport_center_offset(self.rw.size(), rt_size, self.scale);
loc.x -= vco.x; loc.x -= vco.x;
loc.y -= vco.y; loc.y -= vco.y;
loc.x /= self.scale as ScreenPosScalar; loc.x /= self.scale as ScreenPosScalar;
@ -241,7 +242,7 @@ impl App {
fn do_rendering(&mut self) { fn do_rendering(&mut self) {
self.rt.clear(Color::rgb(55, 221, 231)); self.rt.clear(Color::rgb(55, 221, 231));
self.game.render_pre_step(&mut self.res); self.game.render_pre_step(&mut self.res, self.rt.size());
self.game.draw_world(&mut self.rt, &mut self.res); self.game.draw_world(&mut self.rt, &mut self.res);
self.game.draw_entities(&mut self.rt, &mut self.res); self.game.draw_entities(&mut self.rt, &mut self.res);
self.rt.display(); self.rt.display();

View file

@ -6,12 +6,12 @@ use sfml::{
Color, Rect, RectangleShape, RenderStates, RenderTarget, RenderTexture, Shape, Sprite, Color, Rect, RectangleShape, RenderStates, RenderTarget, RenderTexture, Shape, Sprite,
Transformable, Transformable,
}, },
system::Clock, system::{Clock, Vector2u},
SfBox, SfBox,
}; };
use crate::{ use crate::{
graphics::{ScreenPos, ScreenPosScalar, NATIVE_RESOLUTION}, graphics::{ScreenPos, ScreenPosScalar},
math::{wp_to_tp, WorldPos}, math::{wp_to_tp, WorldPos},
res::Res, res::Res,
world::{Tile, TileId, TilePos, World}, world::{Tile, TileId, TilePos, World},
@ -45,7 +45,7 @@ impl GameState {
res.lighting_shader.set_uniform_bool("has_texture", true); res.lighting_shader.set_uniform_bool("has_texture", true);
rs.set_shader(Some(&res.lighting_shader)); rs.set_shader(Some(&res.lighting_shader));
let mut s = Sprite::with_texture(&res.tile_atlas); let mut s = Sprite::with_texture(&res.tile_atlas);
for_each_tile_on_screen(self.camera_offset, |tp, sp| { for_each_tile_on_screen(self.camera_offset, rw.size(), |tp, sp| {
let tile = self.world.tile_at_mut(tp, &self.worldgen); let tile = self.world.tile_at_mut(tp, &self.worldgen);
s.set_position(sp.to_sf_vec()); s.set_position(sp.to_sf_vec());
if tile.bg != Tile::EMPTY { if tile.bg != Tile::EMPTY {
@ -82,21 +82,16 @@ impl GameState {
)); ));
rw.draw(&rect_sh); rw.draw(&rect_sh);
} }
pub fn render_pre_step(&mut self, res: &mut Res) { pub fn render_pre_step(&mut self, res: &mut Res, rt_size: Vector2u) {
res.lighting_shader.set_uniform_current_texture("texture"); res.lighting_shader.set_uniform_current_texture("texture");
res.lighting_shader res.lighting_shader
.set_uniform_float("time", self.clock.elapsed_time().as_seconds() * 10.0); .set_uniform_float("time", self.clock.elapsed_time().as_seconds() * 10.0);
res.lighting_shader.set_uniform_vec2( res.lighting_shader.set_uniform_vec2(
"mouse", "mouse",
Vec2::new( Vec2::new(rt_size.x as f32 / 2.0, rt_size.y as f32 / 2.0),
NATIVE_RESOLUTION.w as f32 / 2.0,
NATIVE_RESOLUTION.h as f32 / 2.0,
),
);
res.lighting_shader.set_uniform_vec2(
"resolution",
Vec2::new(NATIVE_RESOLUTION.w as f32, NATIVE_RESOLUTION.h as f32),
); );
res.lighting_shader
.set_uniform_vec2("resolution", Vec2::new(rt_size.x as f32, rt_size.y as f32));
res.lighting_shader.set_uniform_vec4( res.lighting_shader.set_uniform_vec4(
"lightData", "lightData",
Vec4 { Vec4 {
@ -119,9 +114,13 @@ impl GameState {
} }
} }
pub fn for_each_tile_on_screen(camera_offset: WorldPos, mut f: impl FnMut(TilePos, ScreenPos)) { pub fn for_each_tile_on_screen(
for y in (-32..(NATIVE_RESOLUTION.h as i16) + 32).step_by(32) { camera_offset: WorldPos,
for x in (-32..(NATIVE_RESOLUTION.w as i16) + 32).step_by(32) { rt_size: Vector2u,
mut f: impl FnMut(TilePos, ScreenPos),
) {
for y in (-32..(rt_size.y as i16) + 32).step_by(32) {
for x in (-32..(rt_size.x as i16) + 32).step_by(32) {
f( f(
TilePos { TilePos {
x: wp_to_tp(camera_offset.x.saturating_add(x.try_into().unwrap_or(0))), x: wp_to_tp(camera_offset.x.saturating_add(x.try_into().unwrap_or(0))),

View file

@ -37,11 +37,11 @@ impl ScreenPos {
} }
} }
pub const NATIVE_RESOLUTION: ScreenRes = ScreenRes { w: 960, h: 540 }; const DEFAULT_RES: ScreenRes = ScreenRes { w: 960, h: 540 };
pub fn make_window() -> RenderWindow { pub fn make_window() -> RenderWindow {
let mut rw = RenderWindow::new( let mut rw = RenderWindow::new(
NATIVE_RESOLUTION.to_sf(), DEFAULT_RES.to_sf(),
"Mantle Diver", "Mantle Diver",
Style::DEFAULT, Style::DEFAULT,
&ContextSettings::default(), &ContextSettings::default(),