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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! A library to sweeten the interaction with sdl2
#[deny(
    missing_docs,
    missing_debug_implementations,
//  missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces,
    unused_qualifications
)]

pub use sdl2;


/// The core of this library
/// Engine handle the backend of drawing and deals with sdl2
pub struct Engine<'a> {
    /// The name the engine
    pub name: String,
    /// The size of the Window, formated (width,height,pixel_size)
    pub size: (u32, u32, u32),
    //pub main: (dyn Fn(f64) -> Result<(),&'a String>),
    /// The window , where the drawing methods are
    pub screen: Window,
    event_pump: sdl2::EventPump,
    timer: std::time::SystemTime, // DEFAULTED TO 0
    frame_count: u64,             // DEFAULTED TO 0
    frame_timer: f64,             // DEFAULTED TO 0
    /// the time elapsed between the start of this frame and the old one
    pub elapsed: f64, // DEFAULTED TO 0
    key_pressed: std::collections::HashSet<sdl2::keyboard::Keycode>,
    key_hold: std::collections::HashSet<sdl2::keyboard::Keycode>,
    key_release: std::collections::HashSet<sdl2::keyboard::Keycode>,
    /// The user code run by the Engine
    pub main: &'a (dyn Fn(&mut Engine) -> Result<(), String>),
}

impl<'a> std::fmt::Debug for Engine<'a> {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        return write!(fmt, "Engine {{ pub name: {:?}, pub size: {:?}, pub screen: {:?}, event_pump: sdl2::EventPump, timer: {:?}, frame_count: {:?}, frame_timer: {:?}, pub elapsed: {:?}, key_pressed: {:?}, key_hold: {:?}, key_release: {:?}, main: &(dyn Fn(&mut Engine) -> Result<(),String>) }}",
            self.name,
            self.size,
            self.screen,
            self.timer,
            self.frame_count,
            self.frame_timer,
            self.elapsed,
            self.key_pressed,
            self.key_hold,
            self.key_release
        );
    }
}

impl<'a> Engine<'a> {
    /// Create a new game Engine
    pub fn new(
        name: &str,
        size: (u32, u32, u32),
        main: &'a (dyn Fn(&mut Engine) -> Result<(), String>),
    ) -> Result<Engine<'a>, String> {
        let sdl_ctx = sdl2::init()?;
        let video_subsystem = sdl_ctx.video()?;
        let screen = initialize(video_subsystem, size.0, size.1, size.2, String::from(name))
            .map_err(|err| err.to_string())?;
        let event_pump = sdl_ctx.event_pump()?;
        Ok(Engine {
            name: String::from(name),
            size: size,
            main: main,
            screen: screen,
            event_pump: event_pump,
            timer: std::time::SystemTime::now(),
            frame_count: 0,
            frame_timer: 0.0,
            elapsed: 0.0,
            key_pressed: std::collections::HashSet::new(),
            key_hold: std::collections::HashSet::new(),
            key_release: std::collections::HashSet::new(),
        })
    }
    /// The core of the engine, where crucial value are calculated, like elapsed or the key's
    /// vector
    pub fn new_frame(&mut self) -> Result<bool, String> {
        // Calcaulting elapsed_time
        self.elapsed = (std::time::SystemTime::now()
            .duration_since(self.timer)
            .map_err(|err| err.to_string())?)
        .as_secs_f64();
        // End
        self.timer = std::time::SystemTime::now();
        self.frame_timer += self.elapsed;
        self.frame_count += 1;
        if self.frame_timer > 1.0 {
            self.frame_timer -= 1.0;
            self.screen
                .update_title(&self.name.as_ref(), self.frame_count)?;
            self.frame_count = 0
        }

        for key in &self.key_pressed {
            self.key_hold.insert(*key);
        }
        self.key_pressed.clear();
        self.key_release.clear();

        for event in self.event_pump.poll_iter() {
            match event {
                sdl2::event::Event::Quit {..} /*| sdl2::event::Event::KeyDown {keycode: Some(sdl2::keyboard::Keycode::Escape), .. }*/ => return Ok(false),
                sdl2::event::Event::KeyDown { keycode: Some(keycode), repeat, ..} => {
                    if !repeat {
                        self.key_pressed.insert(keycode);
                    }
                },
                sdl2::event::Event::KeyUp {keycode: Some(keycode), ..} => {
                    self.key_release.insert(keycode);
                    self.key_hold.remove(&keycode);
                    self.key_pressed.remove(&keycode);
                }
                _ => {},
            }
        }
        Ok(true)
    }
    /// Start the engine, lanch the "main" function
    pub fn start(&mut self) -> Result<(), String> {
        (self.main)(self).map_err(|err| err.to_string())?;
        Ok(())
    }
    /// Return the state of "key" via KeyState enum
    pub fn get_key_state(&self, key: sdl2::keyboard::Keycode) -> KeyState {
        if self.key_pressed.contains(&key) {
            return KeyState::Pressed;
        }
        if self.key_hold.contains(&key) {
            return KeyState::Held;
        }
        if self.key_release.contains(&key) {
            return KeyState::Released;
        }
        return KeyState::None;
    }
    ///Return a clone of the vector conataining pressed keys
    pub fn get_pressed(&self) -> std::collections::HashSet<sdl2::keyboard::Keycode> {
        self.key_pressed.clone()
    }
    ///Return a clone of the vector conataining held keys
    pub fn get_held(&self) -> std::collections::HashSet<sdl2::keyboard::Keycode> {
        self.key_hold.clone()
    }
    ///Return a clone of the vector conataining released keys
    pub fn get_released(&self) -> std::collections::HashSet<sdl2::keyboard::Keycode> {
        self.key_release.clone()
    }
    /// Return true if "key" is pressed this frame, else, return false
    pub fn is_pressed(&self, key: sdl2::keyboard::Keycode) -> bool {
        self.key_pressed.contains(&key)
    }
    /// Return true if "key" is held this frame, else, return false
    pub fn is_held(&self, key: sdl2::keyboard::Keycode) -> bool {
        self.key_hold.contains(&key)
    }
    /// Return true if "key" is released this frame, else , return false
    pub fn is_released(&self, key: sdl2::keyboard::Keycode) -> bool {
        self.key_release.contains(&key)
    }
}
/// All the keystate possible
#[derive(Eq, PartialEq, Debug)]
pub enum KeyState {
    ///The key is not pressed nor released nor held
    None,
    ///The key is held
    Held,
    ///The key is pressed
    Pressed,
    ///The key is released
    Released,
}

