teng 0.5.0

A basic game engine for the terminal
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Rendering traits and implementations for renderable objects.
//!
//! This module defines the [`Render`] trait, which enables objects to be rendered
//! to a [`Renderer`]. It also provides implementations of the `Render` trait for
//! common types like `&str`, `String`, `char`, [`Pixel`], and [`Sprite`].
//!
//! **Key Concepts:**
//!
//! *   **`Render` Trait:**  Defines the `render()` method, which takes a `Renderer`,
//!     coordinates (x, y), and a depth value as arguments.  Any type that implements
//!     `Render` can be drawn to the screen.
//! *   **Renderer Abstraction:**  The `Render` trait works with the [`Renderer`] trait,
//!     making rendering code independent of the specific rendering backend (e.g., terminal,
//!     in-memory buffer).
//! *   **Depth-based Rendering:**  The `depth` parameter in `render()` controls the rendering
//!     order, allowing you to layer objects on top of each other. Higher depth values are
//!     rendered on top.
//! *   **Trait Extensions for Styling:**  The `Render` trait provides extension methods like
//!     `with_color()`, `transparent()`, and `with_bg_color()` to easily create styled
//!     renderable objects without modifying the original object.
//!
//! **Implementations of `Render`:**
//!
//! The module provides `Render` implementations for:
//!
//! *   `&str` and `String`:  Renders text strings.
//! *   `char`: Renders a single character.
//! *   [`Pixel`]: Renders a single pixel.
//! *   [`Sprite`]: Renders a sprite (predefined grid of pixels).
//! *   `&T` where `T: Render`: Allows rendering of references to renderable objects.
//!
//! **Styling and Adapters:**
//!
//! The `with_color()`, `transparent()`, and `with_bg_color()` methods don't directly
//! modify the original object. Instead, they return *adapter* structs (`WithColor`,
//! `WithTransparency`, `WithBgColor`) that wrap the original object and apply the
//! styling during rendering.  This allows for flexible and composable styling without
//! changing the underlying data.

use crate::rendering::{color::Color, display::Display, pixel::Pixel, renderer::Renderer};
use std::fmt::Debug;

/// Trait for objects that can be rendered to a [`Renderer`].
///
/// Implement this trait for any type you want to be able to draw on the screen
/// using a `Renderer`.
pub trait Render {
    /// Renders the object to the given `Renderer` at the specified position and depth.
    ///
    /// *   `renderer`: The [`Renderer`] to use for drawing.
    /// *   `x`: The x-coordinate (column) to render at (0-indexed, from left).
    /// *   `y`: The y-coordinate (row) to render at (0-indexed, from top).
    /// *   `depth`: The rendering depth. Higher depth values are rendered on top. Should be forwarded
    ///      to the `Renderer`.
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32);

    /// Creates a new `Render` object with the specified foreground color applied.
    ///
    /// This is a trait extension method that returns a `WithColor` adapter.  It does
    /// not modify the original object but instead creates a new renderable object
    /// that applies the color during rendering.
    ///
    /// # Example
    ///
    /// ```rust ,no_run
    /// use teng::rendering::render::Render;
    /// use teng::rendering::renderer::Renderer;
    /// use teng::rendering::pixel::Pixel;
    ///
    /// struct MyRenderable;
    /// impl Render for MyRenderable {
    ///     fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
    ///         renderer.render_pixel(x, y, Pixel::new('M'), depth);
    ///     }
    /// }
    ///
    /// let my_object = MyRenderable;
    /// let red_object = my_object.with_color([255, 0, 0]); // Create a red version
    ///
    /// // ... later in render function ...
    /// # let mut renderer = panic!("any renderer");
    /// red_object.render(renderer, 10, 5, 0); // Render 'M' in red
    /// ```
    fn with_color(&self, color: [u8; 3]) -> impl Render
    where
        Self: Sized,
    {
        WithColor(color, self)
    }

    // TODO: unused. Is this necessary? transparent_bg might be more useful to have?
    #[doc(hidden)]
    fn transparent(&self) -> impl Render
    where
        Self: Sized,
    {
        WithTransparency(self)
    }

    /// Creates a new `Render` object with the specified background color applied.
    ///
    /// This returns a `WithBgColor` adapter. It does not modify the original object
    /// but creates a new `Render` that applies the background color during rendering.
    ///
    /// For an example, see [`Render::with_color`].
    fn with_bg_color(&self, bg_color: [u8; 3]) -> impl Render
    where
        Self: Sized,
    {
        WithBgColor(bg_color, self)
    }
}

impl Render for &str {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        let mut y = y;
        let mut draw_x = x;
        for (i, c) in self.chars().enumerate() {
            if c == '\n' {
                y += 1;
                draw_x = x;
                continue;
            }
            let pixel = Pixel::new(c);
            renderer.render_pixel(draw_x, y, pixel, depth);
            draw_x += 1;
        }
    }
}

impl Render for String {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        self.as_str().render(renderer, x, y, depth);
    }
}

impl Render for char {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        let pixel = Pixel::new(*self);
        renderer.render_pixel(x, y, pixel, depth);
    }
}

impl Render for Pixel {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        renderer.render_pixel(x, y, *self, depth);
    }
}

// TODO: refactor Sprite into separate module, remove generics? smallvec could help if we flatten the array
/// Represents a sprite (a fixed-size grid of pixels).
///
/// Sprites are useful for pre-defining graphical elements that can be easily
/// rendered at different positions in the game.
///
/// Type Parameters:
///
/// *   `WIDTH`:  The width of the sprite (number of columns).
/// *   `HEIGHT`: The height of the sprite (number of rows).
///
/// # Example
///
/// ```rust
/// use teng::rendering::render::Sprite;
/// use teng::rendering::pixel::Pixel;
///
/// let tree_sprite: Sprite<3, 4> = Sprite::new([
///     [' ', 'A', ' '],
///     ['/', 'W', '\\'],
///     ['I', 'I', 'I'],
///     ['I', 'I', 'I'],
/// ], 1, 1); // Center at 'W' when rendering
/// ```
#[derive(Debug)]
pub struct Sprite<const WIDTH: usize, const HEIGHT: usize> {
    /// 2D array of pixels representing the sprite.
    pub pixels: [[Pixel; WIDTH]; HEIGHT],
    /// Position of the "center" of the sprite (used for rendering positioning).
    center_pos: (usize, usize),
}

impl<const WIDTH: usize, const HEIGHT: usize> Sprite<WIDTH, HEIGHT> {
    /// Gets the height of the sprite in pixels.
    pub fn height(&self) -> usize {
        HEIGHT
    }
    /// Gets the width of the sprite in pixels.
    pub fn width(&self) -> usize {
        WIDTH
    }

    /// Creates a new `Sprite` from a 2D array of characters.
    ///
    /// *   `sprite`: A 2D array (`[[char; WIDTH]; HEIGHT]`) defining the sprite's character representation.
    /// *   `offset_x`: The x-offset of the sprite's center (relative to the top-left corner).
    /// *   `offset_y`: The y-offset of the sprite's center (relative to the top-left corner).
    ///
    /// The `center_pos` is used to control the rendering position of the sprite. When you
    /// call `sprite.render(renderer, x, y, depth)`, the coordinate `(x, y)` will correspond
    /// to the sprite's `center_pos`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use teng::rendering::render::Sprite;
    ///
    /// let smiley_sprite: Sprite<3, 3> = Sprite::new([
    ///     ['-', '-', '-'],
    ///     ['O', ' ', 'O'],
    ///     [' ', 'U', ' '],
    /// ], 1, 1); // Center at ' ' in the middle
    /// ```
    pub fn new(sprite: [[char; WIDTH]; HEIGHT], offset_x: usize, offset_y: usize) -> Self {
        let pixels = sprite.map(|row| row.map(|c| Pixel::new(c)));
        Self {
            pixels,
            center_pos: (offset_x, offset_y),
        }
    }
}

impl<const WIDTH: usize, const HEIGHT: usize> Render for Sprite<WIDTH, HEIGHT> {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        let (center_x, center_y) = self.center_pos;
        let x = x as i32 - center_x as i32;
        let y = y as i32 - center_y as i32;

        for (i, row) in self.pixels.iter().enumerate() {
            for (j, pixel) in row.iter().enumerate() {
                let render_x = x + j as i32;
                let render_y = y + i as i32;
                if render_x < 0 || render_y < 0 {
                    continue;
                }
                renderer.render_pixel(render_x as usize, render_y as usize, *pixel, depth);
            }
        }
    }
}

impl<T: Render> Render for &T {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        (*self).render(renderer, x, y, depth);
    }
}

struct WithColor<T>(pub [u8; 3], pub T);

impl<T: Render> Render for WithColor<T> {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        let mut adapter = ColorRendererAdapter {
            renderer,
            color: self.0,
        };
        self.1.render(&mut adapter, x, y, depth);
    }
}

struct WithBgColor<T>(pub [u8; 3], pub T);

impl<T: Render> Render for WithBgColor<T> {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        let mut adapter = BgColorRendererAdapter {
            renderer,
            bg_color: self.0,
        };
        self.1.render(&mut adapter, x, y, depth);
    }
}

struct WithTransparency<T>(pub T);

impl<T: Render> Render for WithTransparency<T> {
    fn render(&self, renderer: &mut dyn Renderer, x: usize, y: usize, depth: i32) {
        let mut adapter = TransparentRendererAdapter { renderer };
        self.0.render(&mut adapter, x, y, depth);
    }
}

struct ColorRendererAdapter<'a> {
    renderer: &'a mut dyn Renderer,
    color: [u8; 3],
}

impl<'a> Renderer for ColorRendererAdapter<'a> {
    fn render_pixel(&mut self, x: usize, y: usize, pixel: Pixel, depth: i32) {
        self.renderer
            .render_pixel(x, y, pixel.with_color(self.color), depth);
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.renderer.flush()
    }
}

struct BgColorRendererAdapter<'a> {
    renderer: &'a mut dyn Renderer,
    bg_color: [u8; 3],
}

impl<'a> Renderer for BgColorRendererAdapter<'a> {
    fn render_pixel(&mut self, x: usize, y: usize, pixel: Pixel, depth: i32) {
        self.renderer
            .render_pixel(x, y, pixel.with_bg_color(self.bg_color), depth);
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.renderer.flush()
    }
}

struct TransparentRendererAdapter<'a> {
    renderer: &'a mut dyn Renderer,
}

impl<'a> Renderer for TransparentRendererAdapter<'a> {
    fn render_pixel(&mut self, x: usize, y: usize, mut pixel: Pixel, depth: i32) {
        pixel.color = Color::Transparent;
        self.renderer.render_pixel(x, y, pixel, depth);
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.renderer.flush()
    }
}

/// A struct representing a display with "double the resolution" of the terminal.
///
/// This is done by only providing the capability to draw differently colored pixels to the screen.
/// Each pixel is one half of a terminal-sized pixel, and drawn via the Unicode half block characters, '▀' and '▄', and setting their respective foreground and background colors.
pub struct HalfBlockDisplayRender {
    width: usize,
    height: usize,
    display: Display<Color>,
}

impl HalfBlockDisplayRender {
    /// Creates a new `HalfBlockDisplayRender` with the specified width and height.
    ///
    /// # Arguments
    ///
    /// * `width` - The width of the display.
    /// * `height` - The height of the display. This is double the desired height in terminal pixels.
    pub fn new(width: usize, height: usize) -> Self {
        Self {
            width,
            height,
            display: Display::new(width, height, Color::Transparent),
        }
    }

    /// Returns the height of the display.
    pub fn height(&self) -> usize {
        self.height
    }

    /// Returns the width of the display.
    pub fn width(&self) -> usize {
        self.width
    }

    /// Sets the color of a specific pixel in the display. Uses the half-block coordinate space.
    pub fn set_color(&mut self, x: usize, y: usize, color: Color) {
        self.display.set(x, y, color);
    }
    
    /// Returns the color of a specific pixel in the display. Uses the half-block coordinate space.
    /// Returns `None` if the coordinates are out of bounds.
    pub fn get_color(&self, x: usize, y: usize) -> Option<Color> {
        self.display.get(x, y).copied()
    }

    /// Resizes the display to the specified width and height, discarding the current content.
    pub fn resize_discard(&mut self, width: usize, height: usize) {
        self.width = width;
        self.height = height;
        self.display.resize_discard(width, height);
    }

    /// Clears the display, setting all pixels to the transparent color.
    pub fn clear(&mut self) {
        self.display.clear();
    }
}

impl Render for HalfBlockDisplayRender {
    fn render(&self, renderer: &mut dyn Renderer, base_x: usize, base_y: usize, depth: i32) {
        for y_offset in 0..(self.height / 2) {
            for x_offset in 0..self.width {
                let x = base_x + x_offset;
                let y = base_y + y_offset;
                let color_top = *self.display.get(x_offset, 2 * y_offset).unwrap();
                let color_bottom = *self.display.get(x_offset, 2 * y_offset + 1).unwrap();

                match (color_top, color_bottom) {
                    (Color::Transparent, Color::Transparent) => continue,
                    (Color::Transparent, color) => {
                        let mut pixel = Pixel::new('');
                        pixel.color = color;
                        pixel.bg_color = Color::Transparent;
                        renderer.render_pixel(x, y, pixel, depth);
                    }
                    (color, Color::Transparent) => {
                        let mut pixel = Pixel::new('');
                        pixel.color = color;
                        pixel.bg_color = Color::Transparent;
                        renderer.render_pixel(x, y, pixel, depth);
                    }
                    (color_top, color_bottom) => {
                        let mut pixel = Pixel::new('');
                        pixel.color = color_top;
                        pixel.bg_color = color_bottom;
                        renderer.render_pixel(x, y, pixel, depth);
                    }
                }
            }
        }
    }
}