Skip to content

Commit

Permalink
entities: rendering hud with semi transparent background
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Jul 12, 2020
1 parent c5fe4f9 commit 7d9a9dd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/entities/diag_hud.rs
@@ -0,0 +1,31 @@
use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect};
use sdl2::render::WindowCanvas;

pub struct DiagHud {
position: Point,
background_color: Color,
height: u32,
}

impl DiagHud {
pub fn new(position: Point) -> DiagHud {
let background_color = Color::RGBA(0, 0, 0, 0xaa);
let height = 40;
DiagHud {
position,
background_color,
height,
}
}

pub fn render(&self, canvas: &mut WindowCanvas) -> Result<(), String> {
let (width, _) = canvas.window().size();
let rect = Rect::new(self.position.x, self.position.y, width, self.height);

canvas.set_draw_color(self.background_color);
canvas.fill_rect(rect)?;

Ok(())
}
}
1 change: 1 addition & 0 deletions src/entities/mod.rs
@@ -1,3 +1,4 @@
pub mod diag_hud;
pub mod floor;
pub mod grid;
pub mod text;
Expand Down
5 changes: 5 additions & 0 deletions src/game.rs
Expand Up @@ -2,6 +2,7 @@ use crate::engine::assets::image_asset::ImageAsset;
use sdl2::render::WindowCanvas;

use crate::arena::arena::Arena;
use crate::entities::diag_hud::DiagHud;
use crate::entities::floor::Floor;
use crate::entities::grid::Grid;
use crate::entities::text::{FontBlend, Text};
Expand All @@ -17,6 +18,7 @@ pub struct Game<'a> {
floor: Floor<'a>,
grid: Grid,
walls: Walls<'a>,
diag_hud: DiagHud,
diag_text: Text<'a>,

camera_platform: Point,
Expand All @@ -32,13 +34,15 @@ impl<'a> Game<'a> {
let floor = Floor::from_arena(&arena, floor_asset, TILE_SIZE);
let grid = Grid::new(arena.ncols, arena.nrows, TILE_SIZE);
let walls = Walls::new(&arena.walls, wall_asset, TILE_SIZE);
let diag_hud = DiagHud::new(Point::new(0, 0));

let camera_platform = Point::new(0, 0);
Ok(Game {
floor,
walls,
arena,
grid,
diag_hud,
diag_text,
camera_platform,
})
Expand Down Expand Up @@ -69,6 +73,7 @@ impl<'a> Game<'a> {
}
self.floor.render(canvas, &self.camera_platform)?;
self.walls.render(canvas, &self.camera_platform)?;
self.diag_hud.render(canvas)?;
self.diag_text.render(
canvas,
Point::new(10, 10),
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Expand Up @@ -7,7 +7,7 @@ use std::fmt;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::WindowCanvas;
use sdl2::render::{BlendMode, WindowCanvas};
use sdl2::video::{Window, WindowBuildError, WindowBuilder};
use sdl2::{IntegerOrSdlError, Sdl, VideoSubsystem};

Expand Down Expand Up @@ -51,6 +51,8 @@ pub fn start(config: &Config) -> Result<(), Box<dyn Error>> {
let window: Window = build_window(&video_subsystem, &config.window_settings)?;

let mut canvas = build_canvas(window)?;
canvas.set_blend_mode(BlendMode::Blend);

let texture_creator = canvas.texture_creator();

let image_assets = ImageAssets::new(&texture_creator)?;
Expand Down

0 comments on commit 7d9a9dd

Please sign in to comment.