use crate::{
assets::{Assets, MaterialId, MeshId, ModelId, ShaderId, SoundId, TextureId, path_string},
diagnostics::{DiagnosticsConfig, DiagnosticsReport, DrawCounters, RunSettings},
input::Bindings,
material::{MaterialDesc, UniformId, UniformValue},
render::{Frame, rect},
};
use rayengine_core::{
glam::Vec2,
input::Input,
mesh::MeshData,
time::{FixedClock, Tick},
ui::{UiActions, UiInput},
viewport::{ScaleMode, Viewport},
};
use raylib::prelude::*;
use std::{
fmt,
path::{Path, PathBuf},
time::{Duration, Instant},
};
#[derive(Debug)]
pub enum Error {
Config(String),
Backend(String),
Asset(String),
Io(std::io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Config(message) | Self::Backend(message) | Self::Asset(message) => {
f.write_str(message)
}
Self::Io(error) => error.fmt(f),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if let Self::Io(error) = self {
Some(error)
} else {
None
}
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::Io(error)
}
}
#[derive(Clone, Debug)]
pub struct Config {
pub title: String,
pub window_size: (u32, u32),
pub reference_size: Vec2,
pub scale_mode: ScaleMode,
pub fixed_hz: u32,
pub max_catch_up: u32,
pub target_fps: u32,
pub vsync: bool,
pub audio: bool,
pub exit_key: Option<KeyboardKey>,
pub bar_color: Color,
}
impl Config {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
window_size: (1280, 720),
reference_size: Vec2::new(960.0, 540.0),
scale_mode: ScaleMode::Fit,
fixed_hz: 120,
max_catch_up: 8,
target_fps: 120,
vsync: true,
audio: false,
exit_key: Some(KeyboardKey::KEY_ESCAPE),
bar_color: Color::new(9, 14, 24, 255),
}
}
pub fn validate(&self) -> Result<(), Error> {
if self.title.contains('\0') {
return Err(Error::Config("window title cannot contain NUL".into()));
}
validate_size(self.window_size)?;
if !self.reference_size.is_finite()
|| self.reference_size.min_element() < 1.0
|| self.reference_size.max_element() > 8192.0
{
return Err(Error::Config(
"reference dimensions must be finite and between 1 and 8192".into(),
));
}
if !(1..=1000).contains(&self.fixed_hz) || !(1..=1000).contains(&self.max_catch_up) {
return Err(Error::Config(
"fixed_hz and max_catch_up must be between 1 and 1000".into(),
));
}
if self.target_fps > 1000 {
return Err(Error::Config("target_fps must be 0..=1000".into()));
}
Ok(())
}
}
#[derive(Clone, Debug, Default)]
pub struct RunOptions {
pub diagnostics: Option<DiagnosticsConfig>,
pub frames: Option<u64>,
pub screenshot: Option<PathBuf>,
pub size: Option<(u32, u32)>,
pub hidden: bool,
pub uncapped: bool,
}
impl RunOptions {
pub fn from_env() -> Result<Self, Error> {
Self::parse(std::env::args().skip(1))
}
pub fn parse(args: impl IntoIterator<Item = String>) -> Result<Self, Error> {
let mut options = Self::default();
let mut args = args.into_iter();
while let Some(arg) = args.next() {
match arg.as_str() {
"--frames" => {
let value = args
.next()
.ok_or_else(|| Error::Config("--frames needs a positive count".into()))?;
options.frames = Some(
value
.parse::<u64>()
.ok()
.filter(|n| *n > 0)
.ok_or_else(|| {
Error::Config("--frames needs a positive count".into())
})?,
);
}
"--screenshot" => {
options.screenshot = Some(
args.next()
.ok_or_else(|| Error::Config("--screenshot needs a PNG path".into()))?
.into(),
)
}
"--size" => {
let value = args
.next()
.ok_or_else(|| Error::Config("--size needs WIDTHxHEIGHT".into()))?;
let (w, h) = value
.split_once('x')
.ok_or_else(|| Error::Config("--size needs WIDTHxHEIGHT".into()))?;
let size = (
w.parse()
.map_err(|_| Error::Config("invalid width".into()))?,
h.parse()
.map_err(|_| Error::Config("invalid height".into()))?,
);
validate_size(size)?;
options.size = Some(size);
}
"--diagnostics" => {
let path = args
.next()
.ok_or_else(|| Error::Config("--diagnostics needs a JSON path".into()))?;
options
.diagnostics
.get_or_insert_with(|| DiagnosticsConfig::new("unspecified"))
.output = Some(path.into());
}
"--workload" => {
let id = args
.next()
.ok_or_else(|| Error::Config("--workload needs a stable ID".into()))?;
options
.diagnostics
.get_or_insert_with(|| DiagnosticsConfig::new("unspecified"))
.workload = id;
}
"--hidden" => options.hidden = true,
"--uncapped" => options.uncapped = true,
_ => return Err(Error::Config(format!("unknown run option: {arg}"))),
}
}
if let Some(diagnostics) = &options.diagnostics {
diagnostics.validate()?;
}
if let Some(path) = &options.screenshot {
if path.extension().and_then(|e| e.to_str()) != Some("png") {
return Err(Error::Config("screenshot path must end in .png".into()));
}
path_string(path)?;
}
Ok(options)
}
}
#[derive(Clone, Debug, Default)]
pub struct RunReport {
pub diagnostics: Option<DiagnosticsReport>,
pub frames: u64,
pub ticks: u64,
pub dropped_time: Duration,
pub elapsed: Duration,
}
pub struct InitContext<'context, 'audio> {
pub raylib: &'context mut RaylibHandle,
pub thread: &'context RaylibThread,
pub assets: &'context mut Assets<'audio>,
}
impl InitContext<'_, '_> {
pub fn material(&mut self, desc: MaterialDesc) -> Result<MaterialId, Error> {
self.assets.create_material(self.raylib, self.thread, desc)
}
pub fn shader_from_source(
&mut self,
vertex: Option<&str>,
fragment: &str,
) -> Result<ShaderId, Error> {
self.assets
.shader_source(self.raylib, self.thread, vertex, fragment)
}
pub fn shader(
&mut self,
vertex: Option<&Path>,
fragment: impl AsRef<Path>,
) -> Result<ShaderId, Error> {
self.assets
.load_shader(self.raylib, self.thread, vertex, fragment.as_ref())
}
pub fn uniform(
&mut self,
shader: ShaderId,
name: &str,
initial: UniformValue,
) -> Result<UniformId, Error> {
self.assets.uniform(shader, name, initial)
}
pub fn mesh(&mut self, data: &MeshData) -> Result<MeshId, Error> {
self.assets.upload_mesh(self.raylib, self.thread, data)
}
pub fn replace_mesh(&mut self, id: MeshId, data: &MeshData) -> Result<(), Error> {
self.assets.replace_mesh(self.thread, id, data)
}
pub fn texture(&mut self, path: impl AsRef<Path>) -> Result<TextureId, Error> {
self.assets
.load_texture(self.raylib, self.thread, path.as_ref())
}
pub fn texture_from_image(
&mut self,
image: &raylib::prelude::Image,
) -> Result<TextureId, Error> {
self.assets
.upload_texture_image(self.raylib, self.thread, image)
}
pub fn model(&mut self, path: impl AsRef<Path>) -> Result<ModelId, Error> {
self.assets
.load_model(self.raylib, self.thread, path.as_ref())
}
pub fn sound(&mut self, path: impl AsRef<Path>) -> Result<SoundId, Error> {
self.assets.load_sound(path.as_ref())
}
}
pub struct Update<'context, 'audio> {
pub tick: Tick,
pub input: &'context Input,
pub viewport: Viewport,
pub pointer: Option<Vec2>,
pub window_focused: bool,
pub assets: &'context Assets<'audio>,
pub(crate) quit: &'context mut bool,
}
impl Update<'_, '_> {
pub fn ui_input(&self, actions: UiActions) -> UiInput {
UiInput::from_actions(self.input, self.pointer, actions, self.window_focused)
}
pub fn quit(&mut self) {
*self.quit = true;
}
}
pub trait Game {
fn cursor_mode(&self) -> CursorMode {
CursorMode::Free
}
fn bindings(&self) -> Bindings {
Bindings::new()
}
fn init(&mut self, _context: &mut InitContext<'_, '_>) -> Result<(), Error> {
Ok(())
}
fn fixed_update(&mut self, context: &mut Update<'_, '_>);
fn draw(&mut self, frame: &mut Frame<'_, '_>);
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CursorMode {
#[default]
Free,
Captured,
}
#[derive(Default)]
struct CursorState {
focused: bool,
captured: bool,
discard_motion: bool,
}
impl CursorState {
fn sync(&mut self, mode: CursorMode, focused: bool) -> bool {
let capture = focused && mode == CursorMode::Captured;
let changed = capture != self.captured;
self.discard_motion |= changed || focused != self.focused;
self.focused = focused;
self.captured = capture;
changed
}
fn take_motion(&mut self) -> bool {
let discard = std::mem::take(&mut self.discard_motion);
self.focused && !discard
}
#[cfg(test)]
fn update(&mut self, mode: CursorMode, focused: bool) -> (bool, bool) {
let changed = self.sync(mode, focused);
(changed, self.take_motion())
}
}
fn sync_cursor(
raylib: &mut RaylibHandle,
state: &mut CursorState,
mode: CursorMode,
focused: bool,
) {
if state.sync(mode, focused) {
if state.captured {
raylib.disable_cursor();
} else {
raylib.enable_cursor();
}
}
}
pub struct App {
config: Config,
options: RunOptions,
}
impl App {
pub fn new(config: Config) -> Self {
Self {
config,
options: RunOptions::default(),
}
}
pub fn with_options(mut self, options: RunOptions) -> Self {
self.options = options;
self
}
pub fn run(self, game: impl Game) -> Result<RunReport, Error> {
let Self {
mut config,
options,
} = self;
if let Some(size) = options.size {
config.window_size = size;
}
config.validate()?;
if let Some(diagnostics) = &options.diagnostics {
diagnostics.validate()?;
}
if options.frames == Some(0) {
return Err(Error::Config("frame limit must be positive".into()));
}
if let Some(path) = &options.screenshot {
if path.extension().and_then(|e| e.to_str()) != Some("png") {
return Err(Error::Config("screenshot path must end in .png".into()));
}
path_string(path)?;
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
std::fs::create_dir_all(parent)?;
}
}
let mut builder = raylib::init();
builder
.size(config.window_size.0 as i32, config.window_size.1 as i32)
.title(&config.title)
.resizable()
.highdpi()
.log_level(TraceLogLevel::LOG_WARNING);
if config.vsync && !options.uncapped {
builder.vsync();
}
if options.hidden {
builder.hidden().always_run();
}
let (mut raylib, thread) = builder.build();
raylib.set_exit_key(config.exit_key);
raylib.set_target_fps(if options.uncapped {
0
} else {
config.target_fps
});
let audio = if config.audio {
Some(RaylibAudio::init_audio_device().map_err(|e| Error::Backend(e.to_string()))?)
} else {
None
};
let mut assets = Assets::new(audio.as_ref());
let mut game = game;
let bindings = game.bindings();
let mut input = Input::with_capacity(bindings.capacity());
game.init(&mut InitContext {
raylib: &mut raylib,
thread: &thread,
assets: &mut assets,
})?;
let mut cursor = CursorState::default();
let mut target: Option<RenderTexture2D> = None;
let mut target_size = (0, 0);
let mut clock = FixedClock::new(config.fixed_hz, config.max_catch_up);
let start = Instant::now();
let mut previous_frame = start;
let mut report = RunReport {
diagnostics: options.diagnostics.as_ref().map(|diagnostics| {
DiagnosticsReport::new(
diagnostics,
RunSettings {
backend: if cfg!(target_os = "windows") {
"glfw-win32"
} else if cfg!(target_os = "macos") {
"glfw-cocoa"
} else if cfg!(all(unix, not(target_vendor = "apple")))
&& cfg!(feature = "wayland")
{
"glfw-x11+wayland"
} else if cfg!(all(unix, not(target_vendor = "apple"))) {
"glfw-x11"
} else {
"glfw"
},
os: std::env::consts::OS,
arch: std::env::consts::ARCH,
sdk_version: env!("CARGO_PKG_VERSION"),
window_size: config.window_size,
reference_size: config.reference_size.to_array(),
scale_mode: match config.scale_mode {
ScaleMode::Fit => "fit",
ScaleMode::Expand => "expand",
ScaleMode::IntegerFit => "integer-fit",
},
fixed_hz: config.fixed_hz,
max_catch_up: config.max_catch_up,
target_fps: if options.uncapped {
0
} else {
config.target_fps
},
vsync: config.vsync && !options.uncapped,
render_size: (0, 0),
},
assets.resource_counts(),
)
}),
..RunReport::default()
};
let mut quit = false;
while !quit
&& !raylib.window_should_close()
&& options.frames.is_none_or(|limit| report.frames < limit)
{
let now = Instant::now();
let elapsed = now.duration_since(previous_frame);
previous_frame = now;
let window = Vec2::new(
raylib.get_screen_width() as f32,
raylib.get_screen_height() as f32,
);
let view = Viewport::new(window, config.reference_size, config.scale_mode);
if raylib.is_window_minimized() || view.is_none() {
sync_cursor(&mut raylib, &mut cursor, game.cursor_mode(), false);
input.release_all();
raylib
.begin_drawing(&thread)
.clear_background(config.bar_color);
std::thread::sleep(Duration::from_millis(16));
continue;
}
let view = view.expect("non-minimized viewport");
let window_focused = raylib.is_window_focused();
sync_cursor(&mut raylib, &mut cursor, game.cursor_mode(), window_focused);
bindings.sample(&raylib, &mut input);
if cursor.take_motion() {
let delta = raylib.get_mouse_delta();
input.add_pointer_delta(Vec2::new(delta.x, delta.y));
}
let mouse = raylib.get_mouse_position();
let plan = clock.advance(elapsed);
report.dropped_time += plan.dropped;
if let Some(metrics) = &mut report.diagnostics {
metrics.dropped_ns = metrics
.dropped_ns
.saturating_add(plan.dropped.as_nanos().min(u128::from(u64::MAX)) as u64);
}
for step in 0..plan.steps {
let update_start = report.diagnostics.as_ref().map(|_| Instant::now());
game.fixed_update(&mut Update {
tick: Tick {
index: plan.first_tick + u64::from(step),
dt: clock.step().as_secs_f32(),
},
input: &input,
viewport: view,
pointer: (window_focused && !cursor.captured)
.then(|| view.screen_to_ui(Vec2::new(mouse.x, mouse.y)))
.flatten(),
window_focused,
assets: &assets,
quit: &mut quit,
});
if let Some(metrics) = &mut report.diagnostics {
metrics
.update
.record(update_start.expect("diagnostics enabled").elapsed());
metrics.updates = metrics.updates.saturating_add(1);
}
input.consume_edges();
sync_cursor(&mut raylib, &mut cursor, game.cursor_mode(), window_focused);
report.ticks += 1;
if quit {
break;
}
}
let render_start = report.diagnostics.as_ref().map(|_| Instant::now());
let dpi = Vec2::new(
raylib.get_render_width() as f32,
raylib.get_render_height() as f32,
) / window;
let size = if config.scale_mode == ScaleMode::IntegerFit {
(
config.reference_size.x.round() as u32,
config.reference_size.y.round() as u32,
)
} else {
view.render_size(dpi)
};
if size != target_size {
let new_target = raylib
.load_render_texture(&thread, size.0, size.1)
.map_err(|e| Error::Backend(e.to_string()))?;
new_target.texture().set_texture_filter(
&thread,
if config.scale_mode == ScaleMode::IntegerFit {
TextureFilter::TEXTURE_FILTER_POINT
} else {
TextureFilter::TEXTURE_FILTER_BILINEAR
},
);
target = Some(new_target);
target_size = size;
}
let target = target.as_mut().expect("created target");
let draws = {
let mut frame = Frame {
counters: report.diagnostics.as_ref().map(|_| DrawCounters::default()),
raylib: &mut raylib,
thread: &thread,
target,
assets: &mut assets,
viewport: view,
alpha: plan.alpha,
index: report.frames,
};
frame.clear(Color::BLACK);
game.draw(&mut frame);
frame.draw_counters()
};
if let Some(metrics) = &mut report.diagnostics {
metrics
.render
.record(render_start.expect("diagnostics enabled").elapsed());
}
let present_start = report.diagnostics.as_ref().map(|_| Instant::now());
{
let mut draw = raylib.begin_drawing(&thread);
draw.clear_background(config.bar_color);
draw.draw_texture_pro(
target.texture(),
Rectangle::new(0.0, 0.0, size.0 as f32, -(size.1 as f32)),
rect(rayengine_core::collision::Aabb2 {
min: view.origin,
max: view.origin + view.size,
}),
Vector2::zero(),
0.0,
Color::WHITE,
);
}
if let Some(metrics) = &mut report.diagnostics {
metrics
.present
.record(present_start.expect("diagnostics enabled").elapsed());
metrics.settings.render_size = size;
metrics.record_frame(draws.unwrap_or_default(), assets.resource_counts());
metrics.frame.record(now.elapsed());
}
report.frames += 1;
}
if let Some(path) = &options.screenshot {
let image = raylib.load_image_from_screen(&thread);
let png = image
.export_image_to_memory(".png")
.map_err(|e| Error::Backend(e.to_string()))?;
std::fs::write(path, &*png)?;
}
report.elapsed = start.elapsed();
if let Some((path, metrics)) = options
.diagnostics
.as_ref()
.and_then(|d| d.output.as_ref())
.zip(report.diagnostics.as_ref())
{
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
std::fs::create_dir_all(parent)?;
}
let file = std::fs::File::create(path)?;
let mut writer = std::io::BufWriter::new(file);
metrics
.write_json(&mut writer)
.map_err(|error| Error::Io(std::io::Error::other(error)))?;
std::io::Write::flush(&mut writer)?;
}
Ok(report)
}
}
fn validate_size(size: (u32, u32)) -> Result<(), Error> {
if (1..=8192).contains(&size.0) && (1..=8192).contains(&size.1) {
Ok(())
} else {
Err(Error::Config(
"window dimensions must be between 1 and 8192".into(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn capture_releases_on_focus_loss_and_ignores_motion_at_focus_transitions() {
let mut state = CursorState::default();
assert_eq!(state.update(CursorMode::Captured, false), (false, false));
assert_eq!(state.update(CursorMode::Captured, true), (true, false));
assert!(state.captured);
assert_eq!(state.update(CursorMode::Captured, true), (false, true));
assert_eq!(state.update(CursorMode::Captured, false), (true, false));
assert!(!state.captured);
assert_eq!(state.update(CursorMode::Captured, true), (true, false));
assert_eq!(state.update(CursorMode::Captured, true), (false, true));
let mut free = CursorState::default();
assert_eq!(free.update(CursorMode::Free, true), (false, false));
assert_eq!(free.update(CursorMode::Free, true), (false, true));
assert!(!free.captured);
}
#[test]
fn menu_transitions_discard_motion_even_when_changed_between_samples() {
let mut state = CursorState::default();
assert!(state.sync(CursorMode::Captured, true));
assert!(!state.take_motion());
assert!(state.take_motion());
assert!(state.sync(CursorMode::Free, true));
assert!(!state.captured);
assert!(state.sync(CursorMode::Captured, true));
assert!(!state.take_motion()); assert!(state.take_motion());
assert!(state.sync(CursorMode::Captured, false));
assert!(!state.take_motion());
assert!(state.sync(CursorMode::Captured, true));
assert!(!state.take_motion());
assert!(state.take_motion());
}
#[test]
fn bad_configuration_is_rejected_before_opening_window() {
let mut config = Config::new("test");
config.fixed_hz = 0;
assert!(config.validate().is_err());
config.fixed_hz = 120;
config.reference_size.x = f32::NAN;
assert!(config.validate().is_err());
config.reference_size.x = 960.0;
config.title = "bad\0title".into();
assert!(config.validate().is_err());
}
#[test]
fn run_arguments_are_strict_and_bounded() {
let parse = |args: &[&str]| RunOptions::parse(args.iter().map(|s| s.to_string()));
let options = parse(&[
"--frames",
"10",
"--size",
"800x600",
"--screenshot",
"frame.png",
"--hidden",
])
.unwrap();
assert_eq!(options.frames, Some(10));
assert_eq!(options.size, Some((800, 600)));
assert!(options.hidden);
for args in [
&["--frames", "0"][..],
&["--frames"],
&["--size", "0x2"],
&["--screenshot", "out.jpg"],
&["--wat"],
] {
assert!(parse(args).is_err());
}
}
#[test]
#[ignore = "requires a native display and OpenGL context; scripts/native_smoke.sh"]
fn native_render_smoke() {
use rayengine_core::{
camera::{Camera2D as EngineCamera2D, Camera3D as EngineCamera3D},
collision::{Aabb2, Aabb3},
glam::Vec3,
};
let directory =
std::env::temp_dir().join(format!("rayengine-render-smoke-{}", std::process::id()));
std::fs::create_dir(&directory).unwrap();
let png = Image::gen_image_color(8, 8, Color::GREEN)
.export_image_to_memory(".png")
.unwrap();
std::fs::write(directory.join("green.png"), &*png).unwrap();
std::fs::write(
directory.join("triangle.obj"),
"v -1 0 0\nv 1 0 0\nv 0 2 0\nf 1 2 3\n",
)
.unwrap();
struct Probe {
directory: PathBuf,
texture: Option<TextureId>,
model: Option<ModelId>,
}
impl Game for Probe {
fn init(&mut self, context: &mut InitContext<'_, '_>) -> Result<(), Error> {
let path = self.directory.join("green.png");
let first = context.texture(&path)?;
assert_eq!(first, context.texture(&path)?);
context.assets.unload_texture(first);
let fresh = context.texture(&path)?;
assert_ne!(fresh, first);
assert!(context.assets.texture(first).is_none());
let image = Image::gen_image_color(8, 8, Color::GREEN);
let generated = context.texture_from_image(&image)?;
assert_eq!(context.assets.texture(generated).unwrap().width, 8);
context.assets.unload_texture(generated);
let replacement = context.texture_from_image(&image)?;
assert_ne!(replacement, generated);
assert!(context.assets.texture(generated).is_none());
context.assets.unload_texture(fresh);
self.texture = Some(replacement);
let path = self.directory.join("triangle.obj");
let model = context.model(&path)?;
assert_eq!(model, context.model(&path)?);
context.assets.unload_model(model);
let fresh_model = context.model(&path)?;
assert_ne!(fresh_model, model);
assert!(context.assets.model(model).is_none());
self.model = Some(fresh_model);
assert!(context.texture(self.directory.join("missing.png")).is_err());
assert!(context.sound(self.directory.join("missing.wav")).is_err());
Ok(())
}
fn fixed_update(&mut self, _: &mut Update<'_, '_>) {}
fn draw(&mut self, frame: &mut Frame<'_, '_>) {
frame.clear(Color::WHITE);
assert!((frame.viewport.aspect() - 16.0 / 9.0).abs() < 0.0001);
if frame.index == 0 {
frame.world_2d(EngineCamera2D::default(), |canvas| {
assert!(canvas.texture(
self.texture.unwrap(),
Aabb2::from_center(Vec2::ZERO, Vec2::splat(100.0)),
Color::WHITE
));
});
} else {
frame.world_3d(EngineCamera3D::default(), |canvas| {
canvas.cube(
Aabb3::from_center(Vec3::ZERO, Vec3::splat(2.0)),
Color::BLUE,
);
assert!(canvas.model(
self.model.unwrap(),
Vec3::new(3.0, 0.0, 0.0),
1.0,
Color::RED
));
});
}
frame.ui(|ui| {
ui.rectangle(
Aabb2 {
min: Vec2::splat(10.0),
max: Vec2::splat(60.0),
},
Color::RED,
)
});
let mut image = frame.target.texture().load_image().unwrap();
image.flip_vertical();
let center = image.get_color(image.width / 2, image.height / 2);
if frame.index == 0 {
assert_eq!(center, Color::GREEN, "2D texture probe");
} else {
assert!(
center.b > center.r.saturating_add(40),
"3D cube probe: {center:?}"
);
}
let ui_pixel = image.get_color(
(image.width as f32 * 30.0 / frame.viewport.logical_size.x) as i32,
(image.height as f32 * 30.0 / frame.viewport.logical_size.y) as i32,
);
assert!(
ui_pixel.r > 180 && ui_pixel.g < 100,
"scaled UI probe: {ui_pixel:?}"
);
if frame.index == 0 {
frame.raylib.set_window_size(800, 1000);
}
if frame.index == 2 {
assert_eq!(frame.raylib.get_screen_width(), 800);
assert_eq!(frame.raylib.get_screen_height(), 1000);
assert!(frame.viewport.origin.y > 200.0);
}
}
}
let mut config = Config::new("rayengine native render probe");
config.window_size = (960, 540);
config.vsync = false;
let screenshot = directory.join("absolute-path.png");
let report = App::new(config)
.with_options(RunOptions {
frames: Some(3),
hidden: true,
screenshot: Some(screenshot.clone()),
..RunOptions::default()
})
.run(Probe {
directory: directory.clone(),
texture: None,
model: None,
})
.unwrap();
assert_eq!(report.frames, 3);
assert!(
std::fs::read(&screenshot)
.unwrap()
.starts_with(b"\x89PNG\r\n\x1a\n")
);
std::fs::remove_dir_all(directory).unwrap();
}
}
#[cfg(test)]
mod ui_tests;
#[cfg(test)]
mod diagnostics_tests;