Browse Source

Hello World

master
Astro 1 year ago
commit
0effe28bfa
5 changed files with 114 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 10
    0
      Cargo.toml
  3. 18
    0
      examples/example00.rs
  4. 33
    0
      src/graphics.rs
  5. 51
    0
      src/lib.rs

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+/target
2
+Cargo.lock

+ 10
- 0
Cargo.toml View File

@@ -0,0 +1,10 @@
1
+[package]
2
+name = "affection"
3
+version = "0.1.0"
4
+authors = ["Astro <astro@spaceboyz.net>"]
5
+edition = "2018"
6
+
7
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
+
9
+[dependencies]
10
+sdl2 = "0.33"

+ 18
- 0
examples/example00.rs View File

@@ -0,0 +1,18 @@
1
+use affection::*;
2
+
3
+struct Example {
4
+}
5
+
6
+impl Callbacks for Example {
7
+    fn load_state() -> Self {
8
+        Example {}
9
+    }
10
+    fn handle_event(&mut self, ev: Event) {}
11
+    fn update(&mut self) {}
12
+    fn draw(&mut self, graphics: &mut Graphics) {}
13
+    fn clean_up(self) {}
14
+}
15
+
16
+pub fn main() {
17
+    run::<Example>(Config::default());
18
+}

+ 33
- 0
src/graphics.rs View File

@@ -0,0 +1,33 @@
1
+use sdl2::{video::Window, EventPump, event::EventPollIterator};
2
+pub use sdl2::event::Event;
3
+use super::Config;
4
+
5
+pub struct Graphics {
6
+    events: EventPump,
7
+    window: Window,
8
+}
9
+
10
+impl Graphics {
11
+    pub fn new(config: &Config) -> Self {
12
+        let sdl_context = sdl2::init().unwrap();
13
+        let video_subsystem = sdl_context.video().unwrap();
14
+        let mut window = video_subsystem.window(
15
+            &config.window_title,
16
+            config.window_size.0,
17
+            config.window_size.1
18
+        )
19
+            .opengl()
20
+            .build()
21
+            .unwrap();
22
+        window.show();
23
+
24
+        Graphics {
25
+            events: sdl_context.event_pump().unwrap(),
26
+            window,
27
+        }
28
+    }
29
+
30
+    pub fn get_events(&mut self) -> EventPollIterator {
31
+        self.events.poll_iter()
32
+    }
33
+}

+ 51
- 0
src/lib.rs View File

@@ -0,0 +1,51 @@
1
+pub mod graphics;
2
+pub use graphics::{Graphics, Event};
3
+
4
+pub struct Config {
5
+    window_title: String,
6
+    window_size: (u32, u32),
7
+}
8
+
9
+impl Default for Config {
10
+    fn default() -> Self {
11
+        Config {
12
+            window_title: "Affection".to_owned(),
13
+            window_size: (800, 600),
14
+        }
15
+    }
16
+}
17
+
18
+pub trait Callbacks {
19
+    fn load_state() -> Self;
20
+    fn pre_loop(&mut self) {
21
+    }
22
+    fn handle_event(&mut self, ev: Event);
23
+    fn update(&mut self);
24
+    fn draw(&mut self, graphics: &mut Graphics);
25
+    fn clean_up(self);
26
+}
27
+
28
+pub fn run<C: Callbacks>(config: Config) {
29
+    let mut graphics = Graphics::new(&config);
30
+
31
+    let mut state = C::load_state();
32
+    state.pre_loop();
33
+    loop {
34
+        state.update();
35
+        for ev in graphics.get_events() {
36
+            state.handle_event(ev);
37
+        }
38
+        state.draw(&mut graphics);
39
+    }
40
+    state.clean_up();
41
+}
42
+
43
+#[cfg(test)]
44
+mod tests {
45
+    use super::Config;
46
+
47
+    #[test]
48
+    fn it_works() {
49
+        let _config = Config::default();
50
+    }
51
+}

Loading…
Cancel
Save