This commit is contained in:
Yash Karandikar 2022-03-06 13:08:05 -06:00
parent 0e983dd043
commit c344049fd3
Signed by: karx
GPG key ID: A794DA2529474BA5
2 changed files with 21 additions and 12 deletions

View file

@ -1,11 +1,14 @@
use std::cell::Cell; use std::cell::Cell;
use sdl2::{video::{WindowBuildError, Window}, IntegerOrSdlError, render::Canvas, EventPump}; use sdl2::{
render::Canvas,
video::{Window, WindowBuildError},
EventPump, IntegerOrSdlError,
};
pub use sdl2::pixels::Color;
pub use sdl2::keyboard::Keycode;
pub use sdl2::event::Event; pub use sdl2::event::Event;
pub use sdl2::keyboard::Keycode;
pub use sdl2::pixels::Color;
#[derive(Debug)] #[derive(Debug)]
pub struct CatboxError(String); pub struct CatboxError(String);
@ -34,7 +37,7 @@ pub struct Game {
pub title: String, pub title: String,
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,
stopped: Cell<bool> stopped: Cell<bool>,
} }
impl Game { impl Game {
@ -43,7 +46,7 @@ impl Game {
title: title.to_string(), title: title.to_string(),
width, width,
height, height,
stopped: Cell::new(false) stopped: Cell::new(false),
} }
} }
@ -51,7 +54,8 @@ impl Game {
let sdl_context = sdl2::init()?; let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?; let video_subsystem = sdl_context.video()?;
let window = video_subsystem.window(&self.title, self.width, self.height) let window = video_subsystem
.window(&self.title, self.width, self.height)
.position_centered() .position_centered()
.build()?; .build()?;
@ -73,4 +77,4 @@ impl Game {
pub fn terminate(&self) { pub fn terminate(&self) {
self.stopped.set(true); self.stopped.set(true);
} }
} }

View file

@ -1,4 +1,4 @@
use catbox::{Game, Keycode, Event}; use catbox::{Event, Game, Keycode};
fn main() { fn main() {
let mut game = Game::new("catbox demo", 1000, 800); let mut game = Game::new("catbox demo", 1000, 800);
@ -8,9 +8,14 @@ fn main() {
canvas.clear(); canvas.clear();
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => game.terminate(), Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => game.terminate(),
_ => {} _ => {}
} }
} }
}).unwrap(); })
} .unwrap();
}