commit 0effe28bfa33c59303e638f803606e595a7f52aa Author: Astro Date: Wed Mar 4 18:28:07 2020 +0100 Hello World diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8237f98 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "affection" +version = "0.1.0" +authors = ["Astro "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sdl2 = "0.33" diff --git a/examples/example00.rs b/examples/example00.rs new file mode 100644 index 0000000..cf25ff2 --- /dev/null +++ b/examples/example00.rs @@ -0,0 +1,18 @@ +use affection::*; + +struct Example { +} + +impl Callbacks for Example { + fn load_state() -> Self { + Example {} + } + fn handle_event(&mut self, ev: Event) {} + fn update(&mut self) {} + fn draw(&mut self, graphics: &mut Graphics) {} + fn clean_up(self) {} +} + +pub fn main() { + run::(Config::default()); +} diff --git a/src/graphics.rs b/src/graphics.rs new file mode 100644 index 0000000..6ec86e3 --- /dev/null +++ b/src/graphics.rs @@ -0,0 +1,33 @@ +use sdl2::{video::Window, EventPump, event::EventPollIterator}; +pub use sdl2::event::Event; +use super::Config; + +pub struct Graphics { + events: EventPump, + window: Window, +} + +impl Graphics { + pub fn new(config: &Config) -> Self { + let sdl_context = sdl2::init().unwrap(); + let video_subsystem = sdl_context.video().unwrap(); + let mut window = video_subsystem.window( + &config.window_title, + config.window_size.0, + config.window_size.1 + ) + .opengl() + .build() + .unwrap(); + window.show(); + + Graphics { + events: sdl_context.event_pump().unwrap(), + window, + } + } + + pub fn get_events(&mut self) -> EventPollIterator { + self.events.poll_iter() + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..afc3b67 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,51 @@ +pub mod graphics; +pub use graphics::{Graphics, Event}; + +pub struct Config { + window_title: String, + window_size: (u32, u32), +} + +impl Default for Config { + fn default() -> Self { + Config { + window_title: "Affection".to_owned(), + window_size: (800, 600), + } + } +} + +pub trait Callbacks { + fn load_state() -> Self; + fn pre_loop(&mut self) { + } + fn handle_event(&mut self, ev: Event); + fn update(&mut self); + fn draw(&mut self, graphics: &mut Graphics); + fn clean_up(self); +} + +pub fn run(config: Config) { + let mut graphics = Graphics::new(&config); + + let mut state = C::load_state(); + state.pre_loop(); + loop { + state.update(); + for ev in graphics.get_events() { + state.handle_event(ev); + } + state.draw(&mut graphics); + } + state.clean_up(); +} + +#[cfg(test)] +mod tests { + use super::Config; + + #[test] + fn it_works() { + let _config = Config::default(); + } +}