+++ /dev/null
-use core::render::Renderer;
-use geometry::Point;
-use sdl2::gfx::primitives::DrawRenderer;
-use sdl2::rect::Rect;
-use {SCREEN_HEIGHT, SCREEN_WIDTH};
-
-pub trait Boll {
- fn update(&mut self);
- fn draw(&self, renderer: &mut Renderer, size: u32);
-}
-
-pub struct SquareBoll {
- pub pos: Point<f64>,
- pub vel: Point<f64>,
-}
-
-impl Boll for SquareBoll {
- fn update(&mut self) {
- self.vel.y += 0.1;
- self.pos += self.vel;
-
- if self.pos.x < 0.0 {
- self.pos.x = -self.pos.x;
- self.vel.x = -self.vel.x;
- }
- if self.pos.x > SCREEN_WIDTH as f64 {
- self.pos.x = SCREEN_WIDTH as f64 - (self.pos.x - SCREEN_WIDTH as f64);
- self.vel.x = -self.vel.x;
- }
- if self.pos.y < 0.0 {
- self.pos.y = -self.pos.y;
- self.vel.y = -self.vel.y;
- }
- if self.pos.y > SCREEN_HEIGHT as f64 {
- self.pos.y = SCREEN_HEIGHT as f64 - (self.pos.y - SCREEN_HEIGHT as f64);
- self.vel.y = -self.vel.y;
- }
- }
-
- fn draw(&self, renderer: &mut Renderer, size: u32) {
- renderer.canvas().set_draw_color((
- 255 - std::cmp::min(255, (self.vel.length() * 25.0) as u8),
- (255.0 * (self.pos.x / SCREEN_WIDTH as f64)) as u8,
- (255.0 * (self.pos.y / SCREEN_HEIGHT as f64)) as u8,
- 128,
- ));
- let mut r = Rect::new(0, 0, size, size);
- r.center_on((self.pos.x as i32, self.pos.y as i32));
- renderer.canvas().fill_rect(r).unwrap();
- }
-}
-
-pub struct CircleBoll {
- pub boll: SquareBoll,
-}
-
-impl CircleBoll {
- pub fn new(pos: Point<f64>, vel: Point<f64>) -> CircleBoll {
- CircleBoll {
- boll: SquareBoll { pos, vel },
- }
- }
-}
-
-impl Boll for CircleBoll {
- fn update(&mut self) {
- self.boll.update();
- }
-
- fn draw(&self, renderer: &mut Renderer, size: u32) {
- let val = 255 - std::cmp::min(255, (self.boll.vel.length() * 20.0) as u8);
- renderer
- .canvas()
- .filled_circle(
- self.boll.pos.x as i16,
- self.boll.pos.y as i16,
- size as i16,
- (val, val, val, 128),
- )
- .unwrap();
- }
-}
-use boll::*;
use core::controller::ControllerManager;
use core::render::Renderer;
-use geometry::{Point, Dimension};
-use point;
-use rand::Rng;
+use geometry::{Dimension};
use sdl2::event::{Event, WindowEvent};
use sdl2::keyboard::Keycode;
-use sdl2::rect::Rect as SDLRect;
use sdl2::video::SwapInterval;
use sdl2::{EventPump, VideoSubsystem};
use sprites::SpriteManager;
-use std::f32::consts::PI;
-use time::{Duration, Instant, prelude::*};
-
-const FPS: u32 = 60;
-const NS_PER_FRAME: u32 = 1_000_000_000 / FPS;
+use time::{Duration, Instant};
#[derive(Default)]
pub struct AppBuilder {
let canvas = window.into_canvas().build().unwrap();
let sprites = SpriteManager::new(canvas.texture_creator());
- let screen = canvas.output_size().unwrap();
let renderer = Renderer::new(canvas);
video.gl_set_swap_interval(SwapInterval::VSync)?;
renderer,
event_pump,
sprites,
- states: vec!(self.state.unwrap_or_else(|| Box::new(ActiveState::new(screen)))),
+ states: vec!(self.state.unwrap()),
ctrl_man: ControllerManager::new(context.game_controller()?, context.haptic()?),
})
}
fn render(&mut self, renderer: &mut Renderer, sprites: &SpriteManager);
fn handle_event(&mut self, event: Event) -> Option<StateChange>;
}
-
-type Bollar = Vec<Box<dyn Boll>>;
-
-#[derive(Default)]
-pub struct ActiveState {
- screen: Dimension<u32>,
- bolls: Bollar,
- boll_size: u32,
- mario_angle: f64,
-}
-
-impl ActiveState {
- pub fn new(screen: (u32, u32)) -> ActiveState {
- ActiveState {
- bolls: Bollar::new(),
- boll_size: 1,
- screen: Dimension::from(screen),
- ..Default::default()
- }
- }
-
- fn change_boll_count(&mut self, delta: i32) {
- #[allow(clippy::comparison_chain)]
- if delta > 0 {
- for _i in 0..delta {
- self.add_boll();
- }
- } else if delta < 0 {
- for _i in 0..(-delta) {
- self.bolls.pop();
- }
- }
- }
-
- fn add_boll(&mut self) {
- let mut rng = rand::thread_rng();
- self.bolls.push(Box::new(SquareBoll {
- pos: point!(
- rng.gen_range(0, self.screen.width) as f64,
- rng.gen_range(0, self.screen.height) as f64
- ),
- vel: point!(rng.gen_range(-2.0, 2.0), rng.gen_range(-2.0, 2.0)),
- }));
- }
-}
-
-impl AppState for ActiveState {
- fn enter(&mut self, _ctrl_man: &ControllerManager) {}
-
- fn update(&mut self, dt: Duration) -> Option<StateChange> {
- for b in &mut self.bolls {
- b.update();
- }
-
- match dt {
- ns if ns < (NS_PER_FRAME - 90_0000).nanoseconds() => self.change_boll_count(100),
- ns if ns > (NS_PER_FRAME + 90_0000).nanoseconds() => self.change_boll_count(-100),
- _ => {}
- }
-
- None
- }
-
- fn render(&mut self, renderer: &mut Renderer, sprites: &SpriteManager) {
- /* draw square of blocks */ {
- let blocks = 20;
- let size = 32;
- let offset = point!(
- (self.screen.width as i32 - (blocks + 1) * size) / 2,
- (self.screen.height as i32 - (blocks + 1) * size) / 2
- );
- let block = sprites.get("block");
- for i in 0..blocks {
- renderer
- .blit(
- block,
- None,
- SDLRect::new((i) * size + offset.x, offset.y, size as u32, size as u32),
- );
- renderer
- .blit(
- block,
- None,
- SDLRect::new(
- (blocks - i) * size + offset.x,
- (blocks) * size + offset.y,
- size as u32,
- size as u32,
- ),
- );
- renderer
- .blit(
- block,
- None,
- SDLRect::new(
- offset.x,
- (blocks - i) * size + offset.y,
- size as u32,
- size as u32,
- ),
- );
- renderer
- .blit(
- block,
- None,
- SDLRect::new(
- (blocks) * size + offset.x,
- (i) * size + offset.y,
- size as u32,
- size as u32,
- ),
- );
- }
- }
-
- /* draw mario */ {
- let size = 64;
- let offset = point!(
- (self.screen.width as i32 - size) / 2,
- (self.screen.height as i32 - size) / 2
- );
- let radius = 110.0 + size as f32 * 0.5;
- let angle = (self.mario_angle as f32 - 90.0) * PI / 180.0;
- let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32);
- renderer
- .blit_ex(
- sprites.get("mario"),
- None,
- SDLRect::new(
- offset.x + offset2.x,
- offset.y + offset2.y,
- size as u32,
- size as u32,
- ),
- self.mario_angle,
- sdl2::rect::Point::new(size / 2, size / 2),
- false,
- false,
- );
- self.mario_angle = (self.mario_angle + 1.0) % 360.0;
- }
-
- /* draw circles and ellipses*/ {
- let p = point!((self.screen.width / 2) as i16, (self.screen.height / 2) as i16);
- renderer.circle(p, 100, (255, 255, 255));
- renderer.circle(p, 110, (255, 255, 255));
- renderer.ellipse(p, (50, 100), (255, 255, 255));
- renderer.ellipse(p, (110, 55), (255, 255, 255));
- }
-
- for b in &self.bolls {
- b.draw(renderer, self.boll_size);
- }
- }
-
- fn leave(&mut self) {
- println!("number of bolls: {}", self.bolls.len());
- }
-
- fn handle_event(&mut self, event: Event) -> Option<StateChange> {
- match event {
- Event::KeyDown {
- keycode: Some(Keycode::KpPlus),
- ..
- } => self.boll_size = std::cmp::min(self.boll_size + 1, 32),
- Event::KeyDown {
- keycode: Some(Keycode::KpMinus),
- ..
- } => self.boll_size = std::cmp::max(self.boll_size - 1, 1),
- Event::MouseMotion { x, y, .. } => self.bolls.push(Box::new(CircleBoll::new(
- point!(x as f64, y as f64),
- point!(0.0, 0.0),
- ))),
- _ => {}
- }
- None
- }
-}