summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-12-17 17:11:01 +0100
committerNao Pross <naopross@thearcway.org>2018-12-17 17:11:01 +0100
commit1144293d54af7fe0c2a1124edc1f680598b6e6cb (patch)
tree79a83e4dff64afe0a7309c9ea9dc6bad928beee5 /src
parentRemove one layer from demo map for initial testing (diff)
downloadSubconscious-rs-1144293d54af7fe0c2a1124edc1f680598b6e6cb.tar.gz
Subconscious-rs-1144293d54af7fe0c2a1124edc1f680598b6e6cb.zip
Implement rendering (drawing) of tiles
Diffstat (limited to '')
-rw-r--r--src/graphics.rs103
1 files changed, 57 insertions, 46 deletions
diff --git a/src/graphics.rs b/src/graphics.rs
index 947992a..422c884 100644
--- a/src/graphics.rs
+++ b/src/graphics.rs
@@ -1,4 +1,7 @@
use std::collections::HashMap;
+
+use sfml::system::Vector2f;
+
use sfml::window::{
ContextSettings,
Event,
@@ -8,16 +11,21 @@ use sfml::window::{
use sfml::graphics::{
RenderWindow,
RenderTarget,
+ Drawable,
Texture,
Image,
Color,
Rect,
};
+use sfml::graphics::{
+ RectangleShape,
+ Transformable,
+};
#[derive(RustEmbed)]
-#[folder = "res"]
-pub struct Assets;
+#[folder = "res/maps"]
+pub struct MapAssets;
pub fn start() -> RenderWindow {
@@ -64,61 +72,64 @@ pub fn render(window: &mut RenderWindow, state: &crate::game::State) {
for x in 0 .. state.map.width {
// for each layer
for layer in &state.map.layers {
- let tmx_tile = layer.tiles[y as usize][x as usize];
- let tmx_tileset = match state.map.get_tileset_by_gid(tmx_tile) {
- Some(tileset) => tileset,
- None => continue
- };
+ let tmx_tile = layer.tiles[y as usize][x as usize];
+ let tmx_tileset = state.map.get_tileset_by_gid(tmx_tile).unwrap();
// load tileset if not loaded yet
// TODO: load in a separate thread?
if !tilesets.contains_key(&tmx_tileset.first_gid) {
// TODO: replace with iter or error message
assert_eq!(tmx_tileset.images.len(), 1);
-
- (|| {
- // load tileset image
- let tmx_image = &tmx_tileset.images[0];
-
- let asset = match Assets::get(&tmx_image.source) {
- Some(asset) => asset,
- None => return,
- };
-
- let image = match Image::from_memory(asset.as_ref()) {
- Some(image) => image,
- None => return,
- };
-
- // load tiles (textures)
- let tileset_width = tmx_image.width as u32 / tmx_tileset.tile_width;
- let tileset_height = tmx_image.height as u32 / tmx_tileset.tile_height;
-
- for ty in 0 .. tileset_height {
- for tx in 0 .. tileset_width {
- let texture = match Texture::from_image_with_rect(
- &image, &Rect::new(
- (tx * tmx_tileset.tile_width) as i32,
- (ty * tmx_tileset.tile_height) as i32,
- tmx_tileset.tile_width as i32,
- tmx_tileset.tile_height as i32
- )
- ) {
- Some(texture) => texture,
- None => continue,
- };
- // save tile texture
- textures.insert(tmx_tile, texture);
- }
+
+ // load tileset image
+ let tmx_image = &tmx_tileset.images[0];
+ let asset = MapAssets::get(&tmx_image.source).unwrap();
+ let image = Image::from_memory(asset.as_ref()).unwrap();
+
+ // load tiles (textures)
+ let tileset_width = tmx_image.width as u32 / tmx_tileset.tile_width;
+ let tileset_height = tmx_image.height as u32 / tmx_tileset.tile_height;
+
+ for ty in 0 .. tileset_height {
+ for tx in 0 .. tileset_width {
+ let texture = Texture::from_image_with_rect(
+ &image, &Rect::new(
+ (tx * tmx_tileset.tile_width) as i32,
+ (ty * tmx_tileset.tile_height) as i32,
+ tmx_tileset.tile_width as i32,
+ tmx_tileset.tile_height as i32
+ )
+ ).unwrap();
+
+ // save tile texture
+ textures.insert(
+ tmx_tileset.first_gid + ty * tileset_width + tx,
+ texture
+ );
}
+ }
- // save tileset image
- tilesets.insert(tmx_tileset.first_gid, image);
- })();
+ // save tileset image
+ tilesets.insert(tmx_tileset.first_gid, image);
}
- //
+ // draw!
+ // TODO: check map render order
+ let texture = textures.get(&tmx_tile).unwrap();
+ let mut tile_rect = RectangleShape::with_texture(texture);
+
+ tile_rect.set_size(Vector2f {
+ x: tmx_tileset.tile_width as f32,
+ y: tmx_tileset.tile_height as f32
+ });
+
+ tile_rect.set_position(Vector2f {
+ x: (x * tmx_tileset.tile_width) as f32,
+ y: (y * tmx_tileset.tile_height) as f32
+ });
+
+ window.draw(&tile_rect);
}
}
}