-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add drawable implementation * Add text rendering * Fix inline and impl * Fix imports * Update README.md
- Loading branch information
1 parent
b09562f
commit 92ac570
Showing
11 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! Demonstrates drawing text on the canvas | ||
|
||
use qilin::game::context::GameContext; | ||
use qilin::game::game::Game; | ||
use qilin::render::canvas::Canvas; | ||
use qilin::render::color::Color; | ||
|
||
use qilin::scene::Scene; | ||
use qilin::simplified::vec2; | ||
use qilin::text::TextSketch; | ||
use qilin::types::{GameConfig, FPS30}; | ||
use qilin::ScaleMode; | ||
use qilin::WindowOptions; | ||
|
||
struct TextScene; | ||
|
||
impl Scene for TextScene { | ||
// create new empty scene | ||
fn new() -> Self | ||
where | ||
Self: Sized, | ||
{ | ||
Self | ||
} | ||
|
||
// gets called when game enters current scene | ||
fn enter(&mut self) { println!("What do you call a fake noodle?") } | ||
|
||
// gets called when window requests draw updates | ||
fn update(&mut self, canvas: &mut Canvas, _ctx: &mut GameContext) { | ||
canvas.drawable( | ||
&TextSketch::new(vec2(10, 10), include_bytes!("assets/Roboto-Medium.ttf")) | ||
.with_color(Color::RED) | ||
.with_text("Hello World!", 30.0), | ||
); | ||
|
||
canvas.drawable( | ||
&TextSketch::new(vec2(10, 100), include_bytes!("assets/Roboto-Medium.ttf")) | ||
.with_color(Color::YELLOW) | ||
.with_text("Implementing text-rendering was real pain.", 40.0), | ||
); | ||
} | ||
|
||
fn fixed_update(&mut self, _canvas: &mut Canvas, _ctx: &mut GameContext) { | ||
// Will be called X times per second. | ||
// This ensures, physics are applied independent of frame-rate. | ||
// See https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html for FixedUpdate() in Unity. | ||
} | ||
|
||
// gets called when game exits current scene | ||
fn exit(&mut self) { println!("An impasta!") } | ||
} | ||
|
||
fn main() { | ||
Game::new::<TextScene>() // create game object with ShapeScene as entry scene | ||
.with_config(GameConfig { | ||
title: "My Texts".to_string(), // set window title | ||
update_rate_limit: FPS30, // limit update rate to 30 fps, default is 60 fps | ||
width: 800, // set initial width | ||
height: 600, // set initial height | ||
fixed_time_step: Default::default(), // for better docs, see GameConfig or examples/move. | ||
window: WindowOptions { | ||
scale_mode: ScaleMode::AspectRatioStretch, // scale pixels to fit in aspect ratio | ||
resize: true, // make window resizeable | ||
..Default::default() | ||
}, | ||
}) | ||
.play() | ||
.expect("Failed to play game"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use crate::render::canvas::Canvas; | ||
use crate::render::color::Color; | ||
use crate::render::sketch::Drawable; | ||
use fontdue::layout::{CoordinateSystem, Layout, TextStyle}; | ||
use fontdue::Font; | ||
use mint::Vector2; | ||
|
||
/// Sketch to draw text on a canvas. | ||
pub struct TextSketch { | ||
layout: Layout, | ||
font: Font, | ||
color: Color, | ||
pos: Vector2<u32>, | ||
} | ||
|
||
impl TextSketch { | ||
/// Create a new empty [TextSketch] with font data. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// use mint::Vector2; | ||
/// use qilin::text::TextSketch; | ||
/// | ||
/// let font = include_bytes!("myAssets/myFont.ttf"); | ||
/// let sketch = TextSketch::new(Vector2::new(10, 10), font); | ||
/// ``` | ||
#[inline] | ||
pub fn new(pos: Vector2<u32>, font: &[u8]) -> Self { | ||
Self { | ||
layout: Layout::new(CoordinateSystem::PositiveYDown), | ||
color: Color::BLACK, | ||
font: Font::from_bytes(font, Default::default()).expect("Failed to load font"), | ||
pos, | ||
} | ||
} | ||
|
||
/// Set the color of the text. | ||
#[inline] | ||
pub fn with_color(mut self, color: Color) -> Self { | ||
self.color = color; | ||
self | ||
} | ||
|
||
/// Append new text to the layout with `text` as text and `px` as size in pixels. | ||
#[inline] | ||
pub fn with_text(mut self, text: &str, px: f32) -> Self { | ||
self.layout | ||
.append(&[self.font.clone()], &TextStyle::new(text, px, 0)); | ||
self | ||
} | ||
} | ||
|
||
impl Drawable for TextSketch { | ||
#[inline] | ||
fn apply(&self, canvas: &mut Canvas) { | ||
self.layout.glyphs().iter().for_each(|glyph| { | ||
let (metrics, buffer) = self.font.rasterize_config(glyph.key); | ||
|
||
let width = metrics.width; | ||
let height = metrics.height; | ||
|
||
// Calculate the starting position to draw the glyph | ||
let x0 = self.pos.x + glyph.x as u32; | ||
let y0 = self.pos.y + glyph.y as u32; | ||
|
||
// Draw the glyph to the canvas using the specified color | ||
for y in 0..height { | ||
for x in 0..width { | ||
let alpha = buffer[y * width + x] as u8; | ||
if alpha > 0 { | ||
let x_coord = x0 + x as u32; | ||
let y_coord = y0 + y as u32; | ||
canvas.set_pixel(x_coord as usize, y_coord as usize, &self.color); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |