Hello World
commit
0effe28bfa
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
Cargo.lock
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "affection"
|
||||
version = "0.1.0"
|
||||
authors = ["Astro <astro@spaceboyz.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
sdl2 = "0.33"
|
|
@ -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::<Example>(Config::default());
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
|
@ -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<C: Callbacks>(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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue