carose 0.1.0

A lightweight 2D Rust game library for sprites, menus, audio, and input handling.
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
use crate::image::{
    load_sprite_sheet,
    load_image_2d
};
use crate::Window;
mod vectors;



/// Motion-related vectors applied to a sprite.
///
/// Vectors are evaluated by the engine to update sprite movement
/// and physics-like behavior.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Vector {
    /// Constant velocity applied every update tick.
    ///
    /// Values represent `(dx, dy)` in pixels per frame.
    Velocity(i32, i32),

    /// Acceleration applied to velocity each update tick.
    ///
    /// Values represent `(ax, ay)` in pixels per frame².
    Acceleration(i32, i32),
}


/// Rendering data for a sprite.
///
/// Determines how a sprite is drawn to the screen.
#[derive(Clone)]
pub enum SpriteRender {
    /// Solid-color rectangle fill.
    ///
    /// The contained value is a packed color (ARGB or RGB,
    /// depending on renderer configuration).
    Color(u32),

    /// Static bitmap sprite.
    ///
    /// Pixels are stored in row-major order.
    /// A value of `0` is treated as transparent.
    Bitmap {
        /// 2D pixel buffer: rows → columns.
        pixels: Vec<Vec<u32>>,
    },

    /// Animated bitmap sprite.
    ///
    /// Frames are cycled automatically using a fixed frame delay.
    AnimatedBitmap {
        /// Animation frames stored as 2D pixel buffers.
        frames: Vec<Vec<Vec<u32>>>,

        /// Index of the currently displayed frame.
        frame_index: usize,

        /// Number of ticks between frame changes.
        frame_delay: u32,

        /// Internal frame timer.
        frame_timer: u32,
    },
}



/// Logical category of a sprite.
///
/// Used for collision detection, game rules,
/// and sprite management.
#[derive(Clone, PartialEq, Eq, Debug, Copy)]
pub enum SpriteType {
    /// Player-controlled entity.
    Player,

    /// Hostile or AI-controlled entity.
    Enemy,

    /// Projectile such as bullets or spells.
    Projectile,

    /// Static solid object that blocks movement.
    Wall,

    /// Non-interactive visual overlay.
    Overlay,

    /// User-defined sprite category.
    ///
    /// Useful for custom logic without modifying the enum.
    Custom(&'static str),
}


/// A renderable and interactive game entity.
///
/// Sprites represent all visible objects in the world,
/// including players, enemies, projectiles, and environment objects.
pub struct Sprite {
    /// Logical classification of the sprite.
    pub sprite_type: SpriteType,

    /// Current health value.
    ///
    /// When health reaches zero or below, the sprite is considered dead.
    pub health: i32,

    /// Top-left position in screen coordinates.
    pub position: (usize, usize),

    /// Logical size of the sprite in pixels.
    pub size: (usize, usize),

    /// Rendering data used to draw the sprite.
    pub render: SpriteRender,

    /// Whether the sprite blocks movement.
    pub is_solid: bool,

    /// Motion vectors applied to the sprite.
    ///
    /// Includes velocity and acceleration components.
    pub vectors: Vec<Vector>,
}




impl Sprite {
    // Multiply size by a defined amount
    pub fn upscale(&mut self, factor: usize) {
        if factor <= 1 {
            return;
        }

        match &mut self.render {
            SpriteRender::Bitmap { pixels } => {
                let original = pixels.clone();
                let h = original.len();
                let w = if h > 0 { original[0].len() } else { 0 };

                let mut upscaled = vec![vec![0u32; w * factor]; h * factor];

                for y in 0..h {
                    for x in 0..w {
                        let color = original[y][x];
                        if color == 0 { continue; }

                        for sy in 0..factor {
                            for sx in 0..factor {
                                upscaled[y * factor + sy][x * factor + sx] = color;
                            }
                        }
                    }
                }

                *pixels = upscaled;
                self.size = (w * factor, h * factor);
            }

            SpriteRender::AnimatedBitmap { frames, .. } => {
                for frame in frames.iter_mut() {
                    let original = frame.clone();
                    let h = original.len();
                    let w = if h > 0 { original[0].len() } else { 0 };

                    let mut upscaled = vec![vec![0u32; w * factor]; h * factor];

                    for y in 0..h {
                        for x in 0..w {
                            let color = original[y][x];
                            if color == 0 { continue; }

                            for sy in 0..factor {
                                for sx in 0..factor {
                                    upscaled[y * factor + sy][x * factor + sx] = color;
                                }
                            }
                        }
                    }

                    *frame = upscaled;
                }

                if let Some(frame) = frames.first() {
                    self.size = (frame[0].len(), frame.len());
                }
            }

            SpriteRender::Color(_) => {
                // Solid-color sprites just scale logically
                self.size = (self.size.0 * factor, self.size.1 * factor);
            }
        }
    }

