From: Tomas Wenström Date: Sat, 6 Feb 2021 12:47:17 +0000 (+0100) Subject: Added a graphical testing state X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=b0137bb6d47c158bff30f2d4fd1faab9d4945d54;p=kaka%2Frust-sdl-test.git Added a graphical testing state --- diff --git a/src/core/game.rs b/src/core/game.rs index 386057b..8fbbef0 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -1,4 +1,4 @@ -use ActiveState; +use teststate::TestState; use AppState; use common::{Point, Radians}; use core::app::StateChange; @@ -58,7 +58,7 @@ impl AppState for GameState { return Some(StateChange::Pop) } Event::KeyDown { keycode: Some(Keycode::Return), .. } => { - return Some(StateChange::Push(Box::new(ActiveState::new((800, 600))))) + return Some(StateChange::Push(Box::new(TestState::new()))) } Event::KeyDown { keycode: Some(Keycode::Space), .. } => { self.lvlgen.seed = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs() as u32; diff --git a/src/main.rs b/src/main.rs index 502789b..a068a58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate noise; use core::game::GameState; use core::app::*; +mod teststate; mod core; #[macro_use] mod common; diff --git a/src/teststate.rs b/src/teststate.rs new file mode 100644 index 0000000..2f8f240 --- /dev/null +++ b/src/teststate.rs @@ -0,0 +1,78 @@ +use common::{Point, Intersection}; +use core::app::{AppState, StateChange}; +use core::controller::ControllerManager; +use core::render::Renderer; +use point; // defined in common, but loaded from main... +use sdl2::event::Event; +use sprites::SpriteManager; +use time::{Duration, Instant}; + +pub struct TestState { + start: Instant, +} + +impl TestState { + pub fn new() -> TestState { + TestState { + start: Instant::now(), + } + } + + fn draw_intersecting_lines(&self, renderer: &mut Renderer, p1: (i32, i32), p2: (i32, i32), p3: (i32, i32), p4: (i32, i32)) { + let color = match Intersection::lines( + point!(p1.0 as f64, p1.1 as f64), + point!(p2.0 as f64, p2.1 as f64), + point!(p3.0 as f64, p3.1 as f64), + point!(p4.0 as f64, p4.1 as f64) + ) { + Intersection::Point(p) => { + renderer.circle((p.x as i16, p.y as i16), 3, (255, 255, 0)); + (0, 255, 0) + } + Intersection::None => (255, 0, 0) + }; + renderer.draw_line(p1, p2, color); + renderer.draw_line(p3, p4, color); + } +} + +impl AppState for TestState { + fn enter(&mut self, _ctrl_man: &ControllerManager) {} + + fn update(&mut self, _dt: Duration) -> Option { + None + } + + fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) { + // crossing + self.draw_intersecting_lines(renderer, (0, 0), (20, 20), (20, 0), (0, 20)); + self.draw_intersecting_lines(renderer, (30, 10), (50, 10), (40, 0), (40, 20)); + + // just touching + self.draw_intersecting_lines(renderer, (70, 0), (70, 10), (60, 10), (80, 10)); + self.draw_intersecting_lines(renderer, (90, 0), (100, 10), (90, 20), (110, 0)); + self.draw_intersecting_lines(renderer, (120, 0), (140, 20), (120, 20), (130, 10)); + self.draw_intersecting_lines(renderer, (170, 10), (160, 10), (160, 0), (160, 20)); + + // not touching + self.draw_intersecting_lines(renderer, (190, 0), (210, 20), (195, 20), (200, 15)); + + let a = self.start.elapsed().as_seconds_f64().sin() * std::f64::consts::FRAC_PI_2; + let (ax, ay) = ((a.cos() * 200.0) as i32, (a.sin() * 200.0) as i32); + self.draw_intersecting_lines(renderer, (10, 400), (10 + ax, 400 + ay), (30, 200), (30, 600)); + + let x = (self.start.elapsed().as_seconds_f64().cos() * 60.0) as i32; + let y = (self.start.elapsed().as_seconds_f64().sin() * 60.0) as i32; + let x2 = ((self.start.elapsed().as_seconds_f64() + std::f64::consts::FRAC_PI_4).cos() * 60.0) as i32; + let y2 = ((self.start.elapsed().as_seconds_f64() + std::f64::consts::FRAC_PI_4).sin() * 60.0) as i32; + self.draw_intersecting_lines(renderer, (100 + x, 100 + y), (150 + x, 150 + y), (100 + x2, 150 + y2), (150 + x2, 100 + y2)); + self.draw_intersecting_lines(renderer, (250 + x, 85 + y), (250 + x, 165 + y), (210 + x2, 125 + y2), (290 + x2, 125 + y2)); + } + + fn leave(&mut self) { + } + + fn handle_event(&mut self, _event: Event) -> Option { + None + } +}