use spottedcat::{Context, DrawOption3D, Model, Spot, WindowConfig};
use std::time::Duration;
struct BillboardExample {
wall: Model,
character: Model,
billboard_plane: Model,
time: f32,
}
impl Spot for BillboardExample {
fn initialize(ctx: &mut Context) -> Self {
let wall_pixels = vec![
180, 180, 180, 255, 200, 200, 200, 255, 200, 200, 200, 255, 180, 180, 180, 255,
];
let wall_tex = spottedcat::Image::new(
ctx,
spottedcat::Pt::from(2.0),
spottedcat::Pt::from(2.0),
&wall_pixels,
)
.unwrap();
let wall = spottedcat::model::create_cube(ctx, 1.0)
.unwrap()
.with_material(wall_tex);
let char_pixels = vec![
50, 50, 255, 255, 100, 100, 255, 255, 100, 100, 255, 255, 50, 50, 255, 255,
];
let char_tex = spottedcat::Image::new(
ctx,
spottedcat::Pt::from(2.0),
spottedcat::Pt::from(2.0),
&char_pixels,
)
.unwrap();
let character = spottedcat::model::create_cube(ctx, 0.5)
.unwrap()
.with_material(char_tex);
let mut bb_pixels = vec![0; 4 * 64 * 16];
for y in 0..16 {
for x in 0..64 {
let idx = ((y * 64) + x) as usize * 4;
if x < 48 {
bb_pixels[idx] = 0;
bb_pixels[idx + 1] = 255;
bb_pixels[idx + 2] = 0;
bb_pixels[idx + 3] = 255;
} else {
bb_pixels[idx] = 255;
bb_pixels[idx + 1] = 0;
bb_pixels[idx + 2] = 0;
bb_pixels[idx + 3] = 255;
}
}
}
let bb_tex = spottedcat::Image::new(
ctx,
spottedcat::Pt::from(64.0),
spottedcat::Pt::from(16.0),
&bb_pixels,
)
.unwrap();
let billboard_plane = spottedcat::model::create_plane(ctx, 1.0, 0.25)
.unwrap()
.with_material(bb_tex);
Self {
wall,
character,
billboard_plane,
time: 0.0,
}
}
fn update(&mut self, _ctx: &mut Context, dt: Duration) {
self.time += dt.as_secs_f32();
}
fn draw(&mut self, ctx: &mut Context, screen: spottedcat::Image) {
let wall_opts = DrawOption3D::default()
.with_position([0.0, 0.0, 0.0])
.with_scale([0.2, 2.0, 2.0]);
screen.draw(ctx, &self.wall, wall_opts);
let orb_x = (self.time).cos() * 2.0;
let orb_z = (self.time).sin() * 2.0;
let char_pos = [orb_x, -0.5, orb_z];
let char_opts = DrawOption3D::default().with_position(char_pos);
screen.draw(ctx, &self.character, char_opts);
let bb_pos = [char_pos[0], char_pos[1] + 0.6, char_pos[2]];
let bb_opts = DrawOption3D::default()
.with_position(bb_pos)
.with_rotation([0.0, 0.0, 0.0]);
screen.draw(ctx, &self.billboard_plane, bb_opts);
}
fn remove(&mut self, _ctx: &mut Context) {}
}
fn main() {
spottedcat::run::<BillboardExample>(WindowConfig {
title: "Billboard (Option 1) Example".to_string(),
..Default::default()
});
}