main.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #[macro_use] extern crate specs_derive;
  2. use ggez::{
  3. Context,
  4. ContextBuilder,
  5. GameResult,
  6. event::{self, EventHandler},
  7. };
  8. use sdl2::keyboard as sdl;
  9. pub mod consts;
  10. pub mod components;
  11. pub mod game;
  12. pub mod res;
  13. pub mod sys;
  14. use game::MyGame;
  15. impl EventHandler for MyGame {
  16. fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
  17. sys::input::systems(self);
  18. sys::physics::systems(self);
  19. Ok(())
  20. }
  21. fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
  22. sys::drawing::systems(self, ctx)
  23. }
  24. fn key_down_event(
  25. &mut self,
  26. ctx: &mut Context,
  27. keycode: sdl::Keycode,
  28. _keymod: sdl::Mod,
  29. _repeat: bool,
  30. ) {
  31. if keycode == sdl::Keycode::Escape {
  32. ctx.quit().expect("Should never fail");
  33. }
  34. self.world.write_resource::<res::KeySet>().insert(keycode);
  35. }
  36. fn key_up_event(
  37. &mut self,
  38. _ctx: &mut Context,
  39. keycode: sdl::Keycode,
  40. _keymod: sdl::Mod,
  41. _repeat: bool,
  42. ) {
  43. self.world.write_resource::<res::KeySet>().remove(&keycode);
  44. }
  45. }
  46. fn main() -> Result<(), ggez::error::GameError> {
  47. let mut world = specs::World::new();
  48. components::register(&mut world);
  49. res::world_from_file(&mut world, "assets/main.tmx");
  50. // Make a Context and an EventLoop.
  51. let mut ctx = ContextBuilder::new("game", "me")
  52. .add_resource_path({
  53. let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
  54. let mut path = std::path::PathBuf::from(base);
  55. path.push("assets");
  56. path
  57. })
  58. .window_mode(ggez::conf::WindowMode {
  59. width: consts::WINDOW_WIDTH,
  60. height: consts::WINDOW_HEIGHT,
  61. ..Default::default()
  62. })
  63. .build()?;
  64. let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?;
  65. let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
  66. sprites.set_filter(ggez::graphics::FilterMode::Nearest);
  67. world.add_resource(sprites);
  68. world.add_resource(res::KeySet::new());
  69. let mut my_game = MyGame { world };
  70. event::run(&mut ctx, &mut my_game)
  71. }