// DRAW STUFF

use sdl2::rect::Rect;

// ==========START=COLOR==========
#[derive(Debug, Copy, Clone)]
/// A RGBA color interface
pub struct Color {
    /// Red Channel
    pub r: u8,
    /// Green Channel
    pub g: u8,
    /// Blue Channel
    pub b: u8,
    /// Alpha Channel
    pub a: u8,
}

impl Color {
    /// Color
    pub const WHITE: Color = Color {
        r: 255,
        g: 255,
        b: 255,
        a: 255,
    };
    /// Color
    pub const GREY: Color = Color {
        r: 192,
        g: 192,
        b: 192,
        a: 255,
    };
    /// Color
    pub const DARK_GREY: Color = Color {
        r: 128,
        g: 128,
        b: 128,
        a: 255,
    };
    /// Color
    pub const VERY_DARK_GREY: Color = Color {
        r: 64,
        g: 64,
        b: 64,
        a: 255,
    };
    /// Color
    pub const RED: Color = Color {
        r: 255,
        g: 0,
        b: 0,
        a: 255,
    };
    /// Color
    pub const DARK_RED: Color = Color {
        r: 128,
        g: 0,
        b: 0,
        a: 255,
    };
    /// Color
    pub const VERY_DARK_RED: Color = Color {
        r: 64,
        g: 0,
        b: 0,
        a: 255,
    };
    /// Color
    pub const YELLOW: Color = Color {
        r: 255,
        g: 255,
        b: 0,
        a: 255,
    };
    /// Color
    pub const DARK_YELLOW: Color = Color {
        r: 128,
        g: 128,
        b: 0,
        a: 255,
    };
    /// Color
    pub const VERY_DARK_YELLOW: Color = Color {
        r: 64,
        g: 64,
        b: 0,
        a: 255,
    };
    /// Color
    pub const GREEN: Color = Color {
        r: 0,
        g: 255,
        b: 0,
        a: 255,
    };
    /// Color
    pub const DARK_GREEN: Color = Color {
        r: 0,
        g: 128,
        b: 0,
        a: 255,
    };
    /// Color
    pub const VERY_DARK_GREEN: Color = Color {
        r: 0,
        g: 64,
        b: 0,
        a: 255,
    };
    /// Color
    pub const CYAN: Color = Color {
        r: 0,
        g: 255,
        b: 255,
        a: 255,
    };
    /// Color
    pub const DARK_CYAN: Color = Color {
        r: 0,
        g: 128,
        b: 128,
        a: 255,
    };
    /// Color
    pub const VERY_DARK_CYAN: Color = Color {
        r: 0,
        g: 64,
        b: 64,
        a: 255,
    };
    /// Color
    pub const BLUE: Color = Color {
        r: 0,
        g: 0,
        b: 255,
        a: 255,
    };
    /// Color
    pub const DARK_BLUE: Color = Color {
        r: 0,
        g: 0,
        b: 128,
        a: 255,
    };
    /// Color
    pub const VERY_DARK_BLUE: Color = Color {
        r: 0,
        g: 0,
        b: 64,
        a: 255,
    };
    /// Color
    pub const MAGENTA: Color = Color {
        r: 255,
        g: 0,
        b: 255,
        a: 255,
    };
    /// Color
    pub const DARK_MAGENTA: Color = Color {
        r: 128,
        g: 0,
        b: 128,
        a: 255,
    };
    /// Color
    pub const VERY_DARK_MAGENTA: Color = Color {
        r: 64,
        g: 0,
        b: 64,
        a: 255,
    };
    /// Color
    pub const BLACK: Color = Color {
        r: 0,
        g: 0,
        b: 0,
        a: 255,
    };
    /// Create a new Color with the alpha set at 255
    pub fn new(r: u8, g: u8, b: u8) -> Color {
        Color { r, g, b, a: 255 }
    }
    /// Create a new Color with the alpha as an additinal argument
    pub fn new_alpha(r: u8, g: u8, b: u8, a: u8) -> Color {
        Color { r, g, b, a }
    }
    /// Friendly way to transphorm Color to sdl2::pixels::Color
    pub fn to_pixel_color(&self) -> sdl2::pixels::Color {
        sdl2::pixels::Color::RGBA(self.r, self.g, self.b, self.a)
    }
}
// ==========END===COLOR==========

// =========START=WINDOW==========
/// Base struct of the library
/// contains the function to draw onto the window's canvas
pub struct Window {
    /// The canvas's Window, not realy useful
    pub canv: sdl2::render::Canvas<sdl2::video::Window>,
    /// The Window's height, if ever useful
    pub height: u32,
    /// The Window's width, if ever useful
    pub width: u32,
    /// The Window pixel ratio, "1 pixel = px_size pixels" on the physical screen
    pub px_size: u32,
    /// The Window name
    pub name: String,
}

impl std::fmt::Debug for Window {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        return write!(fmt, "Window {{ pub self.canv: sdl2::render::Canvas<sdl2::video::Window>, pub height: {:?}, pub width: {:?}, pub px_size: {:?}, pub name: {:?} }}",
            self.height,
            self.width,
            self.px_size,
            self.name
            );
    }
}

impl Window {
    /// Create a new window from arguments given
    pub fn new(
        canvas: sdl2::render::Canvas<sdl2::video::Window>,
        w: u32,
        h: u32,
        pixel_size: u32,
        name: String,
    ) -> Window {
        Window {
            canv: canvas,
            width: w,
            height: h,
            px_size: pixel_size,
            name: name,
        }
    }
    fn ptsp(&self, num: i32) -> i32 {
        // fn point_to_screen_point()
        ((num/* - 1*/) * self.px_size as i32)
    }
    /// Fill the whole screen with color given
    pub fn clear(&mut self, col: Color) {
        self.canv.set_draw_color(col.to_pixel_color());
        self.canv.clear();
    }
    /// Draw a pixel at x,y
    pub fn draw(&mut self, x: i32, y: i32, col: Color) -> Result<(), String> {
        self.canv.set_draw_color(col.to_pixel_color());
        self.canv
            .fill_rect(Rect::new(
                self.ptsp(x),
                self.ptsp(y),
                self.px_size,
                self.px_size,
            ))
            .map_err(|err| err.to_string())?;
        Ok(())
    }
    /// Draw a filled rectangle starting a x,y with size w,h
    pub fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32, col: Color) -> Result<(), String> {
        self.canv.set_draw_color(col.to_pixel_color());
        self.canv
            .fill_rect(Rect::new(
                self.ptsp(x),
                self.ptsp(y),
                self.ptsp(w) as u32,
                self.ptsp(h) as u32,
            ))
            .map_err(|err| err.to_string())?;
        Ok(())
    }
    /// Update the window's canvas to show change
    pub fn update(&mut self) {
        self.canv.present();
    }
    /// Update the title of the window with format "{name} - {fps}fps"
    pub fn update_title(&mut self, title: &str, frame_count: u64) -> Result<(), String> {
        self.canv
            .window_mut()
            .set_title(format!("{} - {}fps", title, frame_count).as_ref())
            .map_err(|err| err.to_string())?;
        Ok(())
    }
}
// =========END===WINDOW==========
/// Create a window with the name ,size and pixel size given
pub fn initialize(
    video_subsystem: sdl2::VideoSubsystem,
    width: u32,
    height: u32,
    pixsize: u32,
    name: String,
) -> Result<Window, String> {
    let window = video_subsystem
        .window(name.as_ref(), width * pixsize, height * pixsize)
        .opengl()
        .build()
        .map_err(|err| err.to_string())?;
    let canvas: sdl2::render::Canvas<sdl2::video::Window> = window
        .into_canvas()
        .build()
        .map_err(|err| err.to_string())?;
    Ok(Window::new(canvas, width, height, pixsize, name))
}