From 441c884ef96e69c82e6cb37990d810820b473733 Mon Sep 17 00:00:00 2001 From: Olivia Date: Fri, 13 Dec 2024 13:10:48 +0000 Subject: [PATCH] Fix text rendering --- .../src/systems/rendering.rs | 34 ++++++++----------- .../src/systems/rendering.rs | 32 +++++++---------- .../src/systems/rendering.rs | 32 +++++++---------- .../src/systems/rendering.rs | 32 +++++++---------- .../src/systems/rendering.rs | 27 ++++++--------- 5 files changed, 64 insertions(+), 93 deletions(-) diff --git a/code/rust-sokoban-c03-01/src/systems/rendering.rs b/code/rust-sokoban-c03-01/src/systems/rendering.rs index fd68ae1..b94b56e 100644 --- a/code/rust-sokoban-c03-01/src/systems/rendering.rs +++ b/code/rust-sokoban-c03-01/src/systems/rendering.rs @@ -1,8 +1,7 @@ use ggez::{ - conf, - event::{self, KeyCode}, - graphics::{self, Color, DrawParam, Image}, - input::keyboard, + conf, event, + graphics::{self, Canvas, Color, DrawParam, Image, PxScale, Text, TextFragment}, + input::keyboard::{self, KeyCode}, Context, GameResult, }; use glam::Vec2; @@ -16,7 +15,7 @@ use crate::constants::*; pub fn run_rendering(world: &World, context: &mut Context) { // Clearing the screen (this gives us the background colour) - let mut canvas = + let mut canvas = graphics::Canvas::from_frame(context, graphics::Color::from([0.95, 0.95, 0.95, 1.0])); // Get all the renderables with their positions and sort by the position z @@ -41,26 +40,21 @@ pub fn run_rendering(world: &World, context: &mut Context) { // Render any text let mut query = world.query::<&Gameplay>(); let gameplay = query.iter().next().unwrap().1; - draw_text(context, &gameplay.state.to_string(), 525.0, 80.0); - draw_text(context, &gameplay.moves_count.to_string(), 525.0, 100.0); + draw_text(&mut canvas, &gameplay.state.to_string(), 525.0, 80.0); + draw_text(&mut canvas, &gameplay.moves_count.to_string(), 525.0, 100.0); // Finally, present the canvas, this will actually display everything // on the screen. canvas.finish(context).expect("expected to present"); } -pub fn draw_text(context: &mut Context, text_string: &str, x: f32, y: f32) { - let text = graphics::Text::new(text_string); - let destination = Vec2::new(x, y); - let color = Some(Color::new(0.0, 0.0, 0.0, 1.0)); - let dimensions = Vec2::new(0.0, 20.0); +pub fn draw_text(canvas: &mut Canvas, text_string: &str, x: f32, y: f32) { + let mut text = Text::new(TextFragment { + text: text_string.to_string(), + color: Some(Color::new(0.0, 0.0, 0.0, 1.0)), + scale: Some(PxScale::from(20.0)), + ..Default::default() + }); - graphics::queue_text(context, &text, dimensions, color); - graphics::draw_queued_text( - context, - graphics::DrawParam::new().dest(destination), - None, - graphics::FilterMode::Linear, - ) - .expect("expected drawing queued text"); + canvas.draw(&text, Vec2::new(x, y)); } diff --git a/code/rust-sokoban-c03-02/src/systems/rendering.rs b/code/rust-sokoban-c03-02/src/systems/rendering.rs index e0ef963..d774393 100644 --- a/code/rust-sokoban-c03-02/src/systems/rendering.rs +++ b/code/rust-sokoban-c03-02/src/systems/rendering.rs @@ -1,8 +1,7 @@ use ggez::{ - conf, - event::{self, KeyCode}, - graphics::{self, Color, DrawParam, Image}, - input::keyboard, + conf, event, + graphics::{self, Canvas, Color, DrawParam, Image, PxScale, Text, TextFragment}, + input::keyboard::{self, KeyCode}, Context, GameResult, }; use glam::Vec2; @@ -46,28 +45,23 @@ pub fn run_rendering(world: &World, context: &mut Context) { // Render any text let mut query = world.query::<&Gameplay>(); let gameplay = query.iter().next().unwrap().1; - draw_text(context, &gameplay.state.to_string(), 525.0, 80.0); - draw_text(context, &gameplay.moves_count.to_string(), 525.0, 100.0); + draw_text(&mut canvas, &gameplay.state.to_string(), 525.0, 80.0); + draw_text(&mut canvas, &gameplay.moves_count.to_string(), 525.0, 100.0); // Finally, present the canvas, this will actually display everything // on the screen. canvas.finish(context).expect("expected to present"); } -pub fn draw_text(context: &mut Context, text_string: &str, x: f32, y: f32) { - let text = graphics::Text::new(text_string); - let destination = Vec2::new(x, y); - let color = Some(Color::new(0.0, 0.0, 0.0, 1.0)); - let dimensions = Vec2::new(0.0, 20.0); +pub fn draw_text(canvas: &mut Canvas, text_string: &str, x: f32, y: f32) { + let mut text = Text::new(TextFragment { + text: text_string.to_string(), + color: Some(Color::new(0.0, 0.0, 0.0, 1.0)), + scale: Some(PxScale::from(20.0)), + ..Default::default() + }); - graphics::queue_text(context, &text, dimensions, color); - graphics::draw_queued_text( - context, - graphics::DrawParam::new().dest(destination), - None, - graphics::FilterMode::Linear, - ) - .expect("expected drawing queued text"); + canvas.draw(&text, Vec2::new(x, y)); } pub fn get_image(context: &mut Context, renderable: &Renderable, delta: Duration) -> Image { diff --git a/code/rust-sokoban-c03-03/src/systems/rendering.rs b/code/rust-sokoban-c03-03/src/systems/rendering.rs index e0ef963..d774393 100644 --- a/code/rust-sokoban-c03-03/src/systems/rendering.rs +++ b/code/rust-sokoban-c03-03/src/systems/rendering.rs @@ -1,8 +1,7 @@ use ggez::{ - conf, - event::{self, KeyCode}, - graphics::{self, Color, DrawParam, Image}, - input::keyboard, + conf, event, + graphics::{self, Canvas, Color, DrawParam, Image, PxScale, Text, TextFragment}, + input::keyboard::{self, KeyCode}, Context, GameResult, }; use glam::Vec2; @@ -46,28 +45,23 @@ pub fn run_rendering(world: &World, context: &mut Context) { // Render any text let mut query = world.query::<&Gameplay>(); let gameplay = query.iter().next().unwrap().1; - draw_text(context, &gameplay.state.to_string(), 525.0, 80.0); - draw_text(context, &gameplay.moves_count.to_string(), 525.0, 100.0); + draw_text(&mut canvas, &gameplay.state.to_string(), 525.0, 80.0); + draw_text(&mut canvas, &gameplay.moves_count.to_string(), 525.0, 100.0); // Finally, present the canvas, this will actually display everything // on the screen. canvas.finish(context).expect("expected to present"); } -pub fn draw_text(context: &mut Context, text_string: &str, x: f32, y: f32) { - let text = graphics::Text::new(text_string); - let destination = Vec2::new(x, y); - let color = Some(Color::new(0.0, 0.0, 0.0, 1.0)); - let dimensions = Vec2::new(0.0, 20.0); +pub fn draw_text(canvas: &mut Canvas, text_string: &str, x: f32, y: f32) { + let mut text = Text::new(TextFragment { + text: text_string.to_string(), + color: Some(Color::new(0.0, 0.0, 0.0, 1.0)), + scale: Some(PxScale::from(20.0)), + ..Default::default() + }); - graphics::queue_text(context, &text, dimensions, color); - graphics::draw_queued_text( - context, - graphics::DrawParam::new().dest(destination), - None, - graphics::FilterMode::Linear, - ) - .expect("expected drawing queued text"); + canvas.draw(&text, Vec2::new(x, y)); } pub fn get_image(context: &mut Context, renderable: &Renderable, delta: Duration) -> Image { diff --git a/code/rust-sokoban-c03-04/src/systems/rendering.rs b/code/rust-sokoban-c03-04/src/systems/rendering.rs index e0ef963..d774393 100644 --- a/code/rust-sokoban-c03-04/src/systems/rendering.rs +++ b/code/rust-sokoban-c03-04/src/systems/rendering.rs @@ -1,8 +1,7 @@ use ggez::{ - conf, - event::{self, KeyCode}, - graphics::{self, Color, DrawParam, Image}, - input::keyboard, + conf, event, + graphics::{self, Canvas, Color, DrawParam, Image, PxScale, Text, TextFragment}, + input::keyboard::{self, KeyCode}, Context, GameResult, }; use glam::Vec2; @@ -46,28 +45,23 @@ pub fn run_rendering(world: &World, context: &mut Context) { // Render any text let mut query = world.query::<&Gameplay>(); let gameplay = query.iter().next().unwrap().1; - draw_text(context, &gameplay.state.to_string(), 525.0, 80.0); - draw_text(context, &gameplay.moves_count.to_string(), 525.0, 100.0); + draw_text(&mut canvas, &gameplay.state.to_string(), 525.0, 80.0); + draw_text(&mut canvas, &gameplay.moves_count.to_string(), 525.0, 100.0); // Finally, present the canvas, this will actually display everything // on the screen. canvas.finish(context).expect("expected to present"); } -pub fn draw_text(context: &mut Context, text_string: &str, x: f32, y: f32) { - let text = graphics::Text::new(text_string); - let destination = Vec2::new(x, y); - let color = Some(Color::new(0.0, 0.0, 0.0, 1.0)); - let dimensions = Vec2::new(0.0, 20.0); +pub fn draw_text(canvas: &mut Canvas, text_string: &str, x: f32, y: f32) { + let mut text = Text::new(TextFragment { + text: text_string.to_string(), + color: Some(Color::new(0.0, 0.0, 0.0, 1.0)), + scale: Some(PxScale::from(20.0)), + ..Default::default() + }); - graphics::queue_text(context, &text, dimensions, color); - graphics::draw_queued_text( - context, - graphics::DrawParam::new().dest(destination), - None, - graphics::FilterMode::Linear, - ) - .expect("expected drawing queued text"); + canvas.draw(&text, Vec2::new(x, y)); } pub fn get_image(context: &mut Context, renderable: &Renderable, delta: Duration) -> Image { diff --git a/code/rust-sokoban-c03-05/src/systems/rendering.rs b/code/rust-sokoban-c03-05/src/systems/rendering.rs index 709d6fa..d92f403 100644 --- a/code/rust-sokoban-c03-05/src/systems/rendering.rs +++ b/code/rust-sokoban-c03-05/src/systems/rendering.rs @@ -73,8 +73,8 @@ pub fn run_rendering(world: &World, context: &mut Context) { // Render any text let mut query = world.query::<&Gameplay>(); let gameplay = query.iter().next().unwrap().1; - draw_text(context, &gameplay.state.to_string(), 525.0, 80.0); - draw_text(context, &gameplay.moves_count.to_string(), 525.0, 100.0); + draw_text(&mut canvas, &gameplay.state.to_string(), 525.0, 80.0); + draw_text(&mut canvas, &gameplay.moves_count.to_string(), 525.0, 100.0); // Render FPS let fps = format!("FPS: {:.0}", timer::fps(context)); @@ -85,20 +85,15 @@ pub fn run_rendering(world: &World, context: &mut Context) { canvas.finish(context).expect("expected to present"); } -pub fn draw_text(context: &mut Context, text_string: &str, x: f32, y: f32) { - let text = graphics::Text::new(text_string); - let destination = Vec2::new(x, y); - let color = Some(Color::new(0.0, 0.0, 0.0, 1.0)); - let dimensions = Vec2::new(0.0, 20.0); - - graphics::queue_text(context, &text, dimensions, color); - graphics::draw_queued_text( - context, - graphics::DrawParam::new().dest(destination), - None, - graphics::FilterMode::Linear, - ) - .expect("expected drawing queued text"); +pub fn draw_text(canvas: &mut Canvas, text_string: &str, x: f32, y: f32) { + let mut text = Text::new(TextFragment { + text: text_string.to_string(), + color: Some(Color::new(0.0, 0.0, 0.0, 1.0)), + scale: Some(PxScale::from(20.0)), + ..Default::default() + }); + + canvas.draw(&text, Vec2::new(x, y)); } pub fn get_image(context: &mut Context, renderable: &Renderable, delta: Duration) -> String {