Merge branch 'master' of gitea.tforgione.fr:free-rusty-maker/free-rusty-maker

This commit is contained in:
Thomas Forgione 2019-04-03 15:50:00 +02:00
commit 01fdd800e9
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
7 changed files with 288 additions and 236 deletions

View File

@ -143,7 +143,7 @@ fn main() {
let image_197 = superpose(&image_4, &image_193);
vec.push(&image_197);
let image_199 = rotate90(&image_127);
let image_199 = rotate270(&image_31);
vec.push(&image_199);
let image_209 = rotate90(&image_116);

View File

@ -6,7 +6,8 @@ use sfml::window::mouse::Button;
use sfml::graphics::{RectangleShape, RenderTarget, Transformable, Color, Shape};
use rusty::engine::renderer::Renderer;
use rusty::engine::map::{GraphicTile, CollisionTile, Map};
use rusty::engine::map::{CollisionTile, Map};
use rusty::engine::texture::SPRITE_SIZE_I32;
fn main() {
let _ = App::new("Rusty Editor")
@ -17,7 +18,7 @@ fn main() {
let mut running = true;
let mut map = Map::load("./assets/levels/level2.lvl").unwrap();
let mut map = Map::new(50, 50);//;Map::load("./assets/levels/level2.lvl").unwrap();
loop {
let top_panel_size = Vector2::new(renderer.window().size().x as f32, 50.0);
@ -39,11 +40,11 @@ fn main() {
button: Button::Left, x, y,
} => {
if x as f32 >= left_panel_size.x && y as f32 >= top_panel_size.y {
let x = ((x - left_panel_size.x as i32) / 16) as usize;
let y = ((y - top_panel_size.y as i32) / 16) as usize;
let x = ((x - left_panel_size.x as i32) / SPRITE_SIZE_I32) as usize;
let y = ((y - top_panel_size.y as i32) / SPRITE_SIZE_I32) as usize;
if let Some(tile) = map.tile(y, x) {
if tile == CollisionTile::empty() {
if let Some(tile) = map.collision_tile(y, x) {
if tile.is_empty() {
map.set_tile(y, x, CollisionTile::full());
} else {
map.set_tile(y, x, CollisionTile::empty());
@ -74,7 +75,7 @@ fn main() {
for i in 0 .. map.rows() {
for j in 0 .. map.cols() {
let tile = map.at(i, j);
if tile.graphic != GraphicTile::Hidden {
if tile.graphic.is_visible() {
renderer.translate_and_draw(&tile, (left_panel_size.x, top_panel_size.y));
}
}
@ -82,7 +83,10 @@ fn main() {
// Draw the border of the map
let mut border = RectangleShape::new();
border.set_size(((map.cols() * 16) as f32, (map.rows() * 16) as f32));
border.set_size(((
map.cols() * SPRITE_SIZE_I32 as usize) as f32,
(map.rows() * SPRITE_SIZE_I32 as usize) as f32
));
border.set_position((left_panel_size.x, top_panel_size.y));
border.set_fill_color(&Color::rgba(0, 0, 0, 0));
border.set_outline_color(&Color::rgb(0, 0, 0));

View File

@ -12,7 +12,7 @@ use sfml::system::Vector2;
use crate::{Error, Result};
use crate::engine::math::{clamp, Matrix};
use crate::engine::renderer::Drawable;
use crate::engine::texture::Texture;
use crate::engine::texture::{Texture, byte_to_index, SPRITE_SIZE_F32, SPRITE_SIZE_I32};
use crate::engine::character::Damage;
/// This enum represents if the collision happens on the X axis or the Y axis.
@ -82,176 +82,92 @@ impl CollisionTile {
from_bottom: true,
}
}
/// Tests whether a collision tile is full or not.
pub fn is_full(self) -> bool {
self.from_top && self.from_left && self.from_right && self.from_bottom
}
/// Tests whether a collision tile is empty or not.
pub fn is_empty(self) -> bool {
!self.from_top && !self.from_left && !self.from_right && !self.from_bottom
}
}
/// This struct represents a renderable tile linking to its part in the tileset texture.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum GraphicTile {
/// There is nothing to draw.
Hidden,
/// Top left corner of a solid tile.
TopLeft,
/// Top of a solid tile.
Top,
/// Top right corner of a solid tile.
TopRight,
/// Left of a solid tile.
Left,
/// Center of a solid tile.
Center,
/// Right of a solid tile.
Right,
/// Bottom left corner of a solid tile.
BottomLeft,
/// Bottom of a solid tile.
Bottom,
/// Bottom right corner of a solid tile.
BottomRight,
}
pub struct GraphicTile(Option<i32>);
impl GraphicTile {
/// Returns a graphic tile from a collision tile.
///
/// This might be ugly for the moment.
pub fn from_collision_tile(collision: CollisionTile) -> GraphicTile {
if collision == CollisionTile::empty() {
GraphicTile::Hidden
} else {
GraphicTile::Center
}
}
/// Checks if a graphic tile has a top border.
pub fn is_top(self) -> bool {
match self {
GraphicTile::TopLeft | GraphicTile::Top | GraphicTile::TopRight => true,
_ => false,
}
}
/// Checks if a graphic tile has a left border.
pub fn is_left(self) -> bool {
match self {
GraphicTile::TopLeft | GraphicTile::Left | GraphicTile::BottomLeft => true,
_ => false,
}
}
/// Checks if a graphic tile has a right border.
pub fn is_right(self) -> bool {
match self {
GraphicTile::TopRight | GraphicTile::Right | GraphicTile::BottomRight => true,
_ => false,
}
}
/// Checks if a graphic tile has a bottom border.
pub fn is_bottom(self) -> bool {
match self {
GraphicTile::BottomLeft | GraphicTile::Bottom | GraphicTile::BottomRight => true,
_ => false,
}
}
/// Creates a vec containing all the non hidden graphic tiles.
pub fn all() -> Vec<GraphicTile> {
vec![
GraphicTile::TopLeft,
GraphicTile::Top,
GraphicTile::TopRight,
GraphicTile::Left,
GraphicTile::Center,
GraphicTile::Right,
GraphicTile::BottomLeft,
GraphicTile::Bottom,
GraphicTile::BottomRight,
]
}
/// Creates the correct graphic tile depending on the neighbours.
///
/// A none will be considered solid.
pub fn from_neighbour_options(
top: Option<CollisionTile>,
left: Option<CollisionTile>,
right: Option<CollisionTile>,
bottom: Option<CollisionTile>,
) -> GraphicTile {
GraphicTile::from_neighbours(
top.unwrap_or_else(CollisionTile::full),
left.unwrap_or_else(CollisionTile::full),
right.unwrap_or_else(CollisionTile::full),
bottom.unwrap_or_else(CollisionTile::full),
)
pub fn from_neighbour_options(tiles: &[Option<CollisionTile>; 8]) -> GraphicTile {
GraphicTile::from_neighbours(&[
tiles[0].unwrap_or_else(CollisionTile::full),
tiles[1].unwrap_or_else(CollisionTile::full),
tiles[2].unwrap_or_else(CollisionTile::full),
tiles[3].unwrap_or_else(CollisionTile::full),
tiles[4].unwrap_or_else(CollisionTile::full),
tiles[5].unwrap_or_else(CollisionTile::full),
tiles[6].unwrap_or_else(CollisionTile::full),
tiles[7].unwrap_or_else(CollisionTile::full),
])
}
/// Creates the correct graphic tile depending on the neighbours.
pub fn from_neighbours(
top: CollisionTile,
left: CollisionTile,
right: CollisionTile,
bottom: CollisionTile,
) -> GraphicTile {
let mut all = GraphicTile::all()
.into_iter()
.map(|x| (x, true))
.collect::<Vec<_>>();
pub fn from_neighbours(tiles: &[CollisionTile; 8]) -> GraphicTile {
for (ref mut tile, ref mut possible) in &mut all {
if tile.is_top() == (top == CollisionTile::full()) {
*possible = false;
let mut byte = 0;
if !tiles[7].is_full() || !tiles[0].is_full() || !tiles[1].is_full() {
byte += 1;
}
if tile.is_left() == (left == CollisionTile::full()) {
*possible = false;
if !tiles[1].is_full() {
byte += 2;
}
if tile.is_right() == (right == CollisionTile::full()) {
*possible = false;
if !tiles[1] .is_full() || !tiles[2].is_full() || !tiles[3].is_full() {
byte += 4;
}
if tile.is_bottom() == (bottom == CollisionTile::full()) {
*possible = false;
}
if !tiles[3] .is_full() {
byte += 8;
}
for (tile, possible) in all {
if possible {
return tile;
}
if !tiles[3].is_full() || !tiles[4].is_full() || !tiles[5].is_full() {
byte += 16;
}
GraphicTile::Center
if !tiles[5].is_full() {
byte += 32;
}
if !tiles[5].is_full() || !tiles[6].is_full() || !tiles[7].is_full() {
byte += 64;
}
if !tiles[7] .is_full() {
byte += 128;
}
GraphicTile(Some(byte_to_index(byte)))
}
/// Returns the offset to the corresponding graphic tile in the texture.
pub fn offset(self) -> (i32, i32) {
let vertical = match self {
GraphicTile::TopLeft | GraphicTile::Top | GraphicTile::TopRight => 0,
GraphicTile::Left | GraphicTile::Center | GraphicTile::Right => 16,
GraphicTile::BottomLeft | GraphicTile::Bottom | GraphicTile::BottomRight => 32,
_ => 0,
};
match self.0 {
None => (0, 0),
Some(v) => (32 * v, 0)
}
}
let horizontal = match self {
GraphicTile::TopLeft | GraphicTile::Left | GraphicTile::BottomLeft => 0,
GraphicTile::Top | GraphicTile::Center | GraphicTile::Bottom => 16,
GraphicTile::TopRight | GraphicTile::Right | GraphicTile::BottomRight => 32,
_ => 0,
};
// (horizontal, vertical)
(vertical, horizontal)
/// Returns true if the tile is visible.
pub fn is_visible(&self) -> bool {
self.0.is_some()
}
}
@ -274,7 +190,7 @@ impl Drawable for PositionedTile {
fn texture_rect(&self) -> IntRect {
let offset = self.graphic.offset();
IntRect::new(offset.0, offset.1, 16, 16)
IntRect::new(offset.0, offset.1, SPRITE_SIZE_I32, SPRITE_SIZE_I32)
}
fn position(&self) -> Vector2<f32> {
@ -282,14 +198,34 @@ impl Drawable for PositionedTile {
}
}
/// The map represents the tiles contained in a level.
/// The content of a map that needs to be saved.
#[derive(Serialize, Deserialize)]
struct SaveMap {
/// The entrance point of the character in the map.
entrance: (usize, usize),
/// The collision tiles contained in the level.
collision_tiles: Matrix<CollisionTile>,
}
impl SaveMap {
/// Creates a map from the save map.
fn to_map(self) -> Map {
Map::from_entrance_and_collision_tiles(self.entrance, self.collision_tiles)
}
}
/// The map represents the tiles contained in a level.
#[derive(Clone)]
pub struct Map {
/// The entrace point of the character in the map.
entrance: (usize, usize),
/// The tiles contained in the level.
tiles: Matrix<(CollisionTile, GraphicTile)>,
/// The collision tiles contained in the level.
collision_tiles: Matrix<CollisionTile>,
/// The graphic tiles contained in the level.
graphic_tiles: Matrix<GraphicTile>,
}
impl Map {
@ -305,21 +241,29 @@ impl Map {
Map::from_collision_tiles(tiles)
}
/// Creates a SaveMap from a map
fn save_map(&self) -> SaveMap {
SaveMap {
entrance: self.entrance,
collision_tiles: self.collision_tiles.clone(),
}
}
/// Encodes the map into a binary format.
pub fn encode(&self) -> Result<Vec<u8>> {
serialize(&self).map_err(Error::Encoding)
serialize(&self.save_map()).map_err(Error::Encoding)
}
/// Decodes a map from bytes.
pub fn decode(content: &[u8]) -> Result<Map> {
deserialize(content).map_err(Error::Decoding)
deserialize(content).map(SaveMap::to_map).map_err(Error::Decoding)
}
/// Saves the map to a file.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let file = File::create(path.as_ref()).map_err(Error::Save)?;
let mut writer = BufWriter::new(file);
serialize_into(&mut writer, &self).map_err(Error::Encoding)?;
serialize_into(&mut writer, &self.save_map()).map_err(Error::Encoding)?;
Ok(())
}
@ -327,56 +271,84 @@ impl Map {
pub fn load<P: AsRef<Path>>(path: P) -> Result<Map> {
let file = File::open(path.as_ref()).map_err(Error::Load)?;
let mut reader = BufReader::new(file);
Ok(deserialize_from(&mut reader).map_err(Error::Decoding)?)
Ok(deserialize_from(&mut reader).map(SaveMap::to_map).map_err(Error::Decoding)?)
}
/// Creates a map from its tiles.
pub fn from_collision_tiles(tiles: Matrix<CollisionTile>) -> Map {
let rows = tiles.rows();
let cols = tiles.cols();
/// Creates a map from its entrance and collision tiles.
pub fn from_entrance_and_collision_tiles(e: (usize, usize), t: Matrix<CollisionTile>) -> Map {
let rows = t.rows();
let cols = t.cols();
let mut matrix =
Matrix::from_size(rows, cols, (CollisionTile::empty(), GraphicTile::Hidden));
let graphic_tiles = Matrix::from_size(rows, cols, GraphicTile(None));
let mut map = Map {
entrance: e,
collision_tiles: t,
graphic_tiles,
};
for i in 0 .. rows {
for j in 0 .. cols {
let graphic = if tiles[(i, j)] == CollisionTile::full() {
// TODO This is uggly
// If there is an overflow, we should give None instead
let (i, j) = (i as isize, j as isize);
map.graphic_tiles[(i, j)] = map.graphic_tile(i, j);
}
}
GraphicTile::from_neighbour_options(
tiles.get(((i) as usize, (j - 1) as usize)).cloned(),
tiles.get(((i - 1) as usize, (j) as usize)).cloned(),
tiles.get(((i + 1) as usize, (j) as usize)).cloned(),
tiles.get(((i) as usize, (j + 1) as usize)).cloned(),
)
map
}
/// Creates a map from its tiles.
pub fn from_collision_tiles(collision_tiles: Matrix<CollisionTile>) -> Map {
let entrance = Map::find_entrance(&collision_tiles);
Map::from_entrance_and_collision_tiles(entrance, collision_tiles)
}
/// Creates the neighbours of a tile.
pub fn neighbours(&self, i: usize, j: usize) -> [Option<CollisionTile>; 8] {
[
if i > 0 && j > 0 { self.collision_tiles.get(i - 1, j - 1).cloned() } else { None },
if i > 0 { self.collision_tiles.get(i - 1, j ).cloned() } else { None },
if i > 0 { self.collision_tiles.get(i - 1, j + 1).cloned() } else { None },
self.collision_tiles.get(i , j + 1).cloned(),
self.collision_tiles.get(i + 1, j + 1).cloned(),
self.collision_tiles.get(i + 1, j ).cloned(),
if j > 0 { self.collision_tiles.get(i + 1, j - 1).cloned() } else { None },
if j > 0 { self.collision_tiles.get(i , j - 1).cloned() } else { None },
]
}
/// Returns the graphic tile corresponding to the collision tiles.
pub fn graphic_tile(&self, i: usize, j: usize) -> GraphicTile {
if self.collision_tiles[(i, j)].is_full() {
GraphicTile::from_neighbour_options(&self.neighbours(i, j))
} else {
GraphicTile::Hidden
};
matrix[(i, j)] = (tiles[(i, j)], graphic);
}
}
Map {
entrance: Map::find_entrance(&matrix),
tiles: matrix,
GraphicTile(None)
}
}
/// Returns a tile of the map.
pub fn tile(&self, i: usize, j: usize) -> Option<CollisionTile> {
self.tiles.get((i, j)).map(|x| x.0)
pub fn collision_tile(&self, i: usize, j: usize) -> Option<CollisionTile> {
self.collision_tiles.get(i, j).cloned()
}
/// Changes a tile of the map.
pub fn set_tile(&mut self, i: usize, j: usize, tile: CollisionTile) {
self.tiles[(i, j)] = (tile, GraphicTile::from_collision_tile(tile));
// Change the collision tile
self.collision_tiles[(i, j)] = tile;
// Refresh the current graphic tile and their neighbours
use std::cmp::max;
for i in max(i, 1) - 1 ..= (i + 1) {
for j in max(j, 1) - 1 ..= (j + 1) {
let new_tile = self.graphic_tile(i, j);
if let Some(tile) = self.graphic_tiles.get_mut(i, j) {
*tile = new_tile;
}
}
}
}
/// Finds a possible entrance.
pub fn find_entrance(tiles: &Matrix<(CollisionTile, GraphicTile)>) -> (usize, usize) {
pub fn find_entrance(tiles: &Matrix<CollisionTile>) -> (usize, usize) {
(tiles.rows() - 5, 1)
}
@ -388,20 +360,20 @@ impl Map {
/// Returns an iterator to the positioned tiles.
pub fn at(&self, row: usize, col: usize) -> PositionedTile {
PositionedTile {
collision: self.tiles[(row, col)].0,
graphic: self.tiles[(row, col)].1,
position: (col as f32 * 16.0, row as f32 * 16.0),
collision: self.collision_tiles[(row, col)],
graphic: self.graphic_tiles[(row, col)],
position: (col as f32 * SPRITE_SIZE_F32, row as f32 * SPRITE_SIZE_F32),
}
}
/// Returns the number of rows of the map.
pub fn rows(&self) -> usize {
self.tiles.rows()
self.collision_tiles.rows()
}
/// Returns the number of columns of the map.
pub fn cols(&self) -> usize {
self.tiles.cols()
self.collision_tiles.cols()
}
/// Checks whether the bounding box collides with elements of the map.
@ -415,14 +387,14 @@ impl Map {
let mut damage = Damage::None;
let cols = self.tiles.cols() - 1;
let rows = self.tiles.rows() - 1;
let cols = self.collision_tiles.cols() - 1;
let rows = self.collision_tiles.rows() - 1;
let min_col = clamp(new.left / 16.0, 0.0, cols as f32) as usize;
let min_row = clamp(new.top / 16.0, 0.0, rows as f32) as usize;
let min_col = clamp(new.left / SPRITE_SIZE_F32, 0.0, cols as f32) as usize;
let min_row = clamp(new.top / SPRITE_SIZE_F32, 0.0, rows as f32) as usize;
let max_col = clamp((new.left + new.width) / 16.0, 0.0, cols as f32) as usize;
let max_row = clamp((new.top + new.height) / 16.0, 0.0, rows as f32) as usize;
let max_col = clamp((new.left + new.width) / SPRITE_SIZE_F32, 0.0, cols as f32) as usize;
let max_row = clamp((new.top + new.height) / SPRITE_SIZE_F32, 0.0, rows as f32) as usize;
let mut collision_x = false;
let mut collision_y = false;
@ -430,17 +402,17 @@ impl Map {
for col in min_col ..= max_col {
for row in min_row ..= max_row {
let tile_left = col as f32 * 16.0;
let tile_top = row as f32 * 16.0;
let tile_left = col as f32 * SPRITE_SIZE_F32;
let tile_top = row as f32 * SPRITE_SIZE_F32;
let tile = FloatRect::new(tile_left, tile_top, 16.0, 16.0);
let tile = FloatRect::new(tile_left, tile_top, SPRITE_SIZE_F32, SPRITE_SIZE_F32);
if !overlap(new, tile) {
continue;
}
// Collisions between feet and ground
if self.tiles[(row, col)].0.from_top
if self.collision_tiles[(row, col)].from_top
&& old.top + old.height <= tile_top
&& new.top + new.height >= tile_top
{
@ -453,7 +425,7 @@ impl Map {
}
// Collisions between right and right wall
if self.tiles[(row, col)].0.from_left
if self.collision_tiles[(row, col)].from_left
&& old.left + old.width <= tile_left
&& new.left + new.width >= tile_left
{
@ -466,12 +438,12 @@ impl Map {
}
// Collisions between left and left wall
if self.tiles[(row, col)].0.from_right
&& old.left >= tile_left + 16.0
&& new.left <= tile_left + 16.0
if self.collision_tiles[(row, col)].from_right
&& old.left >= tile_left + SPRITE_SIZE_F32
&& new.left <= tile_left + SPRITE_SIZE_F32
{
collision_x = true;
new.left = tile_left + 16.0;
new.left = tile_left + SPRITE_SIZE_F32;
}
if !overlap(new, tile) {
@ -479,12 +451,12 @@ impl Map {
}
// Collisions between head and roof
if self.tiles[(row, col)].0.from_bottom
&& old.top >= tile_top + 16.0
&& new.top <= tile_top + 16.0
if self.collision_tiles[(row, col)].from_bottom
&& old.top >= tile_top + SPRITE_SIZE_F32
&& new.top <= tile_top + SPRITE_SIZE_F32
{
collision_y = true;
new.top = tile_top + 16.0;
new.top = tile_top + SPRITE_SIZE_F32;
}
}
}
@ -496,14 +468,14 @@ impl Map {
}
// Collision between the player and right border of the level
if new.left > cols as f32 * 16.0 {
new.left = cols as f32 * 16.0;
if new.left > cols as f32 * SPRITE_SIZE_F32 {
new.left = cols as f32 * SPRITE_SIZE_F32;
collision_x = true;
}
// Collision between the player and the void
if new.top > self.tiles.rows() as f32 * 16.0 {
new.top = self.tiles.rows() as f32 * 16.0;
if new.top > self.collision_tiles.rows() as f32 * SPRITE_SIZE_F32 {
new.top = self.collision_tiles.rows() as f32 * SPRITE_SIZE_F32;
collision_y = true;
damage = Damage::Death;
}

View File

@ -27,7 +27,7 @@ pub fn duration_as_f32(duration: &Duration) -> f32 {
}
/// A generic matrix type, useful for levels.
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub struct Matrix<T> {
/// The number of rows of the matrix.
rows: usize,
@ -70,13 +70,22 @@ impl<T> Matrix<T> {
}
/// Returns the tile if any, none otherwise.
pub fn get(&self, (row, col): (usize, usize)) -> Option<&T> {
pub fn get(&self, row: usize, col: usize) -> Option<&T> {
if row < self.rows && col < self.cols {
Some(&self[(row, col)])
} else {
None
}
}
/// Returns a mutable reference to the tile if any.
pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> {
if row < self.rows && col < self.cols {
Some(&mut self[(row, col)])
} else {
None
}
}
}
impl<T> Index<(usize, usize)> for Matrix<T> {

View File

@ -6,7 +6,6 @@ use sfml::window::{Event, Style};
use sfml::system::Vector2;
use crate::engine::map::GraphicTile;
use crate::engine::scene::Scene;
use crate::engine::texture::{Texture, TextureManager};
@ -101,7 +100,7 @@ impl Renderer {
for i in 0..rows {
for j in 0..cols {
let tile = map.at(i, j);
if tile.graphic != GraphicTile::Hidden {
if tile.graphic.is_visible() {
self.draw(&tile);
}
}

View File

@ -4,6 +4,7 @@ use sfml::graphics::View;
use sfml::system::Vector2;
use sfml::window::Event;
use crate::engine::texture::SPRITE_SIZE_F32;
use crate::engine::character::Character;
use crate::engine::map::Map;
@ -38,8 +39,8 @@ impl Scene {
/// Adds a character to the scene.
pub fn add(&mut self, character: Character) {
let mut character = character;
character.position.x = self.map.entrance().1 as f32 * 16.0;
character.position.y = self.map.entrance().0 as f32 * 16.0;
character.position.x = self.map.entrance().1 as f32 * SPRITE_SIZE_F32;
character.position.y = self.map.entrance().0 as f32 * SPRITE_SIZE_F32;
self.characters.push(character);
}
@ -69,8 +70,8 @@ impl Scene {
center.y = size.y / 2.0;
}
let right_limit = self.map.cols() as f32 * 16.0;
let bottom_limit = self.map.rows() as f32 * 16.0;
let right_limit = self.map.cols() as f32 * SPRITE_SIZE_F32;
let bottom_limit = self.map.rows() as f32 * SPRITE_SIZE_F32;
if center.x + size.x / 2.0 > right_limit {
center.x = right_limit - size.x / 2.0;

View File

@ -1,11 +1,74 @@
use sfml::graphics::{IntRect, Texture as SfTexture};
/// The number of pixels of a sprite.
pub const SPRITE_SIZE_I32: i32 = 32;
/// The number of pixels of a sprite.
pub const SPRITE_SIZE_F32: f32 = 32.0;
/// Converts the byte with bits corresponding to the neighbours to the offset on the generated
/// tileset.
pub fn byte_to_index(byte: u8) -> i32 {
match byte {
0 => 0,
1 => 1,
4 => 2,
5 => 3,
7 => 4,
16 => 5,
17 => 6,
20 => 7,
21 => 8,
23 => 9,
28 => 10,
29 => 11,
31 => 12,
64 => 13,
65 => 14,
68 => 15,
69 => 16,
71 => 17,
80 => 18,
81 => 19,
84 => 20,
85 => 21,
87 => 22,
92 => 23,
93 => 24,
95 => 25,
112 => 26,
113 => 27,
116 => 28,
117 => 29,
119 => 30,
124 => 31,
125 => 32,
127 => 33,
193 => 34,
197 => 35,
199 => 36,
209 => 37,
213 => 38,
215 => 39,
221 => 40,
223 => 41,
241 => 42,
245 => 43,
247 => 44,
253 => 45,
255 => 46,
_ => panic!("Incorrect byte {}", byte),
}
}
macro_rules! make_textures {
( $(
$enum_name: ident,
$texture_name: ident,
$function_name: ident,
$texture_path: tt, )
$texture_path: tt,
$width: expr,
$height: expr, )
*) => {
/// Describes all the textures that will be used in this game.
@ -39,7 +102,7 @@ macro_rules! make_textures {
/// Creates the texture.
fn $function_name() -> SfTexture {
let bytes = include_bytes!($texture_path).to_vec();
TextureManager::make_texture_from_bytes(bytes)
TextureManager::make_texture_from_bytes(bytes, $width, $height)
}
)*
@ -61,16 +124,20 @@ make_textures!(
mario,
make_mario_texture,
"../../../assets/textures/mario.png",
256,
256,
Overworld,
overworld,
make_overworld_texture,
"../../../assets/textures/overworld.png",
"../../../assets/textures-generated/grass.png",
32 * 47,
32,
);
impl TextureManager {
/// Creates a textures from an array of bytes.
fn make_texture_from_bytes(bytes: Vec<u8>) -> SfTexture {
SfTexture::from_memory(&bytes, &IntRect::new(0, 0, 256, 256))
fn make_texture_from_bytes(bytes: Vec<u8>, width: i32, height: i32) -> SfTexture {
SfTexture::from_memory(&bytes, &IntRect::new(0, 0, width, height))
.expect("Failed to create texture")
}
}