Skip to content

Commit

Permalink
game: improving diag hud and consolidating cameras
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Jul 14, 2020
1 parent 6d28081 commit 30bafbf
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 30 deletions.
22 changes: 22 additions & 0 deletions src/data/cameras.rs
@@ -0,0 +1,22 @@
use sdl2::rect::Point;

pub struct Cameras {
pub platform: Point,
}

impl Cameras {
pub fn new() -> Cameras {
Cameras {
platform: Point::new(0, 0),
}
}
pub fn update(&mut self, player_coords: &Point, window_size: &(u32, u32)) {
let center_screen = Point::new((window_size.0 / 2) as i32, (window_size.1 / 2) as i32);
let centered = Point::new(
player_coords.x - center_screen.x,
player_coords.y - center_screen.y,
);

self.platform = centered;
}
}
1 change: 1 addition & 0 deletions src/data/mod.rs
@@ -1,2 +1,3 @@
pub mod cameras;
pub mod diagnostics;
pub mod player;
2 changes: 1 addition & 1 deletion src/data/player.rs
Expand Up @@ -13,7 +13,7 @@ impl Player {
pub fn new(tile_position: &TilePosition) -> Player {
let radius = TILE_SIZE / 2;
// let velocity = Vector::zero();
let velocity = Vector::new(0.0, -1.0);
let velocity = Vector::new(0.0, -0.5);
Player {
tile_position: tile_position.clone(),
radius,
Expand Down
56 changes: 39 additions & 17 deletions src/game.rs
@@ -1,36 +1,40 @@
use crate::engine::assets::image_asset::ImageAsset;
use std::error::Error;

use sdl2::pixels::Color;
use sdl2::rect::Point;
use sdl2::render::WindowCanvas;

use crate::arena::arena::Arena;
use crate::data::cameras::Cameras;
use crate::data::diagnostics::{Diagnostic, Diagnostics};
use crate::data::player::Player;
use crate::game_props::{AMBER_ACCENT, ANTIQUE_WHITE, RENDER_GRID, TILE_SIZE};
use crate::engine::assets::image_asset::ImageAsset;
use crate::game_props::{
AMBER_ACCENT, ANTIQUE_WHITE, HUD_DIAGNOSITCS_HEIGHT, HUD_DIAGNOSITCS_WIDTH_PERCENT,
RENDER_GRID, TILE_SIZE,
};
use crate::inputs::input::Input;
use crate::views::diag_hud::DiagHud;
use crate::views::floor_view::FloorView;
use crate::views::grid_view::GridView;
use crate::views::hud_diagnostics::HudDiagnostics;
use crate::views::player_view::PlayerView;
use crate::views::text::Text;
use crate::views::walls_view::WallsView;
use sdl2::pixels::Color;
use sdl2::rect::Point;
use std::error::Error;

pub struct Game<'a> {
// views
arena: &'a Arena,
floor: FloorView<'a>,
grid: GridView,
walls: WallsView<'a>,
diag_hud: DiagHud<'a>,
hud_diagnostics: HudDiagnostics<'a>,
player_view: PlayerView,

// data
diagnostics: Diagnostics,
player: Player,

// cameras
camera_platform: Point,
cameras: Cameras,
}

impl<'a> Game<'a> {
Expand All @@ -44,42 +48,60 @@ impl<'a> Game<'a> {
let floor = FloorView::from_arena(&arena, floor_asset, TILE_SIZE);
let grid = GridView::new(arena.ncols, arena.nrows, TILE_SIZE);
let walls = WallsView::new(&arena.walls, wall_asset, TILE_SIZE);
let diag_hud = DiagHud::new(Point::new(0, 0), stats_text);
let hud_diagnostics = HudDiagnostics::new(
Point::new(0, 0),
HUD_DIAGNOSITCS_HEIGHT,
HUD_DIAGNOSITCS_WIDTH_PERCENT,
stats_text,
);
let player_view = PlayerView::new(Color::RGB.call(AMBER_ACCENT));

// data
let player = Player::new(&arena.player);

// cameras
let camera_platform = Point::new(0, 0);
let cameras = Cameras::new();
Ok(Game {
arena,
floor,
grid,
walls,
diag_hud,
hud_diagnostics,
player_view,
diagnostics: Diagnostics::new(),
player,
camera_platform,
cameras,
})
}

pub fn update(&mut self, dt: f32, input: &Input, diagnostics: Diagnostic) {
pub fn update(
&mut self,
dt: f32,
window_size: &(u32, u32),
input: &Input,
diagnostics: Diagnostic,
) {
self.player_view.debug(true);
self.player.update(dt, input);
self.diagnostics.update(diagnostics);
self.cameras.update(
&self.player.tile_position.to_world_point(TILE_SIZE),
&window_size,
);
}

pub fn render(&self, canvas: &mut WindowCanvas) -> Result<(), Box<dyn Error>> {
let window_size = canvas.window().drawable_size();

canvas.set_draw_color(Color::RGB.call(ANTIQUE_WHITE));
canvas.clear();
if RENDER_GRID {
self.grid.render(canvas)?;
}
self.floor.render(canvas, &self.camera_platform)?;
self.walls.render(canvas, &self.camera_platform)?;
self.diag_hud.render(canvas, &self.diagnostics.current())?;
self.floor.render(canvas, &self.cameras.platform)?;
self.walls.render(canvas, &self.cameras.platform)?;
self.hud_diagnostics
.render(canvas, &self.diagnostics.current(), &window_size)?;
self.player_view.render(canvas, &self.player)?;
canvas.present();
Ok(())
Expand Down
3 changes: 3 additions & 0 deletions src/game_props.rs
Expand Up @@ -12,3 +12,6 @@ pub const AMBER_ACCENT: (u8, u8, u8) = (0xFF, 0xE5, 0x7F);

// Debug
pub const DIAGNOSTICS_COUNT: usize = (FRAME_RATE / 5) as usize;

pub const HUD_DIAGNOSITCS_HEIGHT: u32 = 20;
pub const HUD_DIAGNOSITCS_WIDTH_PERCENT: f32 = 0.5;
11 changes: 8 additions & 3 deletions src/lib.rs
Expand Up @@ -70,9 +70,9 @@ pub fn start(config: &Config) -> Result<(), Box<dyn Error>> {
FontStyle::NORMAL,
)?;

let arena = Arena::for_level("mini")?;
// let arena = Arena::for_level("mini")?;
// let arena = Arena::for_level("practice arena")?;
// let arena = Arena::for_level("face off")?;
let arena = Arena::for_level("face off")?;
let mut game = Game::new(&arena, floor_asset, wall_asset, diag_text)?;

println!("starting event loop");
Expand Down Expand Up @@ -141,7 +141,12 @@ fn start_event_loop(
}
polled_ts = timer.ticks();
// TODO: measure time more exact than just on ms resolution
game.update(dt as f32, &input, diagnostics);
game.update(
dt as f32,
&canvas.window().drawable_size(),
&input,
diagnostics,
);
updated_ts = timer.ticks();
game.render(canvas).expect("FATAL: game render failed");
rendered_ts = timer.ticks();
Expand Down
23 changes: 15 additions & 8 deletions src/views/diag_hud.rs → src/views/hud_diagnostics.rs
Expand Up @@ -6,22 +6,28 @@ use crate::data::diagnostics::Diagnostic;
use crate::views::text::{FontBlend, Text};
use std::error::Error;

pub struct DiagHud<'a> {
pub struct HudDiagnostics<'a> {
position: Point,
background_color: Color,
height: u32,
width_percent: f32,

stats_text: Text<'a>,
}

impl<'a> DiagHud<'a> {
pub fn new(position: Point, stats_text: Text<'a>) -> DiagHud {
impl<'a> HudDiagnostics<'a> {
pub fn new(
position: Point,
height: u32,
width_percent: f32,
stats_text: Text<'a>,
) -> HudDiagnostics {
let background_color = Color::RGBA(0, 0, 0, 0xcc);
let height = 20;
DiagHud {
HudDiagnostics {
position,
background_color,
height,
width_percent,
stats_text,
}
}
Expand All @@ -30,27 +36,28 @@ impl<'a> DiagHud<'a> {
&self,
canvas: &mut WindowCanvas,
diagnostics: &Diagnostic,
window_size: &(u32, u32),
) -> Result<(), Box<dyn Error>> {
let (width, _) = canvas.window().size();
let width = (window_size.0 as f32 * self.width_percent) as u32;
let rect = Rect::new(self.position.x, self.position.y, width, self.height);

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

let stats: String = format!(
"FPS: {fps} (P:{poll:02} U:{calc:02} R:{rndr:02}) -> {tot:02}ms +{rem:02}ms",
"FPS: {fps} (P:{poll:02} U:{calc:02} R:{rndr:02}) -> {tot:02}ms",
fps = diagnostics.frame_rate,
poll = diagnostics.time_spent_polling_ms,
calc = diagnostics.time_spent_udpating_ms,
rndr = diagnostics.time_spent_rendering_ms,
tot = diagnostics.time_spent_total_ms,
rem = diagnostics.time_budget_left_ms
);

self.stats_text.render(
canvas,
Point::new(10, 0),
&stats,
width,
Color::WHITE,
FontBlend::Blended,
)?;
Expand Down
2 changes: 1 addition & 1 deletion src/views/mod.rs
@@ -1,4 +1,4 @@
pub mod diag_hud;
pub mod hud_diagnostics;
pub mod floor_view;
pub mod grid_view;
pub mod player_view;
Expand Down
2 changes: 2 additions & 0 deletions src/views/text.rs
Expand Up @@ -38,6 +38,7 @@ impl<'a> Text<'a> {
canvas: &mut WindowCanvas,
bottom_left: Point,
text: &str,
max_width: u32,
color: Color,
font_blend: FontBlend,
) -> Result<(), Box<dyn Error>> {
Expand All @@ -48,6 +49,7 @@ impl<'a> Text<'a> {
};
let texture = self.texture_creator.create_texture_from_surface(&surface)?;
let TextureQuery { width, height, .. } = texture.query();
let width = width.min(max_width);
let rect = Rect::new(bottom_left.x, bottom_left.y, width, height);

canvas.copy(&texture, None, Some(rect))?;
Expand Down

0 comments on commit 30bafbf

Please sign in to comment.