Giter VIP home page Giter VIP logo

Comments (9)

buxx avatar buxx commented on May 29, 2024

I used a workaround : Declare egui_backend at each .draw method don't work (egui never draw). So, I use unsafe code to set egui_backend outside of my state (which is thread shared) : buxx/OpenCombat@2bb3f65

from ggegui.

NemuiSen avatar NemuiSen commented on May 29, 2024

I did not expect someone to use the gui asynchronously so I used Rc instead of Arc, when I have time I will make that little change

from ggegui.

buxx avatar buxx commented on May 29, 2024

Hello. Great news ! Thanks for your work.

from ggegui.

NemuiSen avatar NemuiSen commented on May 29, 2024

If it's not too much trouble, can you explain exactly why you use egui asynchronously and if possible, could you give me an example to test the changes I made (and maybe I'll add it to the examples folder)

from ggegui.

buxx avatar buxx commented on May 29, 2024

I use parallel iteration on the main state which own egui_backend. Maybe there is another way to organize data ? In any case, there is a minimal reproducible example :

Cargo.toml

[package]
name = "demo"
version = "0.1.0"
edition = "2021"

[dependencies]
ggez = "0.8.1"
ggez-egui = "0.3.1"
rayon = "1"

src/main.rs

use ggez::{
    event,
    glam::Vec2,
    graphics::{Canvas, Color, DrawParam},
    Context, GameResult,
};
use ggez_egui::{egui, EguiBackend};
use rayon::prelude::*;

struct Monster;

impl Monster {
    fn do_stuff(&self) {}
}

struct MainState {
    monsters: Vec<Monster>,
    egui_backend: EguiBackend,
}

impl MainState {
    fn new() -> GameResult<MainState> {
        Ok(Self {
            monsters: vec![Monster {}, Monster {}],
            egui_backend: EguiBackend::default(),
        })
    }
}

impl event::EventHandler<ggez::GameError> for MainState {
    fn update(&mut self, ctx: &mut Context) -> GameResult {
        let egui_ctx = self.egui_backend.ctx();
        egui::Window::new("egui-window").show(&egui_ctx, |_ui| {});
        self.egui_backend.update(ctx);

        // Do something requiring thread safe
        (0..self.monsters.len())
            .into_par_iter()
            .for_each(|i| self.update_monster(i));

        Ok(())
    }

    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = Canvas::from_frame(ctx, Color::BLACK);
        canvas.draw(
            &self.egui_backend,
            DrawParam::default().dest(Vec2::new(0., 0.)),
        );
        canvas.finish(ctx)?;
        Ok(())
    }
}

impl MainState {
    fn update_monster(&self, monster_i: usize) {}
}

pub fn main() -> GameResult {
    let cb = ggez::ContextBuilder::new("x", "");
    let (ctx, events_loop) = cb.build()?;
    let state = MainState::new().unwrap();
    event::run(ctx, events_loop, state)
}

from ggegui.

NemuiSen avatar NemuiSen commented on May 29, 2024

Now I understand what happened, how rust implements Sync to a struct when all its fields implement Sync too and EguiBackend used Rc, then it failed because Rc not implement Sync because cannot be shared safely between threads.
And I already tried your example and everything works fine, now I will only make some changes that occurred to me right now and if it doesn't take me too long I will publish the new version today or torrow.

from ggegui.

NemuiSen avatar NemuiSen commented on May 29, 2024

I'm done, but you will have to change in Cargo.toml and in the imports of the library from ggez-egui to ggegui since the idea of changing the name of the library occurred, without knowing what would happen. Anyway, try the new version and if the change worked, you can close the issue

from ggegui.

buxx avatar buxx commented on May 29, 2024

Thank you ! I will try it as soon as possible !

from ggegui.

buxx avatar buxx commented on May 29, 2024

It's perfectly working @NemuiSen ! I close the issue :)

from ggegui.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.