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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! A thin game engine (hence the name). Drawing done with `glium`, game variables done with
//! `glium-types`, windowing done with `winit` and input done with `winit-input-map`. It has easy fxaa
//! support and low boilerplate despite having lots of control.
//! ```
//! use thin_engine::{prelude::*, meshes::screen};
//! use Action::*;
//!
//! #[derive(ToUsize)]
//! enum Action {
//!     Left,
//!     Right,
//!     Jump,
//!     Exit
//! }
//! let (event_loop, window, display) = thin_engine::set_up().unwrap();
//! let mut input = input_map!(
//!     (Left,  KeyCode::KeyA, MouseButton::Left, KeyCode::ArrowLeft),
//!     (Right, KeyCode::KeyD, MouseButton::Right, KeyCode::ArrowRight),
//!     (Jump,  KeyCode::KeyW, KeyCode::ArrowUp, KeyCode::Space),
//!     (Exit,  KeyCode::Escape)
//! );
//! let (box_indices, box_verts) = mesh!(
//!     &display, &screen::INDICES, &screen::VERTICES
//! );
//! let box_shader = Program::from_source(
//! &display, shaders::VERTEX, 
//! "#version 140
//! out vec4 colour;
//! void main() {
//!     colour = vec4(1.0, 0.0, 0.0, 1.0);
//! }", None).unwrap();
//! 
//! let mut player_pos = Vec2::ZERO;
//! let mut player_gravity = 0.0;
//! let mut player_can_jump = true;
//!
//! // camera matrix must be inverse
//! let camera = Mat4::from_scale(Vec3::splat(10.0)).inverse();
//! 
//! let target_delay = Duration::from_secs_f32(1.0/60.0); // target of 60 fps
//! let mut delta_time = 0.016; // change in time between frames
//! thin_engine::run(event_loop, &mut input, |input, target| {
//!     let frame_start = Instant::now();
//!     // set up frame
//!     let size = window.inner_size().into();
//!     display.resize(size);
//!     let mut frame = display.draw();
//!     let view2d = Mat4::view_matrix_2d(size);
//!     
//!     // game logic
//!     player_pos.x += input.axis(Right, Left) * 10.0 * delta_time;
//!     player_gravity += delta_time * 50.0;
//!     player_pos.y -= player_gravity * delta_time;
//!     if player_pos.y < 0.0 {
//!         player_pos.y = 0.0;
//!         player_can_jump = true;
//!     }
//!     if player_can_jump && input.pressed(Jump) {
//!         player_gravity = -20.0;
//!         player_can_jump = false;
//!     }
//!
//!     // draw
//!     frame.clear_color(0.0, 0.0, 0.0, 1.0);
//!     frame.draw(
//!         &box_verts, &box_indices, &box_shader, &uniform! {
//!             view: view2d, camera: camera,
//!             model: Mat4::from_pos(player_pos.extend(0.0)),
//!         }, &DrawParameters::default()
//!     );
//!     
//!     frame.finish().unwrap();
//!     if input.pressed(Exit) { target.exit() }
//!     thread::sleep(target_delay.saturating_sub(frame_start.elapsed()));
//!     delta_time = frame_start.elapsed().as_secs_f32();
//! }).unwrap();
//! ```

use glium::backend::glutin::SimpleWindowBuilder;
use winit::{
    error::EventLoopError,
    event::*,
    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 type WindowTarget = winit::event_loop::EventLoopWindowTarget<()>;

pub mod prelude {
    pub use glium::{
        draw_parameters, IndexBuffer,
        VertexBuffer, Program, Texture2d,
        uniform, Surface, Frame, DrawParameters
    };
    pub use std::time::{Duration, Instant};
    pub use std::thread;
    pub use glium_types::prelude::*;
    pub use crate::{meshes, shaders};
    pub use winit::event::MouseButton;
    pub use winit::keyboard::KeyCode;
    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.
/// ```
/// use thin_engine::prelude::*;
/// let (event_loop, window, display) = thin_engine::set_up().unwrap();
/// #[derive(ToUsize)]
/// enum Actions{
///     Debug
/// }
/// use Actions::*;
/// let mut input = input_map!(
///     (Debug, KeyCode::Space)
/// );
///
/// thin_engine::run(event_loop, &mut input, |_, _| {
///     let mut frame = display.draw();
///     frame.clear_color(0.0, 0.0, 0.0, 1.0);
///     frame.finish().unwrap();
/// });
/// ```
pub fn run<const BINDS: usize, F>(
    event_loop: EventLoop,
    input: &mut InputMap<BINDS>,
    mut logic: F,
) -> Result<(), EventLoopError>
where
    F: FnMut(&mut InputMap<BINDS>, &WindowTarget),
{
    event_loop.run(|event, target| {
        input.update(&event);
        match &event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => target.exit(),
            Event::AboutToWait => {
                logic(input, target);
                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.
/// ```
/// use thin_engine::prelude::*;
/// let (event_loop, window, display) = thin_engine::set_up().unwrap();
/// #[derive(ToUsize)]
/// enum Actions{
///     Debug
/// }
/// use Actions::*;
/// let mut input = input_map!(
///     (Debug, KeyCode::Space)
/// );
///
///
/// 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.0, 0.0, 1.0);
///         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<()>, &WindowTarget),
{
    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 and fxaa.
#[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;
        }
    }
}