Skip to content

Commit

Permalink
Fix text rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
iolivia committed Dec 13, 2024
1 parent feeedb4 commit 5ce83b8
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions code/rust-sokoban-c02-05/src/systems/rendering.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}

0 comments on commit 5ce83b8

Please sign in to comment.