debugging reference and ownership issues

master
MitchellHansen 6 years ago
parent 4075c95dff
commit 8d4e2f1c1e

@ -0,0 +1,8 @@
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0

@ -28,21 +28,44 @@ use ncollide2d::bounding_volume::{self, AABB, BoundingVolumeInterferencesCollect
use ncollide2d::partitioning::BVT; use ncollide2d::partitioning::BVT;
use sfml::graphics::RectangleShape; use sfml::graphics::RectangleShape;
use std::{thread, time}; use std::{thread, time};
use std::cell::RefCell;
use std::rc::Rc;
pub struct tSprite {
pub value1: i32,
value2: f64
}
impl tSprite {
pub fn new() -> tSprite {
tSprite { value1: 0, value2: 0.0 }
}
}
fn main() { fn main() {
// Load the spritsheet // Load the spritesheet
let spritesheet_desc = util::read_spritesheet(String::from("spritesheet_complete.xml")); let spritesheet_desc = util::read_spritesheet(String::from("spritesheet_complete.xml"));
let spritesheet_text = Texture::from_file("spritesheet_complete.png") let spritesheet_text = Texture::from_file("spritesheet_complete.png")
.expect("Couldn't load texture"); .expect("Couldn't load texture");
let mut block_sprite = Sprite::new(); let shared_container: Rc<RefCell<Vec<tSprite>>> = Rc::new(RefCell::new(Vec::new()));
block_sprite.set_texture(&spritesheet_text, false);
block_sprite.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc)); shared_container.borrow_mut().push(tSprite::new());
block_sprite.set_position((64.0, 64.0));
let mut sprite_vec = shared_container.borrow_mut();
sprite_vec.last().unwrap().value1 = 0;
// sp.set_position((0.0,0.0));
//println!("{:?}", sp);
// sp.set_texture(&spritesheet_text, false);
// sp.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc));
// sp.set_position((64.0, 64.0));
return;
let mut block_sprite2 = Sprite::new(); let mut block_sprite2 = Sprite::new();
block_sprite2.set_texture(&spritesheet_text, false); block_sprite2.set_texture(&spritesheet_text, false);
block_sprite2.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc)); block_sprite2.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc));
@ -53,16 +76,16 @@ fn main() {
block_sprite3.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc)); block_sprite3.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &spritesheet_desc));
block_sprite3.set_position((192.0, 64.0)); block_sprite3.set_position((192.0, 64.0));
let idx_and_bounding_spheres: Vec<(&Sprite, AABB<f64>)> = vec![ let static_sprites: Vec<(&Sprite, AABB<f64>)> = vec![
( // (
&block_sprite, // &block_sprite,
{ // {
let bounds = &block_sprite.local_bounds(); // let bounds = &block_sprite.local_bounds();
let pos = &block_sprite.position(); // let pos = &block_sprite.position();
bounding_volume::AABB::new(na::Point2::new(pos.x as f64, pos.y as f64), // bounding_volume::AABB::new(na::Point2::new(pos.x as f64, pos.y as f64),
na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64)) // na::Point2::new((pos.x + bounds.width) as f64, (pos.y + bounds.width) as f64))
}, // },
), // ),
( (
&block_sprite2, &block_sprite2,
{ {
@ -83,7 +106,7 @@ fn main() {
), ),
]; ];
let bvt = BVT::new_balanced(idx_and_bounding_spheres); let bvt = BVT::new_balanced(static_sprites);
let mut sprite = Sprite::new(); let mut sprite = Sprite::new();
sprite.set_texture(&spritesheet_text, false); sprite.set_texture(&spritesheet_text, false);
@ -173,7 +196,7 @@ fn main() {
window.draw(&collision_sprite); window.draw(&collision_sprite);
if interferences.len() == 0 { if interferences.len() == 0 {
window.draw(&block_sprite); // window.draw(&block_sprite);
window.draw(&block_sprite2); window.draw(&block_sprite2);
window.draw(&block_sprite3); window.draw(&block_sprite3);
} }

@ -97,7 +97,7 @@ impl<'s> Player<'s> {
pub fn update(&mut self, delta_t: f32) { pub fn update(&mut self, delta_t: f32) {
self.pos.x += self.delta.x * delta_t * 8.0; self.pos.x += self.delta.x * delta_t * 8.0;
self.pos.y += self.delta.y * delta_t * 8.0; self.pos.y += self.delta.y * delta_t * 8.0;
println!("{:?}", self.delta); //println!("{:?}", self.delta);
let friction = 10.0; let friction = 10.0;
let ratio = 1.0 / (1.0 + delta_t * friction); let ratio = 1.0 / (1.0 + delta_t * friction);
self.delta *= ratio; self.delta *= ratio;

@ -4,6 +4,9 @@ use std::collections::HashMap;
use std::borrow::Cow; use std::borrow::Cow;
use std::str::FromStr; use std::str::FromStr;
use sfml::graphics::IntRect; use sfml::graphics::IntRect;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
pub fn read_spritesheet(filename: String) -> HashMap<String, HashMap<String, i32>> { pub fn read_spritesheet(filename: String) -> HashMap<String, HashMap<String, i32>> {
@ -76,3 +79,10 @@ pub fn grab_sheet_rec(spritename: String, spritesheet: &HashMap<String, HashMap<
*block_desc.get("height").unwrap() *block_desc.get("height").unwrap()
) )
} }
pub fn read_map(filename: String) {
let file = File::open(filename).expect("Could not open file");
for line in BufReader::new(file).lines() {
println!("{:?}", line);
}
}
Loading…
Cancel
Save