working on texture tiles
This commit is contained in:
parent
17fdb4252e
commit
2b95cbb3b3
BIN
game/assets/default-texture.png
Normal file
BIN
game/assets/default-texture.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 72 KiB |
@ -1,3 +1,4 @@
|
||||
pub mod anim_render;
|
||||
pub mod datastore;
|
||||
pub mod discord;
|
||||
pub mod game_config;
|
||||
@ -6,4 +7,4 @@ pub mod math;
|
||||
pub mod non_ref_raylib;
|
||||
pub mod render_layer;
|
||||
pub mod shaders;
|
||||
pub mod anim_render;
|
||||
pub mod world_paint_texture;
|
||||
|
56
game/src/utilities/world_paint_texture.rs
Normal file
56
game/src/utilities/world_paint_texture.rs
Normal file
@ -0,0 +1,56 @@
|
||||
//! Defines a texture that tiles across the whole screen in world space
|
||||
|
||||
use raylib::{
|
||||
camera::Camera2D,
|
||||
color::Color,
|
||||
math::Vector2,
|
||||
prelude::{RaylibDraw, RaylibMode2D},
|
||||
texture::Texture2D,
|
||||
RaylibHandle,
|
||||
};
|
||||
|
||||
use super::non_ref_raylib::HackedRaylibHandle;
|
||||
|
||||
pub struct WorldPaintTexture {
|
||||
texture: Texture2D,
|
||||
}
|
||||
|
||||
impl WorldPaintTexture {
|
||||
/// Construct a new world paint texture
|
||||
pub fn new(texture: Texture2D) -> Self {
|
||||
Self { texture }
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
&self,
|
||||
raylib: &mut RaylibMode2D<'_, HackedRaylibHandle>,
|
||||
origin: Vector2,
|
||||
camera: &Camera2D,
|
||||
) {
|
||||
// Convert the screen edges to world space
|
||||
let top_left = raylib.get_screen_to_world2D(Vector2::new(0.0, 0.0), camera);
|
||||
let bottom_right = raylib.get_screen_to_world2D(raylib.get_screen_size(), camera);
|
||||
|
||||
// Calculate the distance between the edges and the origin
|
||||
let left_edge_distance = top_left.x - origin.x;
|
||||
let right_edge_distance = bottom_right.x - origin.x;
|
||||
|
||||
// Calculate the x position to draw the tile in order for there always to be a tile covering the edges
|
||||
let left_tile_x =
|
||||
(left_edge_distance / self.texture.width as f32).floor() * self.texture.width as f32;
|
||||
let right_tile_x =
|
||||
(right_edge_distance / self.texture.width as f32).ceil() * self.texture.width as f32;
|
||||
|
||||
// Render the tiles
|
||||
raylib.draw_texture_v(
|
||||
&self.texture,
|
||||
Vector2::new(left_tile_x, origin.y),
|
||||
Color::WHITE,
|
||||
);
|
||||
raylib.draw_texture_v(
|
||||
&self.texture,
|
||||
Vector2::new(right_tile_x, origin.y),
|
||||
Color::WHITE,
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user