2023-01-29 22:04:01 -03:30
use glyphon ::{
2023-06-20 06:08:41 +02:00
Attrs , Buffer , Color , Family , FontSystem , Metrics , Resolution , Shaping , SwashCache , TextArea ,
TextAtlas , TextBounds , TextRenderer ,
2023-01-29 22:04:01 -03:30
} ;
2022-05-09 10:19:10 -02:30
use wgpu ::{
2023-04-08 04:15:59 +02:00
CommandEncoderDescriptor , CompositeAlphaMode , DeviceDescriptor , Features , Instance ,
InstanceDescriptor , Limits , LoadOp , MultisampleState , Operations , PresentMode ,
RenderPassColorAttachment , RenderPassDescriptor , RequestAdapterOptions , SurfaceConfiguration ,
TextureFormat , TextureUsages , TextureViewDescriptor ,
2022-05-09 10:19:10 -02:30
} ;
use winit ::{
2022-10-28 00:44:15 -02:30
dpi ::LogicalSize ,
2022-05-09 10:19:10 -02:30
event ::{ Event , WindowEvent } ,
2024-01-15 16:03:02 -03:30
event_loop ::EventLoop ,
2022-10-28 00:44:15 -02:30
window ::WindowBuilder ,
2022-05-09 10:19:10 -02:30
} ;
fn main ( ) {
pollster ::block_on ( run ( ) ) ;
}
async fn run ( ) {
2022-10-28 00:44:15 -02:30
// Set up window
let ( width , height ) = ( 800 , 600 ) ;
2024-01-15 16:03:02 -03:30
let event_loop = EventLoop ::new ( ) . unwrap ( ) ;
2022-10-28 00:44:15 -02:30
let window = WindowBuilder ::new ( )
. with_inner_size ( LogicalSize ::new ( width as f64 , height as f64 ) )
. with_title ( " glyphon hello world " )
. build ( & event_loop )
. unwrap ( ) ;
let size = window . inner_size ( ) ;
let scale_factor = window . scale_factor ( ) ;
// Set up surface
2023-04-08 04:15:59 +02:00
let instance = Instance ::new ( InstanceDescriptor ::default ( ) ) ;
2022-05-09 10:19:10 -02:30
let adapter = instance
2022-05-09 22:40:00 -02:30
. request_adapter ( & RequestAdapterOptions ::default ( ) )
2022-05-09 10:19:10 -02:30
. await
. unwrap ( ) ;
let ( device , queue ) = adapter
. request_device (
2022-05-09 22:40:00 -02:30
& DeviceDescriptor {
2022-05-09 10:19:10 -02:30
label : None ,
2022-05-09 22:40:00 -02:30
features : Features ::empty ( ) ,
limits : Limits ::downlevel_defaults ( ) ,
2022-05-09 10:19:10 -02:30
} ,
None ,
)
. await
. unwrap ( ) ;
2023-04-08 04:15:59 +02:00
let surface = unsafe { instance . create_surface ( & window ) } . expect ( " Create surface " ) ;
2023-02-04 13:22:39 +01:00
let swapchain_format = TextureFormat ::Bgra8UnormSrgb ;
2022-05-09 22:40:00 -02:30
let mut config = SurfaceConfiguration {
usage : TextureUsages ::RENDER_ATTACHMENT ,
2022-05-09 10:19:10 -02:30
format : swapchain_format ,
width : size . width ,
height : size . height ,
2022-11-28 02:23:10 -03:30
present_mode : PresentMode ::Fifo ,
2022-10-18 13:02:31 -02:30
alpha_mode : CompositeAlphaMode ::Opaque ,
2023-04-08 04:15:59 +02:00
view_formats : vec ! [ ] ,
2022-05-09 10:19:10 -02:30
} ;
surface . configure ( & device , & config ) ;
2022-10-28 00:44:15 -02:30
// Set up text renderer
2023-03-19 15:05:53 +01:00
let mut font_system = FontSystem ::new ( ) ;
let mut cache = SwashCache ::new ( ) ;
2022-06-03 00:38:59 -02:30
let mut atlas = TextAtlas ::new ( & device , & queue , swapchain_format ) ;
2023-02-13 22:35:04 -03:30
let mut text_renderer =
TextRenderer ::new ( & mut atlas , & device , MultisampleState ::default ( ) , None ) ;
2023-03-19 15:05:53 +01:00
let mut buffer = Buffer ::new ( & mut font_system , Metrics ::new ( 30.0 , 42.0 ) ) ;
2023-01-26 00:44:29 -03:30
2023-03-19 15:05:53 +01:00
let physical_width = ( width as f64 * scale_factor ) as f32 ;
let physical_height = ( height as f64 * scale_factor ) as f32 ;
2023-01-26 00:44:29 -03:30
2023-03-19 15:05:53 +01:00
buffer . set_size ( & mut font_system , physical_width , physical_height ) ;
2023-06-20 06:08:41 +02:00
buffer . set_text ( & mut font_system , " Hello world! 👋 \n This is rendered with 🦅 glyphon 🦁 \n The text below should be partially clipped. \n a b c d e f g h i j k l m n o p q r s t u v w x y z " , Attrs ::new ( ) . family ( Family ::SansSerif ) , Shaping ::Advanced ) ;
2023-03-19 15:05:53 +01:00
buffer . shape_until_scroll ( & mut font_system ) ;
2022-05-09 10:19:10 -02:30
2024-01-15 16:03:02 -03:30
event_loop
. run ( move | event , target | {
if let Event ::WindowEvent {
window_id : _ ,
event ,
} = event
{
match event {
WindowEvent ::Resized ( size ) = > {
config . width = size . width ;
config . height = size . height ;
surface . configure ( & device , & config ) ;
window . request_redraw ( ) ;
}
WindowEvent ::RedrawRequested = > {
text_renderer
. prepare (
& device ,
& queue ,
& mut font_system ,
& mut atlas ,
Resolution {
width : config . width ,
height : config . height ,
} ,
[ TextArea {
buffer : & buffer ,
left : 10.0 ,
top : 10.0 ,
scale : 1.0 ,
bounds : TextBounds {
left : 0 ,
top : 0 ,
right : 600 ,
bottom : 160 ,
} ,
default_color : Color ::rgb ( 255 , 255 , 255 ) ,
} ] ,
& mut cache ,
)
. unwrap ( ) ;
2022-05-09 10:19:10 -02:30
2024-01-15 16:03:02 -03:30
let frame = surface . get_current_texture ( ) . unwrap ( ) ;
let view = frame . texture . create_view ( & TextureViewDescriptor ::default ( ) ) ;
let mut encoder = device
. create_command_encoder ( & CommandEncoderDescriptor { label : None } ) ;
{
let mut pass = encoder . begin_render_pass ( & RenderPassDescriptor {
label : None ,
color_attachments : & [ Some ( RenderPassColorAttachment {
view : & view ,
resolve_target : None ,
ops : Operations {
load : LoadOp ::Clear ( wgpu ::Color ::BLACK ) ,
store : wgpu ::StoreOp ::Store ,
} ,
} ) ] ,
depth_stencil_attachment : None ,
timestamp_writes : None ,
occlusion_query_set : None ,
} ) ;
2022-05-09 10:19:10 -02:30
2024-01-15 16:03:02 -03:30
text_renderer . render ( & atlas , & mut pass ) . unwrap ( ) ;
}
2022-05-09 10:19:10 -02:30
2024-01-15 16:03:02 -03:30
queue . submit ( Some ( encoder . finish ( ) ) ) ;
frame . present ( ) ;
2023-02-08 23:08:37 +01:00
2024-01-15 16:03:02 -03:30
atlas . trim ( ) ;
}
WindowEvent ::CloseRequested = > target . exit ( ) ,
_ = > { }
}
2022-05-09 10:19:10 -02:30
}
2024-01-15 16:03:02 -03:30
} )
. unwrap ( ) ;
2022-05-09 10:19:10 -02:30
}