drawing.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. use crate::consts;
  2. use crate::com::{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. specs::WriteExpect<'a, SpriteBatch>,
  23. );
  24. fn run(&mut self, (positions, sprites, phase, mut batch): Self::SystemData) {
  25. use specs::Join;
  26. for (pos, spr, _) in (&positions, &sprites, &phase).join() {
  27. let param = graphics::DrawParam {
  28. src: spr.to_rect(),
  29. dest: pos.to_point(),
  30. scale: ggez::nalgebra::Point2::new(consts::SCALE, consts::SCALE),
  31. ..Default::default()
  32. };
  33. batch.add(param);
  34. graphics::draw(
  35. self.ctx,
  36. &*batch,
  37. ggez::nalgebra::Point2::new(0.0, 0.0),
  38. 0.0).unwrap();
  39. batch.clear();
  40. }
  41. }
  42. }
  43. pub fn systems(game: &mut MyGame, ctx: &mut Context) -> ggez::GameResult<()> {
  44. graphics::set_background_color(ctx, graphics::BLACK);
  45. graphics::clear(ctx);
  46. Draw {
  47. ctx,
  48. _phase: com::Background,
  49. }.run_now(&game.world.res);
  50. Draw {
  51. ctx,
  52. _phase: com::Foreground,
  53. }.run_now(&game.world.res);
  54. Draw {
  55. ctx,
  56. _phase: com::Decoration,
  57. }.run_now(&game.world.res);
  58. graphics::present(ctx);
  59. Ok(())
  60. }