Add Color trait

This commit is contained in:
grovesNL 2022-05-09 22:40:00 -02:30
parent 63a1a8f7ce
commit 5605879164
2 changed files with 35 additions and 16 deletions

View file

@ -3,11 +3,12 @@ use glyphon::{
layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle}, layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle},
Font, FontSettings, Font, FontSettings,
}, },
Resolution, TextRenderer, Color, Resolution, TextRenderer,
}; };
use wgpu::{ use wgpu::{
Color, CommandEncoderDescriptor, LoadOp, Operations, RenderPassColorAttachment, Backends, CommandEncoderDescriptor, DeviceDescriptor, Features, Instance, Limits, LoadOp,
RenderPassDescriptor, TextureViewDescriptor, Operations, PresentMode, RenderPassColorAttachment, RenderPassDescriptor,
RequestAdapterOptions, SurfaceConfiguration, TextureUsages, TextureViewDescriptor,
}; };
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
@ -19,18 +20,27 @@ fn main() {
pollster::block_on(run()); pollster::block_on(run());
} }
#[derive(Clone, Copy)]
struct UserData;
impl Color for UserData {
fn color(&self) -> [u8; 4] {
[255, 255, 0, 255]
}
}
async fn run() { async fn run() {
let instance = wgpu::Instance::new(wgpu::Backends::all()); let instance = Instance::new(Backends::all());
let adapter = instance let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions::default()) .request_adapter(&RequestAdapterOptions::default())
.await .await
.unwrap(); .unwrap();
let (device, queue) = adapter let (device, queue) = adapter
.request_device( .request_device(
&wgpu::DeviceDescriptor { &DeviceDescriptor {
label: None, label: None,
features: wgpu::Features::empty(), features: Features::empty(),
limits: wgpu::Limits::downlevel_defaults(), limits: Limits::downlevel_defaults(),
}, },
None, None,
) )
@ -42,12 +52,12 @@ async fn run() {
let surface = unsafe { instance.create_surface(&window) }; let surface = unsafe { instance.create_surface(&window) };
let size = window.inner_size(); let size = window.inner_size();
let swapchain_format = surface.get_preferred_format(&adapter).unwrap(); let swapchain_format = surface.get_preferred_format(&adapter).unwrap();
let mut config = wgpu::SurfaceConfiguration { let mut config = SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT, usage: TextureUsages::RENDER_ATTACHMENT,
format: swapchain_format, format: swapchain_format,
width: size.width, width: size.width,
height: size.height, height: size.height,
present_mode: wgpu::PresentMode::Mailbox, present_mode: PresentMode::Mailbox,
}; };
surface.configure(&device, &config); surface.configure(&device, &config);
@ -81,7 +91,12 @@ async fn run() {
layout.append( layout.append(
fonts.as_slice(), fonts.as_slice(),
&TextStyle::new("Hello world!\nI'm on a new line!", 50.0, 0), &TextStyle::with_user_data(
"Hello world!\nI'm on a new line!",
50.0,
0,
UserData {},
),
); );
text_renderer text_renderer
@ -108,7 +123,7 @@ async fn run() {
view: &view, view: &view,
resolve_target: None, resolve_target: None,
ops: Operations { ops: Operations {
load: LoadOp::Clear(Color::BLACK), load: LoadOp::Clear(wgpu::Color::BLACK),
store: true, store: true,
}, },
}], }],

View file

@ -28,6 +28,10 @@ use wgpu::{
pub use fontdue; pub use fontdue;
pub trait Color: Copy {
fn color(&self) -> [u8; 4];
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PrepareError { pub enum PrepareError {
AtlasFull, AtlasFull,
@ -86,7 +90,7 @@ pub struct Params {
fn try_allocate( fn try_allocate(
atlas_packer: &mut BucketedAtlasAllocator, atlas_packer: &mut BucketedAtlasAllocator,
layout: &Layout, layout: &Layout<impl Color>,
glyph_cache: &mut HashMap<GlyphRasterConfig, GlyphDetails>, glyph_cache: &mut HashMap<GlyphRasterConfig, GlyphDetails>,
width: usize, width: usize,
height: usize, height: usize,
@ -346,7 +350,7 @@ impl TextRenderer {
queue: &Queue, queue: &Queue,
screen_resolution: Resolution, screen_resolution: Resolution,
fonts: &[Font], fonts: &[Font],
layouts: &[&Layout], layouts: &[&Layout<impl Color>],
) -> Result<(), PrepareError> { ) -> Result<(), PrepareError> {
if screen_resolution != self.params.screen_resolution { if screen_resolution != self.params.screen_resolution {
self.params.screen_resolution = screen_resolution; self.params.screen_resolution = screen_resolution;
@ -485,7 +489,7 @@ impl TextRenderer {
pos: [glyph.x.round() as u32, glyph.y.round() as u32], pos: [glyph.x.round() as u32, glyph.y.round() as u32],
dim: [details.width, details.height], dim: [details.width, details.height],
uv: [atlas_x, atlas_y], uv: [atlas_x, atlas_y],
color: [255, 255, 0, 255], color: glyph.user_data.color(),
}) })
.take(4), .take(4),
); );