I am rather new to rust, so the following might also be a misunderstanding of a concept: I took the ggez basic project template from the basic project template which looks like this:
use ggez::{graphics, Context, ContextBuilder, GameResult};
use ggez::event::{self, EventHandler};
fn main() {
// Make a Context.
let (mut ctx, mut event_loop) = ContextBuilder::new("my_game", "Cool Game Author")
.build()
.expect("aieee, could not create ggez context!");
// Create an instance of your event handler.
// Usually, you should provide it with the Context object to
// use when setting your game up.
let mut my_game = MyGame::new(&mut ctx);
// Run!
match event::run(&mut ctx, &mut event_loop, &mut my_game) {
Ok(_) => println!("Exited cleanly."),
Err(e) => println!("Error occured: {}", e)
}
}
struct MyGame {
// Your state here...
}
impl MyGame {
pub fn new(_ctx: &mut Context) -> MyGame {
// Load/create resources such as images here.
MyGame {
// ...
}
}
}
impl EventHandler for MyGame {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
// Update code here...
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
graphics::clear(ctx, graphics::WHITE);
// Draw code here...
graphics::present(ctx)
}
}
And refactored it into two files main.rs and game.rs
The first just contained the main() function, everything else goes into game.rs.
After changing the import in main.rs to
mod game;
use game::MyGame;
use ggez::{graphics, Context, ContextBuilder, GameResult};
use ggez::event::{self, EventHandler};
and adding this to game.rs
use ggez::event::EventHandler;
use ggez::{Context, GameResult, graphics};
everything works provided I make MyGame public.
However, now the refactoring is making a huge change, as MyGame had been private before.
I tried several approaches with pub (in infinite_runner::main) and similar, like crate::main but none was accepted with various error messages.
Now my question is, is there a way of exposing MyGame to main.rs without exposing it to anyone else? It seems like main is not a valid target.