Init
This commit is contained in:
commit
a68b8bc000
13 changed files with 370 additions and 0 deletions
5
src/entities/entity.rs
Normal file
5
src/entities/entity.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
use crate::math::Transform;
|
||||
|
||||
pub trait Entity {
|
||||
fn get_transform(&mut self) -> &mut Transform;
|
||||
}
|
5
src/entities/mod.rs
Normal file
5
src/entities/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
mod entity;
|
||||
mod player;
|
||||
|
||||
pub use entity::Entity;
|
||||
pub use player::Player;
|
21
src/entities/player.rs
Normal file
21
src/entities/player.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use crate::math::Transform;
|
||||
use crate::entities::Entity;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
transform: Transform,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
pub fn new() -> Player {
|
||||
Player {
|
||||
transform: Transform::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity for Player {
|
||||
fn get_transform(&mut self) -> &mut Transform {
|
||||
&mut self.transform
|
||||
}
|
||||
}
|
16
src/main.rs
Normal file
16
src/main.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
mod render;
|
||||
mod entities;
|
||||
mod math;
|
||||
|
||||
use entities::Entity;
|
||||
use entities::Player;
|
||||
use render::debug;
|
||||
use render::vulkan::test;
|
||||
|
||||
fn main() {
|
||||
let mut player = Player::new();
|
||||
debug(&player);
|
||||
player.get_transform().translate(10.0, 20.0);
|
||||
debug(&player);
|
||||
test();
|
||||
}
|
5
src/math/mod.rs
Normal file
5
src/math/mod.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
mod tranform;
|
||||
mod vec2;
|
||||
|
||||
pub use vec2::Vec2;
|
||||
pub use tranform::Transform;
|
40
src/math/tranform.rs
Normal file
40
src/math/tranform.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use super::Vec2;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Transform {
|
||||
position: Vec2,
|
||||
rotation: Vec2,
|
||||
scale: Vec2
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
pub fn new() -> Transform {
|
||||
Transform {
|
||||
position: Vec2::new(),
|
||||
rotation: Vec2::new(),
|
||||
scale: Vec2::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_rotation(&self) -> &Vec2 {
|
||||
&self.rotation
|
||||
}
|
||||
|
||||
pub fn get_position(&self) -> &Vec2 {
|
||||
&self.position
|
||||
}
|
||||
|
||||
pub fn get_scale(&self) -> &Vec2 {
|
||||
&self.scale
|
||||
}
|
||||
|
||||
pub fn translate(&mut self, x: f64, y: f64) {
|
||||
self.position.x += x;
|
||||
self.position.y += y;
|
||||
}
|
||||
|
||||
pub fn rotate(&mut self, x: f64, y: f64) {
|
||||
self.rotation.x += x;
|
||||
self.rotation.y += y;
|
||||
}
|
||||
}
|
36
src/math/vec2.rs
Normal file
36
src/math/vec2.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
#[derive(Debug)]
|
||||
pub struct Vec2 {
|
||||
pub x: f64,
|
||||
pub y: f64
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
pub fn new() -> Vec2 {
|
||||
Vec2 {
|
||||
x: 0.0,
|
||||
y: 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add<Vec2> for Vec2 {
|
||||
type Output = Vec2;
|
||||
|
||||
fn add(self, b: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.x + b.x,
|
||||
y: self.y + b.y
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub<Vec2> for Vec2 {
|
||||
type Output = Vec2;
|
||||
|
||||
fn sub(self, b: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.x - b.x,
|
||||
y: self.y - b.y
|
||||
}
|
||||
}
|
||||
}
|
3
src/render/display.rs
Normal file
3
src/render/display.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub fn debug(data: &impl std::fmt::Debug) {
|
||||
println!("{:#?}", data);
|
||||
}
|
4
src/render/mod.rs
Normal file
4
src/render/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
mod display;
|
||||
pub mod vulkan;
|
||||
|
||||
pub use display::debug;
|
50
src/render/vulkan/mod.rs
Normal file
50
src/render/vulkan/mod.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use vulkano::sync::GpuFuture;
|
||||
use vulkano::command_buffer::CommandBuffer;
|
||||
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
||||
use vulkano::buffer::BufferUsage;
|
||||
use vulkano::buffer::CpuAccessibleBuffer;
|
||||
use vulkano::device::DeviceExtensions;
|
||||
use vulkano::device::Features;
|
||||
use vulkano::device::Device;
|
||||
use vulkano::instance::PhysicalDevice;
|
||||
use vulkano::instance::Instance;
|
||||
use vulkano::instance::InstanceExtensions;
|
||||
|
||||
pub fn test() {
|
||||
let instance = Instance::new(None, &InstanceExtensions::none(), None)
|
||||
.expect("Failed to create instance");
|
||||
|
||||
let physical = PhysicalDevice::enumerate(&instance).next().expect("No device available");
|
||||
|
||||
let queue_family = physical.queue_families()
|
||||
.find(|&q| q.supports_graphics())
|
||||
.expect("couldn't find a graphical queue family");
|
||||
|
||||
let (device, mut queues) = {
|
||||
Device::new(physical, &Features::none(), &DeviceExtensions::none(),
|
||||
[(queue_family, 0.5)].iter().cloned()).expect("failed to create device")
|
||||
};
|
||||
|
||||
let queue = queues.next().unwrap();
|
||||
|
||||
let source_content = 0 .. 64;
|
||||
let source = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), false,
|
||||
source_content).expect("failed to create buffer");
|
||||
|
||||
let dest_content = (0 .. 64).map(|_| 0);
|
||||
let dest = CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), false,
|
||||
dest_content).expect("failed to create buffer");
|
||||
|
||||
let mut builder = AutoCommandBufferBuilder::new(device.clone(), queue.family()).unwrap();
|
||||
builder.copy_buffer(source.clone(), dest.clone()).unwrap();
|
||||
let command_buffer = builder.build().unwrap();
|
||||
|
||||
let finished = command_buffer.execute(queue.clone()).unwrap();
|
||||
|
||||
finished.then_signal_fence_and_flush().unwrap()
|
||||
.wait(None).unwrap();
|
||||
|
||||
let src_content = source.read().unwrap();
|
||||
let dest_content = dest.read().unwrap();
|
||||
assert_eq!(&*src_content, &*dest_content);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue