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 5ce83b8 commit 441c884
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 93 deletions.
34 changes: 14 additions & 20 deletions code/rust-sokoban-c03-01/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 All @@ -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
Expand All @@ -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));
}
32 changes: 13 additions & 19 deletions code/rust-sokoban-c03-02/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 @@ -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 {
Expand Down
32 changes: 13 additions & 19 deletions code/rust-sokoban-c03-03/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 @@ -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 {
Expand Down
32 changes: 13 additions & 19 deletions code/rust-sokoban-c03-04/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 @@ -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 {
Expand Down
27 changes: 11 additions & 16 deletions code/rust-sokoban-c03-05/src/systems/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 {
Expand Down

0 comments on commit 441c884

Please sign in to comment.