Skip to content

Commit

Permalink
formatted code, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bingoobongoo committed May 11, 2024
1 parent 96cb32c commit f31f50c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 87 deletions.
File renamed without changes
7 changes: 5 additions & 2 deletions bdi_game/src/display/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod actions;
pub mod game_display;
pub mod sdl_display;
pub mod rendering;
pub mod resource_manager;
pub mod sdl;
pub mod structures;
pub mod traits;
11 changes: 4 additions & 7 deletions bdi_game/src/display/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,18 @@ impl Hexagon {
x_0 as i16,
(x_0 - a * 3.0_f64.sqrt() / 2.0) as i16,
(x_0 - a * 3.0_f64.sqrt() / 2.0) as i16,

];
let hexagon_vertices_y: [i16; 6] = [
(y_0 - a) as i16,
(y_0 - 0.5*a) as i16,
(y_0 + 0.5*a) as i16,
(y_0 - 0.5 * a) as i16,
(y_0 + 0.5 * a) as i16,
(y_0 + a) as i16,
(y_0 + 0.5*a) as i16,
(y_0 - 0.5*a) as i16,
(y_0 + 0.5 * a) as i16,
(y_0 - 0.5 * a) as i16,
];

(hexagon_vertices_x, hexagon_vertices_y)
}


}

