summaryrefslogtreecommitdiff
path: root/graphics/src/render.rs
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/src/render.rs')
-rw-r--r--graphics/src/render.rs76
1 files changed, 33 insertions, 43 deletions
diff --git a/graphics/src/render.rs b/graphics/src/render.rs
index 7026d50..2dc77a7 100644
--- a/graphics/src/render.rs
+++ b/graphics/src/render.rs
@@ -3,7 +3,7 @@
//! frame specific information in `FrameInfo`.
/// The (prefered) view distance of the game
-const VIEW_DISTANCE: i32 = 10;
+const VIEW_DISTANCE: i32 = 5;
/// The baseline size of all ingame sprites and tile textures
const BASE_TILE_SIZE: i32 = 16;
@@ -20,7 +20,7 @@ use raylib::{
RaylibDraw, RaylibDrawHandle, RaylibHandle, RaylibMode2D, RaylibMode2DExt,
RaylibTextureModeExt,
},
- texture::{RaylibTexture2D, RenderTexture2D},
+ texture::RenderTexture2D,
};
use crate::assets::ImageData;
@@ -46,8 +46,8 @@ impl FrameInfo {
let tile_size = {
let size = width.min(height);
let dist = VIEW_DISTANCE * 2 + 1;
- // TODO: force by 16 scaling levels
- size.div(dist).max(BASE_TILE_SIZE)
+ let pixels = size.div(dist).max(BASE_TILE_SIZE);
+ 1 << (i32::BITS - pixels.leading_zeros())
};
Self {
@@ -68,17 +68,26 @@ pub struct Renderer {
image: ImageData,
/// Pre-rendered map texture that updates if the map changes
/// Stores the hash of the map tiles to check this
- tiles_tex: Option<RenderTexture2D>,
+ tiles_tex: RenderTexture2D,
/// Hash of tiles used to draw on `tiles_tex`
tiles_hash: u64,
}
impl Renderer {
- pub(crate) fn new(image: ImageData) -> Self {
- Self {
+ pub(crate) fn new(
+ handle: &mut RaylibHandle,
+ thread: &RaylibThread,
+ image: ImageData,
+ ) -> crate::Result<Self> {
+ let tiles_tex = {
+ let size = MAP_SIZE as u32 * BASE_TILE_SIZE as u32;
+ handle.load_render_texture(thread, size, size)?
+ };
+
+ Ok(Self {
image,
- tiles_tex: None,
+ tiles_tex,
tiles_hash: 0,
- }
+ })
}
/// Invokes the renderer for the current frame
@@ -86,30 +95,14 @@ impl Renderer {
&'a mut self,
handle: &'a mut RaylibHandle,
thread: &'a RaylibThread,
- ) -> crate::Result<FrameRendererImpl<'a>> {
+ ) -> FrameRendererImpl<'a> {
let info = FrameInfo::new(handle);
-
- // allocate tile texture
- let size = info.tile_size;
- let pixels = (MAP_SIZE as i32) * size;
- match &self.tiles_tex {
- Some(tex) if tex.width() == pixels => (),
- _ => {
- // texture is not yet allocated, or screen sized changed enough to change the tile
- // size
- let tex =
- handle.load_render_texture(thread, pixels as u32, pixels as u32)?;
- self.tiles_tex = Some(tex);
- self.tiles_hash = 0;
- }
- };
-
- Ok(FrameRenderer {
+ FrameRenderer {
handle: handle.begin_drawing(thread),
thread,
info,
renderer: self,
- })
+ }
}
}
@@ -141,8 +134,8 @@ where
/// Draws the dungeon, (tiles and entities)
pub fn draw_dungeon(&mut self, dungeon: &Dungeon) {
- let camera = dungeon.camera();
self.update_tilemap(&dungeon.floor);
+ let camera = dungeon.camera();
let mut renderer = self.camera_renderer(camera);
renderer.draw_tiles();
renderer.draw_entities(dungeon);
@@ -193,21 +186,15 @@ where
return;
}
- let Some(tex) = self.renderer.tiles_tex.as_mut() else {
- // BUG: error here?
- return;
- };
-
+ self.renderer.tiles_hash = hash;
+ let size = BASE_TILE_SIZE;
+ let tex = &mut self.renderer.tiles_tex;
let mut handle = self.handle.begin_texture_mode(self.thread, tex);
- let size = self.info.tile_size;
- handle.clear_background(Color::BLACK);
for pos in Pos::values() {
let (x, y) = pos.xy();
let color = tile_color(floor.get(pos));
handle.draw_rectangle(x as i32 * size, y as i32 * size, size, size, color);
}
-
- self.renderer.tiles_hash = hash;
}
}
impl<'a, T> FrameRenderer<'a, T>
@@ -243,11 +230,14 @@ where
/// Draw dungeon tiles
fn draw_tiles(&mut self) {
- let Some(tex) = &self.renderer.tiles_tex else {
- // BUG: error here?
- return;
- };
- self.handle.draw_texture(tex, 0, 0, Color::WHITE);
+ let tex = &self.renderer.tiles_tex;
+ self.handle.draw_texture_ex(
+ tex,
+ Vector2::zero(),
+ 0.0,
+ (self.info.tile_size / BASE_TILE_SIZE) as f32,
+ Color::WHITE,
+ );
}
/// Draw FPS counter