ry-gfx 0.10.8

Ry Graphics Layer - SDL2 Backend + GPU Instancing + FSR 1.0 for Termux and low-end devices
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
// crates/rydit-gfx/src/camera.rs
// Cámara 2D para Ry-Dit
//
// Soporte para:
// - Posición (x, y)
// - Zoom
// - Rotación
// - Seguimiento suave (lerp)
// - Límites de scroll
// - Conversión mundo↔pantalla

use crate::DrawHandle;

/// Cámara 2D
///
/// Permite transformar coordenadas del mundo a coordenadas de pantalla.
/// Útil para juegos con scroll, zoom, o cámaras que siguen al jugador.
#[derive(Debug, Clone)]
pub struct Camera2D {
    /// Posición X de la cámara en el mundo
    pub x: f32,
    /// Posición Y de la cámara en el mundo
    pub y: f32,
    /// Zoom (1.0 = normal, 2.0 = 2x zoom, 0.5 = half zoom)
    pub zoom: f32,
    /// Rotación en grados
    pub rotation: f32,
    /// Posición objetivo X (para seguimiento suave)
    pub target_x: Option<f32>,
    /// Posición objetivo Y (para seguimiento suave)
    pub target_y: Option<f32>,
    /// Factor de suavizado (0.0-1.0) para follow_smooth
    pub smooth: f32,
    /// Límite mínimo X (None = sin límite)
    pub min_x: Option<f32>,
    /// Límite mínimo Y (None = sin límite)
    pub min_y: Option<f32>,
    /// Límite máximo X (None = sin límite)
    pub max_x: Option<f32>,
    /// Límite máximo Y (None = sin límite)
    pub max_y: Option<f32>,
}

