parent
7e2983374e
commit
296c99f059
4 changed files with 19 additions and 1 deletions
|
@ -3,6 +3,7 @@ use std::{
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// An error that occurred while preparing text for rendering.
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub enum PrepareError {
|
pub enum PrepareError {
|
||||||
AtlasFull,
|
AtlasFull,
|
||||||
|
@ -16,6 +17,7 @@ impl Display for PrepareError {
|
||||||
|
|
||||||
impl Error for PrepareError {}
|
impl Error for PrepareError {}
|
||||||
|
|
||||||
|
/// An error that occurred while rendering text.
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub enum RenderError {
|
pub enum RenderError {
|
||||||
RemovedFromAtlas,
|
RemovedFromAtlas,
|
||||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -11,15 +11,22 @@ use recently_used::RecentlyUsedMap;
|
||||||
pub use text_atlas::TextAtlas;
|
pub use text_atlas::TextAtlas;
|
||||||
pub use text_render::TextRenderer;
|
pub use text_render::TextRenderer;
|
||||||
|
|
||||||
|
/// The color to use when rendering text.
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct Color {
|
pub struct Color {
|
||||||
|
/// The red component of the color.
|
||||||
pub r: u8,
|
pub r: u8,
|
||||||
|
/// The green component of the color.
|
||||||
pub g: u8,
|
pub g: u8,
|
||||||
|
/// The blue component of the color.
|
||||||
pub b: u8,
|
pub b: u8,
|
||||||
|
/// The alpha component of the color.
|
||||||
pub a: u8,
|
pub a: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Allows text to be colored during rendering.
|
||||||
pub trait HasColor: Copy {
|
pub trait HasColor: Copy {
|
||||||
|
/// The color to use when rendering text.
|
||||||
fn color(&self) -> Color;
|
fn color(&self) -> Color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,16 +51,19 @@ pub(crate) struct GlyphToRender {
|
||||||
color: [u8; 4],
|
color: [u8; 4],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The screen resolution to use when rendering text.
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub struct Resolution {
|
pub struct Resolution {
|
||||||
|
/// The width of the screen in pixels.
|
||||||
pub width: u32,
|
pub width: u32,
|
||||||
|
/// The height of the screen in pixels.
|
||||||
pub height: u32,
|
pub height: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub struct Params {
|
pub(crate) struct Params {
|
||||||
screen_resolution: Resolution,
|
screen_resolution: Resolution,
|
||||||
_pad: [u32; 2],
|
_pad: [u32; 2],
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ use wgpu::{
|
||||||
|
|
||||||
use crate::{GlyphDetails, GlyphToRender, Params, RecentlyUsedMap, Resolution};
|
use crate::{GlyphDetails, GlyphToRender, Params, RecentlyUsedMap, Resolution};
|
||||||
|
|
||||||
|
/// An atlas containing a cache of rasterized glyphs that can be rendered.
|
||||||
pub struct TextAtlas {
|
pub struct TextAtlas {
|
||||||
pub(crate) texture_pending: Vec<u8>,
|
pub(crate) texture_pending: Vec<u8>,
|
||||||
pub(crate) texture: Texture,
|
pub(crate) texture: Texture,
|
||||||
|
@ -27,6 +28,7 @@ pub struct TextAtlas {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextAtlas {
|
impl TextAtlas {
|
||||||
|
/// Creates a new `TextAtlas`.
|
||||||
pub fn new(device: &Device, _queue: &Queue, format: TextureFormat) -> Self {
|
pub fn new(device: &Device, _queue: &Queue, format: TextureFormat) -> Self {
|
||||||
let max_texture_dimension_2d = device.limits().max_texture_dimension_2d;
|
let max_texture_dimension_2d = device.limits().max_texture_dimension_2d;
|
||||||
let width = max_texture_dimension_2d;
|
let width = max_texture_dimension_2d;
|
||||||
|
|
|
@ -14,6 +14,7 @@ use crate::{
|
||||||
TextAtlas, TextOverflow,
|
TextAtlas, TextOverflow,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// A text renderer that uses cached glyphs to render text into an existing render pass.
|
||||||
pub struct TextRenderer {
|
pub struct TextRenderer {
|
||||||
vertex_buffer: Buffer,
|
vertex_buffer: Buffer,
|
||||||
vertex_buffer_size: u64,
|
vertex_buffer_size: u64,
|
||||||
|
@ -25,6 +26,7 @@ pub struct TextRenderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextRenderer {
|
impl TextRenderer {
|
||||||
|
/// Creates a new `TextRenderer`.
|
||||||
pub fn new(device: &Device, _queue: &Queue) -> Self {
|
pub fn new(device: &Device, _queue: &Queue) -> Self {
|
||||||
let vertex_buffer_size = next_copy_buffer_size(4096);
|
let vertex_buffer_size = next_copy_buffer_size(4096);
|
||||||
let vertex_buffer = device.create_buffer(&BufferDescriptor {
|
let vertex_buffer = device.create_buffer(&BufferDescriptor {
|
||||||
|
@ -56,6 +58,7 @@ impl TextRenderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Prepares all of the provided layouts for rendering.
|
||||||
pub fn prepare(
|
pub fn prepare(
|
||||||
&mut self,
|
&mut self,
|
||||||
device: &Device,
|
device: &Device,
|
||||||
|
@ -344,6 +347,7 @@ impl TextRenderer {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Renders all layouts that were previously provided to `prepare`.
|
||||||
pub fn render<'pass>(
|
pub fn render<'pass>(
|
||||||
&'pass mut self,
|
&'pass mut self,
|
||||||
atlas: &'pass TextAtlas,
|
atlas: &'pass TextAtlas,
|
||||||
|
|
Loading…
Reference in a new issue