1use ob_engine::render::ShadeLuts;
4use ob_game::anim::Qav;
5use ob_game::game::Game;
6use ob_game::resources::Resources;
7
8fn glyph(c: char) -> [u8; 7] {
10 match c.to_ascii_uppercase() {
11 '0' => [0x0e, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0e],
12 '1' => [0x04, 0x0c, 0x04, 0x04, 0x04, 0x04, 0x0e],
13 '2' => [0x0e, 0x11, 0x01, 0x02, 0x04, 0x08, 0x1f],
14 '3' => [0x1f, 0x02, 0x04, 0x02, 0x01, 0x11, 0x0e],
15 '4' => [0x02, 0x06, 0x0a, 0x12, 0x1f, 0x02, 0x02],
16 '5' => [0x1f, 0x10, 0x1e, 0x01, 0x01, 0x11, 0x0e],
17 '6' => [0x06, 0x08, 0x10, 0x1e, 0x11, 0x11, 0x0e],
18 '7' => [0x1f, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08],
19 '8' => [0x0e, 0x11, 0x11, 0x0e, 0x11, 0x11, 0x0e],
20 '9' => [0x0e, 0x11, 0x11, 0x0f, 0x01, 0x02, 0x0c],
21 'A' => [0x0e, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11],
22 'B' => [0x1e, 0x11, 0x11, 0x1e, 0x11, 0x11, 0x1e],
23 'C' => [0x0e, 0x11, 0x10, 0x10, 0x10, 0x11, 0x0e],
24 'D' => [0x1c, 0x12, 0x11, 0x11, 0x11, 0x12, 0x1c],
25 'E' => [0x1f, 0x10, 0x10, 0x1e, 0x10, 0x10, 0x1f],
26 'F' => [0x1f, 0x10, 0x10, 0x1e, 0x10, 0x10, 0x10],
27 'G' => [0x0e, 0x11, 0x10, 0x17, 0x11, 0x11, 0x0f],
28 'H' => [0x11, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11],
29 'I' => [0x0e, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0e],
30 'J' => [0x07, 0x02, 0x02, 0x02, 0x02, 0x12, 0x0c],
31 'K' => [0x11, 0x12, 0x14, 0x18, 0x14, 0x12, 0x11],
32 'L' => [0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1f],
33 'M' => [0x11, 0x1b, 0x15, 0x15, 0x11, 0x11, 0x11],
34 'N' => [0x11, 0x11, 0x19, 0x15, 0x13, 0x11, 0x11],
35 'O' => [0x0e, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e],
36 'P' => [0x1e, 0x11, 0x11, 0x1e, 0x10, 0x10, 0x10],
37 'Q' => [0x0e, 0x11, 0x11, 0x11, 0x15, 0x12, 0x0d],
38 'R' => [0x1e, 0x11, 0x11, 0x1e, 0x14, 0x12, 0x11],
39 'S' => [0x0f, 0x10, 0x10, 0x0e, 0x01, 0x01, 0x1e],
40 'T' => [0x1f, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04],
41 'U' => [0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e],
42 'V' => [0x11, 0x11, 0x11, 0x11, 0x11, 0x0a, 0x04],
43 'W' => [0x11, 0x11, 0x11, 0x15, 0x15, 0x15, 0x0a],
44 'X' => [0x11, 0x11, 0x0a, 0x04, 0x0a, 0x11, 0x11],
45 'Y' => [0x11, 0x11, 0x11, 0x0a, 0x04, 0x04, 0x04],
46 'Z' => [0x1f, 0x01, 0x02, 0x04, 0x08, 0x10, 0x1f],
47 '.' => [0, 0, 0, 0, 0, 0x0c, 0x0c],
48 ',' => [0, 0, 0, 0, 0x0c, 0x04, 0x08],
49 ':' => [0, 0x0c, 0x0c, 0, 0x0c, 0x0c, 0],
50 '!' => [0x04, 0x04, 0x04, 0x04, 0x04, 0, 0x04],
51 '?' => [0x0e, 0x11, 0x01, 0x02, 0x04, 0, 0x04],
52 '-' => [0, 0, 0, 0x1f, 0, 0, 0],
53 '/' => [0x01, 0x01, 0x02, 0x04, 0x08, 0x10, 0x10],
54 '%' => [0x18, 0x19, 0x02, 0x04, 0x08, 0x13, 0x03],
55 '\'' => [0x04, 0x04, 0x08, 0, 0, 0, 0],
56 '(' => [0x02, 0x04, 0x08, 0x08, 0x08, 0x04, 0x02],
57 ')' => [0x08, 0x04, 0x02, 0x02, 0x02, 0x04, 0x08],
58 _ => [0; 7],
59 }
60}
61
62#[allow(clippy::too_many_arguments)]
63pub fn draw_text(frame: &mut [u32], w: usize, h: usize, x: i32, y: i32, scale: i32, color: u32, text: &str) {
64 let mut cx = x;
65 for ch in text.chars() {
66 let g = glyph(ch);
67 for (row, bits) in g.iter().enumerate() {
68 for col in 0..5 {
69 if bits & (0x10 >> col) == 0 {
70 continue;
71 }
72 for sy in 0..scale {
73 for sx in 0..scale {
74 for (ox, oy, c) in [(scale / 2 + 1, scale / 2 + 1, 0u32), (0, 0, color)] {
76 let px = cx + col * scale + sx + ox;
77 let py = y + row as i32 * scale + sy + oy;
78 if px >= 0 && py >= 0 && (px as usize) < w && (py as usize) < h {
79 frame[py as usize * w + px as usize] = c;
80 }
81 }
82 }
83 }
84 }
85 }
86 cx += 6 * scale;
87 }
88}
89
90pub fn text_width(text: &str, scale: i32) -> i32 {
91 text.chars().count() as i32 * 6 * scale
92}
93
94#[allow(clippy::too_many_arguments)]
96pub fn draw_qav(
97 frame: &mut [u32],
98 w: usize,
99 h: usize,
100 res: &Resources,
101 luts: &ShadeLuts,
102 q: &Qav,
103 t: i32,
104 bob: (f64, f64),
105 base_shade: i32,
106) {
107 if q.frames.is_empty() {
108 return;
109 }
110 let f = &q.frames[q.frame_at(t)];
111 let sc = h as f64 / 200.0;
112 let cx = w as f64 * 0.5;
113 for l in f.layers.iter() {
114 if l.picnum <= 0 {
115 continue;
116 }
117 let tile = res.tiles.get(l.picnum);
118 if tile.is_empty() {
119 continue;
120 }
121 let zoom = if l.z > 0 { l.z as f64 / 65536.0 } else { 1.0 };
122 let (tw, th) = (tile.width as f64, tile.height as f64);
123 let vx = (q.x + l.x) as f64 + bob.0;
124 let vy = (q.y + l.y) as f64 + bob.1;
125 let (left, top) = if l.stat & 16 != 0 {
126 (vx, vy)
127 } else {
128 (
129 vx - (tw * 0.5 + tile.anm.xoffset as f64) * zoom,
130 vy - (th * 0.5 + tile.anm.yoffset as f64) * zoom,
131 )
132 };
133 let x0 = cx + (left - 160.0) * sc;
134 let y0 = top * sc;
135 let sw = tw * zoom * sc;
136 let shh = th * zoom * sc;
137 let lut = luts.get(l.pal);
138 let shade = (l.shade as i32 + base_shade).clamp(0, 63) as usize * 256;
139 let trans = l.stat & 1 != 0;
140 let yflip = l.stat & 4 != 0;
141 let px1 = (x0 + sw).min(w as f64).max(0.0) as usize;
142 let px0 = (x0.max(0.0) as usize).min(px1);
144 let py0 = y0.max(0.0) as usize;
145 let py1 = (y0 + shh).min(h as f64).max(0.0) as usize;
146 for py in py0..py1 {
147 let mut ty = (((py as f64 + 0.5 - y0) / shh) * th) as usize;
148 ty = ty.min(tile.height as usize - 1);
149 if yflip {
150 ty = tile.height as usize - 1 - ty;
151 }
152 let row = &mut frame[py * w..py * w + w];
153 for (px, out) in (px0..px1).zip(&mut row[px0..px1]) {
154 let tx = ((((px as f64 + 0.5 - x0) / sw) * tw) as usize).min(tile.width as usize - 1);
155 let c = tile.pixels[tx * tile.height as usize + ty];
156 if c == 255 {
157 continue;
158 }
159 let rgb = lut[shade + c as usize];
160 *out = if trans {
161 let d = *out;
162 let m = |sh: u32| ((((d >> sh) & 255) + ((rgb >> sh) & 255)) / 2) << sh;
163 m(16) | m(8) | m(0)
164 } else {
165 rgb
166 };
167 }
168 }
169 }
170}
171
172fn tint(frame: &mut [u32], color: (f64, f64, f64), amount: f64) {
173 if amount <= 0.01 {
174 return;
175 }
176 let a = amount.min(0.8);
177 let (r, g, b) = color;
178 for p in frame.iter_mut() {
179 let pr = ((*p >> 16) & 255) as f64;
180 let pg = ((*p >> 8) & 255) as f64;
181 let pb = (*p & 255) as f64;
182 let nr = (pr + (r - pr) * a) as u32;
183 let ng = (pg + (g - pg) * a) as u32;
184 let nb = (pb + (b - pb) * a) as u32;
185 *p = (nr << 16) | (ng << 8) | nb;
186 }
187}
188
189#[allow(clippy::too_many_arguments)]
190pub fn draw(
191 frame: &mut [u32],
192 w: usize,
193 h: usize,
194 res: &Resources,
195 luts: &ShadeLuts,
196 game: &Game,
197 weapon: Option<&Qav>,
198 fps: Option<f64>,
199 message: Option<&str>,
200) {
201 let s = ((h as f64 / 200.0).round() as i32).max(1);
202 if let Some(q) = weapon {
203 let p = &game.player;
204 let bob = (
205 (p.bob_phase * 0.5).sin() * p.bob_amp / 64.0,
206 (p.bob_phase).sin().abs() * p.bob_amp / 64.0,
207 );
208 let sect_shade = if p.sector >= 0 {
209 (game.world.sectors[p.sector as usize].floor.shade as i32 / 2).max(0)
210 } else {
211 0
212 };
213 if !p.dead {
214 draw_qav(frame, w, h, res, luts, q, game.weapon_view().1, bob, sect_shade);
215 }
216 }
217 tint(frame, (255.0, 0.0, 0.0), game.damage_flash * 0.6);
218 tint(frame, (255.0, 230.0, 120.0), game.pickup_flash * 0.12);
220 let (cx, cy) = (w as i32 / 2, h as i32 / 2 - (10 * h as i32 / 200));
222 for d in -2 * s..=2 * s {
223 for t in 0..(s / 2).max(1) {
224 for (x, y) in [(cx + d, cy + t), (cx + t, cy + d)] {
225 if x >= 0 && y >= 0 && (x as usize) < w && (y as usize) < h {
226 frame[y as usize * w + x as usize] = 0x00c0c0c0;
227 }
228 }
229 }
230 }
231 let p = &game.player;
232 let have_bar = status_bar(frame, w, h, res, luts, game);
233 inventory_hud(frame, w, h, res, luts, game);
234 let big = s * 2;
235 if !have_bar {
236 draw_text(
237 frame,
238 w,
239 h,
240 4 * s,
241 h as i32 - 12 * s,
242 big,
243 0x00d02020,
244 &format!("{}", p.health.max(0)),
245 );
246 if p.armor.iter().any(|&a| a > 0) {
247 let txt = format!("ARMOR {}", p.armor.iter().sum::<i32>());
248 draw_text(frame, w, h, 40 * s, h as i32 - 10 * s, s, 0x004080ff, &txt);
249 }
250 let a = &game.arsenal;
252 let def = a.def();
253 if def.ammo != 0 {
254 let txt = format!("{}", a.ammo[def.ammo]);
255 let tw = text_width(&txt, big);
256 draw_text(
257 frame,
258 w,
259 h,
260 w as i32 - tw - 4 * s,
261 h as i32 - 12 * s,
262 big,
263 0x004060ff,
264 &txt,
265 );
266 }
267 let key_colors = [
269 0x00e0e0e0, 0x0040c040, 0x00ff4020, 0x00c0c0ff, 0x00a040ff, 0x00ffff60, 0x00ff80ff,
270 ];
271 for (k, &c) in key_colors.iter().enumerate() {
272 if game.player.keys[k] {
273 let x0 = (w as i32 / 2 - 28 * s) + k as i32 * 8 * s;
274 let y0 = h as i32 - 8 * s;
275 for yy in y0..y0 + 5 * s {
276 for xx in x0..x0 + 6 * s {
277 if xx >= 0 && yy >= 0 && (xx as usize) < w && (yy as usize) < h {
278 frame[yy as usize * w + xx as usize] = c;
279 }
280 }
281 }
282 }
283 }
284 } else if p.armor.iter().any(|&a| a > 0) {
285 tile_text(
286 frame,
287 w,
288 h,
289 res,
290 luts,
291 3,
292 &format!("ARMOR {}", p.armor.iter().sum::<i32>()),
293 72.0,
294 190.0,
295 Anchor::Left,
296 1.0,
297 0,
298 0,
299 );
300 }
301 if let Some(f) = fps {
302 let txt = format!(
303 "{:.0} FPS {}X{} {} KILLS {}/{} X {:.0} Y {:.0} Z {:.0} S {}",
304 f, w, h, game.world.map_name, game.kills, game.total_kills, p.x, p.y, p.z, p.sector
305 );
306 draw_text(frame, w, h, 2 * s, 2 * s, s, 0x00ffff60, &txt);
307 }
308 if let Some(m) = message {
309 if res.tiles.get(font_base(1) + 33).is_empty() {
310 let tw = text_width(m, s);
311 draw_text(frame, w, h, (w as i32 - tw) / 2, 20 * s, s, 0x00ffffff, m);
312 } else {
313 let tw = tile_text_width(res, 1, m, 1.0);
314 tile_text(
315 frame,
316 w,
317 h,
318 res,
319 luts,
320 1,
321 m,
322 160.0 - tw / 2.0,
323 14.0,
324 Anchor::Center,
325 1.0,
326 0,
327 0,
328 );
329 }
330 }
331 if p.dead {
332 let m = "YOU ARE DEAD - PRESS E";
333 let tw = text_width(m, big);
334 draw_text(frame, w, h, (w as i32 - tw) / 2, h as i32 / 2, big, 0x00ff3030, m);
335 }
336}
337
338#[derive(Clone, Copy, PartialEq)]
340pub enum Anchor {
341 Left,
342 Center,
343 Right,
344}
345
346#[allow(clippy::too_many_arguments)]
348pub fn vtile(
349 frame: &mut [u32],
350 w: usize,
351 h: usize,
352 res: &Resources,
353 luts: &ShadeLuts,
354 pic: i32,
355 vx: f64,
356 vy: f64,
357 anchor: Anchor,
358 zoom: f64,
359 shade: i32,
360 pal: u8,
361) -> f64 {
362 let t = res.tiles.get(pic);
363 if t.is_empty() {
364 return 0.0;
365 }
366 let sc = h as f64 / 200.0 * zoom;
367 let base = match anchor {
368 Anchor::Left => 0.0,
369 Anchor::Center => w as f64 * 0.5 - 160.0 * h as f64 / 200.0,
370 Anchor::Right => w as f64 - 320.0 * h as f64 / 200.0,
371 };
372 let x0 = base + vx * h as f64 / 200.0;
373 let y0 = vy * h as f64 / 200.0;
374 let sw = t.width as f64 * sc;
375 let shh = t.height as f64 * sc;
376 let lut = luts.get(pal);
377 let so = shade.clamp(0, 63) as usize * 256;
378 let px0 = x0.max(0.0) as usize;
379 let px1 = (x0 + sw).min(w as f64).max(0.0) as usize;
380 let py0 = y0.max(0.0) as usize;
381 let py1 = (y0 + shh).min(h as f64).max(0.0) as usize;
382 for py in py0..py1 {
383 let ty = (((py as f64 + 0.5 - y0) / sc) as usize).min(t.height as usize - 1);
384 for px in px0..px1 {
385 let tx = (((px as f64 + 0.5 - x0) / sc) as usize).min(t.width as usize - 1);
386 let c = t.pixels[tx * t.height as usize + ty];
387 if c != 255 {
388 frame[py * w + px] = lut[so + c as usize];
389 }
390 }
391 }
392 t.width as f64 * zoom
393}
394
395pub fn font_base(n: i32) -> i32 {
397 4096 + 96 * n
398}
399
400fn font_glyph(res: &Resources, font: i32, c: char) -> Option<(i32, f64)> {
404 let idx = (c as i32 - 32).clamp(0, 95);
405 let height = |f: i32| res.tiles.get(font_base(f) + ('A' as i32 - 32)).height as f64;
406 for f in [font, 1, 0, 3, 4] {
407 let pic = font_base(f) + idx;
408 if !res.tiles.get(pic).is_empty() {
409 let dy = if f == font {
410 0.0
411 } else {
412 (height(font) - height(f)).max(0.0)
413 };
414 return Some((pic, dy));
415 }
416 }
417 None
418}
419
420pub fn tile_text_width(res: &Resources, font: i32, text: &str, zoom: f64) -> f64 {
422 text.chars()
423 .map(|c| match font_glyph(res, font, c).filter(|_| c != ' ') {
424 Some((pic, _)) => res.tiles.get(pic).width as f64 + 1.0,
425 None => 6.0,
426 })
427 .sum::<f64>()
428 * zoom
429}
430
431#[allow(clippy::too_many_arguments)]
433pub fn tile_text(
434 frame: &mut [u32],
435 w: usize,
436 h: usize,
437 res: &Resources,
438 luts: &ShadeLuts,
439 font: i32,
440 text: &str,
441 vx: f64,
442 vy: f64,
443 anchor: Anchor,
444 zoom: f64,
445 shade: i32,
446 pal: u8,
447) -> f64 {
448 let mut x = vx;
449 for c in text.chars() {
450 let Some((pic, dy)) = font_glyph(res, font, c).filter(|_| c != ' ') else {
451 x += 6.0 * zoom;
452 continue;
453 };
454 vtile(frame, w, h, res, luts, pic, x, vy + dy * zoom, anchor, zoom, shade, pal);
455 x += (res.tiles.get(pic).width as f64 + 1.0) * zoom;
456 }
457 x - vx
458}
459
460#[allow(clippy::too_many_arguments)]
462fn status_number(
463 frame: &mut [u32],
464 w: usize,
465 h: usize,
466 res: &Resources,
467 luts: &ShadeLuts,
468 base: i32,
469 n: i32,
470 right_x: f64,
471 vy: f64,
472 anchor: Anchor,
473) {
474 let s = n.max(0).to_string();
475 let dw = res.tiles.get(base).width as f64 + 1.0;
476 let mut x = right_x - dw * s.len() as f64;
477 for c in s.chars() {
478 let d = c as i32 - '0' as i32;
479 vtile(frame, w, h, res, luts, base + d, x, vy, anchor, 1.0, 0, 0);
480 x += dw;
481 }
482}
483
484pub fn inventory_hud(frame: &mut [u32], w: usize, h: usize, res: &Resources, luts: &ShadeLuts, game: &Game) {
486 use ob_game::inventory::{INV_ITEM_TYPES, INV_MAX, INV_NAMES};
487 let inv = &game.inventory;
488 let k = inv.selected;
489 if inv.charge[k] > 0.0 {
490 if let Some(pic) = game.item_pic(INV_ITEM_TYPES[k]) {
492 let t = res.tiles.get(pic);
493 if !t.is_empty() {
494 let zoom = (18.0 / t.width.max(t.height) as f64).min(1.0);
495 let (iw, ih) = (t.width as f64 * zoom, t.height as f64 * zoom);
496 vtile(
497 frame,
498 w,
499 h,
500 res,
501 luts,
502 pic,
503 288.0 - iw / 2.0,
504 188.0 - ih / 2.0,
505 Anchor::Right,
506 zoom,
507 0,
508 0,
509 );
510 }
511 }
512 let pct = (inv.charge[k] / INV_MAX[k] * 100.0).round() as i32;
513 let on = inv.active[k] && k != 0;
514 let label = format!("{} {}%{}", INV_NAMES[k], pct, if on { " ON" } else { "" });
515 let tw = tile_text_width(res, 3, &label, 1.0);
516 tile_text(
517 frame,
518 w,
519 h,
520 res,
521 luts,
522 3,
523 &label,
524 318.0 - tw,
525 166.0,
526 Anchor::Right,
527 1.0,
528 0,
529 0,
530 );
531 }
532 let mut y = 4.0;
533 for (name, secs) in game.powerups.list() {
534 let txt = format!("{name} {:.0}", secs.ceil());
535 let tw = tile_text_width(res, 3, &txt, 1.0);
536 tile_text(frame, w, h, res, luts, 3, &txt, 316.0 - tw, y, Anchor::Right, 1.0, 0, 0);
537 y += 8.0;
538 }
539 if game.player.underwater && game.air < ob_game::game::AIR_SECONDS {
540 let txt = if game.air > 0.0 {
541 format!("AIR {:.0}", game.air.ceil())
542 } else {
543 "DROWNING".to_string()
544 };
545 let tw = tile_text_width(res, 1, &txt, 1.0);
546 tile_text(
547 frame,
548 w,
549 h,
550 res,
551 luts,
552 1,
553 &txt,
554 160.0 - tw / 2.0,
555 150.0,
556 Anchor::Center,
557 1.0,
558 0,
559 0,
560 );
561 }
562}
563
564pub fn status_bar(frame: &mut [u32], w: usize, h: usize, res: &Resources, luts: &ShadeLuts, game: &Game) -> bool {
566 const PANEL_LEFT: i32 = 2201;
567 const PANEL_RIGHT: i32 = 2173;
568 const RED_DIGITS: i32 = 2190;
569 const BLUE_DIGITS: i32 = 2240;
570 let left = res.tiles.get(PANEL_LEFT);
571 if left.is_empty() || res.tiles.get(RED_DIGITS).is_empty() {
572 return false;
573 }
574 let ly = 200.0 - left.height as f64;
575 vtile(frame, w, h, res, luts, PANEL_LEFT, 0.0, ly, Anchor::Left, 1.0, 0, 0);
576 status_number(
577 frame,
578 w,
579 h,
580 res,
581 luts,
582 RED_DIGITS,
583 game.player.health,
584 33.0,
585 ly + 3.0,
586 Anchor::Left,
587 );
588 let a = &game.arsenal;
589 let def = a.def();
590 if def.ammo != 0 {
591 status_number(
592 frame,
593 w,
594 h,
595 res,
596 luts,
597 BLUE_DIGITS,
598 a.ammo[def.ammo],
599 66.0,
600 ly + 3.0,
601 Anchor::Left,
602 );
603 }
604 let right = res.tiles.get(PANEL_RIGHT);
605 if !right.is_empty() {
606 let rx = 320.0 - right.width as f64;
607 let ry = 200.0 - right.height as f64;
608 vtile(frame, w, h, res, luts, PANEL_RIGHT, rx, ry, Anchor::Right, 1.0, 0, 0);
609 const KEY_ICON: i32 = 2220;
613 const KEYS: usize = 6;
614 for k in 0..KEYS {
615 if game.player.keys[k] {
616 vtile(
617 frame,
618 w,
619 h,
620 res,
621 luts,
622 KEY_ICON + k as i32,
623 rx - 2.0 - (KEYS - k) as f64 * 10.0,
624 200.0 - 11.0,
625 Anchor::Right,
626 1.0,
627 0,
628 0,
629 );
630 }
631 }
632 }
633 true
634}