made some rectangles

This commit is contained in:
Mikkel Troels Kongsted 2023-01-20 17:02:03 +01:00
parent 086fcf6dd9
commit df49e28f78
2 changed files with 29 additions and 7 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
target/ target/
SDL2*.dll

View File

@ -1,9 +1,27 @@
use std::time::Duration; use std::time::Duration;
use sdl2::{event::Event, keyboard::Keycode, pixels::Color}; use sdl2::{event::Event, keyboard::Keycode, pixels::Color, rect::Rect, render::Canvas, video::Window};
mod bong; mod bong;
enum Element {
Div(Vec<Element>),
Rectangle(Color),
}
impl Element {
fn render(&self, canvas: &mut Canvas<Window>, x: i32, y: i32) {
match self {
Element::Div(_) => todo!(),
Element::Rectangle(color) => {
canvas.set_draw_color(*color);
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();
@ -20,10 +38,9 @@ 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();
let mut i = 0; loop {
'running: loop {
i = (i + 1) % 255; canvas.set_draw_color(Color::RGB(170, 200, 255));
canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
canvas.clear(); canvas.clear();
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
@ -31,11 +48,15 @@ fn main() {
| Event::KeyDown { | Event::KeyDown {
keycode: Some(Keycode::Escape), keycode: Some(Keycode::Escape),
.. ..
} => break 'running, } => break,
_ => {} _ => {}
} }
} }
// 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))]);
canvas.set_draw_color(Color::RGB(255, 0, 0));
let _ = canvas.fill_rect(Rect::new(0, 0, 50, 50));
canvas.present(); canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));