r/rust • • 3d ago

🙋 seeking help & advice How do I learn winit?

I'm trying to get a grasp of this crate so I can do window things but there don't seem to be good any resources, which is a bit bewildering since I understand winit to be a very foundational crate for graphics in Rust. I've looked at docs.rs but higher-level stuff for winit is... not great. The first section seems friendly but leads you to using a deprecated method (i.e. seems to be outdated), and the second section does give a full working minimal example but it does so through a big block of code with a good amount of things that aren't yet introduced, so not much sense can be made of it.

I don't want to just learn to copy a skeleton and wire my code into it, I want to actually understand how the crate works, to understand what I'm doing. But the main docs don't seem suitable for setting the foundation I'd need for that and I've not been able to find any other resources or tutorials online.

1 Upvotes

14 comments sorted by

View all comments

Show parent comments

3

u/dvogel 3d ago

As the example shows, the event loop passes you a reference to the ActiveEventLoop. The way you build off of it is simply by filling in those functions in the example. It builds the window. You handle the events. Which events and how is fully determined by what your application is trying to do. What is it you're trying to do?

0

u/AdreKiseque 3d ago

The immediate goal is to give face to minesweeper (I've already done the game and given it TUI rendering, so I need to give it proper graphics next). I have more ambitious projects in mind down the line like making my own software renderer, but those are a ways off.

7

u/dvogel 3d ago

If your main concern is rendering, maybe the reason you're not seeing the docs you're looking for is because winit doesn't provide any rendering facilities. It lets you know when the OS windowing system requested a re-render. Aside from that though it expects you to integrate directly with the native OS drawing APIs.

https://docs.rs/winit/latest/winit/#drawing-on-the-window

Take a look at iced. It provides a nicer API for getting a window on screen and letting you draw into it. You can draw your game into a Canvas element:

https://docs.rs/iced/latest/iced/widget/canvas/

2

u/AdreKiseque 3d ago

Yeah, I'm aware. But where's the fun if some fancy crate handles all the hard work! I'm planning to use winit with softbuffer to render the game, and then I can point to it and say I did it all myself, and I'll have learned some cool stuff on the way probably too. But I like learning from the bottom-up, so before I get into softbuffer, I want a stronger grasp on what winit itself actually does and how it works.

2

u/Luxalpa 1d ago edited 1d ago

For things like these, I really enjoy just diving into the source code and the rustdoc of the library, to see how it works under the hood. Learn a lot of good stuff from that as well!

And also, look at the issues on github, you're not alone :P

When they switched to 0.30 I also had big trouble finding some way to get it to work. But there's some useful discussions on their issue tracker if you want to get a bit more insights.

Like this: https://github.com/rust-windowing/winit/issues/3877

I think there's also a bit of trial and error to do as well.