Skip to content

Commit

Permalink
Fix human_rendering of sprites (#12) for testeing #20
Browse files Browse the repository at this point in the history
  • Loading branch information
jjfiv committed Sep 22, 2018
1 parent f69a944 commit f02e26c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ A set of games designed for causal experimentation with deep RL agents.

## Target Rust Version

For building on older GPU clusters, we target rustc 1.25:
For building on older GPU clusters, we target rustc 1.28:
```bash
rustup override set 1.25.0
rustup override set 1.28.0
```

## Projects
Expand Down
17 changes: 8 additions & 9 deletions human_play/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ extern crate quicksilver;
use clap::{App, Arg};
use human_play::color_convert;
use quicksilver::{
geom::Rectangle,
graphics::{Color, Draw, View, Window, WindowBuilder},
geom::{Rectangle, Vector},
graphics::{Color, Draw, View, Window, WindowBuilder, Image, PixelFormat},
run,
};
use toybox::graphics::Drawable;
use toybox::graphics::{ImageBuffer, Drawable, SpriteData};

static mut GAME_ID: usize = 0;

Expand Down Expand Up @@ -57,12 +57,11 @@ impl quicksilver::State for AbstractGame {
let y = sprite.y;
let w = sprite.width() * sprite.scale();
let h = sprite.height() * sprite.scale();
if let Some(color) = sprite.find_visible_color() {
window.draw(
&Draw::rectangle(Rectangle::new(x, y, w, h))
.with_color(color_convert(color)),
);
}

let mut buf = ImageBuffer::alloc(w,h);
buf.render_sprite(sprite.scale, &sprite.data);
let img = Image::from_raw(&buf.data, w as u32, h as u32, PixelFormat::RGBA);
window.draw(&Draw::image(&img, Vector::new(x,y)));
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions toybox/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ impl<'a> From<&'a (u8, u8, u8)> for Color {
pub struct SpriteData {
pub x: i32,
pub y: i32,
scale: i32,
data: Vec<Vec<Color>>,
pub scale: i32,
pub data: Vec<Vec<Color>>,
}
impl SpriteData {
pub fn new(data: Vec<Vec<Color>>, scale: i32) -> SpriteData {
Expand Down Expand Up @@ -232,6 +232,23 @@ impl ImageBuffer {
self.set_pixel(x, y, color)
}
}

pub fn render_sprite(&mut self, scale: i32, data: &Vec<Vec<Color>>) {
let h = data.len() as i32;
let w = data[0].len() as i32;
for yi in 0..h {
for xi in 0..w {
let color = data[yi as usize][xi as usize];
for xt in 0..scale {
for yt in 0..scale {
self.set_pixel_alpha(xi + xt, yi + yt, color)
}
}
}
}
}



pub fn render(&mut self, commands: &[Drawable]) {
for cmd in commands {
Expand Down

0 comments on commit f02e26c

Please sign in to comment.