drawing.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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: mint::Vector2 { x: consts::SCALE, y: consts::SCALE },
  31. ..Default::default()
  32. };
  33. batch.add(param);
  34. }
  35. let basic: graphics::DrawParam = Default::default();
  36. graphics::draw(
  37. self.ctx,
  38. &*batch,
  39. basic).unwrap();
  40. batch.clear();
  41. }
  42. }
  43. pub fn systems(game: &mut MyGame, ctx: &mut Context) -> ggez::GameResult<()> {
  44. graphics::clear(ctx, graphics::BLACK);
  45. Draw {
  46. ctx,
  47. _phase: com::Background,
  48. }.run_now(&game.world);
  49. Draw {
  50. ctx,
  51. _phase: com::Foreground,
  52. }.run_now(&game.world);
  53. Draw {
  54. ctx,
  55. _phase: com::Decoration,
  56. }.run_now(&game.world);
  57. graphics::present(ctx)
  58. }