From 086fcf6dd9662cc4d74c0be7fe8c3f7ddd069766 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Fri, 20 Jan 2023 16:10:18 +0100 Subject: [PATCH] sdl works --- README.md | 10 ++++++++ src/bong/parser.rs | 60 ++++++++++++++++++++++++++-------------------- src/main.rs | 40 ++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index c5f1f9b..ff286b5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # web-stack-project +## Dependencies + +SDL2, SDL-mixer and SDL-image + +### Debian + +``` +sudo apt install libsdl2-2.0-0 libsdl2-dev libsdl2-image-2.0-0 libsdl2-image-dev libsdl2-mixer-2.0-0 libsdl2-mixer-dev +``` + diff --git a/src/bong/parser.rs b/src/bong/parser.rs index aa7cfba..8f0d5dc 100644 --- a/src/bong/parser.rs +++ b/src/bong/parser.rs @@ -40,7 +40,9 @@ impl Parser { } } - fn parse_element(&mut self) -> Result {} + fn parse_element(&mut self) -> Result { + todo!() + } fn parse_value(&mut self) -> Result { match self.current() { @@ -70,19 +72,20 @@ impl Parser { Ok(Node::Object(values)) } Some(t @ (Token::Name(_) | Token::String(_))) => { - let key = match t { - Token::Name(v) => v, - Token::String(v) => &v[1..v.len() - 1].to_string(), - _ => panic!("checked by previous predicate"), - }; - self.step(); - match self.current() { - Some(Token::Equal(_) | Token::Colon(_)) => {} - _ => return Err("expected ':' or '='".to_string()), - } - self.step(); - values[key] = Box::new(self.parse_value()?); - self.parse_object_tail(values) + // let key = match t { + // Token::Name(v) => v, + // Token::String(v) => &v[1..v.len() - 1].to_string(), + // _ => panic!("checked by previous predicate"), + // }; + // self.step(); + // match self.current() { + // Some(Token::Equal(_) | Token::Colon(_)) => {} + // _ => return Err("expected ':' or '='".to_string()), + // } + // self.step(); + // values[key] = Box::new(self.parse_value()?); + // self.parse_object_tail(values) + todo!() } _ => Err("expected Name, String or '}'".to_string()), } @@ -103,18 +106,19 @@ impl Parser { break Ok(Node::Object(values)); } Some(t @ (Token::Name(_) | Token::String(_))) => { - let key = match t { - Token::Name(v) => v, - Token::String(v) => &v[1..v.len() - 1].to_string(), - _ => panic!("unterminated object, checked by previous predicate"), - }; - self.step(); - match self.current() { - Some(Token::Equal(_) | Token::Colon(_)) => {} - _ => return Err("expected ':' or '='".to_string()), - } - self.step(); - values[key] = Box::new(self.parse_value()?); + // let key = match t { + // Token::Name(v) => v, + // Token::String(v) => &v[1..v.len() - 1].to_string(), + // _ => panic!("unterminated object, checked by previous predicate"), + // }; + // self.step(); + // match self.current() { + // Some(Token::Equal(_) | Token::Colon(_)) => {} + // _ => return Err("expected ':' or '='".to_string()), + // } + // self.step(); + // values[key] = Box::new(self.parse_value()?); + todo!() } _ => { break Err( @@ -136,6 +140,10 @@ impl Parser { self.step(); Ok(Node::Array(values)) } + Some(_) => { + values.push(self.parse_value()?); + todo!() + } _ => Err("unterminated array, expected Value or ']'".to_string()), } } diff --git a/src/main.rs b/src/main.rs index 13371d6..d1b91b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,43 @@ +use std::time::Duration; + +use sdl2::{event::Event, keyboard::Keycode, pixels::Color}; + mod bong; fn main() { - println!("Hello, world!"); + let sdl_context = sdl2::init().unwrap(); + let video_subsystem = sdl_context.video().unwrap(); + + let window = video_subsystem + .window("rust-sdl2 demo", 800, 600) + .position_centered() + .build() + .unwrap(); + + let mut canvas = window.into_canvas().build().unwrap(); + + canvas.set_draw_color(Color::RGB(0, 255, 255)); + canvas.clear(); + canvas.present(); + let mut event_pump = sdl_context.event_pump().unwrap(); + let mut i = 0; + 'running: loop { + i = (i + 1) % 255; + canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); + canvas.clear(); + for event in event_pump.poll_iter() { + match event { + Event::Quit { .. } + | Event::KeyDown { + keycode: Some(Keycode::Escape), + .. + } => break 'running, + _ => {} + } + } + // The rest of the game loop goes here... + + canvas.present(); + ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); + } }