Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.26 KB | None | 0 0
  1. use ggez;
  2.  
  3. use ggez::event;
  4. use ggez::graphics;
  5. use ggez::{Context, GameResult};
  6. use std::env;
  7. use std::path;
  8. use mint::Point2;
  9. use cgmath;
  10. use std::time::{Duration, SystemTime};
  11. use ggez::nalgebra as na;
  12.  
  13. mod rain;
  14. use rain::{Blob, Rain};
  15.  
  16. struct MainState {
  17.   // frames: usize,
  18.   // gtext: graphics::Text,
  19.   pools: Vec<Blob>,
  20.   text: String,
  21.   tick: i64
  22.   // updateFps: function
  23. }
  24. impl MainState {
  25.   fn new() -> MainState {
  26.     MainState {
  27.       text: String::from("asdsad"),
  28.       tick: 0,
  29.       pools: (1..10).map(|_x| Blob::new()).collect::<Vec<Blob>>()
  30.     }
  31.   }
  32.  
  33.   fn updateFps(&mut self){
  34.     // let font = graphics::Font::new(ctx, "/DejaVuSerif.ttf")?;
  35.     self.increaseTick();
  36.     self.text = format!("FPS: {}", self.tick);
  37.   }
  38.  
  39.   fn increaseTick(&mut self){
  40.     self.tick += 1;
  41.   }
  42. }
  43. impl event::EventHandler for MainState {
  44.   fn update(&mut self, _ctx: &mut Context) -> GameResult {
  45.  
  46.     for pool in &mut self.pools {
  47.       pool.radius -= 0.1;
  48.     }
  49.     Ok(())
  50.   }
  51.   fn draw(&mut self, ctx: &mut Context) -> GameResult {
  52.     graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into());
  53.     self.updateFps();
  54.     let font = graphics::Font::new(ctx, "/DejaVuSerif.ttf")?;
  55.     let dest_point = cgmath::Point2::new(1.0, 10.0);
  56.     let stext: &str = &self.text;
  57.     let text = graphics::Text::new((stext, font, 48.0));
  58.     graphics::draw(ctx, &text, (dest_point,));
  59.  
  60.     for pool in &self.pools {
  61.       let circle = graphics::Mesh::new_circle(
  62.         ctx,
  63.         graphics::DrawMode::fill(),
  64.         na::Point2::new(pool.position.x, pool.position.y),
  65.         pool.radius,
  66.         0.1,
  67.         graphics::WHITE,
  68.       )?;
  69.       graphics::draw(ctx, &circle, (na::Point2::new(pool.position.x, pool.position.y),))?;
  70.     }
  71.  
  72.     graphics::present(ctx)?;
  73.     Ok(())
  74.   }
  75. }
  76.  
  77. fn main() -> GameResult {
  78.   let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
  79.     let mut path = path::PathBuf::from(manifest_dir);
  80.     path.push("resources");
  81.     path
  82.   } else {
  83.     path::PathBuf::from("./resources")
  84.   };
  85.  
  86.   let cb = ggez::ContextBuilder::new("super_simple", "ggez").add_resource_path(resource_dir);
  87.   let (ctx, event_loop) = &mut cb.build()?;
  88.   let state = &mut MainState::new();
  89.   event::run(ctx, event_loop, state)
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement