Skip to main content

ry_backend/
raylib_draw.rs

1//! Módulo Raylib - Drawing 2D/3D
2//!
3//! Raylib es excelente para:
4//! - Drawing 2D: rectángulos, círculos, líneas, triángulos
5//! - Drawing 3D: cubos, esferas, cilindros, modelos
6//! - Colors: paleta completa
7//! - Camera 3D: perspectiva, ortográfica
8//!
9//! NOTA: El texto TTF se deja a SDL2 (más profesional)
10
11#[cfg(feature = "raylib-backend")]
12use raylib::prelude::*;
13
14/// Color ry-dit (compatible con raylib y SDL2)
15#[derive(Debug, Clone, Copy)]
16pub struct RyColor {
17    pub r: u8,
18    pub g: u8,
19    pub b: u8,
20    pub a: u8,
21}
22
23impl RyColor {
24    // Colores predefinidos
25    pub const BLACK: Self = Self { r: 0, g: 0, b: 0, a: 255 };
26    pub const WHITE: Self = Self { r: 255, g: 255, b: 255, a: 255 };
27    pub const RED: Self = Self { r: 220, g: 60, b: 60, a: 255 };
28    pub const GREEN: Self = Self { r: 60, g: 200, b: 60, a: 255 };
29    pub const BLUE: Self = Self { r: 60, g: 60, b: 220, a: 255 };
30    pub const YELLOW: Self = Self { r: 220, g: 220, b: 60, a: 255 };
31    pub const CYAN: Self = Self { r: 60, g: 200, b: 200, a: 255 };
32    pub const MAGENTA: Self = Self { r: 200, g: 60, b: 200, a: 255 };
33    pub const ORANGE: Self = Self { r: 220, g: 160, b: 60, a: 255 };
34    pub const GRAY: Self = Self { r: 120, g: 120, b: 120, a: 255 };
35    pub const DARK_GRAY: Self = Self { r: 50, g: 50, b: 60, a: 255 };
36
37    #[cfg(feature = "raylib-backend")]
38    pub fn to_raylib(&self) -> raylib::prelude::Color {
39        raylib::prelude::Color::new(self.r, self.g, self.b, self.a)
40    }
41}
42
43/// Contexto de dibujo Raylib
44#[cfg(feature = "raylib-backend")]
45pub struct RaylibDraw {
46    handle: raylib::RaylibHandle,
47    thread: raylib::RaylibThread,
48    width: i32,
49    height: i32,
50}
51
52#[cfg(feature = "raylib-backend")]
53impl RaylibDraw {
54    pub fn new(title: &str, width: i32, height: i32) -> Result<Self, String> {
55        let (handle, thread) = raylib::init(width as u32, height as u32, title, 60);
56        Ok(Self { handle, thread, width, height })
57    }
58
59    pub fn begin_draw(&mut self) {
60        self.handle.begin_drawing(&self.thread);
61    }
62
63    pub fn end_draw(&mut self) {
64        self.handle.end_drawing(&self.thread);
65    }
66
67    pub fn clear(&mut self, color: RyColor) {
68        self.handle.clear_background(color.to_raylib());
69    }
70
71    // 2D Drawing
72    pub fn draw_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: RyColor) {
73        self.handle.draw_rectangle(x, y, w, h, color.to_raylib());
74    }
75
76    pub fn draw_circle(&mut self, x: i32, y: i32, radius: i32, color: RyColor) {
77        self.handle.draw_circle(x, y, radius as f32, color.to_raylib());
78    }
79
80    pub fn draw_line(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, color: RyColor) {
81        self.handle.draw_line(x1, y1, x2, y2, color.to_raylib());
82    }
83
84    pub fn draw_triangle(&mut self, v1: (i32,i32), v2: (i32,i32), v3: (i32,i32), color: RyColor) {
85        let p1 = Vector2::new(v1.0 as f32, v1.1 as f32);
86        let p2 = Vector2::new(v2.0 as f32, v2.1 as f32);
87        let p3 = Vector2::new(v3.0 as f32, v3.1 as f32);
88        self.handle.draw_triangle(p1, p2, p3, color.to_raylib());
89    }
90
91    // 3D Drawing (básico)
92    pub fn draw_cube(&mut self, pos: (f32,f32,f32), size: f32, color: RyColor) {
93        let p = Vector3::new(pos.0, pos.1, pos.2);
94        self.handle.draw_cube(p, size, size, size, color.to_raylib());
95    }
96
97    pub fn draw_sphere(&mut self, pos: (f32,f32,f32), radius: f32, color: RyColor) {
98        let p = Vector3::new(pos.0, pos.1, pos.2);
99        self.handle.draw_sphere(p, radius, color.to_raylib());
100    }
101
102    // Texto (gaming style - usar SDL2 TTF para texto profesional)
103    pub fn draw_text_gaming(&mut self, text: &str, x: i32, y: i32, size: i32, color: RyColor) {
104        self.handle.draw_text(text, x, y, size as f32, color.to_raylib());
105    }
106
107    // Control
108    pub fn should_close(&self) -> bool {
109        self.handle.window_should_close()
110    }
111
112    pub fn is_key_pressed(&self, key: raylib::consts::KeyboardKey) -> bool {
113        self.handle.is_key_pressed(key)
114    }
115}