Revised lib's API to better fit winit's
* switched to slave type API * started implementing winit's initialization
This commit is contained in:
parent
60d79e7d85
commit
ecbcbf0db3
@ -13,11 +13,34 @@ use std::{
|
|||||||
cell::RefCell,
|
cell::RefCell,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//--Application trait-------------------------------------------------------------------------------
|
||||||
|
pub trait Application {
|
||||||
|
|
||||||
|
fn init(&mut self, canvas: &mut Canvas) -> Result<(), &'static str>;
|
||||||
|
|
||||||
|
fn tick(&mut self, canvas: &mut Canvas) -> Result<(), &'static str>;
|
||||||
|
}
|
||||||
|
|
||||||
//--Canvas struct-----------------------------------------------------------------------------------
|
//--Canvas struct-----------------------------------------------------------------------------------
|
||||||
pub struct Canvas {}
|
pub struct Canvas {}
|
||||||
|
|
||||||
impl Canvas {
|
impl Canvas {
|
||||||
|
|
||||||
|
pub fn create<T: Into<String>, A: Application>(title: T, size: Size, app: A) {
|
||||||
|
use winit::{
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
let event_loop = EventLoop::new();
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_inner_size(size)
|
||||||
|
.with_resizable(false)
|
||||||
|
.with_title(title)
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
//--Create functions--
|
//--Create functions--
|
||||||
pub fn create_texture_sprite(&mut self, _size: Size, _scale: f32) -> TextureSprite {
|
pub fn create_texture_sprite(&mut self, _size: Size, _scale: f32) -> TextureSprite {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
|
|||||||
41
src/lib.rs
41
src/lib.rs
@ -1,5 +1,22 @@
|
|||||||
|
mod canvas;
|
||||||
|
pub use canvas::Canvas;
|
||||||
|
pub use canvas::Application;
|
||||||
|
pub mod io;
|
||||||
|
pub mod shape;
|
||||||
|
pub mod sprite;
|
||||||
|
pub mod texture;
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
|
use utils::Size;
|
||||||
|
|
||||||
|
pub fn create_vk_canvas<A: Application>(_title: &'static str, _size: Size, app: A) -> !
|
||||||
|
{
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::{Application, Canvas};
|
||||||
|
|
||||||
fn setup_logger() -> Result<(), fern::InitError> {
|
fn setup_logger() -> Result<(), fern::InitError> {
|
||||||
use fern::colors::{Color, ColoredLevelConfig};
|
use fern::colors::{Color, ColoredLevelConfig};
|
||||||
@ -27,20 +44,24 @@ mod tests {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
struct TestApp {}
|
||||||
fn vk_create() {
|
|
||||||
let canvas = crate::create_vk_canvas("vk_create", 1280, 720)
|
impl Application for TestApp {
|
||||||
.unwrap();
|
|
||||||
|
fn init(&mut self, _canvas: &mut Canvas) -> Result<(), &'static str> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tick(&mut self, _canvas: &mut Canvas) -> Result<(), &'static str> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn vk_run() {
|
fn vk_create() {
|
||||||
|
use crate::utils::Size;
|
||||||
|
|
||||||
let canvas = crate::create_vk_canvas("vk_run", 1280, 720)
|
crate::create_vk_canvas("vk_create", Size {w: 1280, h: 720}, TestApp {});
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
canvas.update();
|
|
||||||
canvas.destroy();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
77
src/main.rs
77
src/main.rs
@ -1,3 +1,4 @@
|
|||||||
|
use canvas::{Application, Canvas};
|
||||||
|
|
||||||
fn setup_logger() -> Result<(), fern::InitError> {
|
fn setup_logger() -> Result<(), fern::InitError> {
|
||||||
use fern::colors::{Color, ColoredLevelConfig};
|
use fern::colors::{Color, ColoredLevelConfig};
|
||||||
@ -25,15 +26,22 @@ fn setup_logger() -> Result<(), fern::InitError> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), &'static str> {
|
struct ExampleApp {}
|
||||||
|
|
||||||
setup_logger()
|
impl Application for ExampleApp {
|
||||||
.map_err(|_| "Failed to setup logger")?;
|
|
||||||
|
|
||||||
let canvas = crate::create_vk_canvas("vk_example", 1280, 720)
|
fn init(&mut self, _canvas: &mut Canvas) -> Result<(), &'static str> {
|
||||||
.unwrap();
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tick(&mut self, canvas: &mut Canvas) -> Result<(), &'static str> {
|
||||||
|
use canvas::{
|
||||||
|
io::Key,
|
||||||
|
sprite::Sprite,
|
||||||
|
utils::{Position, Size, Color, Pixel},
|
||||||
|
shape::{Shape, Rectangle},
|
||||||
|
};
|
||||||
|
|
||||||
while canvas.should_exit() == false {
|
|
||||||
// inputs
|
// inputs
|
||||||
if canvas.key_pressed(Key::A) {
|
if canvas.key_pressed(Key::A) {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
@ -43,57 +51,68 @@ fn main() -> Result<(), &'static str> {
|
|||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
|
||||||
match canvas.get_keys() {
|
match canvas.get_key_presses() {
|
||||||
|
Key::A => unimplemented!(),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
match canvas.get_key_releases() {
|
||||||
Key::A => unimplemented!(),
|
Key::A => unimplemented!(),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mouse = canvas.get_mouse();
|
let _mouse = canvas.get_mouse_position();
|
||||||
|
|
||||||
// outputs
|
// outputs
|
||||||
canvas.set_clear_color(Color::black);
|
canvas.set_clear_color(Color::BLACK);
|
||||||
|
canvas.clear();
|
||||||
|
|
||||||
// white rectangle inset by one in the sprite
|
// white rectangle inset by one in the sprite
|
||||||
let bas_sprite = canvas.create_basic_sprite(Size {w: 40, h: 40}, 1.0);
|
let mut bas_sprite = canvas.create_shape_sprite();
|
||||||
bas_sprite.set_shape(
|
bas_sprite.set_shape(Shape::Rectangle (Rectangle {size: Size {w: 38, h: 38}}));
|
||||||
Shape::Rectangle (Position {x: 1, y: 1},
|
bas_sprite.set_color(Color::WHITE);
|
||||||
Size {w: 38, h: 38},
|
|
||||||
Color::White,
|
|
||||||
));
|
|
||||||
canvas.draw(bas_sprite);
|
canvas.draw(bas_sprite);
|
||||||
|
|
||||||
// 20 x 20 sprite of a picture
|
// 20 x 20 sprite of a picture
|
||||||
let texture = canvas.create_texture_from_file(
|
let texture = canvas.create_texture_from_file(
|
||||||
Position {x: 0, y: 0},
|
Position {x: 0, y: 0},
|
||||||
Some("texture.png"),
|
"texture.png",
|
||||||
Some(Color::White)
|
Some(Color::WHITE)
|
||||||
);
|
);
|
||||||
pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 1.0);
|
let mut pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 1.0);
|
||||||
pix_sprite.set_texture(texture, Some(Position {w: 0, h: 0}));
|
pix_sprite.set_texture(texture, Some(Position {x: 0, y: 0}));
|
||||||
canvas.draw(pix_sprite);
|
canvas.draw(pix_sprite);
|
||||||
|
|
||||||
// scaled sprite of a manually drawed texture
|
// scaled sprite of a manually drawed texture
|
||||||
let texture = canvas.create_texture(Size {w: 20, h: 20}, Some(Color::White));
|
let texture = canvas.create_texture(Size {w: 20, h: 20}, Some(Color::WHITE));
|
||||||
texture.set_pixel(Position {x: 0, y: 0}, Pixel::rgb(0, 255, 0));
|
texture.borrow_mut().set_pixel(Position {x: 0, y: 0}, Pixel::rgb(0, 255, 0));
|
||||||
let _ = texture.pixels();
|
let _ = texture.borrow_mut().iter_mut(); //iterate on whole texture
|
||||||
pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 2.0);
|
pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 2.0);
|
||||||
pix_sprite.set_texture(texture, None);
|
pix_sprite.set_texture(texture, None);
|
||||||
|
let _ = pix_sprite.iter_mut(); //only iterate on the part used by the sprite
|
||||||
canvas.draw(pix_sprite);
|
canvas.draw(pix_sprite);
|
||||||
|
|
||||||
// floating text
|
// floating text
|
||||||
let txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
|
let mut txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
|
||||||
txt_sprite.set_text("text", None);
|
txt_sprite.set_text("text");
|
||||||
|
txt_sprite.set_color(Color::BLACK);
|
||||||
|
|
||||||
// generic operations
|
// generic operations
|
||||||
txt_sprite.set_position(Position {x: 0, y: 0});
|
txt_sprite.set_position(Position {x: 0, y: 0});
|
||||||
txt_sprite.set_rotation(50);
|
txt_sprite.set_rotation(50.0);
|
||||||
txt_sprite.set_alpha(255);
|
txt_sprite.set_alpha(255);
|
||||||
|
txt_sprite.set_scale(0.5);
|
||||||
canvas.draw(txt_sprite);
|
canvas.draw(txt_sprite);
|
||||||
|
|
||||||
canvas.update();
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
canvas.destroy();
|
|
||||||
Ok(())
|
fn main() -> Result<(), &'static str> {
|
||||||
|
use canvas::utils::Size;
|
||||||
|
|
||||||
|
setup_logger()
|
||||||
|
.map_err(|_| "Failed to setup logger")?;
|
||||||
|
canvas::create_vk_canvas("vk_example", Size {w: 1280, h: 720}, ExampleApp {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
src/utils.rs
13
src/utils.rs
@ -1,6 +1,8 @@
|
|||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use log::{debug, error, info, trace, warn};
|
use log::{debug, error, info, trace, warn};
|
||||||
|
|
||||||
|
use winit;
|
||||||
|
|
||||||
//--Position struct---------------------------------------------------------------------------------
|
//--Position struct---------------------------------------------------------------------------------
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
pub x: u32,
|
pub x: u32,
|
||||||
@ -13,6 +15,17 @@ pub struct Size {
|
|||||||
pub h: u32,
|
pub h: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<Size> for winit::dpi::Size {
|
||||||
|
|
||||||
|
fn from(size: Size) -> Self {
|
||||||
|
winit::dpi::Size::Logical (
|
||||||
|
winit::dpi::LogicalSize {
|
||||||
|
width: size.w.into(),
|
||||||
|
height: size.h.into()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//--Pixel struct------------------------------------------------------------------------------------
|
//--Pixel struct------------------------------------------------------------------------------------
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
struct Rgba {
|
struct Rgba {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user