impl Renderable for Hexagon {
Expand Down
150 changes: 72 additions & 78 deletions bdi_game/src/display/sdl.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
pub use super::traits::*;
use sdl2::{self, gfx::primitives::DrawRenderer, render::{SurfaceCanvas, WindowCanvas}, video::WindowContext, image::LoadTexture};

const HEXAGON_H : i16 = 20;
const HEXAGON_S : i16 = 50;
const HEXAGON_VERTICES_X : [i16;6] = [
HEXAGON_S-((1.73/2.)*HEXAGON_H as f32) as i16,
use crate::display::traits::*;
use sdl2::{
self,
gfx::primitives::DrawRenderer,
image::LoadTexture,
render::{SurfaceCanvas, WindowCanvas},
video::WindowContext,
};

const HEXAGON_H: i16 = 20;
const HEXAGON_S: i16 = 50;
const HEXAGON_VERTICES_X: [i16; 6] = [
HEXAGON_S - ((1.73 / 2.) * HEXAGON_H as f32) as i16,
HEXAGON_S,
HEXAGON_S + HEXAGON_H,
HEXAGON_S + HEXAGON_H + ((1.73 / 2.) * HEXAGON_H as f32) as i16,
HEXAGON_S + HEXAGON_H,
HEXAGON_S,
HEXAGON_S+HEXAGON_H,
HEXAGON_S+HEXAGON_H+((1.73/2.)*HEXAGON_H as f32) as i16,
HEXAGON_S+HEXAGON_H,
HEXAGON_S
];
const HEXAGON_VERTICES_Y : [i16;6] = [
HEXAGON_S+HEXAGON_H,
const HEXAGON_VERTICES_Y: [i16; 6] = [
HEXAGON_S + HEXAGON_H,
HEXAGON_S,
HEXAGON_S,
HEXAGON_S+HEXAGON_H,
HEXAGON_S+(2*HEXAGON_H),
HEXAGON_S+(2*HEXAGON_H)
HEXAGON_S + HEXAGON_H,
HEXAGON_S + (2 * HEXAGON_H),
HEXAGON_S + (2 * HEXAGON_H),
];

pub enum UnitSDLFillType{
pub enum UnitSDLFillType {
Texture(String),
Color(sdl2::pixels::Color),
None
None,
}

/// SDL unit
pub struct UnitSDL<'a> {
pub position: &'a (i32, i32),
pub struct UnitSDL {
pub position: (i32, i32),
pub size: u32,
pub filling : UnitSDLFillType,
pub filling: UnitSDLFillType,
}

impl<'a> UnitSDL<'a> {
pub fn new(position: &'a (i32, i32)) -> Self {
impl<'a> UnitSDL {
pub fn new(position: (i32, i32)) -> Self {
Self {
position,
size : 100,
filling : UnitSDLFillType::None,
size: 100,
filling: UnitSDLFillType::None,
}
}
}
Expand All @@ -55,18 +61,16 @@ pub struct DisplayBuilderSDL {
pub struct DisplaySDL {
builder: DisplayBuilderSDL,
canvas: sdl2::render::WindowCanvas,
texture_handler : sdl2::render::TextureCreator<WindowContext>,
texture_handler: sdl2::render::TextureCreator<WindowContext>,
}

impl DisplayBuilder for DisplayBuilderSDL {
type Unit<'a> = UnitSDL<'a> where Self : 'a;
type Unit<'a> = UnitSDL;
type Display<'a> = DisplaySDL;

fn build<'a>(self) -> Self::Display<'a> {

let _image_context = sdl2::image::init(
sdl2::image::InitFlag::PNG | sdl2::image::InitFlag::JPG)
.unwrap();
let _image_context =
sdl2::image::init(sdl2::image::InitFlag::PNG | sdl2::image::InitFlag::JPG).unwrap();

let canvas = self
.video
Expand Down Expand Up @@ -106,7 +110,7 @@ impl DisplayBuilderSDL {
}

impl Display for DisplaySDL {
type Unit<'a> = UnitSDL<'a> where Self : 'a;
type Unit<'a> = UnitSDL;

fn create_cluster<'a>(&self) -> super::structures::Cluster<Self::Unit<'a>> {
super::structures::Cluster::new()
Expand All @@ -115,15 +119,12 @@ impl Display for DisplaySDL {
fn render<'a>(&mut self, cluster: &super::structures::Cluster<Self::Unit<'a>>) {
self.canvas.set_draw_color(sdl2::pixels::Color::WHITE);
for unit in &cluster.objects {
let tile = sdl2::rect::Rect::new(unit.position.0,
unit.position.1,
unit.size,
unit.size);
let tile =
sdl2::rect::Rect::new(unit.position.0, unit.position.1, unit.size, unit.size);
match unit.filling {
UnitSDLFillType::Color(x) =>
self.canvas.set_draw_color(x),
UnitSDLFillType::Texture(_) => {},
UnitSDLFillType::None => {},
UnitSDLFillType::Color(x) => self.canvas.set_draw_color(x),
UnitSDLFillType::Texture(_) => {}
UnitSDLFillType::None => {}
}
// We should CATCH this error.
// Add handling here.
Expand All @@ -135,65 +136,58 @@ impl Display for DisplaySDL {
}

impl DisplaySDL {
pub fn direct_draw(&mut self, unit : &UnitSDL) {
let tile = sdl2::rect::Rect::new(unit.position.0,
unit.position.1,
unit.size,
unit.size);
pub fn direct_draw(&mut self, unit: &UnitSDL) {
let tile = sdl2::rect::Rect::new(unit.position.0, unit.position.1, unit.size, unit.size);
match &unit.filling {
UnitSDLFillType::Color(x) =>{
UnitSDLFillType::Color(x) => {
self.canvas.set_draw_color(*x);
self.canvas.fill_rect(tile).unwrap();
}
UnitSDLFillType::Texture(t) => {
let texture = self.texture_handler.load_texture(t.as_str()).unwrap();
self.canvas.copy(&texture, None, tile).unwrap();
},
UnitSDLFillType::None => {},
}
UnitSDLFillType::None => {}
}

}

pub fn direct_draw_polygon(&mut self, unit : &UnitSDL){

let (x,y) = create_hexagon_vertices(unit);
pub fn direct_draw_polygon(&mut self, unit: &UnitSDL) {
let (x, y) = create_hexagon_vertices(unit);
match unit.filling {
UnitSDLFillType::Color(color) =>{
self.canvas.filled_polygon(&x,&y,color).unwrap();
UnitSDLFillType::Color(color) => {
self.canvas.filled_polygon(&x, &y, color).unwrap();
}
UnitSDLFillType::Texture(_) => {},
UnitSDLFillType::None => {},
UnitSDLFillType::Texture(_) => {}
UnitSDLFillType::None => {}
}

}

pub fn direct_flush(&mut self){
self.canvas.present();
pub fn direct_flush(&mut self) {
self.canvas.present();
}
}

fn create_hexagon_vertices(unit : &UnitSDL) -> ([i16;6],[i16;6]){
let heigth = (unit.size/2) as i16;
fn create_hexagon_vertices(unit: &UnitSDL) -> ([i16; 6], [i16; 6]) {
let heigth = (unit.size / 2) as i16;
let x = unit.position.0 as i16;
let y = unit.position.1 as i16;
let approx_triangle_h = ((1.72/2.)*heigth as f32) as i16;
let hexagon_vertices_x : [i16;6] = [
x-approx_triangle_h,
x,
x+heigth,
x+heigth+approx_triangle_h,
x+heigth,
x
let approx_triangle_h = ((1.72 / 2.) * heigth as f32) as i16;
let hexagon_vertices_x: [i16; 6] = [
x - approx_triangle_h,
x,
x + heigth,
x + heigth + approx_triangle_h,
x + heigth,
x,
];
let hexagon_vertices_y : [i16;6] = [
y+heigth,
y,
y,
y+heigth,
y+(2*heigth),
y+(2*heigth)
let hexagon_vertices_y: [i16; 6] = [
y + heigth,
y,
y,
y + heigth,
y + (2 * heigth),
y + (2 * heigth),
];

(hexagon_vertices_x,hexagon_vertices_y)

(hexagon_vertices_x, hexagon_vertices_y)
}
1 change: 1 addition & 0 deletions bdi_game/src/game/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::{Duration, Instant};

use crate::{
display::sdl::*,
display::traits::DisplayBuilder,
input::Input,
simulation::{simulation::Simulation, world_state::WorldState},
};
Expand Down

0 comments on commit f31f50c

Please sign in to comment.