Skip to content

Commit

Permalink
Piston Renderer example.
Browse files Browse the repository at this point in the history
  • Loading branch information
sinistersnare committed May 2, 2015
1 parent be22003 commit e16e500
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ tags
target
Cargo.lock
*.swp
*.dll
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,11 @@ env:
global:
- secure: zpxgR0Nn7YhCRXPjfR/gcdxAObeJLBoRMW0qeKiyuc4V6Y1guZ9EfnvJ5uL4pRXnP0LZrXm/WwYicXm7J0vTwQwq/4Bf0NXykysZfVrHFTY5Wook/QXJd8OajCwuJa8d/wr6Dx/wGe/80gi8hZibraMS5QkWKgtzz9HCFLM99uw=
language: rust
before_install:
- sudo add-apt-repository ppa:team-xbmc/ppa -y
- sudo apt-get update -qq
install:
- sudo apt-get install libsdl2-dev
script:
- cargo build
- cargo test
Expand Down
21 changes: 20 additions & 1 deletion Cargo.toml
Expand Up @@ -17,7 +17,26 @@ path = "src/lib.rs"
name = "example"
path = "examples/main.rs"

[[example]]
name = "piston"
path = "examples/piston.rs"

[dependencies]
flate2 = "*"
rustc-serialize = "*"
xml-rs = "*"

[dependencies.xml-rs]
git = "https://github.com/netvl/xml-rs.git"


[dev-dependencies.pistoncore-sdl2_window]
git = "https://github.com/pistondevelopers/sdl2_window"

[dev-dependencies.piston]
git = "https://github.com/pistondevelopers/piston"

[dev-dependencies.piston2d-graphics]
git = "https://github.com/pistondevelopers/graphics"

[dev-dependencies.piston2d-opengl_graphics]
git = "https://github.com/pistondevelopers/opengl_graphics"
82 changes: 82 additions & 0 deletions examples/piston.rs
@@ -0,0 +1,82 @@
extern crate piston;
extern crate graphics;
extern crate sdl2_window;
extern crate opengl_graphics;
extern crate tiled;

use std::cell::RefCell;
use std::rc::Rc;
use std::path::Path;
use std::fs::File;

use sdl2_window::Sdl2Window;
use opengl_graphics::{
OpenGL,
Texture,
};
use graphics::{Image, Graphics, default_draw_state};
use graphics::math::Matrix2d;
use piston::event::*;

fn main() {
let (width, height) = (500, 500);
let opengl = OpenGL::_3_2;
let window = Sdl2Window::new(
opengl,
piston::window::WindowSettings::new(
"Tiled + Piston Example".to_string(),
piston::window::Size {width: width, height: height}
).exit_on_esc(true)
);
let window = Rc::new(RefCell::new(window));
let ref mut gl = opengl_graphics::GlGraphics::new(opengl);
let mut blank_image = Image::new();

let tmx_file = File::open(&Path::new("assets/tiled_base64_zlib.tmx")).ok().expect("could not open tmx");
let tmx_map = tiled::parse(tmx_file).unwrap();
let image_path = Path::new("assets/tilesheet.png");
let image_tex = Texture::from_path(&image_path).unwrap();

for e in window.events() {
use piston::event::{RenderEvent};

if let Some(args) = e.render_args() {
gl.draw(args.viewport(), |c, gl| {
graphics::clear([0.0, 0.0, 0.0, 1.0], gl);
render_tiled_map(&tmx_map, &image_tex, &mut blank_image, c.transform, gl);
});
}
}
}

fn render_tiled_map<G>(map: &tiled::Map,
image_tex: &<G as Graphics>::Texture,
drawn_image: &mut Image,
transform: Matrix2d,
g: &mut G)
where G: Graphics
{
let ref tile_set = map.tilesets[0];
let (tile_width, tile_height) = (tile_set.tile_width, tile_set.tile_height);
let tiles_in_row = (tile_set.images[0].width + tile_set.spacing as i32 )/ (tile_width as i32 + tile_set.spacing as i32);

for layer in map.layers.iter() {
for (i, tile_row) in layer.tiles.iter().enumerate() {
for (j, tile_val) in tile_row.iter().enumerate() {
if *tile_val == 0 {
continue;
}
let tile_x = *tile_val % tiles_in_row as u32 - 1;
let tile_y = *tile_val / tiles_in_row as u32;

let pixel_x = ((tile_x) * tile_width) + ((tile_x) * tile_set.spacing);
let pixel_y = ((tile_y) * tile_height) + ((tile_y) * tile_set.spacing);

drawn_image.src_rect([pixel_x as i32, pixel_y as i32, tile_width as i32, tile_height as i32])
.rect([j as f64 * tile_width as f64, i as f64 * tile_height as f64,
tile_width as f64, tile_height as f64])
.draw(image_tex, default_draw_state(), transform, g);
}
}
}
}

0 comments on commit e16e500

Please sign in to comment.