extern crate good_web_game as ggez;
use ggez::event;
use ggez::graphics;
use ggez::{Context, GameResult};
use std::env;
use std::path;
struct MainState {
frames: usize,
text: graphics::Text,
}
impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
let font = graphics::Font::new(ctx, "/LiberationMono-Regular.ttf")?;
let text = graphics::Text::new(("Hello world!", font, 48.0));
let s = MainState { frames: 0, text };
Ok(s)
}
}
impl event::EventHandler for MainState {
fn update(
&mut self,
_ctx: &mut Context,
_quad_ctx: &mut miniquad::graphics::GraphicsContext,
) -> GameResult {
Ok(())
}
fn draw(
&mut self,
ctx: &mut Context,
quad_ctx: &mut miniquad::graphics::GraphicsContext,
) -> GameResult {
graphics::clear(ctx, quad_ctx, [0.1, 0.2, 0.3, 1.0].into());
let offset = self.frames as f32 / 10.0;
let dest_point = cgmath::Point2::new(offset, offset);
graphics::draw(ctx, quad_ctx, &self.text, (dest_point,))?;
graphics::present(ctx, quad_ctx)?;
self.frames += 1;
if (self.frames % 100) == 0 {
println!("FPS: {}", ggez::timer::fps(ctx));
println!("drawable size: {:?}", graphics::drawable_size(quad_ctx));
}
Ok(())
}
}
pub 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")
};
ggez::start(
ggez::conf::Conf::default()
.cache(Some(include_bytes!("resources.tar")))
.physical_root_dir(Some(resource_dir)),
|mut context, mut _quad_ctx| Box::new(MainState::new(&mut context).unwrap()),
)
}