rustbatch/
lib.rs

1//! In order to get the crate running, you have to copy `msvc` folder and `build.rs` from crates
2//! [repository](https://github.com/jakubDoka/rustbatch)
3//! and place it into root of your project.
4//! # RustBatch
5//! This crate provides some performant tools for building big 2D games that deals with huge amount of entities.
6//! So far crate contains:
7//! - OpenGl abstraction based around batching.
8//! - Collision scanner that provides fast collision detection for couple thousand entities
9//! - Multithreaded pathfinder that has no problems ewen with 1000 X 1000 tile-maps
10//! - Math module that contains structs like Matrix, Vector or Rectangle
11//! - Custom per-batch vertex and fragment shader also supported
12//! - Sprite packing so you can take advantage of batching as match as you can
13//! # Warming
14//! Important thing to note is that some functions from render module are calling functions and
15//! using enums from gl crate. Functions has to be loaded first witch you achieve by creating
16//! window.
17//! # Example
18//! ```
19//!extern crate rustbatch;
20//!
21//!use rustbatch::{sdl2, image, gl};
22//!use rustbatch::debug::FPS;
23//!use rustbatch::{Window, Texture, Sprite, Batch};
24//!use rustbatch::{Mat, Vect};
25//!use rustbatch::render::texture::TextureConfig;
26//!use rustbatch::rgba::WHITE;
27//!
28//!fn main() {
29//!    // creating window to draw to and event pump to read input. Ignore
30//!    // gl var, it cannot be dropped otherwise rendering will not work so just leave it be
31//!
32//!
33//!    let (mut window, mut event_pump, _gl, _sdl, _video_subsystem) = Window::new(|sys| {
34//!        sys.window("rusty batch", 400, 400)
35//!            .opengl()
36//!            .resizable()
37//!            .build()
38//!            .unwrap()
39//!    });
40//!
41//!    window.set_background_color(&[0.5f32, 0.5f32, 0.5f32, 1f32]); //gray background
42//!
43//!    // This is wrapped opengl texture object
44//!    let texture = Texture::new(
45//!        "C:/Users/jakub/Documents/programming/rust/src/rustbatch/assets/logo.png",
46//!       TextureConfig::DEFAULT,
47//!    ).unwrap();
48//!
49//!    // Creating sprite. Notice that sprite is just collection of points and it cannot be directly
50//!    // drawn to window
51//!    let mut sprite = Sprite::new(texture.frame());
52//!
53//!    // On the other hand batch owns texture witch can be drawn to window
54//!    let mut batch = Batch::new(texture);
55//!
56//!    // this is just a little helper
57//!    let mut fps = FPS::new(1f32);
58//!
59//!    'main: loop {
60//!        //polling events
61//!        for event in event_pump.poll_iter() {
62//!            match event {
63//!                // break loop if X button on window is pressed
64//!                sdl2::event::Event::Quit { .. } => break 'main,
65//!                _ => {}
66//!            }
67//!        }
68//!
69//!        // i hope you know how to get delta on your own but fps returns is as bonus if you supply
70//!        // 0f32 as delta
71//!        let _delta = fps.increase(0f32);
72//!
73//!        window.clear();
74//!
75//!        // drawing sprite to batch
76//!        // texture color is multiplied by inputted color
77//!        sprite.draw(&mut batch, Vect::ZERO, Vect::mirror(1f32), 0f32, &WHITE);
78//!
79//!        // drawing batch to window
80//!        batch.draw(&mut window.canvas);
81//!
82//!        // Don't forget to clear batch if you not planning to use it as canvas,
83//!        // after all drawing sprites to batch takes some time
84//!        batch.clear();
85//!
86//!        // finishing with window update so you can se it changing
87//!        window.update();
88//!    }
89//!}
90//! ```
91
92
93/// math::vect::vect constructor that works on any type of number
94#[macro_export]
95macro_rules! vect {
96        ($x: expr, $y: expr) => {
97            Vect { x: $x as f32, y: $y as f32}
98        }
99    }
100
101/// math::rect::Rect constructor that works on any type of number
102#[macro_export]
103macro_rules! rect {
104        ($x: expr, $y: expr, $x1: expr, $y1: expr) => {
105            Rect{
106                min: Vect {
107                    x: $x as f32,
108                    y: $y as f32,
109                },
110                max: Vect {
111                    x: $x1 as f32,
112                    y: $y1 as f32,
113                },
114            }
115        }
116    }
117
118/// math::circle::Circ constructor that works on any type of number
119#[macro_export]
120macro_rules! circ {
121    ($x: expr, $y: expr; $r: expr) => {
122        Circ{
123            c: Vect {
124                    x: $x as f32,
125                    y: $y as f32,
126                },
127            r: $r as f32,
128        }
129    };
130}
131
132/// math::raycast::Ray constructor that works on any type of number
133#[macro_export]
134macro_rules! ray {
135    ($ox: expr, $oy: expr, $vx: expr, $vy: expr) => {
136            Ray {
137                o: Vect {
138                    x: $ox as f32,
139                    y: $oy as f32,
140                },
141                v: Vect {
142                    x: $vx as f32,
143                    y: $vy as f32,
144                },
145            }
146        }
147}
148
149/// math::curve::Curve constructor that works on any type of number
150#[macro_export]
151macro_rules! curve {
152    ($ax: expr, $ay: expr; $ahx: expr, $ahy: expr; $bx: expr, $by: expr; $bhx: expr, $bhy: expr) => {
153        Curve {
154            a: Vect {x: $ax as f32, y: $ay as f32},
155            a_handle: Vect {x: $ahx as f32, y: $ahy as f32},
156            b: Vect {x: $bx as f32, y: $by as f32},
157            b_handle: Vect {x: $bhx as f32, y: $bhy as f32},
158            placeholder: false,
159        }
160    }
161}
162
163/// clamp clamps any types that can be compared
164#[macro_export]
165macro_rules! clamp {
166    ($a: expr, $min: expr, $max: expr) => {
167        if $a < $min { $min } else if $a > $max { $max } else { $a }
168    };
169}
170
171pub mod images;
172pub mod render;
173pub mod debug;
174pub mod math;
175pub mod entities;
176
177pub use sdl2;
178pub use image;
179pub use gl;
180pub use rand;
181
182pub use debug::FPS;
183pub use render::{window::Window, texture::{ Texture, TextureConfig, TextureSize}, sprite::Sprite,
184                 batch::Batch, canvas::Canvas, buffer::Buffer, program::Program, shader::Shader};
185pub use entities::{pathfinder::PathFinder, predict, storage::ComponentArray, timer::Timer,
186                   detection::{quadmap::QuadMap, spatial_hash::SpatialHasher}};
187pub use images::Sheet;
188pub use math::{mat::Mat, rgba, vect::Vect, rect::Rect, curve::Curve, base::Base, angle, circle::Circ,
189               raycast::Ray, Intersection, IntersectionPoints};
190
191
192
193