fix quit event to exit game loop

This commit is contained in:
SimonFJ20 2023-01-23 01:01:58 +01:00
parent f223e2401e
commit 5f508eee12

View File

@ -1,6 +1,8 @@
use std::time::Duration; use std::time::Duration;
use sdl2::{event::Event, keyboard::Keycode, pixels::Color, rect::Rect, render::Canvas, video::Window}; use sdl2::{
event::Event, keyboard::Keycode, pixels::Color, rect::Rect, render::Canvas, video::Window,
};
mod bong; mod bong;
@ -16,12 +18,11 @@ impl Element {
Element::Rectangle(color) => { Element::Rectangle(color) => {
canvas.set_draw_color(*color); canvas.set_draw_color(*color);
let _ = canvas.fill_rect(Rect::new(x, y, 50, 50)); let _ = canvas.fill_rect(Rect::new(x, y, 50, 50));
}, }
} }
} }
} }
fn main() { fn main() {
let sdl_context = sdl2::init().unwrap(); let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap(); let video_subsystem = sdl_context.video().unwrap();
@ -38,8 +39,7 @@ fn main() {
canvas.clear(); canvas.clear();
canvas.present(); canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap(); let mut event_pump = sdl_context.event_pump().unwrap();
loop { 'game_loop: loop {
canvas.set_draw_color(Color::RGB(170, 200, 255)); canvas.set_draw_color(Color::RGB(170, 200, 255));
canvas.clear(); canvas.clear();
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
@ -48,12 +48,15 @@ fn main() {
| Event::KeyDown { | Event::KeyDown {
keycode: Some(Keycode::Escape), keycode: Some(Keycode::Escape),
.. ..
} => break, } => break 'game_loop,
_ => {} _ => {}
} }
} }
// The rest of the game loop goes here... // The rest of the game loop goes here...
let dom = Element::Div(vec![Element::Rectangle(Color::RGB(1, 238, 34)), Element::Rectangle(Color::RGB(123, 123, 123))]); let dom = Element::Div(vec![
Element::Rectangle(Color::RGB(1, 238, 34)),
Element::Rectangle(Color::RGB(123, 123, 123)),
]);
canvas.set_draw_color(Color::RGB(255, 0, 0)); canvas.set_draw_color(Color::RGB(255, 0, 0));
let _ = canvas.fill_rect(Rect::new(0, 0, 50, 50)); let _ = canvas.fill_rect(Rect::new(0, 0, 50, 50));