1use jengine::engine::{Color, Game, jEngine, KeyCode};
2use std::time::Instant;
3
4struct BenchState {
5 entity_count: usize,
6 last_stat_update: Instant,
7 frame_count: u32,
8 fps: f32,
9 auto_mode: bool,
10}
11
12impl BenchState {
13 fn new() -> Self {
14 Self {
15 entity_count: 0,
16 last_stat_update: Instant::now(),
17 frame_count: 0,
18 fps: 0.0,
19 auto_mode: true,
20 }
21 }
22
23 fn spawn_batch(&mut self, engine: &mut jEngine, count: usize) {
24 let gw = engine.grid_width();
25 let gh = engine.grid_height();
26
27 for _ in 0..count {
28 let x = (pseudo_rand(self.entity_count as u64) * gw as f32) as u32;
29 let y = (pseudo_rand(self.entity_count as u64 + 7) * gh as f32) as u32;
30
31 let ch = if self.entity_count % 2 == 0 { '@' } else { 'E' };
32 let color = if self.entity_count % 2 == 0 { Color::GREEN } else { Color::RED };
33
34 engine.set_foreground(x, y, ch, color);
35
36 if self.entity_count % 10 == 0 {
37 let px = pseudo_rand(self.entity_count as u64) * gw as f32 * engine.tile_width() as f32;
38 let py = pseudo_rand(self.entity_count as u64 + 1) * gh as f32 * engine.tile_height() as f32;
39 engine.draw_particle(px, py, Color::YELLOW, 2.0);
40 }
41 self.entity_count += 1;
42 }
43 }
44}
45
46fn pseudo_rand(seed: u64) -> f32 {
47 let x = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
48 (x >> 33) as f32 / u32::MAX as f32
49}
50
51impl Game for BenchState {
52 fn update(&mut self, engine: &mut jEngine) {
53 if engine.is_key_pressed(KeyCode::Space) {
54 self.auto_mode = !self.auto_mode;
55 }
56
57 if self.auto_mode || engine.is_key_held(KeyCode::KeyW) {
58 self.spawn_batch(engine, 500);
59 }
60
61 if engine.is_key_pressed(KeyCode::KeyC) {
62 engine.clear();
63 self.entity_count = 0;
64 }
65
66 self.frame_count += 1;
67 let now = Instant::now();
68 let elapsed = now.duration_since(self.last_stat_update).as_secs_f32();
69 if elapsed >= 1.0 {
70 self.fps = self.frame_count as f32 / elapsed;
71 self.frame_count = 0;
72 self.last_stat_update = now;
73 println!("Entities: {}, FPS: {:.1}", self.entity_count, self.fps);
74 }
75 }
76
77 fn render(&mut self, engine: &mut jEngine) {
78 engine.ui.ui_rect(10.0, 10.0, 300.0, 100.0, Color([0.0, 0.0, 0.0, 0.8]));
79 let stats = format!("Entities: {}", self.entity_count);
80 let fps_str = format!("FPS: {:.1}", self.fps);
81 let mode = if self.auto_mode { "AUTO (Space to toggle)" } else { "MANUAL (Hold W to spawn)" };
82
83 engine.ui.ui_text(20.0, 20.0, "PERFORMANCE BENCHMARK", Color::CYAN, Color::TRANSPARENT, None);
84 engine.ui.ui_text(20.0, 40.0, &stats, Color::WHITE, Color::TRANSPARENT, None);
85 engine.ui.ui_text(20.0, 60.0, &fps_str, Color::YELLOW, Color::TRANSPARENT, None);
86 engine.ui.ui_text(20.0, 80.0, mode, Color::GRAY, Color::TRANSPARENT, None);
87 }
88}
89
90fn main() {
91 jEngine::builder()
92 .with_title("jengine Performance Benchmark")
93 .with_size(1280, 720)
94 .run(BenchState::new());
95}