impl Camera2D {
    /// Crear nueva cámara con valores por defecto
    pub fn new() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            zoom: 1.0,
            rotation: 0.0,
            target_x: None,
            target_y: None,
            smooth: 0.1,
            min_x: None,
            min_y: None,
            max_x: None,
            max_y: None,
        }
    }

    /// Crear cámara con posición inicial
    pub fn at(x: f32, y: f32) -> Self {
        Self {
            x,
            y,
            ..Default::default()
        }
    }

    /// Crear cámara con posición y zoom
    pub fn with_zoom(x: f32, y: f32, zoom: f32) -> Self {
        Self {
            x,
            y,
            zoom,
            ..Default::default()
        }
    }

    // ========================================================================
    // FUNCIONES BÁSICAS
    // ========================================================================

    /// Establecer posición de la cámara
    pub fn set_position(&mut self, x: f32, y: f32) {
        self.x = self.clamp_x(x);
        self.y = self.clamp_y(y);
    }

    /// Obtener posición actual
    pub fn get_position(&self) -> (f32, f32) {
        (self.x, self.y)
    }

    /// Establecer zoom (0.1 mínimo, 10.0 máximo)
    pub fn set_zoom(&mut self, zoom: f32) {
        self.zoom = zoom.clamp(0.1, 10.0);
    }

    /// Obtener zoom actual
    pub fn get_zoom(&self) -> f32 {
        self.zoom
    }

    /// Establecer rotación en grados
    pub fn set_rotation(&mut self, angle: f32) {
        self.rotation = angle % 360.0;
    }

    /// Obtener rotación actual
    pub fn get_rotation(&self) -> f32 {
        self.rotation
    }

    // ========================================================================
    // SCROLL
    // ========================================================================

    /// Mover cámara relativamente (scroll)
    pub fn scroll(&mut self, dx: f32, dy: f32) {
        self.x = self.clamp_x(self.x + dx);
        self.y = self.clamp_y(self.y + dy);
    }

    /// Mover cámara a posición absoluta
    pub fn scroll_to(&mut self, x: f32, y: f32) {
        self.set_position(x, y);
    }

    /// Establecer límites de scroll
    pub fn set_bounds(&mut self, min_x: f32, min_y: f32, max_x: f32, max_y: f32) {
        self.min_x = Some(min_x);
        self.min_y = Some(min_y);
        self.max_x = Some(max_x);
        self.max_y = Some(max_y);
        // Aplicar límites inmediatamente
        self.x = self.clamp_x(self.x);
        self.y = self.clamp_y(self.y);
    }

    /// Limpiar límites de scroll
    pub fn clear_bounds(&mut self) {
        self.min_x = None;
        self.min_y = None;
        self.max_x = None;
        self.max_y = None;
    }

    // ========================================================================
    // SEGUIMIENTO DEL JUGADOR
    // ========================================================================

    /// Seguir objetivo instantáneamente
    pub fn follow(&mut self, target_x: f32, target_y: f32) {
        self.target_x = Some(target_x);
        self.target_y = Some(target_y);
        self.x = self.clamp_x(target_x);
        self.y = self.clamp_y(target_y);
    }

    /// Seguir objetivo con suavizado (lerp)
    pub fn follow_smooth(&mut self, target_x: f32, target_y: f32, smooth: f32) {
        self.target_x = Some(target_x);
        self.target_y = Some(target_y);
        self.smooth = smooth.clamp(0.01, 1.0);

        // Interpolar posición actual hacia objetivo
        let dx = target_x - self.x;
        let dy = target_y - self.y;
        self.x = self.clamp_x(self.x + dx * self.smooth);
        self.y = self.clamp_y(self.y + dy * self.smooth);
    }

    /// Establecer offset para seguimiento
    pub fn set_follow_offset(&mut self, offset_x: f32, offset_y: f32) {
        if let Some(tx) = self.target_x {
            if let Some(ty) = self.target_y {
                self.x = self.clamp_x(tx + offset_x);
                self.y = self.clamp_y(ty + offset_y);
            }
        }
    }

    // ========================================================================
    // CONVERSIÓN DE COORDENADAS
    // ========================================================================

    /// Convertir coordenadas del mundo a pantalla
    pub fn world_to_screen(
        &self,
        world_x: f32,
        world_y: f32,
        screen_width: i32,
        screen_height: i32,
    ) -> (i32, i32) {
        // Aplicar transformación inversa: traslación → rotación → escala
        let mut x = world_x - self.x;
        let mut y = world_y - self.y;

        // Rotación inversa
        if self.rotation != 0.0 {
            let rad = -self.rotation.to_radians();
            let cos = rad.cos();
            let sin = rad.sin();
            let rx = x * cos - y * sin;
            let ry = x * sin + y * cos;
            x = rx;
            y = ry;
        }

        // Escala (zoom)
        x *= self.zoom;
        y *= self.zoom;

        // Centrar en pantalla
        let sx = (x + screen_width as f32 / 2.0) as i32;
        let sy = (y + screen_height as f32 / 2.0) as i32;

        (sx, sy)
    }

    /// Convertir coordenadas de pantalla a mundo
    pub fn screen_to_world(
        &self,
        screen_x: i32,
        screen_y: i32,
        screen_width: i32,
        screen_height: i32,
    ) -> (f32, f32) {
        // Aplicar transformación: escala → rotación → traslación
        let mut x = screen_x as f32 - screen_width as f32 / 2.0;
        let mut y = screen_y as f32 - screen_height as f32 / 2.0;

        // Deshacer zoom
        x /= self.zoom;
        y /= self.zoom;

        // Rotación
        if self.rotation != 0.0 {
            let rad = self.rotation.to_radians();
            let cos = rad.cos();
            let sin = rad.sin();
            let rx = x * cos - y * sin;
            let ry = x * sin + y * cos;
            x = rx;
            y = ry;
        }

        // Trasladar al mundo
        x += self.x;
        y += self.y;

        (x, y)
    }

    // ========================================================================
    // APLICAR CÁMARA AL DRAW
    // ========================================================================

    /// Aplicar transformaciones de cámara al DrawHandle
    ///
    /// Esto debe llamarse ANTES de cualquier draw call en el frame.
    /// Nota: Esta función es para uso interno del engine.
    pub fn apply(&self, _d: &mut DrawHandle) {
        // Por ahora, las transformaciones se aplican manualmente
        // en las funciones de draw usando world_to_screen
        //
        // En una implementación futura, podríamos usar
        // raylib::Camera2D nativo para transformaciones automáticas
    }

    /// Aplicar transformaciones de cámara para SDL2
    ///
    /// Retorna las coordenadas transformadas para usar con SDL2.
    /// Usar junto con world_to_screen para coordenadas correctas.
    pub fn apply_sdl2(
        &self,
        world_x: f32,
        world_y: f32,
        screen_width: i32,
        screen_height: i32,
    ) -> (i32, i32) {
        // Aplicar zoom
        let zoomed_x = (world_x - self.x) * self.zoom;
        let zoomed_y = (world_y - self.y) * self.zoom;

        // Centrar en pantalla
        let screen_x = (zoomed_x + screen_width as f32 / 2.0) as i32;
        let screen_y = (zoomed_y + screen_height as f32 / 2.0) as i32;

        (screen_x, screen_y)
    }

    /// Obtener matriz de transformación para SDL2 (futura implementación)
    pub fn get_transform_matrix(&self) -> [f32; 6] {
        // [scale_x, shear_x, rotate_x, scale_y, shear_y, translate_y]
        [
            self.zoom,                  // scale_x
            0.0,                        // shear_x
            self.rotation.to_radians(), // rotate
            self.zoom,                  // scale_y
            0.0,                        // shear_y
            0.0,                        // translate (se calcula en runtime)
        ]
    }

    // ========================================================================
    // UTILIDADES
    // ========================================================================

    /// Clamp X con límites
    fn clamp_x(&self, x: f32) -> f32 {
        let mut result = x;
        if let Some(min) = self.min_x {
            result = result.max(min);
        }
        if let Some(max) = self.max_x {
            result = result.min(max);
        }
        result
    }

    /// Clamp Y con límites
    fn clamp_y(&self, y: f32) -> f32 {
        let mut result = y;
        if let Some(min) = self.min_y {
            result = result.max(min);
        }
        if let Some(max) = self.max_y {
            result = result.min(max);
        }
        result
    }

    /// Resetear cámara a valores por defecto
    pub fn reset(&mut self) {
        *self = Self::new();
    }

    /// Centrar cámara en posición
    pub fn center_on(&mut self, x: f32, y: f32) {
        self.set_position(x, y);
    }
}

impl Default for Camera2D {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// ESTADO GLOBAL DE CÁMARA
// ============================================================================

use std::cell::RefCell;
use std::rc::Rc;

thread_local! {
    /// Cámara global para uso en módulos RyDit
    static CAMERA: Rc<RefCell<Camera2D>> = Rc::new(RefCell::new(Camera2D::new()));
}

/// Obtener referencia a la cámara global
pub fn get_camera() -> Rc<RefCell<Camera2D>> {
    CAMERA.with(|c| c.clone())
}

// ============================================================================
// TESTS
// ============================================================================

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

    #[test]
    fn test_camera_new() {
        let camera = Camera2D::new();
        assert_eq!(camera.x, 0.0);
        assert_eq!(camera.y, 0.0);
        assert_eq!(camera.zoom, 1.0);
        assert_eq!(camera.rotation, 0.0);
    }

    #[test]
    fn test_camera_set_position() {
        let mut camera = Camera2D::new();
        camera.set_position(100.0, 200.0);
        assert_eq!(camera.get_position(), (100.0, 200.0));
    }

    #[test]
    fn test_camera_set_zoom() {
        let mut camera = Camera2D::new();
        camera.set_zoom(2.0);
        assert_eq!(camera.get_zoom(), 2.0);

        // Test límites
        camera.set_zoom(0.05);
        assert_eq!(camera.get_zoom(), 0.1); // mínimo

        camera.set_zoom(15.0);
        assert_eq!(camera.get_zoom(), 10.0); // máximo
    }

    #[test]
    fn test_camera_scroll() {
        let mut camera = Camera2D::new();
        camera.scroll(10.0, 20.0);
        assert_eq!(camera.get_position(), (10.0, 20.0));

        camera.scroll(-5.0, -10.0);
        assert_eq!(camera.get_position(), (5.0, 10.0));
    }

    #[test]
    fn test_camera_follow() {
        let mut camera = Camera2D::new();
        camera.follow(100.0, 200.0);
        assert_eq!(camera.get_position(), (100.0, 200.0));
    }

    #[test]
    fn test_camera_follow_smooth() {
        let mut camera = Camera2D::new();
        camera.follow_smooth(100.0, 200.0, 0.5);

        // Después de un smooth, debería estar a mitad de camino
        let (x, y) = camera.get_position();
        assert!((x - 50.0).abs() < 1.0); // ~50% de 100
        assert!((y - 100.0).abs() < 1.0); // ~50% de 200
    }

    #[test]
    fn test_camera_bounds() {
        let mut camera = Camera2D::new();
        camera.set_bounds(0.0, 0.0, 100.0, 100.0);

        camera.set_position(150.0, 150.0);
        let (x, y) = camera.get_position();
        assert_eq!(x, 100.0); // clamp al máximo
        assert_eq!(y, 100.0);

        camera.set_position(-50.0, -50.0);
        let (x, y) = camera.get_position();
        assert_eq!(x, 0.0); // clamp al mínimo
        assert_eq!(y, 0.0);
    }

    #[test]
    fn test_camera_world_to_screen() {
        let camera = Camera2D::with_zoom(0.0, 0.0, 1.0);
        let (sx, sy) = camera.world_to_screen(0.0, 0.0, 800, 600);

        // Sin transformación, el centro del mundo debería estar en el centro de pantalla
        assert_eq!(sx, 400);
        assert_eq!(sy, 300);
    }

    #[test]
    fn test_camera_screen_to_world() {
        let camera = Camera2D::with_zoom(0.0, 0.0, 1.0);
        let (wx, wy) = camera.screen_to_world(400, 300, 800, 600);

        // Centro de pantalla debería ser centro del mundo
        assert!((wx - 0.0).abs() < 0.01);
        assert!((wy - 0.0).abs() < 0.01);
    }

    #[test]
    fn test_camera_reset() {
        let mut camera = Camera2D::with_zoom(100.0, 200.0, 2.0);
        camera.reset();

        assert_eq!(camera.x, 0.0);
        assert_eq!(camera.y, 0.0);
        assert_eq!(camera.zoom, 1.0);
    }

    #[test]
    fn test_camera_functions_exist() {
        // Verificar que las funciones existen
        let _ = Camera2D::new;
        let _ = Camera2D::at;
        let _ = Camera2D::with_zoom;
        let _ = get_camera;
    }
}