veeenu / hudhook

A videogame overlay framework written in Rust, supporting DirectX and OpenGL
MIT License
167 stars 27 forks source link

Question about loading and using font #178

Closed ruby3141 closed 3 months ago

ruby3141 commented 3 months ago

Try loading font on ImguiRenderLoop.initialize() and holding FontId on ImguiRenderLoop struct and got this kind of compile error.

error[E0277]: `*const Font` cannot be shared between threads safely
   --> some_random_crate\src\lib.rs:11:1
    |
11  | hudhook::hudhook!(ImguiOpenGl3Hooks, StructImplImguiRenderLoop::new());
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------^
    | |                                    |
    | |                                    this tail expression is of type `StructImplImguiRenderLoop`
    | `*const Font` cannot be shared between threads safely
    | required by a bound introduced by this call
    |
    = help: within `std::option::Option<FontId>`, the trait `Sync` is not implemented for `*const Font`

Also tried to get FontId from ui.fonts().fonts() but it doesn't work. (regardless of the index value)

let font_ids = ui.fonts().fonts();
let font = font_ids.get(1).unwrap();
let font_token = ui.push_font(*font);

How can I load my font and use it? I spend about a day finding solution but ran out of idea.

ruby3141 commented 3 months ago

Found out latter one was actually working. \ There was a unrelated mistake elsewhere and I didn't found it before writing this.

It doesn't seems like right approach to me tho...

veeenu commented 3 months ago

ImguiRenderLoop needs to be Send and Sync as we do not have further information about the threads it may be called upon. This makes it impossible to store raw pointers in there.

You do not need to necessarily hold on to that pointer directly. You can just hold on to the index in the font atlas and retrieve the font on each frame like you are doing above. If you really want to hold on to the FontId, you can unsafely implement Send and Sync for your structure.

This is how I do this myself:

https://github.com/veeenu/darksoulsiii-practice-tool/blob/master/practice-tool/src/practice_tool.rs#L23-L30 https://github.com/veeenu/darksoulsiii-practice-tool/blob/master/practice-tool/src/practice_tool.rs#L479

ruby3141 commented 3 months ago

Thank you for kind answer.