1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use glium::backend::glutin::SimpleWindowBuilder;
use winit::{
    error::EventLoopError,
    event::*,
    event_loop::EventLoopWindowTarget,
    window::Window,
};
use winit_input_map::InputMap;
pub use glium;
pub use glium_types;
pub use winit;
pub use winit_input_map as input_map;

pub mod meshes;
pub mod shaders;

pub type Display = glium::Display<glium::glutin::surface::WindowSurface>;
pub type EventLoop = winit::event_loop::EventLoop<()>;

pub mod prelude {
    pub use glium::{
        draw_parameters, IndexBuffer,
        VertexBuffer, Program, Texture2d,
        uniform, Surface, Frame, DrawParameters
    };
    pub use glium_types::{vectors::*, mesh, matrices::*, quaternion::*, params};
    pub use crate::{meshes, shaders};
    pub use winit::keyboard::KeyCode;
    pub use winit::event::MouseButton;
    pub use winit::window::{Fullscreen, CursorGrabMode};
    pub use crate::input_map::*;
}
///used to quickly set up thin engine.
pub fn set_up() -> Result<(EventLoop, Window, Display), EventLoopError> {
    let event_loop = EventLoop::new()?;
    event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
    let (window, display) = SimpleWindowBuilder::new().build(&event_loop);
    Ok((event_loop, window, display))
}
/// used to quickly set up logic. handles closed and input events for you. the `logic` var will be
/// run every frame.
/// ```
/// let (event_loop, window, display) = thin_engine::set_up();
///
/// enum Actions{
///     Debug
/// }
/// impl Into<usize> for Actions{
///     fn into(self) -> usize{
///         self as usize
///     }
/// }
/// use Actions::*;
/// let mut input = thin_engine::Input::new([
///     (vec![InputCode::keycode(KeyCode::Space), Debug)
/// ]);
///
/// run(event_loop, &mut input, |_|{
///     let mut frame = display.draw();
///     frame.clear_color(0, 0, 0, 0);
///     frame.finish().unwrap();
/// });
/// ```
pub fn run<const BINDS: usize, F1>(
    event_loop: EventLoop,
    input: &mut InputMap<BINDS>,
    mut logic: F1,
) -> Result<(), EventLoopError>
where
    F1: FnMut(&mut InputMap<BINDS>),
{
    event_loop.run(|event, target| {
        input.update(&event);
        match &event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => target.exit(),
            Event::AboutToWait => {
                logic(input);
                input.init();
            }
            _ => (),
        }
    })?;
    Ok(())
}
/// used to quickly set up logic. handles closed and input events for you. the `logic` var will be
/// run every frame. the `event_handler` var is
/// for if you want more control over the event handling and is run multiple times before logic.
/// ```
/// let (event_loop, window, display) = thin_engine::set_up();
///
/// enum Actions{
///     Debug
/// }
/// impl Into<usize> for Actions{
///     fn into(self) -> usize{
///         self as usize
///     }
/// }
/// use Actions::*;
/// let mut input = thin_engine::Input::new([
///     (vec![InputCode::keycode(KeyCode::Space), Debug)
/// ]);
///
///
/// thin_engine::run_with_event_handler(
///     event_loop,
///     &mut input,
///     |event, target| {
///         match event {
///             //do something with events
///             _ => ()
///         }
///     }, |_|{
///         let mut frame = display.draw();
///         frame.clear_color(0, 0, 0, 1);
///         frame.finish().unwrap();
/// });
/// ```
pub fn run_with_event_handler<const BINDS: usize, F1, F2>(
    event_loop: EventLoop,
    input: &mut InputMap<BINDS>,
    mut event_handler: F2,
    mut logic: F1,
) -> Result<(), EventLoopError>
where
    F1: FnMut(&mut InputMap<BINDS>),
    F2: FnMut(&Event<()>, &EventLoopWindowTarget<()>),
{
    event_loop.run(|event, target| {
        input.update(&event);
        event_handler(&event, target);
        match &event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => target.exit(),
            Event::AboutToWait => {
                logic(input);
                input.init();
            }
            _ => (),
        }
    })?;
    Ok(())
}
/// resizable depth texture. recomended to  use with gliums `SimpleFrameBuffer` to draw onto a texture you can use
/// in another shader! usefull for fxaa
#[derive(Default)]
pub struct ResizableTexture2D {
    pub size: (u32, u32),
    pub texture: Option<glium::Texture2d>
}
impl ResizableTexture2D {
    pub fn resize(&mut self, display: &Display, new_size: (u32, u32)) {
        if self.size.0 != new_size.0 || self.size.1 != new_size.1 {
            self.texture = glium::Texture2d::empty(display, new_size.0, new_size.1).ok();
            self.size = new_size;
        }
    }
    pub fn resize_to_display(&mut self, display: &Display) {
        let new_size = display.get_framebuffer_dimensions();
        if self.size.0 != new_size.0 || self.size.1 != new_size.1 {
            self.texture = glium::Texture2d::empty(display, new_size.0, new_size.1).ok();
            self.size = new_size;
        }
    }
    /// borrows the texture or panics. to handle failed borrows use `self.texture.as_ref()` instead
    pub fn texture(&self) -> &glium::Texture2d {
        self.texture.as_ref().expect("texture was not initialised. maybe use 'new()' instead of 'default()'")
    }
    pub fn new(size: (u32, u32), display: &Display) -> Self {
        Self { size, texture: glium::Texture2d::empty(display, size.0, size.1).ok() }
    }
}
/// resizable depth texture. use with gliums `SimpleFrameBuffer::WithDepthTexture()` 
/// to create a texture you can draw on! usefull for things like fog.
#[derive(Default)]
pub struct ResizableDepthTexture2D {
    size: (u32, u32),
    pub texture: Option<glium::texture::DepthTexture2d>
}
impl ResizableDepthTexture2D {
    pub fn resize(&mut self, display: &Display, new_size: (u32, u32)) {
        if self.size.0 != new_size.0 || self.size.1 != new_size.1 {
            self.texture = glium::texture::DepthTexture2d::empty(display, new_size.0, new_size.1).ok();
            self.size = new_size;
        }
    }
    /// borrows the texture or panics. to handle failed borrows use `self.texture.as_ref()` instead
    pub fn texture(&self) -> &glium::texture::DepthTexture2d {
        self.texture.as_ref().expect("texture was not initialised. maybe use 'new()' instead of 'default()'")
    }
    pub fn new(size: (u32, u32), display: &Display) -> Self {
        Self { size, texture: glium::texture::DepthTexture2d::empty(display, size.0, size.1).ok() }
    }
    pub fn resize_to_display(&mut self, display: &Display) {
        let new_size = display.get_framebuffer_dimensions();
        if self.size.0 != new_size.0 || self.size.1 != new_size.1 {
            self.texture = glium::texture::DepthTexture2d::empty(display, new_size.0, new_size.1).ok();
            self.size = new_size;
        }
    }
}