Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to draw vector of Mesh => ggez::graphics::mesh::Mesh is private module #787

Closed
laticoda opened this issue Jun 28, 2020 · 4 comments
Closed
Labels

Comments

@laticoda
Copy link

I dont know how to work with a vector of meshes to draw it

i got this error
let mut vec_of_mesh: Vecggez::graphics::mesh::Mesh = Vec::new();
| ^^^^ private module

vec_of_mesh.push(mb.line(foo_A, foo_B, foo_C).unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct ggez::graphics::mesh::Mesh, found &mut ggez::graphics::mesh::MeshBuilder

my code

struct MainState {   
    meshes: Vec<ggez::graphics::Mesh>,
}

impl EventHandler for MainState {
fn draw(&mut self, ctx: &mut ggez::Context) -> ggez::GameResult {
        
        graphics::clear(ctx, Color::new(0.0, 0.0, 0.0, 1.0));

        let mb = &mut graphics::MeshBuilder::new();        

        let foo_A = &[
                Point2::new(200.0, 200.0),
                Point2::new(400.0, 200.0),
                Point2::new(400.0, 400.0),
                Point2::new(200.0, 400.0),
                Point2::new(200.0, 300.0),
            ];

        let foo_B = 4.0;
        let foo_C = Color::new(1.0, 0.0, 0.0, 1.0);
        let mut vec_of_mesh: Vec<ggez::graphics::mesh::Mesh> = Vec::new();
        vec_of_mesh.push(mb.line(foo_A, foo_B, foo_C).unwrap());
        for m in &vec_of_mesh {
            graphics::draw(ctx, m, graphics::DrawParam::new())?;
        }
        graphics::present(ctx).expect("error presenting");
        Ok(())
    }
@BrettWitty
Copy link
Contributor

Did you forget to run build(ctx) to create the mesh?

In particular I think the line with the error needs to be:
vec_of_mesh.push(mb.line(foo_A, foo_B, foo_C).build(ctx).unwrap());

@laticoda
Copy link
Author

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5dd2db33cf493c4efe410688b8273b73

line 53 private method
line 56
**method not found in std::result::Result<&mut ggez::graphics::mesh::MeshBuilder, ggez::error::GameError>
**

@Manghi
Copy link
Contributor

Manghi commented Jun 30, 2020

There are two issues I see:

  1. The mesh module is purposefully private so that users of this library can't access it; it is only public if accessed within the ggez crate. That is why it's marked pub(crate) in mod.rs (see here). But the developers know that you need to access the goodies within so they still let you do so without going into the mesh module. All of the goodies are made public because of line 58 pub use crate::graphics::mesh::*; (see here). This enables you to access the Mesh data structure by going through the graphics module.

  2. mb.line(foo_A, foo_B, foo_C) returns a Result<MeshBuilder, GameError>, so you need to handle the Result first before you can call build(ctx) on it. The try operator ? is some syntactic sugar to help you with this. It would be easier to see the flow of things if you split line 56 in your example into three lines for clarity.

All together:

let mut vec_of_mesh: Vec<graphics::Mesh> = Vec::new(); // use graphics::Mesh and not graphics::mesh::Mesh

let mb = mb.line(foo_A, foo_B, foo_C)?; // Handle the result first using '?'
let built_mesh = mb.build(ctx)?;        // Then build the mesh (and yet again handle the result)
vec_of_mesh.push(built_mesh);           // Finally store it in your vector

Hope this helps.

One other small thing. You have both

use ggez::graphics;
...
use ggez::graphics::{Color, DrawMode};

but you can shorten this to

use ggez::graphics::{self, Color, DrawMode};

I would recommend referring to these resources if you want to learn more:
https://doc.rust-lang.org/reference/visibility-and-privacy.html
https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/the-question-mark-operator-for-easier-error-handling.html

@laticoda
Copy link
Author

laticoda commented Jul 3, 2020

thank you everybody

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants