Revised lib's API to better fit winit's

* switched to slave type API
* started implementing winit's initialization
This commit is contained in:
Steins7 2022-06-29 21:13:29 +02:00
parent 60d79e7d85
commit ecbcbf0db3
4 changed files with 115 additions and 39 deletions

View File

@ -13,11 +13,34 @@ use std::{
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-----------------------------------------------------------------------------------
pub struct 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--
pub fn create_texture_sprite(&mut self, _size: Size, _scale: f32) -> TextureSprite {
unimplemented!();

View File

@ -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)]
mod tests {
use crate::{Application, Canvas};
fn setup_logger() -> Result<(), fern::InitError> {
use fern::colors::{Color, ColoredLevelConfig};
@ -27,20 +44,24 @@ mod tests {
Ok(())
}
#[test]
fn vk_create() {
let canvas = crate::create_vk_canvas("vk_create", 1280, 720)
.unwrap();
struct TestApp {}
impl Application for TestApp {
fn init(&mut self, _canvas: &mut Canvas) -> Result<(), &'static str> {
Ok(())
}
fn tick(&mut self, _canvas: &mut Canvas) -> Result<(), &'static str> {
Ok(())
}
}
#[test]
fn vk_run() {
fn vk_create() {
use crate::utils::Size;
let canvas = crate::create_vk_canvas("vk_run", 1280, 720)
.unwrap();
canvas.update();
canvas.destroy();
crate::create_vk_canvas("vk_create", Size {w: 1280, h: 720}, TestApp {});
}
}

View File

@ -1,3 +1,4 @@
use canvas::{Application, Canvas};
fn setup_logger() -> Result<(), fern::InitError> {
use fern::colors::{Color, ColoredLevelConfig};
@ -25,15 +26,22 @@ fn setup_logger() -> Result<(), fern::InitError> {
Ok(())
}
fn main() -> Result<(), &'static str> {
struct ExampleApp {}
setup_logger()
.map_err(|_| "Failed to setup logger")?;
impl Application for ExampleApp {
let canvas = crate::create_vk_canvas("vk_example", 1280, 720)
.unwrap();
fn init(&mut self, _canvas: &mut Canvas) -> Result<(), &'static str> {
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
if canvas.key_pressed(Key::A) {
unimplemented!();
@ -43,57 +51,68 @@ fn main() -> Result<(), &'static str> {
unimplemented!();
}
match canvas.get_keys() {
match canvas.get_key_presses() {
Key::A => unimplemented!(),
_ => (),
}
match canvas.get_key_releases() {
Key::A => unimplemented!(),
_ => (),
}
let mouse = canvas.get_mouse();
let _mouse = canvas.get_mouse_position();
// outputs
canvas.set_clear_color(Color::black);
canvas.set_clear_color(Color::BLACK);
canvas.clear();
// white rectangle inset by one in the sprite
let bas_sprite = canvas.create_basic_sprite(Size {w: 40, h: 40}, 1.0);
bas_sprite.set_shape(
Shape::Rectangle (Position {x: 1, y: 1},
Size {w: 38, h: 38},
Color::White,
));
let mut bas_sprite = canvas.create_shape_sprite();
bas_sprite.set_shape(Shape::Rectangle (Rectangle {size: Size {w: 38, h: 38}}));
bas_sprite.set_color(Color::WHITE);
canvas.draw(bas_sprite);
// 20 x 20 sprite of a picture
let texture = canvas.create_texture_from_file(
Position {x: 0, y: 0},
Some("texture.png"),
Some(Color::White)
"texture.png",
Some(Color::WHITE)
);
pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 1.0);
pix_sprite.set_texture(texture, Some(Position {w: 0, h: 0}));
let mut pix_sprite = canvas.create_texture_sprite(Size {w: 20, h: 20}, 1.0);
pix_sprite.set_texture(texture, Some(Position {x: 0, y: 0}));
canvas.draw(pix_sprite);
// scaled sprite of a manually drawed texture
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));
let _ = texture.pixels();
let texture = canvas.create_texture(Size {w: 20, h: 20}, Some(Color::WHITE));
texture.borrow_mut().set_pixel(Position {x: 0, y: 0}, Pixel::rgb(0, 255, 0));
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.set_texture(texture, None);
let _ = pix_sprite.iter_mut(); //only iterate on the part used by the sprite
canvas.draw(pix_sprite);
// floating text
let txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
txt_sprite.set_text("text", None);
let mut txt_sprite = canvas.create_text_sprite(Size {w: 10, h: 10}, 5.0);
txt_sprite.set_text("text");
txt_sprite.set_color(Color::BLACK);
// generic operations
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_scale(0.5);
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 {});
}

View File

@ -1,6 +1,8 @@
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use winit;
//--Position struct---------------------------------------------------------------------------------
pub struct Position {
pub x: u32,
@ -13,6 +15,17 @@ pub struct Size {
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------------------------------------------------------------------------------------
#[derive(Copy, Clone)]
struct Rgba {