Advertisement
Guest User

Graphics code, sludge edition.

a guest
Jul 12th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.40 KB | None | 0 0
  1. use engine::world;
  2. use glutin_window::GlutinWindow as Window;
  3. use opengl_graphics::{ GlGraphics, OpenGL };
  4. use piston::input::*;
  5. use graphics::*;
  6. use grenderer::unitcolors;
  7. use piston::window::Window as PistonWindow;
  8. const TILES_PER_LINE: u64 = 32;
  9.  
  10.  
  11. pub struct Game<'a> {
  12.    pub gameworld: world::World<'a>,
  13.     pub gamedraw: GlGraphics,
  14.     pub gamewindow: Window,
  15.     pub backround: [f32; 4],
  16.     pub tiles: [[[f32; 4]; 32]; 32],
  17.     pub viewzone: [u64; 2],
  18. }
  19.  
  20. impl<'a> Game<'a> {
  21.     pub fn render(&mut self, args: &RenderArgs) {
  22.         let backround = self.backround;
  23.         let window = &self.gamewindow;
  24.         let tiles = &self.tiles;
  25.         let tilespace_x = (window.size().width / 4) * 3;
  26.         let tilespace_y = (window.size().height / 4) * 3;
  27.         let tilesize = tilespace_x as u64 / TILES_PER_LINE;
  28.         let square = rectangle::square(0.0, 0.0, tilesize as f64);
  29.         self.gamedraw.draw(args.viewport(), |c, gl| {
  30.             clear(backround, gl);
  31.             let mut posmultx = 0;
  32.             let mut posmulty = 0;
  33.             for i in tiles {
  34.                
  35.                 posmulty=0;
  36.                 for j in i {
  37.                     let transform = c.transform.trans((posmultx*tilesize) as f64, (posmulty*tilesize) as f64)
  38.                                     .rot_rad(0.0)
  39.                                     .trans(tilesize as f64 * -1.0, tilesize as f64 * -1.0);
  40.                     rectangle(*j, square, transform, gl);
  41.                     posmulty += 1;
  42.                 }
  43.                 posmultx += 1;
  44.             }
  45.         });
  46.     }
  47.     pub fn update(&mut self, args: &UpdateArgs) {
  48.         let mut posx = 0;
  49.         let mut posy = 0;
  50.         let mut newtiles: [[[f32; 4]; 32]; 32] = [[[0.0; 4]; 32]; 32];
  51.         for i in &self.tiles {
  52.            
  53.             posy=0;
  54.             for j in i {
  55.                 match self.gameworld.unitmap.get(&world::Position { x: (posx + self.viewzone[0]) as i64, y: (posy + self.viewzone[1]) as i64}) {
  56.                     Some(unit) => {
  57.                         newtiles[posx as usize][posy as usize] = unitcolors::matchunit(&unit)
  58.                     }
  59.                     None => {
  60.                         newtiles[posx as usize][posy as usize] = [1.0; 4]
  61.                     }
  62.                 }
  63.                 posy += 1;
  64.             }
  65.             posx += 1;
  66.         }
  67.         self.tiles = newtiles;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement