drawing.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use crate::consts;
  2. use crate::components::{self,Position,Sprite};
  3. use crate::game::MyGame;
  4. use ggez::{
  5. Context,
  6. graphics,
  7. graphics::spritebatch::SpriteBatch,
  8. };
  9. use specs::RunNow;
  10. /// The `Draw` system is parameterized by which phase we want to draw
  11. /// in.
  12. pub struct Draw<'t, Phase> {
  13. pub ctx: &'t mut Context,
  14. pub sprites: &'t mut SpriteBatch,
  15. pub _phase: Phase,
  16. }
  17. impl<'a, 't, Phase : specs::Component> specs::System<'a> for Draw<'t, Phase> {
  18. type SystemData = (
  19. specs::ReadStorage<'a, Position>,
  20. specs::ReadStorage<'a, Sprite>,
  21. specs::ReadStorage<'a, Phase>,
  22. );
  23. fn run(&mut self, (positions, sprites, phase): Self::SystemData) {
  24. use specs::Join;
  25. for (pos, spr, _) in (&positions, &sprites, &phase).join() {
  26. let param = graphics::DrawParam {
  27. src: spr.to_rect(),
  28. dest: pos.to_point(),
  29. scale: ggez::nalgebra::Point2::new(consts::SCALE, consts::SCALE),
  30. ..Default::default()
  31. };
  32. self.sprites.add(param);
  33. graphics::draw(
  34. self.ctx,
  35. self.sprites,
  36. ggez::nalgebra::Point2::new(0.0, 0.0),
  37. 0.0).unwrap();
  38. self.sprites.clear();
  39. }
  40. }
  41. }
  42. pub fn systems(game: &mut MyGame, ctx: &mut Context) -> ggez::GameResult<()> {
  43. graphics::set_background_color(ctx, graphics::BLACK);
  44. graphics::clear(ctx);
  45. Draw {
  46. ctx,
  47. sprites: &mut game.sprites,
  48. _phase: components::Background,
  49. }.run_now(&game.world.res);
  50. Draw {
  51. ctx,
  52. sprites: &mut game.sprites,
  53. _phase: components::Foreground,
  54. }.run_now(&game.world.res);
  55. Draw {
  56. ctx,
  57. sprites: &mut game.sprites,
  58. _phase: components::Decoration,
  59. }.run_now(&game.world.res);
  60. graphics::draw(
  61. ctx,
  62. &game.sprites,
  63. ggez::nalgebra::Point2::new(0.0, 0.0),
  64. 0.0
  65. )?;
  66. game.sprites.clear();
  67. graphics::present(ctx);
  68. Ok(())
  69. }