Module agents::app[][src]

Expand description

The application structure and supporting traits.

Example

use agents::app::{App, State};
use anyhow::{Context, Result};
use draw2d::graphics::{
    Graphics,
    layer::{LayerHandle, Batch},
    texture_atlas::TextureHandle,
    vertex::Vertex2d
};
use glfw::Window;
use std::time::Duration;

/// Every binary will have a structure of some sort.
struct Example {
  layer: LayerHandle
}

/// The structure can have whatever implementation functions it needs.
impl Example {

  /// The constructor has access to both the window and graphics.
  pub fn create(
    window: &mut glfw::Window,
    graphics: &mut Graphics
  ) -> Result<Self> {
    Ok(Self {
      layer: graphics.add_layer_to_top()
    })
  }
}

/// The example must implement State to be used with an application.
/// All methods have defaults, so none are *required* for a bare minimum
/// working example.
impl State for Example {
  fn update(
    &mut self,
    _: &mut Window,
    g: &mut Graphics,
    _dt: Duration
  ) -> Result<()> {
    let mut batch = Batch::empty();
    batch.vertices.extend_from_slice(&[
        Vertex2d {
            pos: [-100.0, -100.0],
            ..Default::default()
        },
        Vertex2d {
            pos: [100.0, -100.0],
            ..Default::default()
        },
        Vertex2d {
            pos: [0.0, 100.0],
            rgba: [0.1, 0.2, 0.8, 1.0],
            ..Default::default()
        },
    ]);
    g.get_layer_mut(&self.layer).push_batch(batch);
    Ok(())
  }
}

/// Finally, start the application and let it run until the user quits.
fn main() -> Result<()> {
  App::new(Example::create)?.main_loop()
}

Structs

The main application.

The timer is used to compute the time to update and the average update duration.

Traits

Each application maintains an instance of State which controls the actual behavior and rendering.