    /// Create a solid-color rectangular sprite.
    pub fn new_color(
        position: (usize, usize),
        size: (usize, usize),
        sprite_type: SpriteType,
        health: i32,
        color: u32,
        is_solid: bool,
    ) -> Self {
        Sprite {
            sprite_type,
            health,
            position,
            size,
            render: SpriteRender::Color(color),
            is_solid,
            vectors: Vec::new(),
        }
    }

    /// Create a bitmap sprite from a 2D pixel buffer.
    ///
    /// Pixels with value `0` are treated as transparent.
    pub fn new_bitmap(
        position: (usize, usize),
        sprite_type: SpriteType,
        health: i32,
        pixels: Vec<Vec<u32>>,
        is_solid: bool,
    ) -> Self {
        let height = pixels.len();
        let width = if height > 0 { pixels[0].len() } else { 0 };

        Sprite {
            sprite_type,
            health,
            position,
            size: (width, height),
            render: SpriteRender::Bitmap { pixels },
            is_solid,
            vectors: Vec::new(),
        }
    }

    /// Create an animated bitmap sprite from preloaded frames.
    ///
    /// All frames are assumed to be the same size.
    pub fn new_animated_bitmap(
        position: (usize, usize),
        sprite_type: SpriteType,
        health: i32,
        frames: Vec<Vec<Vec<u32>>>,
        frame_delay: u32,
        is_solid: bool,
    ) -> Self {
        let (width, height) = if let Some(frame) = frames.first() {
            let h = frame.len();
            let w = if h > 0 { frame[0].len() } else { 0 };
            (w, h)
        } else {
            (0, 0)
        };

        Sprite {
            sprite_type,
            health,
            position,
            size: (width, height),
            render: SpriteRender::AnimatedBitmap {
                frames,
                frame_index: 0,
                frame_delay,
                frame_timer: 0,
            },
            is_solid,
            vectors: Vec::new(),
        }
    }

    /// Create a wall sprite (solid, indestructible).
    pub fn new_wall(
        position: (usize, usize),
        size: (usize, usize),
    ) -> Self {
        Sprite {
            sprite_type: SpriteType::Wall,
            health: i32::MAX,
            position,
            size,
            render: SpriteRender::Color(0x555555),
            is_solid: true,
            vectors: Vec::new(),
        }
    }
}

impl Window {

    /// Create a bitmap sprite from an image file.
    ///
    /// The image is loaded from disk and used as the sprite's pixel data.
    /// Returns the index of the newly created sprite.
    pub fn create_bitmap_sprite_from_file(
        &mut self,
        position: (usize, usize),
        path: &str,
        sprite_type: SpriteType,
    ) -> usize {
        let bitmap = load_image_2d(path).expect("Failed to load image");
        self.create_bitmap_sprite(position, bitmap, sprite_type)
    }

    /// Create an animated sprite from multiple image files.
    ///
    /// Each file represents a single animation frame.
    /// Returns the index of the newly created sprite.
    pub fn create_animated_bitmap_sprite_from_files(
        &mut self,
        position: (usize, usize),
        health: i32,
        paths: Vec<String>,
        sprite_type: SpriteType,
        frame_delay: u32,
    ) -> usize {
        let frames: Vec<Vec<Vec<u32>>> = paths
            .iter()
            .map(|path| load_image_2d(path).expect("Failed to load image"))
            .collect();

        self.create_animated_bitmap_sprite(position, health, frames, sprite_type, frame_delay)
    }

    /// Create an animated sprite from preloaded frames.
    ///
    /// Each frame is a 2D bitmap. All frames are assumed to be the same size.
    /// Returns the index of the newly created sprite.
    pub fn create_animated_sprite(
        &mut self,
        position: (usize, usize),
        size: (usize, usize),
        sprite_type: SpriteType,
        health: i32,
        frames: Vec<Vec<Vec<u32>>>,
        frame_delay: u32,
    ) -> usize {
        self.sprites.push(Sprite {
            sprite_type,
            health,
            position,
            size,
            render: SpriteRender::AnimatedBitmap {
                frames,
                frame_index: 0,
                frame_delay,
                frame_timer: 0,
            },
            is_solid: false,
            vectors: Vec::new(),
        });
        self.sprites.len() - 1
    }

    /// Create a solid-colored rectangular sprite.
    ///
    /// Useful for debug objects, simple entities, or placeholders.
    /// Returns the index of the newly created sprite.
    pub fn create_colored_sprite(
        &mut self,
        position: (usize, usize),
        size: (usize, usize),
        sprite_type: SpriteType,
        health: i32,
        color: u32,
    ) -> usize {
        self.sprites.push(
            Sprite::new_color(position, size, sprite_type, health, color, false)
        );

        self.sprites.push(Sprite {
            sprite_type,
            health,
            position,
            size,
            render: SpriteRender::Color(color),
            is_solid: false,
            vectors: vec![Vector::Velocity(0, 0)],
        });
        self.sprites.len() - 1
    }

    /// Create a bitmap sprite from an already loaded 2D pixel buffer.
    ///
    /// Pixels with value `0` are treated as transparent.
    /// Returns the index of the newly created sprite.
    pub fn create_bitmap_sprite(
        &mut self,
        position: (usize, usize),
        bitmap: Vec<Vec<u32>>,
        sprite_type: SpriteType,
    ) -> usize {
        

        self.sprites.push(
            Sprite::new_bitmap(position, sprite_type, 1, bitmap, false)
        );

        self.sprites.len() - 1
    }

    /// Create an animated bitmap sprite from preloaded frames.
    ///
    /// Frames are advanced automatically using `frame_delay`.
    /// Returns the index of the newly created sprite.
    pub fn create_animated_bitmap_sprite(
        &mut self,
        position: (usize, usize),
        health: i32,
        bitmaps: Vec<Vec<Vec<u32>>>,
        sprite_type: SpriteType,
        frame_delay: u32,
    ) -> usize {
        self.sprites.push(
            Sprite::new_animated_bitmap(position, sprite_type, health, bitmaps, frame_delay, false)
        );

        self.sprites.len() - 1
    }

    /// Create an animated sprite from a sprite sheet.
    ///
    /// The sprite sheet is sliced into frames using the provided
    /// width and height.
    /// Returns the index of the newly created sprite.
    pub fn create_animated_sprite_from_sheet(
        &mut self,
        position: (usize, usize),
        health: i32,
        sheet: &str,
        width: u32,
        height: u32,
        sprite_type: SpriteType,
        frame_delay: u32,
    ) -> usize {
        let bitmaps = load_sprite_sheet(sheet, width, height)
            .expect("Failed to load sprite frames from sheet");

        let (width, height) = if let Some(frame) = bitmaps.first() {
            let h = frame.len();
            let w = if h > 0 { frame[0].len() } else { 0 };
            (w, h)
        } else {
            (0, 0)
        };

        self.sprites.push(Sprite {
            sprite_type,
            health,
            position,
            size: (width, height),
            render: SpriteRender::AnimatedBitmap {
                frames: bitmaps,
                frame_index: 0,
                frame_delay,
                frame_timer: 0,
            },
            is_solid: false,
            vectors: Vec::new(),
        });

        self.sprites.len() - 1
    }

