diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-25 17:37:49 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-25 17:37:49 +0200 |
commit | 5bf5a200ada9ba03ca2a12c1503318a97166d7c7 (patch) | |
tree | 0936ea088efd11eaee00a88dcb368e7271fb985b /pixel-client | |
parent | 3403fc2d55f510c29bf7e74d85433801cd99cbbc (diff) | |
download | hurrycurry-5bf5a200ada9ba03ca2a12c1503318a97166d7c7.tar hurrycurry-5bf5a200ada9ba03ca2a12c1503318a97166d7c7.tar.bz2 hurrycurry-5bf5a200ada9ba03ca2a12c1503318a97166d7c7.tar.zst |
pc: show fps
Diffstat (limited to 'pixel-client')
-rw-r--r-- | pixel-client/src/main.rs | 5 | ||||
-rw-r--r-- | pixel-client/src/menu/background.rs | 17 | ||||
-rw-r--r-- | pixel-client/src/menu/main.rs | 6 | ||||
-rw-r--r-- | pixel-client/src/menu/mod.rs | 16 | ||||
-rw-r--r-- | pixel-client/src/menu/settings.rs | 26 | ||||
-rw-r--r-- | pixel-client/src/profiler.rs | 60 | ||||
-rw-r--r-- | pixel-client/src/render/font.rs | 20 | ||||
-rw-r--r-- | pixel-client/src/ui.rs | 14 |
8 files changed, 146 insertions, 18 deletions
diff --git a/pixel-client/src/main.rs b/pixel-client/src/main.rs index 8c231031..9ed0ebec 100644 --- a/pixel-client/src/main.rs +++ b/pixel-client/src/main.rs @@ -21,6 +21,7 @@ use game::Game; use hurrycurry_protocol::glam::Vec2; use menu::main::MainMenu; use network::Network; +use profiler::ProfilerOverlay; use render::Renderer; use sdl2::{event::Event, keyboard::KeyboardState, mouse::MouseState, pixels::Color}; use std::time::{Duration, Instant}; @@ -29,6 +30,7 @@ pub mod game; pub mod helper; pub mod menu; pub mod network; +pub mod profiler; pub mod render; pub mod tilemap; pub mod ui; @@ -95,8 +97,8 @@ fn main() { }; let mut events = sdl_context.event_pump().unwrap(); - let mut last_tick = Instant::now(); + let mut profiler = ProfilerOverlay::new(); 'mainloop: loop { let (width, height) = canvas.output_size().unwrap(); @@ -126,6 +128,7 @@ fn main() { State::Quit => (), } + profiler.update(&mut renderer); canvas.set_draw_color(Color::BLACK); canvas.clear(); renderer.submit(&mut canvas); diff --git a/pixel-client/src/menu/background.rs b/pixel-client/src/menu/background.rs index daf89360..dd5ab97c 100644 --- a/pixel-client/src/menu/background.rs +++ b/pixel-client/src/menu/background.rs @@ -1,3 +1,20 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ use crate::{ render::{sprite::SpriteDraw, AtlasLayout, Renderer}, tilemap::Tilemap, diff --git a/pixel-client/src/menu/main.rs b/pixel-client/src/menu/main.rs index 37cecd3e..f5ea5cd6 100644 --- a/pixel-client/src/menu/main.rs +++ b/pixel-client/src/menu/main.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use super::{background::MenuBackground, settings::Settings}; +use super::{background::MenuBackground, settings::SettingsMenu}; use crate::{ game::Game, network::Network, @@ -35,7 +35,7 @@ pub struct MainMenu { pub ui_state: UiState, server_address: String, next_state: Option<Box<State>>, - settings: Option<Settings>, + settings: Option<SettingsMenu>, } impl MainMenu { @@ -82,7 +82,7 @@ impl MainMenu { } ui.textedit(80., &mut self.server_address); if ui.button(80., "Settings") { - self.settings = Some(Settings::default()) + self.settings = Some(SettingsMenu::default()) } if ui.button(80., "Quit") { self.next_state = Some(Box::new(State::Quit)); diff --git a/pixel-client/src/menu/mod.rs b/pixel-client/src/menu/mod.rs index d712eaa9..f09e4fdf 100644 --- a/pixel-client/src/menu/mod.rs +++ b/pixel-client/src/menu/mod.rs @@ -1,4 +1,20 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ pub mod main; pub mod background; pub mod settings; diff --git a/pixel-client/src/menu/settings.rs b/pixel-client/src/menu/settings.rs index db524496..90668039 100644 --- a/pixel-client/src/menu/settings.rs +++ b/pixel-client/src/menu/settings.rs @@ -1,11 +1,33 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ use crate::ui::Ui; +pub struct Settings { + pub username: String, +} + #[derive(Default)] -pub struct Settings {} +pub struct SettingsMenu {} -impl Settings { +impl SettingsMenu { pub fn draw(&mut self, ui: &mut Ui) -> bool { ui.text("Settings placeholder"); + return ui.button(80., "Back"); } } diff --git a/pixel-client/src/profiler.rs b/pixel-client/src/profiler.rs new file mode 100644 index 00000000..8d3f4689 --- /dev/null +++ b/pixel-client/src/profiler.rs @@ -0,0 +1,60 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +use crate::render::{sprite::SpriteDraw, Renderer}; +use hurrycurry_protocol::glam::Vec2; +use std::time::Instant; + +pub struct ProfilerOverlay { + frames: usize, + fps_timer_start: Instant, + fps: f32, +} + +impl ProfilerOverlay { + pub fn new() -> Self { + Self { + fps: 0., + fps_timer_start: Instant::now(), + frames: 0, + } + } + pub fn update(&mut self, renderer: &mut Renderer) { + self.frames += 1; + + let t = self.fps_timer_start.elapsed(); + + if t.as_secs_f32() > 0.2 { + self.fps = self.frames as f32 / t.as_secs_f32(); + self.frames = 0; + self.fps_timer_start += t; + } + let size = renderer.draw_text( + Vec2::ZERO, + &format!("FPS: {:.0}", self.fps), + 0.3, + Some([255, 150, 255, 255]), + ); + renderer.draw_ui(SpriteDraw::screen( + renderer.misc_textures.solid, + i32::MAX - 1, + Vec2::ZERO, + size, + Some([0, 0, 0, 200]), + )) + } +} diff --git a/pixel-client/src/render/font.rs b/pixel-client/src/render/font.rs index 43df9ca7..95cb3fab 100644 --- a/pixel-client/src/render/font.rs +++ b/pixel-client/src/render/font.rs @@ -42,20 +42,22 @@ impl FontTextures { } impl<'a> Renderer<'a> { - pub fn draw_text(&mut self, position: Vec2, text: &str) -> Vec2 { + pub fn draw_text( + &mut self, + position: Vec2, + text: &str, + scale: f32, + tint: Option<[u8; 4]>, + ) -> Vec2 { let mut cursor = position; let mut line_height = 0f32; for c in text.chars() { if (c as u32) < 128 { let r = self.font_textures.glyphs[c as usize]; - self.draw_ui(SpriteDraw::overlay( - r, - cursor, - Vec2::new(r.width() as f32, r.height() as f32), - None, - )); - cursor.x += r.width() as f32; - line_height = line_height.max(r.height() as f32) + let size = Vec2::new(r.width() as f32, r.height() as f32) * scale; + self.draw_ui(SpriteDraw::overlay(r, cursor, size, tint)); + cursor.x += size.x; + line_height = line_height.max(size.y) } } (cursor - position.y) + Vec2::Y * line_height diff --git a/pixel-client/src/ui.rs b/pixel-client/src/ui.rs index 6410630f..684fb5b3 100644 --- a/pixel-client/src/ui.rs +++ b/pixel-client/src/ui.rs @@ -122,13 +122,19 @@ impl FocusDevice { impl<'a, 'b> Ui<'a, 'b> { pub fn text(&mut self, text: &str) { let margin = Vec2::splat(2.); - let size = margin + self.renderer.draw_text(self.cursor + margin, text) + margin; + let size = margin + + self + .renderer + .draw_text(self.cursor + margin, text, 1., None) + + margin; self.advance(size); } pub fn button(&mut self, w: f32, label: &str) -> bool { let c = self.cursor; let margin = Vec2::splat(4.); - let text_size = self.renderer.draw_text(self.cursor + margin, label); + let text_size = self + .renderer + .draw_text(self.cursor + margin, label, 1., None); let size = margin + Vec2::new(w, text_size.y) + margin; self.index += 1; @@ -170,7 +176,9 @@ impl<'a, 'b> Ui<'a, 'b> { pub fn textedit(&mut self, w: f32, content: &mut String) { let c = self.cursor; let margin = Vec2::splat(4.); - let text_size = self.renderer.draw_text(self.cursor + margin, &content); + let text_size = self + .renderer + .draw_text(self.cursor + margin, &content, 1., None); let size = margin + Vec2::new(w, text_size.y) + margin; self.index += 1; |