added tileset support

This commit is contained in:
dani 2023-07-02 07:40:29 +00:00
parent cb668400f4
commit 150233728c
5 changed files with 57 additions and 7 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

@ -5,6 +5,7 @@ const HEIGHT: i32 = 1080 / 3;
struct World {
img: Image,
font: Tileset,
pos: Vec2<i32>,
}
@ -18,6 +19,7 @@ impl Game for World {
window_state.scramble_palette();
Self {
img: Image::load_data(include_bytes!("assets/test.gif")),
font: Tileset::load_data(include_bytes!("assets/ega-8x14.gif"), Vec2 { x: 16, y: 16 }),
pos: Vec2::zero(),
}
}
@ -38,7 +40,7 @@ impl Game for World {
fn draw(&self, target: &mut Image) {
target.clear();
target.draw_image(Vec2 { x: 200, y: 100 }, &self.img);
target.draw_image(self.pos, &self.img);
target.draw_image(self.pos, self.font.get(68));
target.draw_line(
Vec2 {

View File

@ -68,15 +68,16 @@ impl Image {
//todo: write proper implementation later
for y in 0..src_rect.size.y {
for x in 0..src_rect.size.x {
let x = x + src_rect.pos.x;
let y = y + src_rect.pos.y;
//todo: implement better(very stupid to do per pixel)
if x + pos.x >= 0
&& y + pos.y >= 0
&& x + pos.x < self.size.x
&& y + pos.y < self.size.y
{
let p = image.get_pixel(Vec2 { x, y });
let p = image.get_pixel(Vec2 {
x: x + src_rect.pos.x,
y: y + src_rect.pos.y,
});
if p > 0 {
self.set_pixel(
Vec2 {
@ -129,4 +130,8 @@ impl Image {
pub fn set_pixel(&mut self, pos: Vec2<i32>, color: u8) {
self.data[(pos.x + self.size.x * pos.y) as usize] = color;
}
pub fn size(&self) -> Vec2<i32> {
self.size
}
}

View File

@ -1,12 +1,13 @@
mod hexmap;
mod image;
mod rect;
mod tileset;
mod vec2;
mod window;
mod tileset;
pub use hexmap::*;
pub use image::*;
pub use rect::*;
pub use tileset::*;
pub use vec2::*;
pub use window::*;

View File

@ -1,7 +1,49 @@
use crate::{Image, Vec2};
use crate::{Image, Rect, Vec2};
use std::fs;
struct Tileset {
pub struct Tileset {
count: i32,
size: Vec2<i32>,
images: Vec<Image>,
}
impl Tileset {
pub fn load(path: &str, tile_count: Vec2<i32>) -> Self {
Self::load_data(fs::read(path).unwrap().as_slice(), tile_count)
}
pub fn load_data(data: &[u8], tile_count: Vec2<i32>) -> Self {
let img = Image::load_data(data);
let mut images: Vec<Image> = vec![];
let size = Vec2 {
x: img.size().x / tile_count.x,
y: img.size().y / tile_count.y,
};
for y in 0..tile_count.y {
for x in 0..tile_count.x {
let mut image = Image::new(size);
image.draw_image_partial(
Vec2::zero(),
&img,
Rect {
pos: Vec2 {
x: x * size.x,
y: y * size.y,
},
size,
},
);
images.push(image);
}
}
Tileset {
count: images.len() as i32,
size,
images,
}
}
pub fn get(&self, idx: i32) -> &Image {
&self.images[idx as usize]
}
}