    /// Create an indestructible wall sprite.
    ///
    /// Walls are solid and block movement.
    /// Returns the index of the newly created sprite.
    pub fn create_wall(&mut self, position: (usize, usize), size: (usize, usize)) -> usize {
        self.sprites.push(Sprite {
            sprite_type: SpriteType::Wall,
            health: i32::MAX,
            position,
            size,
            render: SpriteRender::Color(0x555555),
            is_solid: true,
            vectors: Vec::new(),
        });
        self.sprites.len() - 1
    }

    /// Advance the animation state of an animated sprite render.
    pub fn advance_animation(render: &mut SpriteRender) {
        if let SpriteRender::AnimatedBitmap {
            frames,
            frame_index,
            frame_delay,
            frame_timer,
        } = render {
            *frame_timer += 1;
            if *frame_timer >= *frame_delay {
                *frame_timer = 0;
                *frame_index = (*frame_index + 1) % frames.len();
            }
        }
    }

    /// Move a sprite to a new position.
    pub fn move_sprite(&mut self, index: usize, new_pos: (usize, usize)) {
        if let Some(sprite) = self.sprites.get_mut(index) {
            sprite.position = new_pos;
        }
    }

    /// Remove a sprite by index.
    pub fn remove_sprite(&mut self, index: usize) {
        if index < self.sprites.len() {
            self.sprites.remove(index);
        }
    }

    /// Invoke a callback for each sprite of a given type that has died.
    pub fn on_death<F>(&mut self, sprite_type: SpriteType, mut on_death: F)
    where
        F: FnMut(&mut Window, usize),
    {
        for i in 0..self.sprites.len() {
            if self.sprites[i].sprite_type == sprite_type
                && self.sprites[i].health <= 0
            {
                on_death(self, i);
            }
        }
    }

    /// Invoke a callback when two sprite types collide.
    pub fn on_collision<F>(
        &mut self,
        a_type: SpriteType,
        b_type: SpriteType,
        mut on_collision: F,
    )
    where
        F: FnMut(&mut Window, usize, usize),
    {
        let len = self.sprites.len();

        for i in 0..len {
            if self.sprites[i].sprite_type != a_type {
                continue;
            }

            let (x1, y1) = self.sprites[i].position;
            let (w1, h1) = self.sprites[i].size;

            for j in 0..len {
                if i == j || self.sprites[j].sprite_type != b_type {
                    continue;
                }

                let (x2, y2) = self.sprites[j].position;
                let (w2, h2) = self.sprites[j].size;

                if x1 < x2 + w2 &&
                   x1 + w1 > x2 &&
                   y1 < y2 + h2 &&
                   y1 + h1 > y2
                {
                    on_collision(self, i, j);
                }
            }
        }
    }

    /// Change the health of sprites when they collide with another type.
    pub fn change_health_on_collision(
        &mut self,
        target_type: SpriteType,
        collider_type: SpriteType,
        health: i32,
    ) {
        let len = self.sprites.len();
        for i in 0..len {
            if self.sprites[i].sprite_type != target_type { continue; }

            for j in 0..len {
                if i == j || self.sprites[j].sprite_type != collider_type { continue; }

                let (s1, s2) = if i < j {
                    let (left, right) = self.sprites.split_at_mut(j);
                    (&mut left[i], &mut right[0])
                } else {
                    let (left, right) = self.sprites.split_at_mut(i);
                    (&mut right[0], &mut left[j])
                };

                if s1.position.0 < s2.position.0 + s2.size.0
                    && s1.position.0 + s1.size.0 > s2.position.0
                    && s1.position.1 < s2.position.1 + s2.size.1
                    && s1.position.1 + s1.size.1 > s2.position.1
                {
                    s1.health = s1.health.saturating_add(health);
                }
            }
        }
    }

    /// Remove all dead sprites of a given type.
    pub fn remove_on_death(&mut self, sprite_type: SpriteType) {
        let mut dead_indices = Vec::new();
        for (i, sprite) in self.sprites.iter().enumerate() {
            if sprite.sprite_type == sprite_type && sprite.health <= 0 {
                dead_indices.push(i);
            }
        }

        for &i in dead_indices.iter().rev() {
            self.remove_sprite(i);
        }
    }

    /// Remove sprites of a given type when they collide with another type.
    pub fn remove_on_collision(
        &mut self,
        collider_type: SpriteType,
        remove_type: SpriteType,
    ) {
        let mut dead_indices = Vec::new();
        let len = self.sprites.len();

        for i in 0..len {
            if self.sprites[i].sprite_type != remove_type {
                continue;
            }

            let (x1, y1) = self.sprites[i].position;
            let (w1, h1) = self.sprites[i].size;

            for j in 0..len {
                if i == j || self.sprites[j].sprite_type != collider_type {
                    continue;
                }

                let (x2, y2) = self.sprites[j].position;
                let (w2, h2) = self.sprites[j].size;

                if x1 < x2 + w2 &&
                   x1 + w1 > x2 &&
                   y1 < y2 + h2 &&
                   y1 + h1 > y2
                {
                    dead_indices.push(i);
                    break;
                }
            }
        }

        for &i in dead_indices.iter().rev() {
            self.remove_sprite(i);
        }
    }

    /// Remove sprites that are completely outside the screen bounds.
    pub fn remove_if_out_of_screen(&mut self, sprite_type: SpriteType) {
        let mut dead_indices = Vec::new();

        for (i, sprite) in self.sprites.iter().enumerate() {
            if sprite.sprite_type != sprite_type { continue; }

            let x = sprite.position.0 as i32;
            let y = sprite.position.1 as i32;
            let w = sprite.size.0 as i32;
            let h = sprite.size.1 as i32;

            if x + w <= 0 || x >= self.width as i32
                || y + h <= 0 || y >= self.height as i32
            {
                dead_indices.push(i);
            }
        }

        for &i in dead_indices.iter().rev() {
            self.remove_sprite(i);
        }
    }

    /// Clamp sprites of a given type so they remain inside the screen.
    pub fn prevent_leaving_screen(&mut self, sprite_type: SpriteType) {
        for sprite in self.sprites.iter_mut() {
            if sprite.sprite_type != sprite_type { continue; }

            let (w, h) = sprite.size;
            let (x, y) = sprite.position;

            let new_x = x.clamp(0, self.width.saturating_sub(w));
            let new_y = y.clamp(0, self.height.saturating_sub(h));

            sprite.position = (new_x, new_y);
        }
    }

    /// Increase a score value when sprites of a given type die.
    pub fn increment_on_sprite_death(
        &mut self,
        sprite_type: SpriteType,
        score: &mut i32,
        points: i32,
    ) {
        for sprite in self.sprites.iter_mut() {
            if sprite.sprite_type != sprite_type {
                continue;
            }

            if sprite.health <= 0 {
                *score += points;
                sprite.health = i32::MIN;
            }
        }
    }

    /// Change sprite health when fully outside the screen.
    pub fn change_health_offscreen(&mut self, sprite_type: SpriteType, health_change: i32) {
        let (width, height) = self.get_size();
        for sprite in &mut self.sprites {
            if sprite.sprite_type != sprite_type {
                continue;
            }

            let (x, y) = sprite.position;
            let (w, h) = sprite.size;

            if x + w <= 0 || x >= width || y + h <= 0 || y >= height {
                sprite.health = sprite.health.saturating_add(health_change);
            }
        }
    }
}