gutted and playing with pixel manipulation

master
mitchellhansen 6 years ago
parent 94ce0bcd30
commit f844982553

@ -11,7 +11,6 @@ cgmath = "0.17.0"
simple-stopwatch="0.1.4" simple-stopwatch="0.1.4"
ncollide2d = "0.19.1" ncollide2d = "0.19.1"
nalgebra = "0.18.0" nalgebra = "0.18.0"
image = "0.21.2"

@ -1,52 +0,0 @@
use crate::entstate::EntState;
use sfml::graphics::Sprite;
use sfml::graphics::Transformable;
use ncollide2d::partitioning::BVT;
use ncollide2d::bounding_volume::AABB;
use ncollide2d::bounding_volume;
use std::cell::Ref;
pub struct Collision<'a> {
dynamic_bvh : Option<BVT<&'a Sprite<'a>, AABB<f64>>>,
static_bvh : Option<BVT<&'a Sprite<'a>, AABB<f64>>>,
}
impl<'a> Collision<'a> {
pub fn new() -> Collision<'a> {
Collision {
dynamic_bvh: Option::None,
static_bvh: Option::None,
}
}
pub fn gen_bvt(mut self, entity_state: &'a EntState<'a>) {
// let mut dynamic_sprites: Vec<(&'a Sprite<'a>, AABB<f64>)>= (Vec::new());
// {
// let vec = entity_state.dynamic_entities.borrow();
// for i in vec.iter() {
// let bounds = i.global_bounds();
// let pos = i.position();
// let volume = 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));
//
// dynamic_sprites.push((i, volume));
// }
// }
// self.dynamic_bvh = Some(BVT::new_balanced(dynamic_sprites));
// let mut static_sprites: Vec<(&Sprite, AABB<f64>)> = Vec::new();
// {
// for i in self.static_entities {
// let bounds = i.local_bounds();
// let pos = i.position();
// let volume = 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));
//
// static_sprites.push((i, volume));
// }
// }
// self.static_bvh = Some(BVT::new_balanced(static_sprites));
}
}

@ -1,131 +0,0 @@
use std::collections::HashMap;
use sfml::graphics::Texture;
use crate::util;
use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;
use sfml::graphics::Sprite;
use sfml::graphics::Transformable;
use std::cell::RefCell;
use crate::player::Player;
use ncollide2d::partitioning::BVT;
use ncollide2d::bounding_volume::AABB;
use crate::resources::Resources;
use ncollide2d::bounding_volume;
use std::rc::Rc;
pub struct EntState<'a> {
pub dynamic_entities: Rc<RefCell<Vec< Sprite<'a> >>>,
pub static_entities : Rc<RefCell<Vec< Sprite<'a> >>>,
pub player : Player<'a>,
}
impl<'a> EntState<'a> {
pub fn new() -> EntState<'a> {
EntState{
dynamic_entities: Rc::new(RefCell::new(Vec::new())),
static_entities: Rc::new(RefCell::new(Vec::new())),
player: Player::new(),
}
}
pub fn read_static_entities(mut self, filename: String, resources: &'a Resources) {
let mut sprite : Sprite = Sprite::new();
sprite.set_texture(&resources.spritesheet_text, false);
sprite.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &resources.spritesheet_desc));
sprite.set_position((0.0,0.0));
//self.static_entities.get_mut().push(sprite);
// let file = File::open(filename).expect("Could not open file");
//
// let mut first_line: bool = true;
// let mut w: f32 = 0.0;
// let mut h: f32 = 0.0;
//
// let mut x: i32;
// let mut y: i32 = 0;
//
//
// for line in BufReader::new(file).lines() {
//
// if first_line {
//
// first_line = !first_line;
// let val = line.unwrap();
// let arr : Vec<&str> = val.split_whitespace().collect();
//
// w = arr.get(0).unwrap().parse::<f32>().unwrap();
// h = arr.get(1).unwrap().parse::<f32>().unwrap();
//
// } else {
//
// x = 0;
//
// let val = line.unwrap();
// for i in val.split_whitespace() {
// match i.parse::<i32>().unwrap() {
// 0 => {
// // Do nothing for now
// }
// 1 => {
// let mut sprite : Sprite<'a> = Sprite::new();
// sprite.set_texture(&self.spritesheet_text, false);
// sprite.set_texture_rect(&util::grab_sheet_rec(String::from("blockBrown.png"), &self.spritesheet_desc));
// sprite.set_position((x as f32 * w, y as f32 * h));
//
// self.static_entities.push(RefCell::new(sprite));
// }
// _ => {
// panic!("ahhhhhh");
// }
// }
// x += 1;
// }
// y += 1;
// }
// }
}
// pub fn read_dynamic_entities(&'a mut self, filename: String) {
//
// let file = File::open(filename).expect("Could not open file");
//
// for line in BufReader::new(file).lines() {
//
// let val = line.unwrap();
// let arr : Vec<&str> = val.split_whitespace().collect();
//
// let e = arr.get(0).unwrap();
// let x = arr.get(1).unwrap().parse::<f32>().unwrap();
// let y = arr.get(2).unwrap().parse::<f32>().unwrap();
//
// match *e {
// "enemy" => {
// let mut sprite = Sprite::new();
// sprite.set_texture(&self.spritesheet_text, false);
// sprite.set_texture_rect(&util::grab_sheet_rec(String::from("enemyFloating_1.png"), &self.spritesheet_desc));
// sprite.set_position((x, y));
//
// self.dynamic_entities.push(RefCell::new(sprite));
// }
// "player" => {
// let mut sprite = Sprite::new();
// sprite.set_texture(&self.spritesheet_text, false);
// sprite.set_texture_rect(&util::grab_sheet_rec(String::from("playerBlue_up3.png"), &self.spritesheet_desc));
// sprite.set_position((x, y));
//
// self.dynamic_entities.push(RefCell::new(sprite));
// }
// _ => {
// // Do nothing
// }
// }
// }
// }
}

