#collision-detection #2d-game #opengl #solution #2d-rendering #batch #fps

rustbatch

purely game dewelopment crate that offers simple but powerfull 2D rendering and some fast solutions for game world bottle necks

5 unstable releases

0.4.0 Oct 17, 2020
0.3.1 Oct 4, 2020
0.2.2 Oct 3, 2020
0.2.1 Sep 26, 2020
0.1.2 Sep 24, 2020

#653 in Graphics APIs

MIT/Apache

6.5MB
3.5K SLoC

Contains (Windows DLL, 1.5MB) msvc/dll/64/SDL2.dll, (Windows DLL, 1.5MB) SDL2.dll, (Windows DLL, 1MB) msvc/dll/86/SDL2.dll, (static library, 1MB) msvc/lib/64/SDL2test.lib, (static library, 1MB) msvc/lib/86/SDL2test.lib, (static library, 150KB) msvc/lib/64/SDL2.lib and 3 more.

rustbatch

This is my attempt to make 2D game library in rust. My main focus is performance and library provides fast solutions for some bottle necks of games like collizion detection or path finding. Though main feature of library is opengl wrapper build around batching. Check out examples repository where i demonstrated capabilities on rendering and processing 10k boids with 60 fps. With a help of rustbatch Scanner there is no need to make 100 milion iterations everiy frame.

If you are wondering why are first tree versions of rustbatch yanked, lets say i did not test core featires sufficiently.

Also, rustbatch has its discord channel.


lib.rs:

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.

RustBatch

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

Warming

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.

Example

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)
           .opengl()
           .resizable()
           .build()
           .unwrap()
   });

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

   // This is wrapped opengl texture object
   let texture = Texture::new(
       "C:/Users/jakub/Documents/programming/rust/src/rustbatch/assets/logo.png",
      TextureConfig::DEFAULT,
   ).unwrap();

   // 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);

       window.clear();

       // 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
       batch.clear();

       // finishing with window update so you can se it changing
       window.update();
   }
}

Dependencies

~34MB
~505K SLoC