Collide with the walls instead of the grid
[kaka/rust-sdl-test.git] / src / main.rs
index d99da89..d60ad55 100644 (file)
 extern crate rand;
 extern crate sdl2;
 extern crate time;
+extern crate noise;
 
-use std::collections::hash_map::RandomState;
-use std::collections::HashMap;
-use std::f32::consts::PI;
+use core::game::GameState;
+use core::app::*;
 
-use rand::Rng;
-use sdl2::event::Event;
-use sdl2::EventPump;
-use sdl2::gfx::primitives::DrawRenderer;
-use sdl2::image::LoadTexture;
-use sdl2::keyboard::Keycode;
-use sdl2::pixels::Color;
-use sdl2::rect::Rect;
-use sdl2::render::BlendMode;
-use sdl2::render::Canvas;
-use sdl2::render::Texture;
-use sdl2::render::TextureCreator;
-use sdl2::video::Window;
-use sdl2::video::WindowContext;
-use time::PreciseTime;
-
-use boll::{Boll, SquareBoll, CircleBoll};
-use common::Point2D;
-
-#[macro_use] mod common;
 mod boll;
+mod core;
+mod geometry;
+mod sprites;
+mod teststate;
+mod util;
 
-const SCREEN_WIDTH: u32 = 1280;
-const SCREEN_HEIGHT: u32 = (SCREEN_WIDTH as f64 * (1440.0 / 2560.0)) as u32;
-const FPS: u32 = 60;
-const NS_PER_FRAME: u32 = 1_000_000_000 / FPS;
-
-type Bollar = Vec<Box<dyn Boll>>;
-
-fn init() -> (Canvas<Window>, EventPump) {
-    let context = sdl2::init().unwrap();
-    let video = context.video().unwrap();
-    let _image_context = sdl2::image::init(sdl2::image::InitFlag::PNG).unwrap();
-    let window = video.window("SDL test", SCREEN_WIDTH, SCREEN_HEIGHT)
-        .position_centered()
-        .opengl()
-        .build()
-        .unwrap();
-    context.mouse().show_cursor(false);
-    let mut canvas = window.into_canvas().build().unwrap();
-    canvas.set_blend_mode(BlendMode::Add);
-    canvas.set_draw_color(Color::RGB(0, 0, 0));
-    canvas.clear();
-    canvas.present();
-    let event_pump = context.event_pump().unwrap();
-    (canvas, event_pump)
-}
-
-fn load_textures(texture_creator: &TextureCreator<WindowContext>) -> HashMap<&str, Texture, RandomState> {
-    let mut textures = HashMap::new();
-    textures.insert("block", texture_creator.load_texture("res/block.bmp").unwrap());
-    textures.insert("mario", texture_creator.load_texture("res/mario-trans.png").unwrap());
-    textures
-}
+const SCREEN_WIDTH: u16 = 1280;
+const SCREEN_HEIGHT: u16 = (SCREEN_WIDTH as f64 * (1440.0 / 2560.0)) as u16;
 
 fn main() {
     println!("starting...");
-    let (mut canvas, mut event_pump) = init();
-
-    let mut frame_count: u64 = 0;
-    let mut fps_time = PreciseTime::now();
-
-    let mut bolls: Bollar = Bollar::new();
-    let mut boll_size = 1;
-
-    let texture_creator = canvas.texture_creator();
-    let textures = load_textures(&texture_creator);
-    let mut mario_angle = 0.0;
-
-    'running: loop {
-        let loop_start = PreciseTime::now();
-        canvas.set_draw_color(Color::RGB(0, 0, 0));
-        canvas.clear();
-        {
-            let blocks = 20;
-            let size = 32;
-            let offset = point!((SCREEN_WIDTH as i32 - (blocks + 1) * size) / 2, (SCREEN_HEIGHT as i32 - (blocks + 1) * size) / 2);
-            for i in 0..blocks {
-                canvas.copy(&(textures.get("block").unwrap()), None, Rect::new((i) * size + offset.x, (0) * size + offset.y, size as u32, size as u32)).unwrap();
-                canvas.copy(&(textures.get("block").unwrap()), None, Rect::new((blocks - i) * size + offset.x, (blocks) * size + offset.y, size as u32, size as u32)).unwrap();
-                canvas.copy(&(textures.get("block").unwrap()), None, Rect::new((0) * size + offset.x, (blocks - i) * size + offset.y, size as u32, size as u32)).unwrap();
-                canvas.copy(&(textures.get("block").unwrap()), None, Rect::new((blocks) * size + offset.x, (i) * size + offset.y, size as u32, size as u32)).unwrap();
-            }
-        }
-        {
-            let size = 64;
-            let offset = point!((SCREEN_WIDTH as i32 - size) / 2, (SCREEN_HEIGHT as i32 - size) / 2);
-            let radius = 110.0 + size as f32 * 0.5;
-            let angle = (mario_angle as f32 - 90.0) * PI / 180.0;
-            let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32);
-            canvas.copy_ex(&(textures.get("mario").unwrap()), None, Rect::new(offset.x + offset2.x, offset.y + offset2.y, size as u32, size as u32), mario_angle, sdl2::rect::Point::new(size / 2, size / 2), false, false).unwrap();
-            mario_angle += 1.0;
-            if mario_angle >= 360.0 { mario_angle -= 360.0 }
-        }
-        {
-            let p = point!((SCREEN_WIDTH / 2) as i16, (SCREEN_HEIGHT / 2) as i16);
-            canvas.circle(p.x, p.y, 100, Color::RGB(255, 255, 255)).unwrap();
-            canvas.aa_circle(p.x, p.y, 110, Color::RGB(255, 255, 255)).unwrap();
-            canvas.ellipse(p.x, p.y, 50, 100, Color::RGB(255, 255, 255)).unwrap();
-            canvas.aa_ellipse(p.x, p.y, 110, 55, Color::RGB(255, 255, 255)).unwrap();
-        }
-
-        for b in &mut bolls {
-            b.update();
-            b.draw(&mut canvas, boll_size);
-        }
-
-//        window.gl_swap_window();
-        for event in event_pump.poll_iter() {
-            match event {
-                Event::Quit { .. } | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
-                    break 'running;
-                }
-                Event::KeyDown { keycode: Some(Keycode::F11), .. } => {
-                    canvas.window_mut()
-                        .set_fullscreen(sdl2::video::FullscreenType::True).unwrap();
-                }
-                Event::KeyDown { keycode: Some(Keycode::KpPlus), .. } => { boll_size = std::cmp::min(boll_size + 1, 32) }
-                Event::KeyDown { keycode: Some(Keycode::KpMinus), .. } => { boll_size = std::cmp::max(boll_size - 1, 1) }
-                Event::MouseMotion { x, y, .. } => {
-                    bolls.push(Box::new(CircleBoll::new(
-                        point!(x as f64, y as f64),
-                        point!(0.0, 0.0),
-                    )))
-                }
-                _ => {}
-            }
-        }
-        canvas.present();
-
-        match loop_start.to(PreciseTime::now()).num_nanoseconds() {
-            Some(ns) if ns < (NS_PER_FRAME - 50_0000) as i64 => { change_boll_count(&mut bolls, 100) }
-            Some(ns) if ns > (NS_PER_FRAME + 50_0000) as i64 => { change_boll_count(&mut bolls, -100) }
-            _ => {}
-        }
-
-        frame_count += 1;
-        if frame_count == FPS as u64 {
-            let duration = fps_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as f64 / 1_000_000_000.0;
-            println!("fps: {}", frame_count as f64 / duration);
-            frame_count = 0;
-            fps_time = PreciseTime::now();
-        }
-    }
-
-    println!("number of bolls: {}", bolls.len());
-}
-
-fn change_boll_count(mut bolls: &mut Bollar, delta: i32) {
-    if delta > 0 {
-        for _i in 0..delta {
-            add_boll(&mut bolls);
-        }
-    } else if delta < 0 {
-        for _i in 0..delta {
-            bolls.pop();
-        }
-    }
-}
+    let mut app = App::new()
+        .with_resolution(SCREEN_WIDTH, SCREEN_HEIGHT)
+//        .with_state(Box::new(ActiveState::new((SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32))))
+        .with_state(Box::new(GameState::new()))
+        .with_title("SDL test")
+        .build()
+        .unwrap();
+    app.load_sprites(&[("block", "res/block.bmp"), ("mario", "res/mario-trans.png")]);
 
-fn add_boll(bolls: &mut Bollar) {
-    let mut rng = rand::thread_rng();
-    bolls.push(Box::new(SquareBoll {
-        pos: point!(rng.gen_range(0, SCREEN_WIDTH) as f64, rng.gen_range(0, SCREEN_HEIGHT) as f64),
-        vel: point!(rng.gen_range(-2.0, 2.0), rng.gen_range(-2.0, 2.0)),
-    }));
+    app.start();
 }