Skip to content

Instantly share code, notes, and snippets.

@eduardonunesp
Created January 4, 2022 16:27
Show Gist options
  • Save eduardonunesp/fd76b27007bde1df207dc331b9b7b6c8 to your computer and use it in GitHub Desktop.
Save eduardonunesp/fd76b27007bde1df207dc331b9b7b6c8 to your computer and use it in GitHub Desktop.
use std::env;
use std::path;
use ggez::event::EventHandler;
use ggez::{event, graphics, Context, GameResult};
const GRID_SIZE: (usize, usize) = (10, 10);
const GRID_CELL_SIZE: (usize, usize) = (64, 64);
const SCREEN_SIZE: (f32, f32) = (
GRID_SIZE.0 as f32 * GRID_CELL_SIZE.0 as f32,
GRID_SIZE.1 as f32 * GRID_CELL_SIZE.1 as f32,
);
struct GameState {
// Your state here...
}
impl GameState {
pub fn new(_ctx: &mut Context) -> GameResult<GameState> {
// Load/create resources such as images here.
Ok(GameState {
// ...
})
}
}
impl EventHandler for GameState {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
// Update code here...
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
graphics::clear(ctx, graphics::BLACK);
// Draw code here...
graphics::present(ctx)
}
}
fn main() -> GameResult {
let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
let mut path = path::PathBuf::from(manifest_dir);
path.push("resources");
path
} else {
path::PathBuf::from("./resources")
};
let (ctx, events_loop) =
&mut ggez::ContextBuilder::new("game-name", "[email protected]")
.window_setup(ggez::conf::WindowSetup::default().title("game-name"))
.window_mode(ggez::conf::WindowMode::default().dimensions(SCREEN_SIZE.0, SCREEN_SIZE.1))
.backend(ggez::conf::Backend::OpenGL { major: 3, minor: 2 })
.add_resource_path(resource_dir)
.build()?;
let state = &mut GameState::new(ctx)?;
event::run(ctx, events_loop, state)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment