gravita-renderer 0.1.0

Minimal CPU-based 2D rendering utilities for prototyping and simple games
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
//! Minimal 2D rendering helpers shared across examples.
//!
//! This crate intentionally stays lightweight: it provides
//! simple drawing primitives on top of a raw RGBA frame buffer.
//! Higher-level scene logic lives in the individual examples.
//!
//! # Design Philosophy
//!
//! - **CPU-based**: All rendering happens on the CPU, no GPU required
//! - **Frame buffer**: Works with any `&mut [u8]` RGBA buffer
//! - **Coordinate system**: Y increases upward (world space), caller handles conversion
//!
//! # Available Primitives
//!
//! - [`clear`] — Fill the entire frame with a solid color
//! - [`draw_circle`] — Draw a filled circle
//! - [`draw_line`] — Draw a 1-pixel wide line (Bresenham's algorithm)
//! - [`draw_axes`] — Draw X/Y debug axes
//!
//! # Example
//!
//! ```ignore
//! use gravita_renderer::{clear, draw_circle, draw_line};
//! use gravita_math::Vec2;
//!
//! let mut frame = vec![0u8; 800 * 600 * 4];
//!
//! // Clear to dark blue
//! clear(&mut frame, [0x20, 0x20, 0x40, 0xff]);
//!
//! // Draw a white circle
//! draw_circle(&mut frame, Vec2::new(400.0, 300.0), 50.0, [0xff; 4], 800, 600);
//! ```

#![warn(missing_docs)]

use gravita_math::Vec2;

/// Clear the entire frame to a solid color (RGBA).
pub fn clear(frame: &mut [u8], color: [u8; 4]) {
    for px in frame.chunks_exact_mut(4) {
        px.copy_from_slice(&color);
    }
}

/// Draw a filled circle into the frame.
pub fn draw_circle(
    frame: &mut [u8],
    center: Vec2,
    radius: f32,
    color: [u8; 4],
    width: u32,
    height: u32,
) {
    let cx = center.x.round() as i32;
    let cy = center.y.round() as i32;
    let r = radius.round() as i32;

    for y in (cy - r).max(0)..(cy + r).min(height as i32) {
        for x in (cx - r).max(0)..(cx + r).min(width as i32) {
            let dx = x - cx;
            let dy = y - cy;
            if dx * dx + dy * dy <= r * r {
                let idx = ((y as u32 * width + x as u32) * 4) as usize;
                if idx + 3 < frame.len() {
                    frame[idx..idx + 4].copy_from_slice(&color);
                }
            }
        }
    }
}

/// Draw a 1‑pixel wide line between two points using Bresenham's algorithm.
pub fn draw_line(
    frame: &mut [u8],
    start: Vec2,
    end: Vec2,
    color: [u8; 4],
    width: u32,
    height: u32,
) {
    let x0 = start.x.round() as i32;
    let y0 = start.y.round() as i32;
    let x1 = end.x.round() as i32;
    let y1 = end.y.round() as i32;

    let dx = (x1 - x0).abs();
    let dy = (y1 - y0).abs();
    let sx = if x0 < x1 { 1 } else { -1 };
    let sy = if y0 < y1 { 1 } else { -1 };
    let mut err = dx - dy;

    let mut x = x0;
    let mut y = y0;

    loop {
        if x >= 0 && x < width as i32 && y >= 0 && y < height as i32 {
            let idx = ((y as u32 * width + x as u32) * 4) as usize;
            if idx + 3 < frame.len() {
                frame[idx..idx + 4].copy_from_slice(&color);
            }
        }

        if x == x1 && y == y1 {
            break;
        }

        let e2 = 2 * err;
        if e2 > -dy {
            err -= dy;
            x += sx;
        }
        if e2 < dx {
            err += dx;
            y += sy;
        }
    }
}

/// Draw simple X/Y axes crossing at `origin`, useful for debugging.
pub fn draw_axes(frame: &mut [u8], origin: Vec2, color: [u8; 4], width: u32, height: u32) {
    let origin = Vec2::new(origin.x, origin.y);
    // Horizontal axis
    draw_line(
        frame,
        Vec2::new(0.0, origin.y),
        Vec2::new(width as f32, origin.y),
        color,
        width,
        height,
    );
    // Vertical axis
    draw_line(
        frame,
        Vec2::new(origin.x, 0.0),
        Vec2::new(origin.x, height as f32),
        color,
        width,
        height,
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    const WIDTH: u32 = 100;
    const HEIGHT: u32 = 100;

    fn create_frame() -> Vec<u8> {
        vec![0u8; (WIDTH * HEIGHT * 4) as usize]
    }

    fn pixel_at(frame: &[u8], x: u32, y: u32) -> [u8; 4] {
        let idx = ((y * WIDTH + x) * 4) as usize;
        [frame[idx], frame[idx + 1], frame[idx + 2], frame[idx + 3]]
    }

    // =========================================================================
    // Clear
    // =========================================================================

    #[test]
    fn clear_fills_entire_frame() {
        let mut frame = create_frame();
        let color = [0x12, 0x34, 0x56, 0x78];
        clear(&mut frame, color);

        // Check first pixel
        assert_eq!(pixel_at(&frame, 0, 0), color);
        // Check last pixel
        assert_eq!(pixel_at(&frame, WIDTH - 1, HEIGHT - 1), color);
        // Check middle pixel
        assert_eq!(pixel_at(&frame, 50, 50), color);
    }

    #[test]
    fn clear_with_black() {
        let mut frame = create_frame();
        // Fill with white first
        clear(&mut frame, [0xFF, 0xFF, 0xFF, 0xFF]);
        // Then clear to black
        clear(&mut frame, [0x00, 0x00, 0x00, 0xFF]);
        assert_eq!(pixel_at(&frame, 50, 50), [0x00, 0x00, 0x00, 0xFF]);
    }

    // =========================================================================
    // Draw Circle
    // =========================================================================

    #[test]
    fn draw_circle_at_center() {
        let mut frame = create_frame();
        let color = [0xFF, 0x00, 0x00, 0xFF];
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            10.0,
            color,
            WIDTH,
            HEIGHT,
        );

        // Center should be colored
        assert_eq!(pixel_at(&frame, 50, 50), color);
    }

    #[test]
    fn draw_circle_does_not_affect_outside() {
        let mut frame = create_frame();
        let color = [0xFF, 0x00, 0x00, 0xFF];
        draw_circle(&mut frame, Vec2::new(50.0, 50.0), 5.0, color, WIDTH, HEIGHT);

        // Far corner should still be black (unaffected)
        assert_eq!(pixel_at(&frame, 0, 0), [0, 0, 0, 0]);
    }

    #[test]
    fn draw_circle_clipped_at_edge() {
        let mut frame = create_frame();
        let color = [0xFF, 0x00, 0x00, 0xFF];
        // Circle partially outside left edge
        draw_circle(
            &mut frame,
            Vec2::new(-5.0, 50.0),
            10.0,
            color,
            WIDTH,
            HEIGHT,
        );

        // Should not panic and should affect edge pixels
        assert_eq!(pixel_at(&frame, 0, 50), color);
    }

    #[test]
    fn draw_circle_completely_outside_does_not_panic() {
        let mut frame = create_frame();
        let color = [0xFF, 0x00, 0x00, 0xFF];
        // Circle completely outside
        draw_circle(
            &mut frame,
            Vec2::new(-100.0, -100.0),
            10.0,
            color,
            WIDTH,
            HEIGHT,
        );
        // Should not panic - frame should be unchanged
        assert_eq!(pixel_at(&frame, 0, 0), [0, 0, 0, 0]);
    }

    #[test]
    fn draw_circle_zero_radius() {
        let mut frame = create_frame();
        let color = [0xFF, 0x00, 0x00, 0xFF];
        // Zero radius circle (essentially a point)
        draw_circle(&mut frame, Vec2::new(50.0, 50.0), 0.0, color, WIDTH, HEIGHT);
        // Should not panic
    }

    // =========================================================================
    // Draw Line
    // =========================================================================

    #[test]
    fn draw_line_horizontal() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        draw_line(
            &mut frame,
            Vec2::new(10.0, 50.0),
            Vec2::new(90.0, 50.0),
            color,
            WIDTH,
            HEIGHT,
        );

        // Start point
        assert_eq!(pixel_at(&frame, 10, 50), color);
        // End point
        assert_eq!(pixel_at(&frame, 90, 50), color);
        // Middle point
        assert_eq!(pixel_at(&frame, 50, 50), color);
    }

    #[test]
    fn draw_line_vertical() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        draw_line(
            &mut frame,
            Vec2::new(50.0, 10.0),
            Vec2::new(50.0, 90.0),
            color,
            WIDTH,
            HEIGHT,
        );

        assert_eq!(pixel_at(&frame, 50, 10), color);
        assert_eq!(pixel_at(&frame, 50, 90), color);
        assert_eq!(pixel_at(&frame, 50, 50), color);
    }

    #[test]
    fn draw_line_diagonal() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        draw_line(
            &mut frame,
            Vec2::new(10.0, 10.0),
            Vec2::new(90.0, 90.0),
            color,
            WIDTH,
            HEIGHT,
        );

        // Start and end should be colored
        assert_eq!(pixel_at(&frame, 10, 10), color);
        assert_eq!(pixel_at(&frame, 90, 90), color);
    }

    #[test]
    fn draw_line_clipped_does_not_panic() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        // Line extending outside frame
        draw_line(
            &mut frame,
            Vec2::new(-50.0, 50.0),
            Vec2::new(150.0, 50.0),
            color,
            WIDTH,
            HEIGHT,
        );
        // Should not panic
        assert_eq!(pixel_at(&frame, 0, 50), color);
        assert_eq!(pixel_at(&frame, 99, 50), color);
    }

    #[test]
    fn draw_line_completely_outside_does_not_panic() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        draw_line(
            &mut frame,
            Vec2::new(-50.0, -50.0),
            Vec2::new(-10.0, -10.0),
            color,
            WIDTH,
            HEIGHT,
        );
        // Should not panic - frame unchanged
        assert_eq!(pixel_at(&frame, 0, 0), [0, 0, 0, 0]);
    }

    #[test]
    fn draw_line_single_point() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        // Start == end
        draw_line(
            &mut frame,
            Vec2::new(50.0, 50.0),
            Vec2::new(50.0, 50.0),
            color,
            WIDTH,
            HEIGHT,
        );
        assert_eq!(pixel_at(&frame, 50, 50), color);
    }

    // =========================================================================
    // Draw Axes
    // =========================================================================

    #[test]
    fn draw_axes_draws_cross() {
        let mut frame = create_frame();
        let color = [0x00, 0x00, 0xFF, 0xFF];
        draw_axes(&mut frame, Vec2::new(50.0, 50.0), color, WIDTH, HEIGHT);

        // Check horizontal axis
        assert_eq!(pixel_at(&frame, 0, 50), color);
        assert_eq!(pixel_at(&frame, 99, 50), color);
        // Check vertical axis
        assert_eq!(pixel_at(&frame, 50, 0), color);
        assert_eq!(pixel_at(&frame, 50, 99), color);
    }

    #[test]
    fn draw_axes_at_origin() {
        let mut frame = create_frame();
        let color = [0x00, 0x00, 0xFF, 0xFF];
        draw_axes(&mut frame, Vec2::new(0.0, 0.0), color, WIDTH, HEIGHT);
        // Should not panic
    }

    // =========================================================================
    // Visual Output Correctness Tests
    // =========================================================================

    /// Count how many pixels have a specific color
    fn count_pixels_with_color(frame: &[u8], color: [u8; 4]) -> usize {
        frame.chunks_exact(4).filter(|px| px == &color).count()
    }

    /// Check if a pixel is colored (non-zero)
    #[allow(dead_code)]
    fn is_pixel_colored(frame: &[u8], x: u32, y: u32, width: u32) -> bool {
        let idx = ((y * width + x) * 4) as usize;
        frame[idx] != 0 || frame[idx + 1] != 0 || frame[idx + 2] != 0 || frame[idx + 3] != 0
    }

    #[test]
    fn visual_circle_fills_approximate_area() {
        let mut frame = create_frame();
        let color = [0xFF, 0x00, 0x00, 0xFF];
        let radius = 10.0;
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            radius,
            color,
            WIDTH,
            HEIGHT,
        );

        let filled_pixels = count_pixels_with_color(&frame, color);
        // Theoretical area: π * r² ≈ 314.159
        // Due to rasterization, actual count will differ but should be close
        let expected_area = std::f32::consts::PI * radius * radius;
        let tolerance = expected_area * 0.15; // 15% tolerance for rasterization

        assert!(
            (filled_pixels as f32 - expected_area).abs() < tolerance,
            "Circle area mismatch: got {filled_pixels} pixels, expected ~{expected_area:.0} \
             (±{tolerance:.0})"
        );
    }

    #[test]
    fn visual_circle_is_symmetric() {
        let mut frame = create_frame();
        let color = [0xFF, 0x00, 0x00, 0xFF];
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            15.0,
            color,
            WIDTH,
            HEIGHT,
        );

        // Check horizontal symmetry (exclude boundary at radius=15 due to rasterization)
        for offset in 1..=14 {
            let left = pixel_at(&frame, 50 - offset, 50);
            let right = pixel_at(&frame, 50 + offset, 50);
            assert_eq!(left, right, "Horizontal asymmetry at offset {offset}");
        }

        // Check vertical symmetry (exclude boundary)
        for offset in 1..=14 {
            let up = pixel_at(&frame, 50, 50 - offset);
            let down = pixel_at(&frame, 50, 50 + offset);
            assert_eq!(up, down, "Vertical asymmetry at offset {offset}");
        }
    }

    #[test]
    fn visual_circle_boundary_is_correct() {
        let mut frame = create_frame();
        let color = [0xFF, 0x00, 0x00, 0xFF];
        let radius = 10.0;
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            radius,
            color,
            WIDTH,
            HEIGHT,
        );

        // Pixels just inside radius should be colored
        assert_eq!(pixel_at(&frame, 50, 41), color); // Just inside top
        assert_eq!(pixel_at(&frame, 50, 59), color); // Just inside bottom
        assert_eq!(pixel_at(&frame, 41, 50), color); // Just inside left
        assert_eq!(pixel_at(&frame, 59, 50), color); // Just inside right

        // Pixels outside radius should NOT be colored
        assert_eq!(pixel_at(&frame, 50, 38), [0, 0, 0, 0]); // Outside top
        assert_eq!(pixel_at(&frame, 50, 62), [0, 0, 0, 0]); // Outside bottom
    }

    #[test]
    fn visual_line_is_continuous() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        draw_line(
            &mut frame,
            Vec2::new(10.0, 10.0),
            Vec2::new(90.0, 90.0),
            color,
            WIDTH,
            HEIGHT,
        );

        // For a 45° diagonal, Bresenham should produce a continuous line
        // Check that no pixel along the expected path is uncolored
        let mut colored_count = 0;
        for i in 10..=90 {
            // The diagonal should color pixels at approximately (i, i)
            // Due to Bresenham, check if either (i, i) or adjacent pixel is colored
            if pixel_at(&frame, i, i) == color {
                colored_count += 1;
            }
        }

        // At least 80% of expected points should be directly on the line
        assert!(
            colored_count >= 65,
            "Line should be continuous: only {colored_count} of 81 pixels colored"
        );
    }

    #[test]
    fn visual_line_no_gaps_horizontal() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        draw_line(
            &mut frame,
            Vec2::new(10.0, 50.0),
            Vec2::new(90.0, 50.0),
            color,
            WIDTH,
            HEIGHT,
        );

        // Horizontal line should have no gaps
        for x in 10..=90 {
            assert_eq!(
                pixel_at(&frame, x, 50),
                color,
                "Gap in horizontal line at x={x}"
            );
        }
    }

    #[test]
    fn visual_line_no_gaps_vertical() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        draw_line(
            &mut frame,
            Vec2::new(50.0, 10.0),
            Vec2::new(50.0, 90.0),
            color,
            WIDTH,
            HEIGHT,
        );

        // Vertical line should have no gaps
        for y in 10..=90 {
            assert_eq!(
                pixel_at(&frame, 50, y),
                color,
                "Gap in vertical line at y={y}"
            );
        }
    }

    #[test]
    fn visual_line_steep_slope_continuous() {
        let mut frame = create_frame();
        let color = [0x00, 0xFF, 0x00, 0xFF];
        // Steep slope (more vertical than horizontal)
        draw_line(
            &mut frame,
            Vec2::new(45.0, 10.0),
            Vec2::new(55.0, 90.0),
            color,
            WIDTH,
            HEIGHT,
        );

        // Count colored pixels to ensure line is drawn
        let filled = count_pixels_with_color(&frame, color);
        // Line length ≈ sqrt((55-45)² + (90-10)²) ≈ 80.6
        assert!(
            filled >= 75,
            "Steep line should have ~80 pixels, got {filled}"
        );
    }

    #[test]
    fn visual_color_channels_independent() {
        let mut frame = create_frame();

        // Draw with pure red
        draw_circle(
            &mut frame,
            Vec2::new(25.0, 50.0),
            5.0,
            [0xFF, 0x00, 0x00, 0xFF],
            WIDTH,
            HEIGHT,
        );
        // Draw with pure green
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            5.0,
            [0x00, 0xFF, 0x00, 0xFF],
            WIDTH,
            HEIGHT,
        );
        // Draw with pure blue
        draw_circle(
            &mut frame,
            Vec2::new(75.0, 50.0),
            5.0,
            [0x00, 0x00, 0xFF, 0xFF],
            WIDTH,
            HEIGHT,
        );

        // Verify each circle has correct color
        let red_px = pixel_at(&frame, 25, 50);
        let green_px = pixel_at(&frame, 50, 50);
        let blue_px = pixel_at(&frame, 75, 50);

        assert_eq!(red_px, [0xFF, 0x00, 0x00, 0xFF], "Red channel incorrect");
        assert_eq!(
            green_px,
            [0x00, 0xFF, 0x00, 0xFF],
            "Green channel incorrect"
        );
        assert_eq!(blue_px, [0x00, 0x00, 0xFF, 0xFF], "Blue channel incorrect");
    }

    #[test]
    fn visual_alpha_channel_preserved() {
        let mut frame = create_frame();
        let semi_transparent = [0xFF, 0x00, 0x00, 0x80]; // 50% alpha
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            10.0,
            semi_transparent,
            WIDTH,
            HEIGHT,
        );

        let px = pixel_at(&frame, 50, 50);
        assert_eq!(px[3], 0x80, "Alpha channel not preserved");
    }

    #[test]
    fn visual_overdraw_replaces_pixels() {
        let mut frame = create_frame();

        // Draw red circle first
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            20.0,
            [0xFF, 0x00, 0x00, 0xFF],
            WIDTH,
            HEIGHT,
        );
        // Draw smaller blue circle on top
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            10.0,
            [0x00, 0x00, 0xFF, 0xFF],
            WIDTH,
            HEIGHT,
        );

        // Center should be blue (overwritten)
        assert_eq!(pixel_at(&frame, 50, 50), [0x00, 0x00, 0xFF, 0xFF]);
        // Outer ring should still be red
        assert_eq!(pixel_at(&frame, 50, 35), [0xFF, 0x00, 0x00, 0xFF]);
    }

    #[test]
    fn visual_multiple_shapes_composite_correctly() {
        let mut frame = create_frame();
        let bg = [0x20, 0x20, 0x20, 0xFF];
        let circle_color = [0xFF, 0x00, 0x00, 0xFF];
        let line_color = [0x00, 0xFF, 0x00, 0xFF];

        clear(&mut frame, bg);
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            30.0,
            circle_color,
            WIDTH,
            HEIGHT,
        );
        draw_line(
            &mut frame,
            Vec2::new(0.0, 50.0),
            Vec2::new(99.0, 50.0),
            line_color,
            WIDTH,
            HEIGHT,
        );

        // Line should overwrite circle where they intersect
        assert_eq!(pixel_at(&frame, 50, 50), line_color);
        // Circle should be visible where line doesn't cross
        assert_eq!(pixel_at(&frame, 50, 30), circle_color);
        // Background should be visible outside both
        assert_eq!(pixel_at(&frame, 5, 5), bg);
    }

    #[test]
    fn visual_frame_buffer_integrity() {
        let mut frame = create_frame();
        let original_len = frame.len();

        // Draw various shapes
        clear(&mut frame, [0x10, 0x20, 0x30, 0xFF]);
        draw_circle(
            &mut frame,
            Vec2::new(50.0, 50.0),
            25.0,
            [0xFF, 0x00, 0x00, 0xFF],
            WIDTH,
            HEIGHT,
        );
        draw_line(
            &mut frame,
            Vec2::new(0.0, 0.0),
            Vec2::new(99.0, 99.0),
            [0x00, 0xFF, 0x00, 0xFF],
            WIDTH,
            HEIGHT,
        );
        draw_axes(
            &mut frame,
            Vec2::new(50.0, 50.0),
            [0x00, 0x00, 0xFF, 0xFF],
            WIDTH,
            HEIGHT,
        );

        // Frame buffer should maintain its size
        assert_eq!(
            frame.len(),
            original_len,
            "Frame buffer size changed during rendering"
        );

        // All pixels should have exactly 4 bytes (RGBA)
        for (i, chunk) in frame.chunks_exact(4).enumerate() {
            assert_eq!(chunk.len(), 4, "Pixel {i} should have exactly 4 bytes");
        }
    }
}