main.rs 2.6 KB

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