[][src]Crate rustbatch

In order to get the crate running, you have to copy msvc folder and build.rs from crates repository and place it into root of your project.


This crate provides some performant tools for building big 2D games that deals with huge amount of entities. So far crate contains:

  • OpenGl abstraction based around batching.
  • Collision scanner that provides fast collision detection for couple thousand entities
  • Multithreaded pathfinder that has no problems ewen with 1000 X 1000 tile-maps
  • Math module that contains structs like Matrix, Vector or Rectangle
  • Custom per-batch vertex and fragment shader also supported
  • Sprite packing so you can take advantage of batching as match as you can


Important thing to note is that some functions from render module are calling functions and using enums from gl crate. Functions has to be loaded first witch you achieve by creating window.


extern crate rustbatch;

use rustbatch::{sdl2, image, gl};
use rustbatch::debug::FPS;
use rustbatch::{Window, Texture, Sprite, Batch};
use rustbatch::{Mat, Vect};
use rustbatch::render::texture::TextureConfig;
use rustbatch::rgba::WHITE;

fn main() {
   // creating window to draw to and event pump to read input. Ignore
   // gl var, it cannot be dropped otherwise rendering will not work so just leave it be

   let (mut window, mut event_pump, _gl, _sdl, _video_subsystem) = Window::new(|sys| {
       sys.window("rusty batch", 400, 400)

   window.set_background_color(&[0.5f32, 0.5f32, 0.5f32, 1f32]); //gray background

   // This is wrapped opengl texture object
   let texture = Texture::new(

   // Creating sprite. Notice that sprite is just collection of points and it cannot be directly
   // drawn to window
   let mut sprite = Sprite::new(texture.frame());

   // On the other hand batch owns texture witch can be drawn to window
   let mut batch = Batch::new(texture);

   // this is just a little helper
   let mut fps = FPS::new(1f32);

   'main: loop {
       //polling events
       for event in event_pump.poll_iter() {
           match event {
               // break loop if X button on window is pressed
               sdl2::event::Event::Quit { .. } => break 'main,
               _ => {}

       // i hope you know how to get delta on your own but fps returns is as bonus if you supply
       // 0f32 as delta
       let _delta = fps.increase(0f32);


       // drawing sprite to batch
       // texture color is multiplied by inputted color
       sprite.draw(&mut batch, Vect::ZERO, Vect::mirror(1f32), 0f32, &WHITE);

       // drawing batch to window
       batch.draw(&mut window.canvas);

       // Don't forget to clear batch if you not planning to use it as canvas,
       // after all drawing sprites to batch takes some time

       // finishing with window update so you can se it changing


pub use sdl2;
pub use image;
pub use gl;
pub use rand;
pub use debug::FPS;
pub use render::window::Window;
pub use render::texture::Texture;
pub use render::texture::TextureConfig;
pub use render::texture::TextureSize;
pub use render::sprite::Sprite;
pub use render::batch::Batch;
pub use render::canvas::Canvas;
pub use render::buffer::Buffer;
pub use render::program::Program;
pub use render::shader::Shader;
pub use entities::pathfinder::PathFinder;
pub use entities::predict;
pub use entities::storage::ComponentArray;
pub use entities::timer::Timer;
pub use entities::detection::quadmap::QuadMap;
pub use entities::detection::spatial_hash::SpatialHasher;
pub use images::Sheet;
pub use math::mat::Mat;
pub use math::rgba;
pub use math::vect::Vect;
pub use math::rect::Rect;
pub use math::curve::Curve;
pub use math::base::Base;
pub use math::angle;
pub use math::circle::Circ;
pub use math::raycast::Ray;
pub use math::Intersection;
pub use math::IntersectionPoints;



render is module with all opengl related functionality. Create window first then you can use all features freely.



math::circle::Circ constructor that works on any type of number


clamp clamps any types that can be compared


math::curve::Curve constructor that works on any type of number


math::raycast::Ray constructor that works on any type of number


math::rect::Rect constructor that works on any type of number


math::vect::vect constructor that works on any type of number