@ -5,24 +5,20 @@
extern crate quick_xml; extern crate quick_xml;
extern crate sfml; extern crate sfml;
extern crate cgmath; extern crate cgmath;
extern crate image;
mod slider;
mod timer; mod timer;
mod player;
mod input; mod input;
mod util; mod util;
mod entstate;
mod resources;
mod collision;
use crate::player::Player;
use crate::timer::Timer; use crate::timer::Timer;
use crate::input::Input; use crate::input::Input;
use crate::entstate::EntState; use crate::slider::Slider;
use crate::resources::Resources;
use crate::collision::Collision;
extern crate nalgebra as na; extern crate nalgebra as na;
extern crate ncollide2d;
use image::{GenericImageView, GenericImage, DynamicImage, Pixel, SubImage};
use sfml::graphics::{ use sfml::graphics::{
Color, RenderTarget, RenderWindow, Color, RenderTarget, RenderWindow,
@ -30,34 +26,48 @@ use sfml::graphics::{
use sfml::window::{ Event, Key, Style}; use sfml::window::{ Event, Key, Style};
use sfml::system::Vector2 as sfVec2; use sfml::system::Vector2 as sfVec2;
fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec<image::Rgba<u8>> {
fn main() { let mut pixels: Vec<image::Rgba<u8>> = Vec::new();
if img.in_bounds(x+1, y+1) {pixels.push(img.get_pixel(x+1, y+1))}
if img.in_bounds(x+1, y) {pixels.push(img.get_pixel(x+1, y))}
if img.in_bounds(x+1, y-1) {pixels.push(img.get_pixel(x+1, y-1))}
// Resources if img.in_bounds(x, y+1) {pixels.push(img.get_pixel(x, y+1))}
// Holds textures and their descriptions if img.in_bounds(x, y-1) {pixels.push(img.get_pixel(x, y-1))}
// Collision if img.in_bounds(x-1, y+1) {pixels.push(img.get_pixel(x-1, y+1))}
// Holds the BVT which has a reference or owns the Sprite if img.in_bounds(x-1, y) {pixels.push(img.get_pixel(x-1, y))}
if img.in_bounds(x-1, y-1) {pixels.push(img.get_pixel(x-1, y-1))}
pixels
}
// EntState fn main() {
// Holds the entities and the player
// Entities (Sprites) have a texture so Resources must live longer let mut img = image::open("test.jpg").unwrap();
// Collision has references to sprites so EntState must live longer let xy = img.dimensions();
// Resources { EntState { Collision } } } for x in 0..xy.0 {
for y in 0..xy.1 {
let mut pixel = img.get_pixel(x, y);
let mut resources = Resources::new(); let v = surrounding_pixels(x, y, &img);
let mut collision = Collision::new();
let mut state = EntState::new();
// state.read_static_entities(String::from("static_entities.txt"), &resources); // let mut avg: Pixel;
//state.read_static_entities(String::from("static_entities.txt"), &resources); //
//state.read_dynamic_entities(String::from("dynamic_entities.txt")); // for p in v {
// avg += p;
// }
//state.gen_bvt(); pixel.data[0] = 1;
img.put_pixel(x, y, pixel);
}
}
img.save("fractal.png").unwrap();
let mut window = RenderWindow::new( let mut window = RenderWindow::new(
(512, 512), (512, 512),
@ -66,11 +76,12 @@ fn main() {
&Default::default(), &Default::default(),
); );
let mut player = Player::new();
let mut timer = Timer::new(); let mut timer = Timer::new();
let mut input = Input::new(); let mut input = Input::new();
let mut slider = Slider::new(40.0, None);
let step_size: f32 = 0.005; let step_size: f32 = 0.005;
let mut elapsed_time: f32; let mut elapsed_time: f32;
let mut delta_time: f32; let mut delta_time: f32;
@ -93,16 +104,12 @@ fn main() {
} }
if input.is_held(Key::W) { if input.is_held(Key::W) {
player.impulse(&sfVec2::new(0.0, -1.0));
} }
if input.is_held(Key::A) { if input.is_held(Key::A) {
player.impulse(&sfVec2::new(-1.0, 0.0));
} }
if input.is_held(Key::S) { if input.is_held(Key::S) {
player.impulse(&sfVec2::new(0.0, 1.0));
} }
if input.is_held(Key::D) { if input.is_held(Key::D) {
player.impulse(&sfVec2::new(1.0, 0.0));
} }
elapsed_time = timer.elap_time(); elapsed_time = timer.elap_time();
@ -117,34 +124,9 @@ fn main() {
accumulator_time -= step_size; accumulator_time -= step_size;
} }
// // intersection test
// let mut interferences = Vec::new();
// {
// // Get the AABB bounding box
// let (bv, _) = player.future_bounding_aabb(delta_time);
// let mut thing = BoundingVolumeInterferencesCollector::new(&bv, &mut interferences);
// bvt.visit(&mut thing);
// }
//
// let collision_rect = player.collision(&interferences, delta_time);
player.update(delta_time);
//
// let mut collision_sprite = RectangleShape::new();
// collision_sprite.set_position((collision_rect.left, collision_rect.top));
// collision_sprite.set_size((collision_rect.width, collision_rect.height));
window.clear(&Color::BLACK); window.clear(&Color::BLACK);
window.draw(&player);
//window.draw(&collision_sprite);
// for ent in state.static_entities.get_mut().iter() { window.draw(&slider);
// window.draw(ent);
// }
//
// for ent in state.dynamic_entities.get_mut().iter() {
// window.draw(ent);
// }
window.display(); window.display();

@ -1,133 +0,0 @@
use sfml::system::Vector2;
use sfml::graphics::{CircleShape, Color, Drawable,
RenderStates, RenderTarget};
use sfml::graphics::Transformable;
use sfml::graphics::Shape;
use sfml::graphics::Sprite;
use ncollide2d::bounding_volume::AABB;
use sfml::graphics::FloatRect;
pub struct Player<'s> {
head: CircleShape<'s>,
delta: Vector2<f32>,
pub pos: Vector2<f32>,
default_impulse: f32,
}
impl<'s> Player<'s> {
pub fn impulse(&mut self, delta_v: &Vector2<f32>) {
self.delta.x += delta_v.x * self.default_impulse;
self.delta.y += delta_v.y * self.default_impulse;
}
pub fn velocity(&mut self, delta_v: &Vector2<f32>) {
self.delta.x = delta_v.x * self.default_impulse;
self.delta.y = delta_v.y * self.default_impulse;
}
pub fn collision(&mut self, objects: &Vec<&Sprite>, delta_t: f32) -> FloatRect {
let mut collided = FloatRect::new(0.0,0.0,0.0,0.0);
let (_, future_bounding) = self.future_bounding_aabb(delta_t);
for i in objects {
match future_bounding.intersection(&i.global_bounds()) {
Some(overlap) => {
// Get the bounds of the object we're intersecting
let intersector = &i.global_bounds();
let bounding_box = future_bounding;
let mut deflection = self.delta;
let mut reposition = self.pos;
if overlap.width < overlap.height {
if bounding_box.left + bounding_box.width >= intersector.left &&
bounding_box.left < intersector.left {
deflection.x = -0.1 * delta_t;
reposition.x = intersector.left - bounding_box.width - 1.0;
} else if bounding_box.left <= intersector.left + intersector.width &&
bounding_box.left + bounding_box.width > intersector.left + bounding_box.width {
deflection.x = 0.1 * delta_t;
reposition.x = intersector.left + intersector.width + 1.0;
}
} else {
if bounding_box.top + bounding_box.height >= intersector.top &&
bounding_box.top < intersector.top {
deflection.y = -0.1 * delta_t;
reposition.y = intersector.top - bounding_box.height - 1.0;
} else if bounding_box.top <= intersector.top + intersector.height &&
bounding_box.top + bounding_box.height > intersector.top + bounding_box.height{
deflection.y = 0.1 * delta_t;
reposition.y = intersector.top + intersector.height + 1.0;
}
}
self.head.set_position(reposition);
self.pos = reposition;
self.velocity(&deflection);
collided = overlap;
},
None => continue
}
}
return collided;
}
pub fn future_bounding_aabb(&mut self, delta_t: f32) -> (AABB<f64>, FloatRect) {
let mut bounds = self.head.global_bounds();
bounds.left += self.delta.x * delta_t * 8.0;
bounds.top += self.delta.y * delta_t * 8.0;
let a = na::Point2::new(bounds.left as f64, bounds.top as f64);
let b = na::Point2::new((bounds.left + bounds.width) as f64, (bounds.top + bounds.height) as f64);
(AABB::new(a, b), bounds)
}
pub fn update(&mut self, delta_t: f32) {
self.pos.x += self.delta.x * delta_t * 8.0;
self.pos.y += self.delta.y * delta_t * 8.0;
//println!("{:?}", self.delta);
let friction = 10.0;
let ratio = 1.0 / (1.0 + delta_t * friction);
self.delta *= ratio;
// Gravity
// self.delta.y += 45.0 * delta_t;
self.head.set_position((self.pos.x, self.pos.y));
}
pub fn new() -> Self {
let mut delta = Vector2::new(0.0, 0.0);
let mut pos = Vector2::new(0.0, 0.0);
let mut head = CircleShape::new(10.0, 10);
head.set_position((delta.x, delta.y));
head.set_fill_color(&Color::RED);
Self { head, delta, pos,
default_impulse: 10.0}
}
}
impl<'s> Drawable for Player<'s> {
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
&'a self,
render_target: &mut RenderTarget,
_: RenderStates<'texture, 'shader, 'shader_texture>,
) {
render_target.draw(&self.head);
}
}

