From 49c47ac7182b5fa896e457e82a3faf5c4a46a74f Mon Sep 17 00:00:00 2001 From: crumblingstatue Date: Sun, 2 Apr 2023 00:30:03 +0200 Subject: [PATCH] Super basic map rendering --- Cargo.lock | 10 ++++++++-- Cargo.toml | 3 +++ src/app.rs | 39 ++++++++++++++++++++++++++++++++++----- src/game.rs | 21 ++++++++++----------- src/graphics.rs | 8 ++++---- src/main.rs | 1 + 6 files changed, 60 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a27edf..aad7944 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -300,6 +300,11 @@ dependencies = [ "slab", ] +[[package]] +name = "gamedebug_core" +version = "0.1.0" +source = "git+https://github.com/crumblingstatue/gamedebug_core.git#0afa12c84d865e266ccf6ee31970ff70da0e2ac6" + [[package]] name = "gdk-pixbuf-sys" version = "0.16.3" @@ -534,6 +539,7 @@ dependencies = [ "anyhow", "egui-sfml", "fnv", + "gamedebug_core", "hecs", "rand", "rfd", @@ -690,9 +696,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.54" +version = "1.0.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" +checksum = "1d0dd4be24fcdcfeaa12a432d588dc59bbad6cad3510c67e74a2b6b2fc950564" dependencies = [ "unicode-ident", ] diff --git a/Cargo.toml b/Cargo.toml index ce71f84..2d562a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,6 @@ git = "https://github.com/crumblingstatue/s2dc.git" [dependencies.sfml-xt] git = "https://github.com/crumblingstatue/sfml-xt.git" + +[dependencies.gamedebug_core] +git = "https://github.com/crumblingstatue/gamedebug_core.git" diff --git a/src/app.rs b/src/app.rs index 9b4b775..b683698 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,3 +1,5 @@ +use egui_sfml::{egui, SfEgui}; +use gamedebug_core::imm; use sfml::{ graphics::{Color, RenderTarget, RenderWindow}, window::Event, @@ -7,19 +9,23 @@ use crate::{game::GameState, graphics, res::Res}; /// Application level state (includes game and ui state, etc.) pub struct App { - rw: RenderWindow, - should_quit: bool, - game: GameState, - res: Res, + pub rw: RenderWindow, + pub should_quit: bool, + pub game: GameState, + pub res: Res, + pub sf_egui: SfEgui, } impl App { pub fn new() -> anyhow::Result { + let rw = graphics::make_window(); + let sf_egui = SfEgui::new(&rw); Ok(Self { - rw: graphics::make_window(), + rw, should_quit: false, game: GameState::default(), res: Res::load()?, + sf_egui, }) } @@ -28,11 +34,13 @@ impl App { self.do_event_handling(); self.do_update(); self.do_rendering(); + gamedebug_core::inc_frame(); } } fn do_event_handling(&mut self) { while let Some(ev) = self.rw.poll_event() { + self.sf_egui.add_event(&ev); match ev { Event::Closed => self.should_quit = true, _ => {} @@ -45,6 +53,27 @@ impl App { fn do_rendering(&mut self) { self.rw.clear(Color::BLACK); self.game.draw_world(&mut self.rw, &self.res); + self.sf_egui + .do_frame(|ctx| { + egui::Window::new("Debug").show(ctx, |ui| { + ui.label("Cam x"); + ui.add(egui::DragValue::new(&mut self.game.camera_offset.x)); + ui.label("Cam y"); + ui.add(egui::DragValue::new(&mut self.game.camera_offset.y)); + ui.separator(); + egui::ScrollArea::vertical().show(ui, |ui| { + gamedebug_core::for_each_imm(|info| match info { + gamedebug_core::Info::Msg(msg) => { + ui.label(msg); + } + gamedebug_core::Info::Rect(_, _, _, _, _) => todo!(), + }); + }); + gamedebug_core::clear_immediates(); + }); + }) + .unwrap(); + self.sf_egui.draw(&mut self.rw, None); self.rw.display(); } } diff --git a/src/game.rs b/src/game.rs index b9d93cf..ad1e6f2 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,3 +1,4 @@ +use gamedebug_core::imm_dbg; use sfml::graphics::{Rect, RenderTarget, RenderWindow, Sprite, Transformable}; use crate::{ @@ -8,8 +9,8 @@ use crate::{ }; pub struct GameState { - camera_offset: WorldPos, - world: World, + pub camera_offset: WorldPos, + pub world: World, } impl GameState { pub(crate) fn draw_world(&mut self, rw: &mut RenderWindow, res: &Res) { @@ -24,19 +25,17 @@ impl GameState { } fn for_each_tile(camera_offset: WorldPos, mut f: impl FnMut(TilePos, ScreenPos)) { - for y in (camera_offset.y..camera_offset.y + NATIVE_RESOLUTION.h as WorldPosScalar).step_by(32) - { - for x in - (camera_offset.x..camera_offset.x + NATIVE_RESOLUTION.w as WorldPosScalar).step_by(32) - { + for y in (0..NATIVE_RESOLUTION.h).step_by(32) { + for x in (0..NATIVE_RESOLUTION.w).step_by(32) { f( TilePos { - x: x / 32, - y: y / 32, + x: (camera_offset.x + x as i32) / 32, + y: (camera_offset.y + y as i32) / 32, }, ScreenPos { - x: (x - camera_offset.x) as i16, - y: (y - camera_offset.y) as i16, + x: imm_dbg!(imm_dbg!(x as i32) - (imm_dbg!(camera_offset.x as i32)) % 32) + as i16, + y: (y as i32 - (camera_offset.y as i32 % 32)) as i16, }, ) } diff --git a/src/graphics.rs b/src/graphics.rs index 480b24a..0fcc9bf 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -6,15 +6,15 @@ use sfml::{ use sfml_xt::graphics::RenderWindowExt; pub struct ScreenRes { - pub w: u16, - pub h: u16, + pub w: i16, + pub h: i16, } impl ScreenRes { fn to_sf(&self) -> VideoMode { VideoMode { - width: self.w.into(), - height: self.h.into(), + width: self.w as _, + height: self.h as _, bits_per_pixel: 32, } } diff --git a/src/main.rs b/src/main.rs index bd40d10..1aea6c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod world; use app::App; fn try_main() -> anyhow::Result<()> { + gamedebug_core::set_enabled(true); let mut app = App::new()?; app.do_game_loop(); Ok(())