123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #[macro_use] extern crate specs_derive;
- use ggez::{
- Context,
- ContextBuilder,
- GameResult,
- event::{self, EventHandler},
- };
- use sdl2::keyboard as sdl;
- pub mod consts;
- pub mod com;
- pub mod game;
- pub mod res;
- pub mod sys;
- use game::MyGame;
- impl EventHandler for MyGame {
- fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
- sys::input::systems(self);
- sys::physics::systems(self);
- Ok(())
- }
- fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
- sys::drawing::systems(self, ctx)
- }
- fn key_down_event(
- &mut self,
- ctx: &mut Context,
- keycode: sdl::Keycode,
- _keymod: sdl::Mod,
- _repeat: bool,
- ) {
- if keycode == sdl::Keycode::Escape {
- ctx.quit().expect("Should never fail");
- }
- self.world.write_resource::<res::KeySet>().insert(keycode);
- }
- fn key_up_event(
- &mut self,
- _ctx: &mut Context,
- keycode: sdl::Keycode,
- _keymod: sdl::Mod,
- _repeat: bool,
- ) {
- self.world.write_resource::<res::KeySet>().remove(&keycode);
- }
- }
- fn main() -> Result<(), ggez::error::GameError> {
- let mut world = specs::World::new();
- com::register(&mut world);
- res::world_from_file(&mut world, "assets/main.tmx");
- // Make a Context and an EventLoop.
- let mut ctx = ContextBuilder::new("game", "me")
- .add_resource_path({
- let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
- let mut path = std::path::PathBuf::from(base);
- path.push("assets");
- path
- })
- .window_mode(ggez::conf::WindowMode {
- width: consts::WINDOW_WIDTH,
- height: consts::WINDOW_HEIGHT,
- ..Default::default()
- })
- .build()?;
- let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?;
- let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
- sprites.set_filter(ggez::graphics::FilterMode::Nearest);
- world.add_resource(sprites);
- world.add_resource(res::KeySet::new());
- let mut my_game = MyGame { world };
- event::run(&mut ctx, &mut my_game)
- }
|