@ -1,19 +0,0 @@
use crate::util;
use sfml::graphics::Texture;
use std::collections::HashMap;
pub struct Resources {
pub spritesheet_desc : HashMap<String, HashMap<String, i32>>,
pub spritesheet_text : Texture,
}
impl Resources {
pub fn new() -> Resources {
Resources {
spritesheet_desc: util::read_spritesheet(String::from("spritesheet_complete.xml")),
spritesheet_text: Texture::from_file("spritesheet_complete.png")
.expect("Couldn't load texture")
}
}
}

@ -0,0 +1,51 @@
use sfml::window::{Key, Event};
use sfml::graphics::{
CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape,
Transformable,
};
pub struct Slider<'a> {
value: f32,
left_button: RectangleShape<'a>,
right_button: RectangleShape<'a>,
}
impl<'a> Slider<'a> {
pub fn new(y_position: f32, value: Option<f32>) -> Self {
let val = match value {
Some(v) => v,
None => 0.0,
};
let mut slider = Slider {
value: val,
left_button: RectangleShape::new(),
right_button: RectangleShape::new(),
};
slider.left_button.set_position((0.0,y_position));
slider.right_button.set_position((50.0,y_position));
slider.left_button.set_size((20.0, 30.0));
slider.right_button.set_size((20.0, 30.0));
slider.left_button.set_fill_color(&sfml::graphics::Color::GREEN);
slider.right_button.set_fill_color(&sfml::graphics::Color::GREEN);
slider
}
}
impl<'s> Drawable for Slider<'s> {
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
&'a self,
render_target: &mut RenderTarget,
_: RenderStates<'texture, 'shader, 'shader_texture>,
) {
render_target.draw(&self.left_button);
render_target.draw(&self.right_button)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 MiB

Loading…
Cancel
Save