You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/src/main.rs

264 lines
7.4 KiB

6 years ago
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
6 years ago
6 years ago
extern crate quick_xml;
extern crate sfml;
6 years ago
extern crate cgmath;
extern crate image;
extern crate rand;
6 years ago
mod slider;
6 years ago
mod timer;
mod input;
mod util;
6 years ago
use sfml::graphics::*;
use sfml::system::*;
use sfml::window::*;
6 years ago
use crate::timer::Timer;
use crate::input::Input;
use crate::slider::Slider;
6 years ago
extern crate nalgebra as na;
use image::{GenericImageView, GenericImage, DynamicImage, Pixel, SubImage};
6 years ago
use sfml::graphics::{
Color, RenderTarget, RenderWindow,
};
use sfml::window::{ Event, Key, Style};
use sfml::system::Vector2 as sfVec2;
// The container trait for all the shaders
trait Effect: Drawable {
fn update(&mut self, t: f32, x: f32, y: f32);
fn name(&self) -> &str;
fn as_drawable(&self) -> &Drawable;
}
// ======= LARGE MULTISPRITE SHADER DEMO ===========
struct Edge<'t> {
surface: RenderTexture,
bg_sprite: Sprite<'t>,
entities: Vec<Sprite<'t>>,
shader: Shader<'static>,
}
impl<'t> Edge<'t> {
fn new(bg_texture: &'t Texture, entity_texture: &'t Texture) -> Self {
let mut surface = RenderTexture::new(800, 600, false).unwrap();
surface.set_smooth(true);
let mut bg_sprite = Sprite::with_texture(bg_texture);
bg_sprite.set_position((135., 100.));
let mut entities = Vec::new();
for i in 0..6 {
let mut entity = Sprite::with_texture(entity_texture);
entity.set_texture_rect(&IntRect::new(96 * i, 0, 96, 96));
entities.push(entity);
}
let mut shader = Shader::from_file(None, None, Some("resources/edge.frag")).unwrap();
shader.set_uniform_current_texture("texture");
Self {
surface,
bg_sprite,
entities,
shader,
}
}
}
impl<'t> Drawable for Edge<'t> {
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
&'a self,
target: &mut RenderTarget,
mut states: RenderStates<'texture, 'shader, 'shader_texture>,
) {
states.shader = Some(&self.shader);
target.draw_with_renderstates(&Sprite::with_texture(self.surface.texture()), states);
}
}
impl<'t> Effect for Edge<'t> {
fn update(&mut self, t: f32, x: f32, y: f32) {
self.shader
.set_uniform_float("edge_threshold", 1. - (x + y) / 2.);
let entities_len = self.entities.len() as f32;
for (i, en) in self.entities.iter_mut().enumerate() {
let pos = (
(0.25 * (t * i as f32 + (entities_len - i as f32))).cos() * 300. + 350.,
(0.25 * (t * (entities_len - i as f32) + i as f32)).cos() * 200. + 250.,
);
en.set_position(pos);
}
self.surface.clear(&Color::WHITE);
self.surface.draw(&self.bg_sprite);
for en in &self.entities {
self.surface.draw(en);
}
self.surface.display();
}
fn as_drawable(&self) -> &Drawable {
self
}
fn name(&self) -> &str {
"edge post-effect"
}
}
fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec<image::Rgba<u8>> {
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, y+1) {pixels.push(img.get_pixel(x, y+1))}
6 years ago
if x > 0 {
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))}
}
6 years ago
if y > 0 {
if img.in_bounds(x+1, y-1) {pixels.push(img.get_pixel(x+1, y-1))}
if img.in_bounds(x, y-1) {pixels.push(img.get_pixel(x, y-1))}
if x > 0 {
if img.in_bounds(x - 1, y - 1) { pixels.push(img.get_pixel(x - 1, y - 1)) }
}
}
6 years ago
pixels
}
6 years ago
fn main() {
6 years ago
let mut img = image::open("test.jpg").unwrap();
let xy = img.dimensions();
6 years ago
println!("Starting");
for x in 0..xy.0 {
for y in 0..xy.1 {
let mut pixel = img.get_pixel(x, y);
let v = surrounding_pixels(x, y, &img);
let mut avg = v.first().unwrap().clone();
for p in v {
let r: u16 = (avg.data[0] as u16 + p.data[0] as u16);
let g: u16 = (avg.data[1] as u16 + p.data[1] as u16);
let b: u16 = (avg.data[2] as u16 + p.data[2] as u16);
let a: u16 = (avg.data[3] as u16 + p.data[3] as u16);
avg.data[0] = (r/2) as u8;
avg.data[1] = (g/2) as u8;
avg.data[2] = (b/2) as u8;
avg.data[3] = (a/2) as u8;
}
pixel.data[0] = avg.data[0];
pixel.data[1] = avg.data[1];
pixel.data[2] = avg.data[2];
pixel.data[3] = avg.data[3];
img.put_pixel(x, y, pixel);
}
}
println!("Ending");
img.save("fractal.png").unwrap();
6 years ago
let mut window = RenderWindow::new(
7 years ago
(512, 512),
6 years ago
"Custom drawable",
Style::CLOSE,
&Default::default(),
);
6 years ago
let mut timer = Timer::new();
let mut input = Input::new();
//==========================================
let font = Font::from_file("resources/sansation.ttf").unwrap();
let mut bg_texture = Texture::from_file("resources/sfml.png").unwrap();
bg_texture.set_smooth(true);
let mut entity_texture = Texture::from_file("resources/devices.png").unwrap();
entity_texture.set_smooth(true);
let mut effects: [Box<Effect>; 1] = [
Box::new(Edge::new(&bg_texture, &entity_texture)),
];
let mut current = 0;
let text_bg_texture = Texture::from_file("resources/text-background.png").unwrap();
let mut text_bg = Sprite::with_texture(&text_bg_texture);
text_bg.set_position((0., 520.));
text_bg.set_color(&Color::rgba(255, 255, 255, 200));
//==========================================
6 years ago
let mut slider = Slider::new(40.0, None);
6 years ago
let step_size: f32 = 0.005;
6 years ago
let mut elapsed_time: f32;
let mut delta_time: f32;
6 years ago
let mut accumulator_time: f32 = 0.0;
6 years ago
let mut current_time: f32 = timer.elap_time();
6 years ago
while window.is_open() {
6 years ago
while let Some(event) = window.poll_event() {
match event {
6 years ago
Event::Closed => return,
Event::KeyPressed { code, .. } => {
if code == Key::Escape {
return;
}
}
6 years ago
_ => {}
}
6 years ago
input.ingest(&event)
}
if input.is_held(Key::W) {
}
if input.is_held(Key::A) {
}
if input.is_held(Key::S) {
}
if input.is_held(Key::D) {
}
6 years ago
elapsed_time = timer.elap_time();
6 years ago
delta_time = elapsed_time - current_time;
current_time = elapsed_time;
if delta_time > 0.02 {
delta_time = 0.02;
6 years ago
}
6 years ago
accumulator_time += delta_time;
6 years ago
6 years ago
while (accumulator_time - step_size) >= step_size {
accumulator_time -= step_size;
}
6 years ago
let x = window.mouse_position().x as f32 / window.size().x as f32;
let y = window.mouse_position().y as f32 / window.size().y as f32;
effects[current].update(elapsed_time*1000.0, x, y);
6 years ago
window.clear(&Color::BLACK);
7 years ago
window.draw(effects[current].as_drawable());
window.draw(&slider);
6 years ago
window.display();
6 years ago
}
}